|
|
@@ -0,0 +1,187 @@
|
|
|
+const loggerFactory = require('@alias/logger');
|
|
|
+
|
|
|
+const logger = loggerFactory('growi:routes:apiv3:page'); // eslint-disable-line no-unused-vars
|
|
|
+
|
|
|
+const express = require('express');
|
|
|
+const { body } = require('express-validator');
|
|
|
+
|
|
|
+const router = express.Router();
|
|
|
+
|
|
|
+// const ErrorV3 = require('../../models/vo/error-apiv3');
|
|
|
+
|
|
|
+/**
|
|
|
+ * @swagger
|
|
|
+ * tags:
|
|
|
+ * name: Page
|
|
|
+ */
|
|
|
+
|
|
|
+/**
|
|
|
+ * @swagger
|
|
|
+ *
|
|
|
+ * components:
|
|
|
+ * schemas:
|
|
|
+ * Page:
|
|
|
+ * description: Page
|
|
|
+ * type: object
|
|
|
+ * properties:
|
|
|
+ * _id:
|
|
|
+ * type: string
|
|
|
+ * description: page ID
|
|
|
+ * example: 5e07345972560e001761fa63
|
|
|
+ * __v:
|
|
|
+ * type: number
|
|
|
+ * description: DB record version
|
|
|
+ * example: 0
|
|
|
+ * commentCount:
|
|
|
+ * type: number
|
|
|
+ * description: count of comments
|
|
|
+ * example: 3
|
|
|
+ * createdAt:
|
|
|
+ * type: string
|
|
|
+ * description: date created at
|
|
|
+ * example: 2010-01-01T00:00:00.000Z
|
|
|
+ * creator:
|
|
|
+ * $ref: '#/components/schemas/User'
|
|
|
+ * extended:
|
|
|
+ * type: object
|
|
|
+ * description: extend data
|
|
|
+ * example: {}
|
|
|
+ * grant:
|
|
|
+ * type: number
|
|
|
+ * description: grant
|
|
|
+ * example: 1
|
|
|
+ * grantedUsers:
|
|
|
+ * type: array
|
|
|
+ * description: granted users
|
|
|
+ * items:
|
|
|
+ * type: string
|
|
|
+ * description: user ID
|
|
|
+ * example: ["5ae5fccfc5577b0004dbd8ab"]
|
|
|
+ * lastUpdateUser:
|
|
|
+ * $ref: '#/components/schemas/User'
|
|
|
+ * liker:
|
|
|
+ * type: array
|
|
|
+ * description: granted users
|
|
|
+ * items:
|
|
|
+ * type: string
|
|
|
+ * description: user ID
|
|
|
+ * example: []
|
|
|
+ * path:
|
|
|
+ * type: string
|
|
|
+ * description: page path
|
|
|
+ * example: /
|
|
|
+ * redirectTo:
|
|
|
+ * type: string
|
|
|
+ * description: redirect path
|
|
|
+ * example: ""
|
|
|
+ * revision:
|
|
|
+ * type: string
|
|
|
+ * description: page revision
|
|
|
+ * seenUsers:
|
|
|
+ * type: array
|
|
|
+ * description: granted users
|
|
|
+ * items:
|
|
|
+ * type: string
|
|
|
+ * description: user ID
|
|
|
+ * example: ["5ae5fccfc5577b0004dbd8ab"]
|
|
|
+ * status:
|
|
|
+ * type: string
|
|
|
+ * description: status
|
|
|
+ * enum:
|
|
|
+ * - 'wip'
|
|
|
+ * - 'published'
|
|
|
+ * - 'deleted'
|
|
|
+ * - 'deprecated'
|
|
|
+ * example: published
|
|
|
+ * updatedAt:
|
|
|
+ * type: string
|
|
|
+ * description: date updated at
|
|
|
+ * example: 2010-01-01T00:00:00.000Z
|
|
|
+ *
|
|
|
+ * LikeParams:
|
|
|
+ * description: LikeParams
|
|
|
+ * type: object
|
|
|
+ * properties:
|
|
|
+ * pageId:
|
|
|
+ * type: string
|
|
|
+ * description: page ID
|
|
|
+ * example: 5e07345972560e001761fa63
|
|
|
+ * bool:
|
|
|
+ * type: boolean
|
|
|
+ * description: boolean for like status
|
|
|
+ */
|
|
|
+module.exports = (crowi) => {
|
|
|
+ const accessTokenParser = require('../../middleware/access-token-parser')(crowi);
|
|
|
+ const loginRequired = require('../../middleware/login-required')(crowi);
|
|
|
+ const csrf = require('../../middleware/csrf')(crowi);
|
|
|
+
|
|
|
+ const globalNotificationService = crowi.getGlobalNotificationService();
|
|
|
+ const { Page, GlobalNotificationSetting } = crowi.models;
|
|
|
+ const { ApiV3FormValidator } = crowi.middlewares;
|
|
|
+
|
|
|
+
|
|
|
+ const validator = {
|
|
|
+ likes: [
|
|
|
+ body('pageId').isString(),
|
|
|
+ body('bool').isBoolean(),
|
|
|
+ ],
|
|
|
+ };
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @swagger
|
|
|
+ *
|
|
|
+ * /page/likes:
|
|
|
+ * put:
|
|
|
+ * tags: [Page]
|
|
|
+ * summary: /page/likes
|
|
|
+ * description: Update liked status
|
|
|
+ * operationId: updateLikedStatus
|
|
|
+ * requestBody:
|
|
|
+ * content:
|
|
|
+ * application/json:
|
|
|
+ * schema:
|
|
|
+ * $ref: '#/components/schemas/LikeParams'
|
|
|
+ * responses:
|
|
|
+ * 200:
|
|
|
+ * description: Succeeded to update liked status.
|
|
|
+ * content:
|
|
|
+ * application/json:
|
|
|
+ * schema:
|
|
|
+ * $ref: '#/components/schemas/Page'
|
|
|
+ */
|
|
|
+ router.put('/likes', accessTokenParser, loginRequired, csrf, validator.likes, ApiV3FormValidator, async(req, res) => {
|
|
|
+ const { pageId, bool } = req.body;
|
|
|
+
|
|
|
+ let page;
|
|
|
+ try {
|
|
|
+ page = await Page.findByIdAndViewer(pageId, req.user);
|
|
|
+ if (page == null) {
|
|
|
+ return res.apiv3Err(`Page '${pageId}' is not found or forbidden`);
|
|
|
+ }
|
|
|
+ if (bool) {
|
|
|
+ page = await page.like(req.user);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ page = await page.unlike(req.user);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ logger.error('update-like-failed', err);
|
|
|
+ return res.apiv3Err(err, 500);
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ // global notification
|
|
|
+ await globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_LIKE, page, req.user);
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ logger.error('Like notification failed', err);
|
|
|
+ }
|
|
|
+
|
|
|
+ const result = { page };
|
|
|
+ result.seenUser = page.seenUsers;
|
|
|
+ return res.apiv3({ result });
|
|
|
+ });
|
|
|
+
|
|
|
+ return router;
|
|
|
+};
|