user.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. module.exports = function(crowi, app) {
  2. const User = crowi.model('User');
  3. const Bookmark = crowi.model('Bookmark');
  4. const ApiResponse = require('../util/apiResponse');
  5. const actions = {};
  6. const api = {};
  7. actions.api = api;
  8. api.bookmarks = function(req, res) {
  9. const options = {
  10. skip: req.query.offset || 0,
  11. limit: req.query.limit || 50,
  12. };
  13. Bookmark.findByUser(req.user, options, (err, bookmarks) => {
  14. res.json(bookmarks);
  15. });
  16. };
  17. api.checkUsername = function(req, res) {
  18. const username = req.query.username;
  19. User.findUserByUsername(username)
  20. .then((userData) => {
  21. if (userData) {
  22. return res.json({ valid: false });
  23. }
  24. return res.json({ valid: true });
  25. })
  26. .catch((err) => {
  27. return res.json({ valid: true });
  28. });
  29. };
  30. /**
  31. * @swagger
  32. *
  33. * /_api/users.list:
  34. * get:
  35. * tags: [Users, apiv1]
  36. * operationId: listUsersV1
  37. * summary: /_api/users.list
  38. * description: Get list of users
  39. * parameters:
  40. * - in: query
  41. * name: user_ids
  42. * schema:
  43. * type: string
  44. * description: user IDs
  45. * example: 5e06fcc7516d64004dbf4da6,5e098d53baa2ac004e7d24ad
  46. * responses:
  47. * 200:
  48. * description: Succeeded to get list of users.
  49. * content:
  50. * application/json:
  51. * schema:
  52. * properties:
  53. * ok:
  54. * $ref: '#/components/schemas/V1Response/properties/ok'
  55. * users:
  56. * type: array
  57. * items:
  58. * $ref: '#/components/schemas/User'
  59. * description: user list
  60. * 403:
  61. * $ref: '#/components/responses/403'
  62. * 500:
  63. * $ref: '#/components/responses/500'
  64. */
  65. /**
  66. * @api {get} /users.list Get user list
  67. * @apiName GetUserList
  68. * @apiGroup User
  69. *
  70. * @apiParam {String} user_ids
  71. */
  72. api.list = async function(req, res) {
  73. const userIds = req.query.user_ids || null; // TODO: handling
  74. let userFetcher;
  75. if (!userIds || userIds.split(',').length <= 0) {
  76. userFetcher = User.findAllUsers();
  77. }
  78. else {
  79. userFetcher = User.findUsersByIds(userIds.split(','));
  80. }
  81. const data = {};
  82. try {
  83. const users = await userFetcher.populate(User.IMAGE_POPULATION);
  84. data.users = users.map((user) => {
  85. // omit email
  86. if (user.isEmailPublished !== true) { // compare to 'true' because Crowi original data doesn't have 'isEmailPublished'
  87. user.email = undefined;
  88. }
  89. return user.toObject({ virtuals: true });
  90. });
  91. }
  92. catch (err) {
  93. return res.json(ApiResponse.error(err));
  94. }
  95. return res.json(ApiResponse.success(data));
  96. };
  97. return actions;
  98. };