main.js 1.9 KB

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