users.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. Meteor.publish('user-miniprofile', function (usernames) {
  2. check(usernames, Array);
  3. // eslint-disable-next-line no-console
  4. // console.log('usernames:', usernames);
  5. const ret = ReactiveCache.getUsers(
  6. {
  7. $or: [
  8. { username: { $in: usernames } },
  9. { importUsernames: { $in: usernames } },
  10. ],
  11. },
  12. {
  13. fields: {
  14. ...Users.safeFields,
  15. importUsernames: 1,
  16. },
  17. },
  18. true,
  19. );
  20. return ret;
  21. });
  22. Meteor.publish('user-admin', function () {
  23. const ret = Meteor.users.find(this.userId, {
  24. fields: {
  25. isAdmin: 1,
  26. teams: 1,
  27. orgs: 1,
  28. authenticationMethod: 1,
  29. },
  30. });
  31. return ret;
  32. });
  33. Meteor.publish('user-authenticationMethod', function (match) {
  34. check(match, String);
  35. const ret = ReactiveCache.getUsers(
  36. { $or: [{ _id: match }, { email: match }, { username: match }] },
  37. {
  38. fields: {
  39. authenticationMethod: 1,
  40. teams: 1,
  41. orgs: 1,
  42. },
  43. },
  44. true,
  45. );
  46. return ret;
  47. });
  48. // Secure user search publication for board sharing
  49. Meteor.publish('user-search', function (searchTerm) {
  50. check(searchTerm, String);
  51. // Only allow logged-in users to search for other users
  52. if (!this.userId) {
  53. return this.ready();
  54. }
  55. // Create a regex for case-insensitive search
  56. const searchRegex = new RegExp(searchTerm, 'i');
  57. // Search for users by username, fullname, or email
  58. const ret = ReactiveCache.getUsers(
  59. {
  60. $or: [
  61. { username: searchRegex },
  62. { 'profile.fullname': searchRegex },
  63. { 'emails.address': searchRegex }
  64. ]
  65. },
  66. {
  67. fields: {
  68. _id: 1,
  69. username: 1,
  70. 'profile.fullname': 1,
  71. 'profile.avatarUrl': 1,
  72. 'profile.initials': 1,
  73. 'emails.address': 1,
  74. 'emails.verified': 1,
  75. authenticationMethod: 1,
  76. isAdmin: 1,
  77. loginDisabled: 1,
  78. teams: 1,
  79. orgs: 1,
  80. },
  81. },
  82. true,
  83. );
  84. return ret;
  85. });
  86. // update last connection date and last connection average time (in seconds) for a user
  87. // function UpdateLastConnectionDateAndLastConnectionAverageTime(lstUsers) {
  88. // let lastConnectionAverageTime;
  89. // lstUsers.forEach((currentUser) => {
  90. // lastConnectionAverageTime =
  91. // currentUser.lastConnectionAverageTimeInSecs !== undefined
  92. // ? currentUser.lastConnectionAverageTimeInSecs
  93. // : 0;
  94. // lastConnectionAverageTime =
  95. // currentUser.lastConnectionDate !== undefined
  96. // ? ((new Date().getTime() - currentUser.lastConnectionDate.getTime()) /
  97. // 1000 +
  98. // lastConnectionAverageTime) /
  99. // 2
  100. // : 0;
  101. // Users.update(currentUser._id, {
  102. // $set: {
  103. // lastConnectionDate: new Date(),
  104. // lastConnectionAverageTimeInSecs: parseInt(lastConnectionAverageTime),
  105. // },
  106. // });
  107. // });
  108. // }
  109. if (Meteor.isServer) {
  110. /* Got this error, so using this code only when metrics enabled with process.env... below
  111. I20221023-09:15:09.599(3)? Exception in onConnection callback: TypeError: Cannot read property 'userId' of null
  112. I20221023-09:15:09.601(3)? at server/publications/users.js:106:44
  113. I20221023-09:15:09.601(3)? at Array.forEach (<anonymous>)
  114. I20221023-09:15:09.601(3)? at server/publications/users.js:102:46
  115. I20221023-09:15:09.601(3)? at runWithEnvironment (packages/meteor.js:1347:24)
  116. I20221023-09:15:09.601(3)? at packages/meteor.js:1360:14
  117. I20221023-09:15:09.601(3)? at packages/ddp-server/livedata_server.js:1614:9
  118. I20221023-09:15:09.601(3)? at Hook.forEach (packages/callback-hook/hook.js:110:15)
  119. I20221023-09:15:09.601(3)? at Hook.each (packages/callback-hook/hook.js:122:17)
  120. I20221023-09:15:09.602(3)? at Server._handleConnect (packages/ddp-server/livedata_server.js:1612:27)
  121. I20221023-09:15:09.602(3)? at packages/ddp-server/livedata_server.js:1496:18
  122. */
  123. if (process.env.WEKAN_METRICS_ACCEPTED_IP_ADDRESS) {
  124. /*
  125. Meteor.onConnection(function (connection) {
  126. // console.log(
  127. // 'Meteor.server.stream_server.open_sockets',
  128. // Meteor.server.stream_server.open_sockets,
  129. // );
  130. //console.log('connection.Id on connection...', connection.id);
  131. // connection.onClose(() => {
  132. // console.log('connection.Id on close...', connection.id);
  133. // // Get all user that were connected to this socket
  134. // // And update last connection date and last connection average time (in seconds) for each user
  135. // let lstOfUserThatWasConnectedToThisSocket = ReactiveCache.getUsers({
  136. // lastconnectedSocketId: connection.id,
  137. // }, {}, true).fetch();
  138. // if (
  139. // lstOfUserThatWasConnectedToThisSocket !== undefined &&
  140. // lstOfUserThatWasConnectedToThisSocket.length > 0
  141. // ) {
  142. // console.log({ lstOfUserThatWasConnectedToThisSocket });
  143. // UpdateLastConnectionDateAndLastConnectionAverageTime(
  144. // lstOfUserThatWasConnectedToThisSocket,
  145. // );
  146. // }
  147. // });
  148. // Meteor.server.stream_server.open_sockets.forEach((socket) =>
  149. // console.log('meteor session', socket._meteorSession.userId),
  150. // );
  151. // update last connected user date (needed for one of the KPI)
  152. Meteor.server.stream_server.open_sockets.forEach(
  153. (socket) => {
  154. if (socket?._meteorSession?.userId) {
  155. Users.update(socket._meteorSession.userId, {
  156. $set: {
  157. lastConnectionDate: new Date(),
  158. },
  159. });
  160. }
  161. });
  162. });
  163. */
  164. }
  165. }