users.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. Users = Meteor.users;
  2. // Search a user in the complete server database by its name or username. This
  3. // is used for instance to add a new user to a board.
  4. var searchInFields = ['username', 'profile.name'];
  5. Users.initEasySearch(searchInFields, {
  6. use: 'mongo-db',
  7. returnFields: searchInFields
  8. });
  9. Users.helpers({
  10. boards: function() {
  11. return Boards.find({ userId: this._id });
  12. },
  13. starredBoards: function() {
  14. var starredBoardIds = this.profile.starredBoards || [];
  15. return Boards.find({_id: {$in: starredBoardIds}});
  16. },
  17. hasStarred: function(boardId) {
  18. var starredBoardIds = this.profile.starredBoards || [];
  19. return _.contains(starredBoardIds, boardId);
  20. },
  21. isBoardMember: function() {
  22. var board = Boards.findOne(Session.get('currentBoard'));
  23. return board && _.contains(_.pluck(board.members, 'userId'), this._id) &&
  24. _.where(board.members, {userId: this._id})[0].isActive;
  25. },
  26. isBoardAdmin: function() {
  27. var board = Boards.findOne(Session.get('currentBoard'));
  28. if (this.isBoardMember(board))
  29. return _.where(board.members, {userId: this._id})[0].isAdmin;
  30. }
  31. });
  32. Users.before.insert(function(userId, doc) {
  33. doc.profile = {};
  34. // connect profile.status default
  35. doc.profile.status = 'offline';
  36. // slugify to username
  37. //doc.username = getSlug(doc.profile.name, '');
  38. });
  39. if (Meteor.isServer) {
  40. // Each board document contains the de-normalized number of users that have
  41. // starred it. If the user star or unstar a board, we need to update this
  42. // counter.
  43. // We need to run this code on the server only, otherwise the incrementation
  44. // will be done twice.
  45. Users.after.update(function(userId, user, fieldNames) {
  46. // The `starredBoards` list is hosted on the `profile` field. If this
  47. // field hasn't been modificated we don't need to run this hook.
  48. if (! _.contains(fieldNames, 'profile'))
  49. return;
  50. // To calculate a diff of board starred ids, we get both the previous
  51. // and the newly board ids list
  52. var getStarredBoardsIds = function(doc) {
  53. return doc.profile && doc.profile.starredBoards;
  54. };
  55. var oldIds = getStarredBoardsIds(this.previous);
  56. var newIds = getStarredBoardsIds(user);
  57. // The _.difference(a, b) method returns the values from a that are not in
  58. // b. We use it to find deleted and newly inserted ids by using it in one
  59. // direction and then in the other.
  60. var incrementBoards = function(boardsIds, inc) {
  61. _.forEach(boardsIds, function(boardId) {
  62. Boards.update(boardId, {$inc: {stars: inc}});
  63. });
  64. };
  65. incrementBoards(_.difference(oldIds, newIds), -1);
  66. incrementBoards(_.difference(newIds, oldIds), +1);
  67. });
  68. // XXX i18n
  69. Users.after.insert(function(userId, doc) {
  70. var ExampleBoard = {
  71. title: 'Welcome Board',
  72. userId: doc._id,
  73. permission: 'private'
  74. };
  75. // Insert the Welcome Board
  76. Boards.insert(ExampleBoard, function(err, boardId) {
  77. _.forEach(['Basics', 'Advanced'], function(title) {
  78. var list = {
  79. title: title,
  80. boardId: boardId,
  81. userId: ExampleBoard.userId,
  82. // XXX Not certain this is a bug, but we except these fields get
  83. // inserted by the Lists.before.insert collection-hook. Since this
  84. // hook is not called in this case, we have to dublicate the logic and
  85. // set them here.
  86. archived: false,
  87. createdAt: new Date()
  88. };
  89. Lists.insert(list);
  90. });
  91. });
  92. });
  93. }