ChatManager.mjs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import UserInfoController from '../User/UserInfoController.mjs'
  2. import UserGetter from '../User/UserGetter.mjs'
  3. import { callbackify } from '@overleaf/promise-utils'
  4. async function injectUserInfoIntoThreads(threads) {
  5. const userIds = new Set()
  6. for (const thread of Object.values(threads)) {
  7. if (thread.resolved) {
  8. userIds.add(thread.resolved_by_user_id)
  9. }
  10. for (const message of thread.messages) {
  11. userIds.add(message.user_id)
  12. }
  13. }
  14. const projection = {
  15. _id: true,
  16. first_name: true,
  17. last_name: true,
  18. email: true,
  19. }
  20. const users = await UserGetter.promises.getUsers(userIds, projection)
  21. const usersById = new Map()
  22. for (const user of users) {
  23. usersById.set(
  24. user._id.toString(),
  25. UserInfoController.formatPersonalInfo(user)
  26. )
  27. }
  28. for (const thread of Object.values(threads)) {
  29. if (thread.resolved) {
  30. thread.resolved_by_user = usersById.get(thread.resolved_by_user_id)
  31. }
  32. for (const message of thread.messages) {
  33. message.user = usersById.get(message.user_id)
  34. }
  35. }
  36. return threads
  37. }
  38. export default {
  39. injectUserInfoIntoThreads: callbackify(injectUserInfoIntoThreads),
  40. promises: {
  41. injectUserInfoIntoThreads,
  42. },
  43. }