summaryrefslogtreecommitdiffstats
path: root/models/unsavedEdits.js
blob: 87a70e22bc7656344e1c4a36fe0c20acf560db68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// This collection shouldn't be manipulated directly by instead throw the
// `UnsavedEdits` API on the client.
UnsavedEditCollection = new Mongo.Collection('unsaved-edits');

UnsavedEditCollection.attachSchema(new SimpleSchema({
  fieldName: {
    type: String,
  },
  docId: {
    type: String,
  },
  value: {
    type: String,
  },
  userId: {
    type: String,
  },
}));

if (Meteor.isServer) {
  function isAuthor(userId, doc, fieldNames = []) {
    return userId === doc.userId && fieldNames.indexOf('userId') === -1;
  }
  UnsavedEditCollection.allow({
    insert: isAuthor,
    update: isAuthor,
    remove: isAuthor,
    fetch: ['userId'],
  });
}

UnsavedEditCollection.before.insert((userId, doc) => {
  doc.userId = userId;
});