user.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * @swagger
  3. *
  4. * components:
  5. * schemas:
  6. * User:
  7. * description: User
  8. * type: object
  9. * properties:
  10. * __v:
  11. * type: number
  12. * description: record version
  13. * example: 0
  14. * _id:
  15. * type: string
  16. * description: user ID
  17. * example: 5ae5fccfc5577b0004dbd8ab
  18. * lang:
  19. * type: string
  20. * description: language
  21. * example: 'en_US'
  22. * status:
  23. * type: integer
  24. * description: status
  25. * example: 0
  26. * admin:
  27. * type: boolean
  28. * description: whether the admin
  29. * example: false
  30. * email:
  31. * type: string
  32. * description: E-Mail address
  33. * example: alice@aaa.aaa
  34. * username:
  35. * type: string
  36. * description: username
  37. * example: alice
  38. * name:
  39. * type: string
  40. * description: full name
  41. * example: Alice
  42. * createdAt:
  43. * type: string
  44. * description: date created at
  45. * example: 2010-01-01T00:00:00.000Z
  46. */
  47. module.exports = function(crowi, app) {
  48. const User = crowi.model('User');
  49. const Bookmark = crowi.model('Bookmark');
  50. const ApiResponse = require('../util/apiResponse');
  51. const actions = {};
  52. const api = {};
  53. actions.api = api;
  54. api.bookmarks = function(req, res) {
  55. const options = {
  56. skip: req.query.offset || 0,
  57. limit: req.query.limit || 50,
  58. };
  59. Bookmark.findByUser(req.user, options, (err, bookmarks) => {
  60. res.json(bookmarks);
  61. });
  62. };
  63. api.checkUsername = function(req, res) {
  64. const username = req.query.username;
  65. User.findUserByUsername(username)
  66. .then((userData) => {
  67. if (userData) {
  68. return res.json({ valid: false });
  69. }
  70. return res.json({ valid: true });
  71. })
  72. .catch((err) => {
  73. return res.json({ valid: true });
  74. });
  75. };
  76. /**
  77. * @swagger
  78. *
  79. * /users.list:
  80. * get:
  81. * tags: [Users, CrowiCompatibles]
  82. * operationId: listUsersV1
  83. * summary: /users.list
  84. * description: Get list of users
  85. * parameters:
  86. * - in: query
  87. * name: user_ids
  88. * schema:
  89. * type: string
  90. * description: user IDs
  91. * example: 5e06fcc7516d64004dbf4da6,5e098d53baa2ac004e7d24ad
  92. * responses:
  93. * 200:
  94. * description: Succeeded to get list of users.
  95. * content:
  96. * application/json:
  97. * schema:
  98. * properties:
  99. * ok:
  100. * $ref: '#/components/schemas/V1Response/properties/ok'
  101. * users:
  102. * type: array
  103. * items:
  104. * $ref: '#/components/schemas/User'
  105. * description: user list
  106. * 403:
  107. * $ref: '#/components/responses/403'
  108. * 500:
  109. * $ref: '#/components/responses/500'
  110. */
  111. /**
  112. * @api {get} /users.list Get user list
  113. * @apiName GetUserList
  114. * @apiGroup User
  115. *
  116. * @apiParam {String} user_ids
  117. */
  118. api.list = async function(req, res) {
  119. const userIds = req.query.user_ids || null; // TODO: handling
  120. let userFetcher;
  121. if (!userIds || userIds.split(',').length <= 0) {
  122. userFetcher = User.findAllUsers();
  123. }
  124. else {
  125. userFetcher = User.findUsersByIds(userIds.split(','));
  126. }
  127. const data = {};
  128. try {
  129. const users = await userFetcher;
  130. data.users = users.map((user) => {
  131. // omit email
  132. if (user.isEmailPublished !== true) { // compare to 'true' because Crowi original data doesn't have 'isEmailPublished'
  133. user.email = undefined;
  134. }
  135. return user.toObject({ virtuals: true });
  136. });
  137. }
  138. catch (err) {
  139. return res.json(ApiResponse.error(err));
  140. }
  141. return res.json(ApiResponse.success(data));
  142. };
  143. return actions;
  144. };