| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- const logger = require('@alias/logger')('growi:service:GlobalNotification');
- const nodePath = require('path');
- /**
- * the service class of GlobalNotificationSetting
- */
- class GlobalNotificationService {
- constructor(crowi) {
- this.crowi = crowi;
- this.mailer = crowi.getMailer();
- this.slack = crowi.slack;
- this.GlobalNotification = crowi.model('GlobalNotificationSetting');
- this.User = crowi.model('User');
- this.appTitle = crowi.appService.getAppTitle();
- }
- notifyByMail(notification, mailOption) {
- this.mailer.send(Object.assign(mailOption, { to: notification.toEmail }));
- }
- notifyBySlack(notification, slackOption) {
- this.slack.sendGlobalNotification(notification, slackOption);
- }
- fire(notifications, option) {
- notifications.forEach((notification) => {
- if (notification.__t === 'mail') {
- this.notifyByMail(notification, option.mail);
- }
- else if (notification.__t === 'slack') {
- this.notifyBySlack(notification, option.slack);
- }
- });
- }
- /**
- * send notification at page creation
- * @memberof GlobalNotification
- * @param {obejct} page
- */
- async notifyPageCreate(page) {
- const notifications = await this.GlobalNotification.findSettingByPathAndEvent(page.path, 'pageCreate');
- const lang = 'en-US'; // FIXME
- const baseOption = {
- template: nodePath.join(this.crowi.localeDir, `${lang}/notifications/pageCreate.txt`),
- vars: {
- appTitle: this.appTitle,
- path: page.path,
- username: page.creator.username,
- },
- };
- const option = {
- mail: {
- subject: `#pageCreate - ${page.creator.username} created ${page.path}`,
- ...baseOption,
- },
- slack: {
- ...baseOption,
- },
- };
- logger.debug('notifyPageCreate', option);
- this.fire(notifications, option);
- }
- /**
- * send notification at page edit
- * @memberof GlobalNotification
- * @param {obejct} page
- */
- async notifyPageEdit(page) {
- const notifications = await this.GlobalNotification.findSettingByPathAndEvent(page.path, 'pageEdit');
- const lang = 'en-US'; // FIXME
- const baseOption = {
- template: nodePath.join(this.crowi.localeDir, `${lang}/notifications/pageEdit.txt`),
- vars: {
- appTitle: this.appTitle,
- path: page.path,
- username: page.creator.username,
- },
- };
- const option = {
- mail: {
- subject: `#pageEdit - ${page.creator.username} edited ${page.path}`,
- ...baseOption,
- },
- slack: {
- ...baseOption,
- },
- };
- logger.debug('notifyPageEdit', option);
- this.fire(notifications, option);
- }
- /**
- * send notification at page deletion
- * @memberof GlobalNotification
- * @param {obejct} page
- */
- async notifyPageDelete(page) {
- const notifications = await this.GlobalNotification.findSettingByPathAndEvent(page.path, 'pageDelete');
- const lang = 'en-US'; // FIXME
- const baseOption = {
- template: nodePath.join(this.crowi.localeDir, `${lang}/notifications/pageDelete.txt`),
- vars: {
- appTitle: this.appTitle,
- path: page.path,
- username: page.creator.username,
- },
- };
- const option = {
- mail: {
- subject: `#pageDelete - ${page.creator.username} deleted ${page.path}`, // FIXME
- ...baseOption,
- },
- slack: {
- ...baseOption,
- },
- };
- this.fire(notifications, option);
- }
- /**
- * send notification at page move
- * @memberof GlobalNotification
- * @param {obejct} page
- */
- async notifyPageMove(page, oldPagePath, user) {
- const notifications = await this.GlobalNotification.findSettingByPathAndEvent(page.path, 'pageMove');
- const lang = 'en-US'; // FIXME
- const baseOption = {
- template: nodePath.join(this.crowi.localeDir, `${lang}/notifications/pageMove.txt`),
- vars: {
- appTitle: this.appTitle,
- oldPath: oldPagePath,
- newPath: page.path,
- username: user.username,
- },
- };
- const option = {
- mail: {
- subject: `#pageMove - ${user.username} moved ${page.path} to ${page.path}`, // FIXME
- ...baseOption,
- },
- slack: {
- ...baseOption,
- },
- };
- this.fire(notifications, option);
- }
- /**
- * send notification at page like
- * @memberof GlobalNotification
- * @param {obejct} page
- */
- async notifyPageLike(page, user) {
- const notifications = await this.GlobalNotification.findSettingByPathAndEvent(page.path, 'pageLike');
- const lang = 'en-US'; // FIXME
- const baseOption = {
- template: nodePath.join(this.crowi.localeDir, `${lang}/notifications/pageLike.txt`),
- vars: {
- appTitle: this.appTitle,
- path: page.path,
- username: page.creator.username,
- },
- };
- const option = {
- mail: {
- subject: `#pageLike - ${user.username} liked ${page.path}`,
- ...baseOption,
- },
- slack: {
- ...baseOption,
- },
- };
- this.fire(notifications, option);
- }
- /**
- * send notification at page comment
- * @memberof GlobalNotification
- * @param {obejct} page
- * @param {obejct} comment
- */
- async notifyComment(comment, path) {
- const notifications = await this.GlobalNotification.findSettingByPathAndEvent(path, 'comment');
- const lang = 'en-US'; // FIXME
- const user = await this.User.findOne({ _id: comment.creator });
- const baseOption = {
- template: nodePath.join(this.crowi.localeDir, `${lang}/notifications/comment.txt`),
- vars: {
- appTitle: this.appTitle,
- path,
- username: user.username,
- comment: comment.comment,
- },
- };
- const option = {
- mail: {
- subject: `#comment - ${user.username} commented on ${path}`,
- ...baseOption,
- },
- slack: {
- ...baseOption,
- },
- };
- this.fire(notifications, option);
- }
- }
- module.exports = GlobalNotificationService;
|