| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- import { Types } from 'mongoose';
- import loggerFactory from '../../utils/logger';
- import { getModelSafely } from '../util/mongoose-utils';
- import ActivityDefine from '../util/activityDefine';
- import Crowi from '../crowi';
- const logger = loggerFactory('growi:service:CommentService');
- class CommentService {
- crowi!: Crowi;
- inAppNotificationService!: any;
- commentEvent!: any;
- constructor(crowi: Crowi) {
- this.crowi = crowi;
- this.inAppNotificationService = crowi.inAppNotificationService;
- this.commentEvent = crowi.event('comment');
- // init
- this.initCommentEventListeners();
- }
- initCommentEventListeners(): void {
- // create
- this.commentEvent.on('create', async(savedComment) => {
- try {
- const Page = getModelSafely('Page') || require('../models/page')(this.crowi);
- await Page.updateCommentCount(savedComment.page);
- const savedActivity = await this.createByPageComment(savedComment);
- let targetUsers: Types.ObjectId[] = [];
- targetUsers = await savedActivity.getNotificationTargetUsers();
- await this.inAppNotificationService.emitSocketIo(targetUsers);
- await this.inAppNotificationService.upsertByActivity(targetUsers, savedActivity);
- }
- catch (err) {
- logger.error('Error occurred while handling the comment create event:\n', err);
- }
- });
- // update
- this.commentEvent.on('update', (userId, pageId) => {
- this.commentEvent.onUpdate();
- // TODO: 79713
- // const { inAppNotificationService } = this.crowi;
- // inAppNotificationService.emitSocketIo(userId, pageId);
- });
- // remove
- this.commentEvent.on('remove', async(comment) => {
- this.commentEvent.onRemove();
- try {
- const Page = getModelSafely('Page') || require('../models/page')(this.crowi);
- await Page.updateCommentCount(comment.page);
- }
- catch (err) {
- logger.error('Error occurred while updating the comment count:\n', err);
- }
- });
- }
- /**
- * @param {Comment} comment
- * @return {Promise}
- */
- createByPageComment = function(comment) {
- const { activityService } = this.crowi;
- const parameters = {
- user: comment.creator,
- targetModel: ActivityDefine.MODEL_PAGE,
- target: comment.page,
- eventModel: ActivityDefine.MODEL_COMMENT,
- event: comment._id,
- action: ActivityDefine.ACTION_COMMENT,
- };
- return activityService.createByParameters(parameters);
- };
- }
- module.exports = CommentService;
|