Source: lib/db/inbox/writes.js

  1. import { ObjectID } from "bson";
  2. import { getInboxCollection } from "../mongodb";
  3. /**
  4. * Marks some inbox messages as read.
  5. */
  6. export async function markSomeInboxMessagesAsRead(inboxIds) {
  7. const collection = await getInboxCollection();
  8. const bulk = collection.initializeOrderedBulkOp();
  9. inboxIds.forEach((id) => {
  10. bulk.find({ _id: ObjectID(id) }).update({
  11. $set: {
  12. read: true,
  13. lastUpdatedAt: new Date().toISOString(),
  14. },
  15. });
  16. });
  17. const result = await bulk.execute();
  18. return result;
  19. }
  20. /**
  21. * Marks all inbox messages as read.
  22. */
  23. export async function markAllInboxMessagesAsRead(userEmail) {
  24. const collection = await getInboxCollection();
  25. const result = await collection.updateMany(
  26. {
  27. dreamOwnerUserEmail: userEmail,
  28. },
  29. {
  30. $set: {
  31. read: true,
  32. lastUpdatedAt: new Date().toISOString(),
  33. },
  34. }
  35. );
  36. return result;
  37. }
  38. /**
  39. * Deletes all inbox messages.
  40. */
  41. export async function deleteAllInboxMessages(userEmail) {
  42. const collection = await getInboxCollection();
  43. const result = await collection.deleteMany({
  44. dreamOwnerUserEmail: userEmail,
  45. });
  46. return result;
  47. }
  48. /**
  49. * Deletes the inbox messages with the provided IDs.
  50. */
  51. export async function deleteSomeInboxMessages(inboxIds) {
  52. const collection = await getInboxCollection();
  53. const bulk = collection.initializeOrderedBulkOp();
  54. inboxIds.forEach((id) => {
  55. bulk.find({ _id: ObjectID(id) }).delete();
  56. });
  57. const result = await bulk.execute();
  58. return result;
  59. }