Publish the Announcement plugin source code

Hi
Accourding to the discord, please publish the Announcement plugin source code.

Thanks

Hi @ali,
Is there any update about publishing Announcement plugin source code?

Thanks

You can find the plugin script here:

const announcementModel = toolsLib.database.createModel(
	'announcement',
	{
		created_by: {
			type: 'integer',
			onDelete: 'CASCADE',
			allowNull: false,
			references: {
				model: 'Users',
				key: 'id'
			}
		},
		title: {
			type: 'string',
			allowNull: false
		},
		message: {
			type: 'text',
			allowNull: false
		},
		type: {
			type: 'string',
			allowNull: false,
			defaultValue: 'info'
		}
	}
);

const { checkSchema } = expressValidator;

app.get('/plugins/announcements', [
	checkSchema({
		limit: {
			in: ['query'],
			isInt: true,
			optional: true,
			errorMessage: 'must be an integer'
		},
		page: {
			in: ['query'],
			isInt: true,
			optional: true,
			errorMessage: 'must be an integer'
		},
		order_by: {
			in: ['query'],
			isString: true,
			optional: true,
			errorMessage: 'must be a string'
		},
		order: {
			in: ['query'],
			isString: true,
			isIn: {
				options: [['asc', 'desc']],
			},
			errorMessage: 'must be one of [asc, desc]',
			optional: true
		},
		start_date: {
			in: ['query'],
			isISO8601: true,
			errorMessage: 'must be an iso date',
			optional: true
		},
		end_date: {
			in: ['query'],
			isISO8601: true,
			errorMessage: 'must be an iso date',
			optional: true
		}
	})
], (req, res) => {
	const errors = expressValidator.validationResult(req);
	if (!errors.isEmpty()) {
		return res.status(400).json({ errors: errors.array() });
	}

	const { limit, page, order_by, order, start_date, end_date } = req.query;

	loggerPlugin.info(
		req.uuid,
		'GET /plugins/announcements query',
		limit,
		page,
		order_by,
		order,
		start_date,
		end_date
	);

	const pagination = toolsLib.database.paginationQuery(limit, page);
	const ordering = toolsLib.database.orderingQuery(order_by, order);
	const timeframe = toolsLib.database.timeframeQuery(start_date, end_date);

	const query = {
		where: {},
		order: [ordering],
		attributes: {
			exclude: ['created_by']
		},
		...pagination
	};

	if (timeframe) query.where.created_at = timeframe;

	announcementModel.findAndCountAll(query)
		.then(toolsLib.database.convertSequelizeCountAndRows)
		.then((announcements) => {
			return res.json(announcements);
		})
		.catch((err) => {
			loggerPlugin.error(
				req.uuid,
				'GET /plugins/announcements err',
				err.message
			);
			return res.status(err.status || 400).json({ message: err.message });
		});
});

app.post('/plugins/announcement', [
	toolsLib.security.verifyBearerTokenExpressMiddleware(['admin']),
	checkSchema({
		title: {
			in: ['body'],
			errorMessage: 'must be a string',
			isString: true,
			isLength: {
				errorMessage: 'must be minimum length of 1',
				options: { min: 1 }
			},
			optional: false
		},
		message: {
			in: ['body'],
			errorMessage: 'must be a string',
			isString: true,
			isLength: {
				errorMessage: 'must be minimum length of 5',
				options: { min: 1 }
			},
			optional: false
		},
		type: {
			in: ['body'],
			errorMessage: 'must be a string',
			isLength: {
				errorMessage: 'must be minimum length of 1',
				options: { min: 1 }
			},
			isString: true,
			optional: true
		}
	})
], (req, res) => {
	const errors = expressValidator.validationResult(req);
	if (!errors.isEmpty()) {
		return res.status(400).json({ errors: errors.array() });
	}

	loggerPlugin.verbose(
		req.uuid,
		'POST /plugins/announcement auth',
		req.auth.sub
	);

	let { title, message, type } = req.body;

	if (!type) type = 'info';

	loggerPlugin.info(
		req.uuid,
		'POST /plugins/announcement title',
		title,
		'type',
		type
	);

	announcementModel.create({
		created_by: req.auth.sub.id,
		title,
		message,
		type
	})
		.then((announcement) => {
			return res.json(announcement);
		})
		.catch((err) => {
			loggerPlugin.error(
				req.uuid,
				'POST /plugins/announcement err',
				err.message
			);
			return res.status(err.status || 400).json({ message: err.message });
		});
});

app.delete('/plugins/announcement', [
	toolsLib.security.verifyBearerTokenExpressMiddleware(['admin']),
	checkSchema({
		id: {
			in: ['query'],
			errorMessage: 'must include an id',
			isInt: true,
			optional: false
		}
	})
], (req, res) => {
	const errors = expressValidator.validationResult(req);
	if (!errors.isEmpty()) {
		return res.status(400).json({ errors: errors.array() });
	}

	loggerPlugin.verbose(
		req.uuid,
		'DELETE /plugins/announcement auth',
		req.auth.sub
	);

	const { id } = req.query;

	loggerPlugin.info(
		req.uuid,
		'DELETE /plugins/announcement id',
		id
	);

	announcementModel.findOne({
		where: { id }
	})
		.then((announcement) => {
			if (!announcement) {
				throw new Error('Not found');
			}
			return announcement.destroy();
		})
		.then(() => {
			return res.json({ message: 'Success' });
		})
		.catch((err) => {
			loggerPlugin.error(
				req.uuid,
				'DELETE /plugins/announcement err',
				err.message
			);
			return res.status(err.status || 400).json({ message: err.message });
		});
});
1 Like

Thank you,
Whould you please share the Admin_view code too?
I need to know how to develop admin view with React/antd same as what I’m doing in the developing Web_view plugin. there isn’t any example in documentation, only description that we can inject html code in Admin_view…

Thank you for your time