users.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. getInitials: function() {
  32. var profile = this.profile || {};
  33. if (profile.initials)
  34. return profile.initials;
  35. else if (profile.fullname) {
  36. return _.reduce(profile.fullname.split(/\s+/), function(memo, word) {
  37. return memo + word[0];
  38. }, '').toUpperCase();
  39. } else {
  40. return this.username[0].toUpperCase();
  41. }
  42. },
  43. toggleBoardStar: function(boardId) {
  44. var queryType = this.hasStarred(boardId) ? '$pull' : '$addToSet';
  45. var query = {};
  46. query[queryType] = {
  47. 'profile.starredBoards': boardId
  48. };
  49. Meteor.users.update(this._id, query);
  50. }
  51. });
  52. Meteor.methods({
  53. setUsername: function(username) {
  54. check(username, String);
  55. var nUsersWithUsername = Users.find({username: username}).count();
  56. if (nUsersWithUsername > 0) {
  57. throw new Meteor.Error('username-already-taken');
  58. } else {
  59. Users.update(this.userId, {$set: {
  60. username: username
  61. }});
  62. }
  63. }
  64. });
  65. Users.before.insert(function(userId, doc) {
  66. doc.profile = doc.profile || {};
  67. if (! doc.username && doc.profile.name) {
  68. doc.username = doc.profile.name.toLowerCase().replace(/\s/g, '');
  69. }
  70. });
  71. if (Meteor.isServer) {
  72. // Let mongoDB ensure username unicity
  73. Meteor.startup(function() {
  74. Users._collection._ensureIndex({
  75. username: 1
  76. }, { unique: true });
  77. });
  78. // Each board document contains the de-normalized number of users that have
  79. // starred it. If the user star or unstar a board, we need to update this
  80. // counter.
  81. // We need to run this code on the server only, otherwise the incrementation
  82. // will be done twice.
  83. Users.after.update(function(userId, user, fieldNames) {
  84. // The `starredBoards` list is hosted on the `profile` field. If this
  85. // field hasn't been modificated we don't need to run this hook.
  86. if (! _.contains(fieldNames, 'profile'))
  87. return;
  88. // To calculate a diff of board starred ids, we get both the previous
  89. // and the newly board ids list
  90. var getStarredBoardsIds = function(doc) {
  91. return doc.profile && doc.profile.starredBoards;
  92. };
  93. var oldIds = getStarredBoardsIds(this.previous);
  94. var newIds = getStarredBoardsIds(user);
  95. // The _.difference(a, b) method returns the values from a that are not in
  96. // b. We use it to find deleted and newly inserted ids by using it in one
  97. // direction and then in the other.
  98. var incrementBoards = function(boardsIds, inc) {
  99. _.forEach(boardsIds, function(boardId) {
  100. Boards.update(boardId, {$inc: {stars: inc}});
  101. });
  102. };
  103. incrementBoards(_.difference(oldIds, newIds), -1);
  104. incrementBoards(_.difference(newIds, oldIds), +1);
  105. });
  106. // XXX i18n
  107. Users.after.insert(function(userId, doc) {
  108. var ExampleBoard = {
  109. title: 'Welcome Board',
  110. userId: doc._id,
  111. permission: 'private'
  112. };
  113. // Insert the Welcome Board
  114. Boards.insert(ExampleBoard, function(err, boardId) {
  115. _.forEach(['Basics', 'Advanced'], function(title) {
  116. var list = {
  117. title: title,
  118. boardId: boardId,
  119. userId: ExampleBoard.userId,
  120. // XXX Not certain this is a bug, but we except these fields get
  121. // inserted by the Lists.before.insert collection-hook. Since this
  122. // hook is not called in this case, we have to dublicate the logic and
  123. // set them here.
  124. archived: false,
  125. createdAt: new Date()
  126. };
  127. Lists.insert(list);
  128. });
  129. });
  130. });
  131. }
  132. // Presence indicator
  133. if (Meteor.isClient) {
  134. Presence.state = function() {
  135. return {
  136. currentBoardId: Session.get('currentBoard')
  137. };
  138. };
  139. }