|
@@ -1,11 +1,15 @@
|
|
|
|
|
+import { SUPPORTED_ACTION_TYPE, SUPPORTED_TARGET_MODEL_TYPE } from '~/interfaces/activity';
|
|
|
|
|
+import { generateAddActivityMiddleware } from '~/server/middlewares/add-activity';
|
|
|
import loggerFactory from '~/utils/logger';
|
|
import loggerFactory from '~/utils/logger';
|
|
|
|
|
|
|
|
import { apiV3FormValidator } from '../../middlewares/apiv3-form-validator';
|
|
import { apiV3FormValidator } from '../../middlewares/apiv3-form-validator';
|
|
|
|
|
|
|
|
|
|
+
|
|
|
const logger = loggerFactory('growi:routes:apiv3:bookmarks'); // eslint-disable-line no-unused-vars
|
|
const logger = loggerFactory('growi:routes:apiv3:bookmarks'); // eslint-disable-line no-unused-vars
|
|
|
|
|
|
|
|
const express = require('express');
|
|
const express = require('express');
|
|
|
const { body, query, param } = require('express-validator');
|
|
const { body, query, param } = require('express-validator');
|
|
|
|
|
+
|
|
|
const { serializeUserSecurely } = require('../../models/serializers/user-serializer');
|
|
const { serializeUserSecurely } = require('../../models/serializers/user-serializer');
|
|
|
|
|
|
|
|
const router = express.Router();
|
|
const router = express.Router();
|
|
@@ -71,6 +75,9 @@ module.exports = (crowi) => {
|
|
|
const loginRequiredStrictly = require('../../middlewares/login-required')(crowi);
|
|
const loginRequiredStrictly = require('../../middlewares/login-required')(crowi);
|
|
|
const loginRequired = require('../../middlewares/login-required')(crowi, true);
|
|
const loginRequired = require('../../middlewares/login-required')(crowi, true);
|
|
|
const csrf = require('../../middlewares/csrf')(crowi);
|
|
const csrf = require('../../middlewares/csrf')(crowi);
|
|
|
|
|
+ const addActivity = generateAddActivityMiddleware(crowi);
|
|
|
|
|
+
|
|
|
|
|
+ const activityEvent = crowi.event('activity');
|
|
|
|
|
|
|
|
const { Page, Bookmark, User } = crowi.models;
|
|
const { Page, Bookmark, User } = crowi.models;
|
|
|
|
|
|
|
@@ -257,7 +264,7 @@ module.exports = (crowi) => {
|
|
|
* schema:
|
|
* schema:
|
|
|
* $ref: '#/components/schemas/Bookmark'
|
|
* $ref: '#/components/schemas/Bookmark'
|
|
|
*/
|
|
*/
|
|
|
- router.put('/', accessTokenParser, loginRequiredStrictly, csrf, validator.bookmarks, apiV3FormValidator, async(req, res) => {
|
|
|
|
|
|
|
+ router.put('/', accessTokenParser, loginRequiredStrictly, csrf, addActivity, validator.bookmarks, apiV3FormValidator, async(req, res) => {
|
|
|
const { pageId, bool } = req.body;
|
|
const { pageId, bool } = req.body;
|
|
|
const userId = req.user?._id;
|
|
const userId = req.user?._id;
|
|
|
|
|
|
|
@@ -265,9 +272,10 @@ module.exports = (crowi) => {
|
|
|
return res.apiv3Err('A logged in user is required.');
|
|
return res.apiv3Err('A logged in user is required.');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ let page;
|
|
|
let bookmark;
|
|
let bookmark;
|
|
|
try {
|
|
try {
|
|
|
- const page = await Page.findByIdAndViewer(pageId, req.user);
|
|
|
|
|
|
|
+ page = await Page.findByIdAndViewer(pageId, req.user);
|
|
|
if (page == null) {
|
|
if (page == null) {
|
|
|
return res.apiv3Err(`Page '${pageId}' is not found or forbidden`);
|
|
return res.apiv3Err(`Page '${pageId}' is not found or forbidden`);
|
|
|
}
|
|
}
|
|
@@ -277,10 +285,6 @@ module.exports = (crowi) => {
|
|
|
if (bookmark == null) {
|
|
if (bookmark == null) {
|
|
|
if (bool) {
|
|
if (bool) {
|
|
|
bookmark = await Bookmark.add(page, req.user);
|
|
bookmark = await Bookmark.add(page, req.user);
|
|
|
-
|
|
|
|
|
- const pageEvent = crowi.event('page');
|
|
|
|
|
- // in-app notification
|
|
|
|
|
- pageEvent.emit('bookmark', page, req.user);
|
|
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
|
logger.warn(`Removing the bookmark for ${page._id} by ${req.user._id} failed because the bookmark does not exist.`);
|
|
logger.warn(`Removing the bookmark for ${page._id} by ${req.user._id} failed because the bookmark does not exist.`);
|
|
@@ -306,6 +310,14 @@ module.exports = (crowi) => {
|
|
|
bookmark.depopulate('user');
|
|
bookmark.depopulate('user');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ const activityId = res.locals.activity._id;
|
|
|
|
|
+ const parameters = {
|
|
|
|
|
+ targetModel: SUPPORTED_TARGET_MODEL_TYPE.MODEL_PAGE,
|
|
|
|
|
+ target: page,
|
|
|
|
|
+ action: bool ? SUPPORTED_ACTION_TYPE.ACTION_PAGE_BOOKMARK : SUPPORTED_ACTION_TYPE.ACTION_PAGE_UNBOOKMARK,
|
|
|
|
|
+ };
|
|
|
|
|
+ activityEvent.emit('update', activityId, parameters, page);
|
|
|
|
|
+
|
|
|
return res.apiv3({ bookmark });
|
|
return res.apiv3({ bookmark });
|
|
|
});
|
|
});
|
|
|
|
|
|