main.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Code that is shared between client and server, i.e. sent to both.
  2. // main.js is loaded last:
  3. // http://docs.meteor.com/#/full/structuringyourapp
  4. // Methods that provide write access to the data.
  5. Meteor.methods({
  6. addDoc: function () {
  7. Meteor._debug("addDoc, this", this);
  8. // Not logged-in.
  9. if (!this.userId) {
  10. return;
  11. }
  12. else {
  13. let doc = {
  14. owner: this.userId,
  15. createdOn: new Date(),
  16. title: "my new doc"
  17. };
  18. const docid = Documents.insert(doc);
  19. return docid;
  20. }
  21. },
  22. // Changing doc privacy settings.
  23. updateDocPrivacy: function (doc) {
  24. Meteor._debug("updatedocPrivacy", this.userId, doc)
  25. if (!this.userId) {
  26. return;
  27. }
  28. let res = Documents.update({ _id: doc._id, owner: this.userId }, { $set: { isPrivate: doc.isPrivate } });
  29. Meteor._debug("update", res);
  30. },
  31. // Adding editors to a document.
  32. addEditingUser: function (docid) {
  33. let doc = Documents.findOne({ _id: docid });
  34. if (!doc) {
  35. // No doc: give up.
  36. return;
  37. }
  38. if (!this.userId) {
  39. // No logged-in user: give up.
  40. return;
  41. }
  42. // Now I have a doc and possibly a user.
  43. let user = Meteor.user().profile;
  44. let eUsers = EditingUsers.findOne({ docId: doc._id });
  45. // No editing users have been stored yet.
  46. if (!eUsers) {
  47. eUsers = {
  48. docId: doc._id,
  49. users: {}
  50. };
  51. }
  52. user.lastEdit = new Date();
  53. eUsers.users[this.userId] = user;
  54. // Upsert: insert or update if filter matches.
  55. EditingUsers.upsert({ _id: eUsers._id }, eUsers);
  56. }
  57. });