2
0

page.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. module.exports = function(app, models) {
  2. var mongoose = require('mongoose')
  3. , debug = require('debug')('crowi:models:page')
  4. , ObjectId = mongoose.Schema.Types.ObjectId
  5. , GRANT_PUBLIC = 1
  6. , GRANT_RESTRICTED = 2
  7. , GRANT_SPECIFIED = 3
  8. , GRANT_OWNER = 4
  9. , PAGE_GRANT_ERROR = 1
  10. , pageSchema;
  11. function populatePageData(pageData, revisionId, callback) {
  12. debug('pageData', pageData.revision);
  13. if (revisionId) {
  14. pageData.revision = revisionId;
  15. }
  16. pageData.latestRevision = pageData.revision;
  17. pageData.populate([
  18. {path: 'revision', model: 'Revision'},
  19. {path: 'liker', options: { limit: 11 }},
  20. {path: 'seenUsers', options: { limit: 11 }},
  21. ], function (err, pageData) {
  22. models.Page.populate(pageData, {path: 'revision.author', model: 'User'}, function(err, pageData) {
  23. return callback(err, pageData);
  24. });
  25. });
  26. }
  27. pageSchema = new mongoose.Schema({
  28. path: { type: String, required: true, index: true },
  29. revision: { type: ObjectId, ref: 'Revision' },
  30. redirectTo: { type: String, index: true },
  31. grant: { type: Number, default: GRANT_PUBLIC, index: true },
  32. grantedUsers: [{ type: ObjectId, ref: 'User' }],
  33. liker: [{ type: ObjectId, ref: 'User', index: true }],
  34. seenUsers: [{ type: ObjectId, ref: 'User', index: true }],
  35. createdAt: { type: Date, default: Date.now },
  36. updatedAt: Date
  37. });
  38. pageSchema.methods.isPublic = function() {
  39. if (!this.grant || this.grant == GRANT_PUBLIC) {
  40. return true;
  41. }
  42. return false;
  43. };
  44. pageSchema.methods.isGrantedFor = function(userData) {
  45. if (this.isPublic()) {
  46. return true;
  47. }
  48. if (this.grantedUsers.indexOf(userData._id) >= 0) {
  49. return true;
  50. }
  51. return false;
  52. };
  53. pageSchema.methods.isLatestRevision = function() {
  54. // populate されていなくて判断できない
  55. if (!this.latestRevision || !this.revision) {
  56. return true;
  57. }
  58. return (this.latestRevision == this.revision._id.toString());
  59. };
  60. pageSchema.methods.isUpdatable = function(previousRevision) {
  61. var revision = this.latestRevision || this.revision;
  62. if (revision != previousRevision) {
  63. return false;
  64. }
  65. return true;
  66. };
  67. pageSchema.methods.isLiked = function(userData) {
  68. if (undefined === this.populated('liker')) {
  69. if (this.liker.indexOf(userData._id) != -1) {
  70. return true;
  71. }
  72. return true;
  73. } else {
  74. return this.liker.some(function(likedUser) {
  75. return likedUser._id.toString() == userData._id.toString();
  76. });
  77. }
  78. };
  79. pageSchema.methods.like = function(userData, callback) {
  80. var self = this;
  81. if (undefined === this.populated('liker')) {
  82. var added = this.liker.addToSet(userData._id);
  83. if (added.length > 0) {
  84. this.save(function(err, data) {
  85. debug('liker updated!', added);
  86. return callback(err, data);
  87. });
  88. } else {
  89. debug('liker not updated');
  90. return callback(null, this);
  91. }
  92. } else {
  93. models.Page.update(
  94. {_id: self._id},
  95. { $addToSet: { liker: userData._id }},
  96. function(err, numAffected, raw) {
  97. debug('Updated liker,', err, numAffected, raw);
  98. callback(null, self);
  99. }
  100. );
  101. }
  102. };
  103. pageSchema.methods.unlike = function(userData, callback) {
  104. var self = this;
  105. if (undefined === this.populated('liker')) {
  106. var removed = this.liker.pull(userData._id);
  107. if (removed.length > 0) {
  108. this.save(function(err, data) {
  109. debug('unlike updated!', removed);
  110. return callback(err, data);
  111. });
  112. } else {
  113. debug('unlike not updated');
  114. callback(null, this);
  115. }
  116. } else {
  117. models.Page.update(
  118. {_id: self._id},
  119. { $pull: { liker: userData._id }},
  120. function(err, numAffected, raw) {
  121. debug('Updated liker (unlike)', err, numAffected, raw);
  122. callback(null, self);
  123. }
  124. );
  125. }
  126. };
  127. pageSchema.methods.seen = function(userData, callback) {
  128. var self = this;
  129. if (undefined === this.populated('seenUsers')) {
  130. var added = this.seenUsers.addToSet(userData._id);
  131. if (added.length > 0) {
  132. this.save(function(err, data) {
  133. debug('seenUsers updated!', added);
  134. return callback(err, data);
  135. });
  136. } else {
  137. debug('seenUsers not updated');
  138. return callback(null, this);
  139. }
  140. } else {
  141. models.Page.update(
  142. {_id: self._id},
  143. { $addToSet: { seenUsers: userData._id }},
  144. function(err, numAffected, raw) {
  145. debug('Updated seenUsers,', err, numAffected, raw);
  146. callback(null, self);
  147. }
  148. );
  149. }
  150. };
  151. pageSchema.statics.getGrantLabels = function() {
  152. var grantLabels = {};
  153. grantLabels[GRANT_PUBLIC] = '公開';
  154. grantLabels[GRANT_RESTRICTED] = 'リンクを知っている人のみ';
  155. //grantLabels[GRANT_SPECIFIED] = '特定ユーザーのみ';
  156. grantLabels[GRANT_OWNER] = '自分のみ';
  157. return grantLabels;
  158. };
  159. pageSchema.statics.normalizePath = function(path) {
  160. if (!path.match(/^\//)) {
  161. path = '/' + path;
  162. }
  163. return path;
  164. };
  165. pageSchema.statics.isCreatableName = function(name) {
  166. var forbiddenPages = [
  167. /\^|\$|\*|\+/,
  168. /^\/_api\/.*/,
  169. /^\/\-\/.*/,
  170. /^\/_r\/.*/,
  171. /.+\/edit$/,
  172. /^\/(register|login|logout|admin|me|files|trash|paste|comments).+/,
  173. ];
  174. var isCreatable = true;
  175. forbiddenPages.forEach(function(page) {
  176. var pageNameReg = new RegExp(page);
  177. if (name.match(pageNameReg)) {
  178. isCreatable = false;
  179. return ;
  180. }
  181. });
  182. return isCreatable;
  183. };
  184. pageSchema.statics.updateRevision = function(pageId, revisionId, cb) {
  185. this.update({_id: pageId}, {revision: revisionId}, {}, function(err, data) {
  186. cb(err, data);
  187. });
  188. };
  189. pageSchema.statics.findUpdatedList = function(offset, limit, cb) {
  190. this
  191. .find({})
  192. .sort('updatedAt', -1)
  193. .skip(offset)
  194. .limit(limit)
  195. .exec(function(err, data) {
  196. cb(err, data);
  197. });
  198. };
  199. pageSchema.statics.findPageById = function(id, userData, cb) {
  200. var Page = this;
  201. this.findOne({_id: id}, function(err, pageData) {
  202. if (pageData === null) {
  203. return cb(new Error('Page Not Found'), null);
  204. }
  205. if (!pageData.isGrantedFor(userData)) {
  206. return cb(PAGE_GRANT_ERROR, null);
  207. }
  208. return populatePageData(pageData, null, cb);
  209. });
  210. };
  211. pageSchema.statics.findPage = function(path, userData, revisionId, options, cb) {
  212. var Page = this;
  213. this.findOne({path: path}, function(err, pageData) {
  214. if (pageData === null) {
  215. return cb(new Error('Page Not Found'), null);
  216. }
  217. if (!pageData.isGrantedFor(userData)) {
  218. return cb(PAGE_GRANT_ERROR, null);
  219. }
  220. return populatePageData(pageData, revisionId, cb);
  221. });
  222. };
  223. pageSchema.statics.findListByPageIds = function(ids, options, cb) {
  224. };
  225. pageSchema.statics.findListByStartWith = function(path, userData, options, cb) {
  226. if (!options) {
  227. options = {sort: 'updatedAt', desc: -1, offset: 0, limit: 50};
  228. }
  229. var opt = {
  230. sort: options.sort || 'updatedAt',
  231. desc: options.desc || -1,
  232. offset: options.offset || 0,
  233. limit: options.limit || 50
  234. };
  235. var sortOpt = {};
  236. sortOpt[opt.sort] = opt.desc;
  237. var queryReg = new RegExp('^' + path);
  238. var sliceOption = options.revisionSlice || {$slice: 1};
  239. var q = this.find({
  240. path: queryReg,
  241. redirectTo: null,
  242. $or: [
  243. {grant: null},
  244. {grant: GRANT_PUBLIC},
  245. {grant: GRANT_RESTRICTED, grantedUsers: userData._id},
  246. {grant: GRANT_SPECIFIED, grantedUsers: userData._id},
  247. {grant: GRANT_OWNER, grantedUsers: userData._id},
  248. ],
  249. })
  250. .populate('revision')
  251. .sort(sortOpt)
  252. .skip(opt.offset)
  253. .limit(opt.limit);
  254. q.exec(function(err, data) {
  255. cb(err, data);
  256. });
  257. };
  258. pageSchema.statics.updatePage = function(page, updateData, cb) {
  259. // TODO foreach して save
  260. this.update({_id: page._id}, {$set: updateData}, function(err, data) {
  261. return cb(err, data);
  262. });
  263. };
  264. pageSchema.statics.updateGrant = function(page, grant, userData, cb) {
  265. this.update({_id: page._id}, {$set: {grant: grant}}, function(err, data) {
  266. if (grant == GRANT_PUBLIC) {
  267. page.grantedUsers = [];
  268. } else {
  269. page.grantedUsers = [];
  270. page.grantedUsers.push(userData._id);
  271. }
  272. page.save(function(err, data) {
  273. return cb(err, data);
  274. });
  275. });
  276. };
  277. // Instance method でいいのでは
  278. pageSchema.statics.pushToGrantedUsers = function(page, userData, cb) {
  279. if (!page.grantedUsers || !Array.isArray(page.grantedUsers)) {
  280. page.grantedUsers = [];
  281. }
  282. page.grantedUsers.push(userData._id);
  283. page.save(function(err, data) {
  284. return cb(err, data);
  285. });
  286. };
  287. pageSchema.statics.pushRevision = function(pageData, newRevision, user, cb) {
  288. pageData.revision = newRevision._id;
  289. pageData.updatedAt = Date.now();
  290. newRevision.save(function(err, newRevision) {
  291. pageData.save(function(err, data) {
  292. if (err) {
  293. console.log('Error on save page data', err);
  294. cb(err, null);
  295. return;
  296. }
  297. cb(err, data);
  298. });
  299. });
  300. };
  301. pageSchema.statics.create = function(path, body, user, options, cb) {
  302. var Page = this
  303. , format = options.format || 'markdown'
  304. , redirectTo = options.redirectTo || null;
  305. this.findOne({path: path}, function(err, pageData) {
  306. if (pageData) {
  307. cb(new Error('Cannot create new page to existed path'), null);
  308. return;
  309. }
  310. var newPage = new Page();
  311. newPage.path = path;
  312. newPage.createdAt = Date.now();
  313. newPage.updatedAt = Date.now();
  314. newPage.redirectTo = redirectTo;
  315. newPage.save(function (err, newPage) {
  316. var newRevision = models.Revision.prepareRevision(newPage, body, user, {format: format});
  317. Page.pushRevision(newPage, newRevision, user, function(err, data) {
  318. if (err) {
  319. console.log('Push Revision Error on create page', err);
  320. }
  321. cb(err, data);
  322. return;
  323. });
  324. });
  325. });
  326. };
  327. pageSchema.statics.rename = function(pageData, newPageName, user, options, cb) {
  328. var Page = this
  329. , path = pageData.path
  330. , createRedirectPage = options.createRedirectPage || 0
  331. , moveUnderTrees = options.moveUnderTrees || 0;
  332. // pageData の path を変更
  333. this.updatePage(pageData, {updatedAt: Date.now(), path: newPageName}, function(err, data) {
  334. if (err) {
  335. return cb(err, null);
  336. }
  337. // reivisions の path を変更
  338. models.Revision.updateRevisionListByPath(path, {path: newPageName}, {}, function(err, data) {
  339. if (err) {
  340. return cb(err, null);
  341. }
  342. pageData.path = newPageName;
  343. if (createRedirectPage) {
  344. Page.create(path, 'redirect ' + newPageName, user, {redirectTo: newPageName}, function(err, data) {
  345. // @TODO error handling
  346. return cb(err, pageData);
  347. });
  348. } else {
  349. return cb(err, pageData);
  350. }
  351. });
  352. });
  353. };
  354. pageSchema.statics.getHistories = function() {
  355. // TODO
  356. return;
  357. };
  358. pageSchema.statics.GRANT_PUBLIC = GRANT_PUBLIC;
  359. pageSchema.statics.GRANT_RESTRICTED = GRANT_RESTRICTED;
  360. pageSchema.statics.GRANT_SPECIFIED = GRANT_SPECIFIED;
  361. pageSchema.statics.GRANT_OWNER = GRANT_OWNER;
  362. pageSchema.statics.PAGE_GRANT_ERROR = PAGE_GRANT_ERROR;
  363. models.Page = mongoose.model('Page', pageSchema);
  364. return models.Page;
  365. };