main.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. addComment: function (comment) {
  7. Meteor._debug("addComment method running", comment);
  8. // We are connected.
  9. if (this.userId) {
  10. comment.createdOn = new Date();
  11. comment.userId = this.userId;
  12. return Comments.insert(comment);
  13. }
  14. return null;
  15. },
  16. addDoc: function () {
  17. Meteor._debug("addDoc, this", this);
  18. // Not logged-in.
  19. if (!this.userId) {
  20. return;
  21. }
  22. else {
  23. let doc = {
  24. owner: this.userId,
  25. createdOn: new Date(),
  26. title: "my new doc"
  27. };
  28. const docid = Documents.insert(doc);
  29. return docid;
  30. }
  31. },
  32. // Changing doc privacy settings.
  33. updateDocPrivacy: function (doc) {
  34. Meteor._debug("updatedocPrivacy", this.userId, doc)
  35. if (!this.userId) {
  36. return;
  37. }
  38. let res = Documents.update({ _id: doc._id, owner: this.userId }, { $set: { isPrivate: doc.isPrivate } });
  39. Meteor._debug("update", res);
  40. },
  41. // Adding editors to a document.
  42. addEditingUser: function (docid) {
  43. let doc = Documents.findOne({ _id: docid });
  44. if (!doc) {
  45. // No doc: give up.
  46. return;
  47. }
  48. if (!this.userId) {
  49. // No logged-in user: give up.
  50. return;
  51. }
  52. // Now I have a doc and possibly a user.
  53. let user = Meteor.user().profile;
  54. let eUsers = EditingUsers.findOne({ docId: doc._id });
  55. // No editing users have been stored yet.
  56. if (!eUsers) {
  57. eUsers = {
  58. docId: doc._id,
  59. users: {}
  60. };
  61. }
  62. user.lastEdit = new Date();
  63. eUsers.users[this.userId] = user;
  64. // Upsert: insert or update if filter matches.
  65. EditingUsers.upsert({ _id: eUsers._id }, eUsers);
  66. }
  67. });