unsavedEdits.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 {
  32. this.unset();
  33. }
  34. },
  35. },
  36. modifiedAt: {
  37. type: Date,
  38. denyUpdate: false,
  39. // eslint-disable-next-line consistent-return
  40. autoValue() {
  41. if (this.isInsert || this.isUpsert || this.isUpdate) {
  42. return new Date();
  43. } else {
  44. this.unset();
  45. }
  46. },
  47. },
  48. }),
  49. );
  50. if (Meteor.isServer) {
  51. function isAuthor(userId, doc, fieldNames = []) {
  52. return userId === doc.userId && fieldNames.indexOf('userId') === -1;
  53. }
  54. Meteor.startup(() => {
  55. UnsavedEditCollection._collection._ensureIndex({ modifiedAt: -1 });
  56. UnsavedEditCollection._collection._ensureIndex({ userId: 1 });
  57. });
  58. UnsavedEditCollection.allow({
  59. insert: isAuthor,
  60. update: isAuthor,
  61. remove: isAuthor,
  62. fetch: ['userId'],
  63. });
  64. }
  65. export default UnsavedEditCollection;