2015-08-31 15:09:53 +02:00
|
|
|
// This collection shouldn't be manipulated directly by instead throw the
|
|
|
|
// `UnsavedEdits` API on the client.
|
|
|
|
UnsavedEditCollection = new Mongo.Collection('unsaved-edits');
|
|
|
|
|
2019-06-26 17:47:27 -05:00
|
|
|
UnsavedEditCollection.attachSchema(
|
|
|
|
new SimpleSchema({
|
|
|
|
fieldName: {
|
|
|
|
type: String,
|
2016-04-22 00:59:05 +02:00
|
|
|
},
|
2019-06-26 17:47:27 -05:00
|
|
|
docId: {
|
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
value: {
|
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
userId: {
|
|
|
|
type: String,
|
|
|
|
// eslint-disable-next-line consistent-return
|
|
|
|
autoValue() {
|
|
|
|
if (this.isInsert && !this.isSet) {
|
|
|
|
return this.userId;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
createdAt: {
|
|
|
|
type: Date,
|
|
|
|
optional: true,
|
|
|
|
// eslint-disable-next-line consistent-return
|
|
|
|
autoValue() {
|
|
|
|
if (this.isInsert) {
|
|
|
|
return new Date();
|
2019-09-05 12:29:45 -05:00
|
|
|
} else if (this.isUpsert) {
|
|
|
|
return { $setOnInsert: new Date() };
|
2019-06-26 17:47:27 -05:00
|
|
|
} else {
|
|
|
|
this.unset();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
modifiedAt: {
|
|
|
|
type: Date,
|
|
|
|
denyUpdate: false,
|
|
|
|
// eslint-disable-next-line consistent-return
|
|
|
|
autoValue() {
|
|
|
|
if (this.isInsert || this.isUpsert || this.isUpdate) {
|
|
|
|
return new Date();
|
|
|
|
} else {
|
|
|
|
this.unset();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2019-06-28 12:52:09 -05:00
|
|
|
}),
|
2019-06-26 17:47:27 -05:00
|
|
|
);
|
2015-08-31 15:09:53 +02:00
|
|
|
|
|
|
|
if (Meteor.isServer) {
|
|
|
|
function isAuthor(userId, doc, fieldNames = []) {
|
|
|
|
return userId === doc.userId && fieldNames.indexOf('userId') === -1;
|
|
|
|
}
|
2017-02-18 02:18:39 +01:00
|
|
|
Meteor.startup(() => {
|
2021-10-09 15:56:16 +03:00
|
|
|
UnsavedEditCollection._collection.createIndex({ modifiedAt: -1 });
|
|
|
|
UnsavedEditCollection._collection.createIndex({ userId: 1 });
|
2017-02-18 02:18:39 +01:00
|
|
|
});
|
2015-08-31 15:09:53 +02:00
|
|
|
UnsavedEditCollection.allow({
|
|
|
|
insert: isAuthor,
|
|
|
|
update: isAuthor,
|
|
|
|
remove: isAuthor,
|
2015-09-03 23:12:46 +02:00
|
|
|
fetch: ['userId'],
|
2015-08-31 15:09:53 +02:00
|
|
|
});
|
|
|
|
}
|
2019-06-26 17:47:27 -05:00
|
|
|
|
|
|
|
export default UnsavedEditCollection;
|