users.js 4.5 KB

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