art.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. var express = require('express');
  2. var createError = require('http-errors')
  3. var router = express.Router();
  4. const models = require('../../../models');
  5. const utils = require('../../../libs/utils');
  6. const auth = require('../../../libs/auth');
  7. let artUserPopulates = Object.keys(models.Art.schema.paths)
  8. .filter(path => 'User' == models.Art.schema.path(path).options.ref)
  9. .map(path => ({ path, select: 'username name' }));
  10. console.log('artUserPopulates:', artUserPopulates);
  11. /**表头 */
  12. router.get('/pager/headers', auth.need('read:own', 'art'), utils.pager.getHeadersBuilder(models.Art));
  13. /**所有作品列表 */
  14. router.get('/pager/all', auth.need('read:any', 'art'), (req, res, next) => {
  15. (async () => {
  16. req.query.base = { drop: false };
  17. next();
  18. })().catch(next)
  19. }, utils.pager.getListBuilder(models.Art, artUserPopulates));
  20. /**回收站列表 */
  21. router.get('/pager/removed', auth.need('read:own', 'art'),
  22. (req, res, next) => {
  23. (async () => {
  24. req.query.base = { drop: true }
  25. let ac = await auth.getAc(req);
  26. if (!ac.can('user').readAny('art').granted) {
  27. req.query.base.user = req.session.user._id;
  28. }
  29. next();
  30. })().catch(next)
  31. }, utils.pager.getListBuilder(models.Art, artUserPopulates));
  32. /**我的作品列表 */
  33. router.get('/pager/my', auth.need('read:own', 'art'), (req, res, next) => {
  34. (async () => {
  35. req.query.base = {
  36. user: req.session.user._id,
  37. drop: false,
  38. }
  39. next();
  40. })().catch(next)
  41. }, utils.pager.getListBuilder(models.Art, artUserPopulates));
  42. /**获取某个用户的作品列表 */
  43. router.get('/pager/user/:username', auth.need('read:any', 'art'), (req, res, next) => {
  44. (async () => {
  45. let { username } = req.params;
  46. let user = await models.User.findOne({ username });
  47. if (!user) throw createError(404, '用户不存在');
  48. req.query.base = {
  49. user: user._id,
  50. drop: false,
  51. }
  52. next();
  53. })().catch(next)
  54. }, utils.pager.getListBuilder(models.Art, artUserPopulates));
  55. /**待发布列表*/
  56. router.get('/pager/pending', auth.need('read:any', 'art-publish'), (req, res, next) => {
  57. (async () => {
  58. req.query.base = {
  59. drop: false,
  60. status: 7000,
  61. $or: [{ publishSchedule: { $exists: false } }, { publishSchedule: null }]
  62. }
  63. next();
  64. })().catch(next)
  65. }, utils.pager.getListBuilder(models.Art, artUserPopulates));
  66. /**
  67. *
  68. * 获取作品基础信息, 排除任何二进制数据
  69. *
  70. */
  71. router.get('/:id', auth.need('read:own', 'art'), function (req, res, next) {
  72. (async function () {
  73. let user = req.session.user._id;
  74. let id = req.params.id;
  75. utils.validators.validateId(id);
  76. /**@type {Query} */
  77. let query = models.Art.findOne({ $or: [{ _id: id }, { pageId: id }] });
  78. query.populate({ path: 'bins', select: '-data' });
  79. artUserPopulates.forEach(p => {
  80. query.populate(p);
  81. })
  82. query.populate({ path: 'publishSchedule', select: 'done' });
  83. let doc = await query.exec();
  84. if (!doc) throw createError(404, '作品不存在!')
  85. let ac = await auth.getAc(req);
  86. if (!ac.can('user').readAny('art').granted && doc.user._id != req.session.user._id) {
  87. throw createError(403, '权限不足');
  88. }
  89. let out = doc.toObject();
  90. if (doc.work) { out.zip = utils.thumb.zipPath(doc._id); }
  91. if (out.upstream) {
  92. let { upstreamBase } = req.app.get('app-config');
  93. out.upstreamUrl = `${upstreamBase}/app/pages/detail/${doc._id}`;
  94. }
  95. res.json(out);
  96. })().catch(next)
  97. });
  98. /**
  99. * 更新作品tags,name
  100. * patch-meta
  101. */
  102. router.patch('/meta', auth.need('update:own', 'art'), (req, res, next) => {
  103. (async function () {
  104. let user = req.session.user._id;
  105. let { tags, name, desc, epgs, pageId, } = req.body;
  106. if (!tags || !name || !epgs || !Array.isArray(tags)) {
  107. throw createError(400, '参数错误');
  108. }
  109. tags = tags.map(t => t.trim()).filter(t => t);
  110. utils.validators.validateId(pageId, 'ID错误');
  111. let doc = await models.Art.findById(pageId);
  112. if (!doc) throw createError(404, '作品不存在!');
  113. let ac = await auth.getAc(req);
  114. let isOwner = doc.user == user;
  115. if (!ac.can('user').updateAny('art').granted && !isOwner) {
  116. throw createError(403, '权限不足');
  117. }
  118. doc.tags = tags;
  119. doc.epgs = epgs;
  120. doc.name = name;
  121. doc.desc = desc;
  122. await doc.save();
  123. res.json({
  124. msg: 'ok'
  125. })
  126. })().catch(next)
  127. });
  128. ////////other-apis//////
  129. router.get('/agg/tags', function (req, res, next) {
  130. (async function () {
  131. let tags = await models.Art.aggregate([
  132. { $project: { tags: 1 } },
  133. { $unwind: '$tags' },
  134. {
  135. $group: {
  136. _id: '$tags',
  137. count: { $sum: 1 }
  138. }
  139. },
  140. { $sort: { count: -1 } },
  141. ]);
  142. tags = tags.map(t => ({ value: t._id, label: t._id }));
  143. res.json(tags);
  144. })().catch(next)
  145. });
  146. module.exports = router;