unsavedEdits.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // This collection shouldn't be manipulated directly by instead throw the
  2. // `UnsavedEdits` API on the client.
  3. UnsavedEditCollection = new Mongo.Collection('unsaved-edits');
  4. UnsavedEditCollection.attachSchema(
  5. new SimpleSchema({
  6. fieldName: {
  7. type: String,
  8. },
  9. docId: {
  10. type: String,
  11. },
  12. value: {
  13. type: String,
  14. },
  15. userId: {
  16. type: String,
  17. // eslint-disable-next-line consistent-return
  18. autoValue() {
  19. if (this.isInsert && !this.isSet) {
  20. return this.userId;
  21. }
  22. },
  23. },
  24. createdAt: {
  25. type: Date,
  26. optional: true,
  27. // eslint-disable-next-line consistent-return
  28. autoValue() {
  29. if (this.isInsert) {
  30. return new Date();
  31. } else if (this.isUpsert) {
  32. return { $setOnInsert: new Date() };
  33. } else {
  34. this.unset();
  35. }
  36. },
  37. },
  38. modifiedAt: {
  39. type: Date,
  40. denyUpdate: false,
  41. // eslint-disable-next-line consistent-return
  42. autoValue() {
  43. if (this.isInsert || this.isUpsert || this.isUpdate) {
  44. return new Date();
  45. } else {
  46. this.unset();
  47. }
  48. },
  49. },
  50. }),
  51. );
  52. if (Meteor.isServer) {
  53. function isAuthor(userId, doc, fieldNames = []) {
  54. return userId === doc.userId && fieldNames.indexOf('userId') === -1;
  55. }
  56. Meteor.startup(() => {
  57. UnsavedEditCollection._collection.createIndex({ modifiedAt: -1 });
  58. UnsavedEditCollection._collection.createIndex({ userId: 1 });
  59. });
  60. UnsavedEditCollection.allow({
  61. insert: isAuthor,
  62. update: isAuthor,
  63. remove: isAuthor,
  64. fetch: ['userId'],
  65. });
  66. }
  67. export default UnsavedEditCollection;