| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- const loggerFactory = require('@alias/logger');
- const logger = loggerFactory('growi:routes:apiv3:bookmarks'); // eslint-disable-line no-unused-vars
- const express = require('express');
- const { body, query } = require('express-validator');
- const router = express.Router();
- /**
- * @swagger
- * tags:
- * name: Bookmarks
- */
- /**
- * @swagger
- *
- * components:
- * schemas:
- * Bookmark:
- * description: Bookmark
- * type: object
- * properties:
- * _id:
- * type: string
- * description: page ID
- * example: 5e07345972560e001761fa63
- * __v:
- * type: number
- * description: DB record version
- * example: 0
- * createdAt:
- * type: string
- * description: date created at
- * example: 2010-01-01T00:00:00.000Z
- * page:
- * $ref: '#/components/schemas/Page/properties/_id'
- * user:
- * $ref: '#/components/schemas/User/properties/_id'
- *
- * BookmarkParams:
- * description: BookmarkParams
- * type: object
- * properties:
- * pageId:
- * type: string
- * description: page ID
- * example: 5e07345972560e001761fa63
- * bool:
- * type: boolean
- * description: boolean for bookmark status
- */
- module.exports = (crowi) => {
- const accessTokenParser = require('../../middlewares/access-token-parser')(crowi);
- const loginRequired = require('../../middlewares/login-required')(crowi);
- const csrf = require('../../middlewares/csrf')(crowi);
- const apiV3FormValidator = require('../../middlewares/apiv3-form-validator')(crowi);
- const { Page, Bookmark, User } = crowi.models;
- const validator = {
- bookmarks: [
- body('pageId').isString(),
- body('bool').isBoolean(),
- ],
- bookmarkInfo: [
- query('pageId').isMongoId(),
- ],
- };
- /**
- * @swagger
- *
- * /bookmarks:
- * get:
- * tags: [Bookmarks]
- * summary: /bookmarks
- * description: Get bookmarked status
- * operationId: getBookmarkedStatus
- * parameters:
- * - name: pageId
- * in: query
- * description: page id
- * schema:
- * type: string
- * responses:
- * 200:
- * description: Succeeded to get bookmarked status.
- * content:
- * application/json:
- * schema:
- * $ref: '#/components/schemas/Bookmark'
- */
- router.get('/', accessTokenParser, loginRequired, validator.bookmarkInfo, async(req, res) => {
- const { pageId } = req.query;
- try {
- const bookmarks = await Bookmark.findByPageIdAndUserId(pageId, req.user);
- const sumOfBookmarks = await Bookmark.countByPageId(pageId);
- return res.apiv3({ bookmarks, sumOfBookmarks });
- }
- catch (err) {
- logger.error('get-bookmark-failed', err);
- return res.apiv3Err(err, 500);
- }
- });
- // select page from bookmark where userid = userid
- /**
- * @swagger
- *
- * /bookmarks/{userId}:
- * get:
- * tags: [Bookmarks]
- * summary: /bookmarks/{userId}
- * description: Get my bookmarked status
- * operationId: getMyBookmarkedStatus
- * parameters:
- * - name: userId
- * in: path
- * required: true
- * description: user id
- * schema:
- * type: string
- * - name: page
- * in: query
- * description: selected page number
- * schema:
- * type: number
- * - name: limit
- * in: query
- * description: page item limit
- * schema:
- * type: number
- * - name: offset
- * in: query
- * description: page item offset
- * schema:
- * type: number
- * responses:
- * 200:
- * description: Succeeded to get my bookmarked status.
- * content:
- * application/json:
- * schema:
- * $ref: '#/components/schemas/Bookmark'
- */
- validator.myBookmarkList = [
- query('page').isInt({ min: 1 }),
- query('limit').if(value => value != null).isInt({ max: 300 }).withMessage('You should set less than 300 or not to set limit.'),
- ];
- router.get('/:userId', accessTokenParser, loginRequired, validator.myBookmarkList, apiV3FormValidator, async(req, res) => {
- const { userId } = req.params;
- const page = req.query.page;
- const limit = parseInt(req.query.limit) || await crowi.configManager.getConfig('crowi', 'customize:showPageLimitationM') || 30;
- if (userId == null) {
- return res.apiv3Err('User id is not found or forbidden', 400);
- }
- if (limit == null) {
- return res.apiv3Err('Could not catch page limit', 400);
- }
- try {
- const paginationResult = await Bookmark.paginate(
- {
- user: { $in: userId },
- },
- {
- populate: {
- path: 'page',
- model: 'Page',
- populate: {
- path: 'lastUpdateUser',
- model: 'User',
- select: User.USER_PUBLIC_FIELDS,
- },
- },
- page,
- limit,
- },
- );
- return res.apiv3({ paginationResult });
- }
- catch (err) {
- logger.error('get-bookmark-failed', err);
- return res.apiv3Err(err, 500);
- }
- });
- /**
- * @swagger
- *
- * /bookmarks:
- * put:
- * tags: [Bookmarks]
- * summary: /bookmarks
- * description: Update bookmarked status
- * operationId: updateBookmarkedStatus
- * requestBody:
- * content:
- * application/json:
- * schema:
- * $ref: '#/components/schemas/BookmarkParams'
- * responses:
- * 200:
- * description: Succeeded to update bookmarked status.
- * content:
- * application/json:
- * schema:
- * $ref: '#/components/schemas/Bookmark'
- */
- router.put('/', accessTokenParser, loginRequired, csrf, validator.bookmarks, apiV3FormValidator, async(req, res) => {
- const { pageId, bool } = req.body;
- let bookmark;
- try {
- const page = await Page.findByIdAndViewer(pageId, req.user);
- if (page == null) {
- return res.apiv3Err(`Page '${pageId}' is not found or forbidden`);
- }
- if (bool) {
- bookmark = await Bookmark.add(page, req.user);
- }
- else {
- bookmark = await Bookmark.removeBookmark(page, req.user);
- }
- }
- catch (err) {
- logger.error('update-bookmark-failed', err);
- return res.apiv3Err(err, 500);
- }
- bookmark.depopulate('page');
- bookmark.depopulate('user');
- return res.apiv3({ bookmark });
- });
- /**
- * @swagger
- *
- * /count-bookmarks:
- * get:
- * tags: [Bookmarks]
- * summary: /bookmarks
- * description: Count bookmsrks
- * requestBody:
- * content:
- * application/json:
- * schema:
- * $ref: '#/components/schemas/BookmarkParams'
- * responses:
- * 200:
- * description: Succeeded to count bookmarks.
- * content:
- * application/json:
- * schema:
- * $ref: '#/components/schemas/Bookmark'
- */
- return router;
- };
|