Source: lib/db/posts/reads.js

  1. import { ObjectID } from "bson";
  2. import { getPostsCollection } from "../mongodb";
  3. import { decryptPost } from "../../security/transformations";
  4. import { getUserByEmail } from "../reads";
  5. /**
  6. * Get a post by its id and optionally filter the results
  7. *
  8. * @param {string} postId The post id
  9. */
  10. export async function getPostById(postId) {
  11. const collection = await getPostsCollection();
  12. const cursor = collection.find({ _id: ObjectID(postId) }).limit(1);
  13. const result = await cursor.toArray();
  14. await cursor.close();
  15. if (result.lenght === 0) {
  16. return null;
  17. }
  18. const data = result[0];
  19. if (data.visibility === "private") {
  20. const decryptedPost = decryptPost(data.dream);
  21. return {
  22. ...data,
  23. dream: decryptedPost,
  24. };
  25. }
  26. return data;
  27. }
  28. /**
  29. * Get all the posts from a user
  30. *
  31. * @param {string} userEmail The user email
  32. */
  33. export async function getPosts(userEmail) {
  34. const [user, collection] = await Promise.all([
  35. getUserByEmail(userEmail),
  36. getPostsCollection(),
  37. ]);
  38. const cursor = collection
  39. .find({ userId: ObjectID(user._id) })
  40. .sort({ _id: -1 })
  41. .limit(200);
  42. const rawResult = await cursor.toArray();
  43. await cursor.close();
  44. if (rawResult.lenght === 0) {
  45. return null;
  46. }
  47. const result = [];
  48. for (let data of rawResult) {
  49. if (data.visibility === "private") {
  50. data = {
  51. ...data,
  52. dream: decryptPost(data.dream),
  53. };
  54. }
  55. result.push(data);
  56. }
  57. return result;
  58. }
  59. /**
  60. * Gets the insights from the posts of a user
  61. *
  62. * @param {string} userEmail The user email
  63. */
  64. export async function getPostsInsights(userEmail) {
  65. const [user, collection] = await Promise.all([
  66. getUserByEmail(userEmail),
  67. getPostsCollection(),
  68. ]);
  69. const cursor = collection
  70. .find({ userId: ObjectID(user._id) })
  71. .sort({ _id: -1 })
  72. .limit(200);
  73. const rawResult = await cursor.toArray();
  74. await cursor.close();
  75. if (rawResult.lenght === 0) {
  76. return null;
  77. }
  78. const result = [];
  79. for (let data of rawResult) {
  80. result.push({
  81. createdAt: data.createdAt,
  82. dreamId: data._id,
  83. wordFrequency: data.wordFrequency,
  84. characterCount: data.characterCount,
  85. });
  86. }
  87. return result;
  88. }
  89. /**
  90. * Gets the latest public posts from a user
  91. */
  92. export async function getLatestPublicPosts() {
  93. const collection = await getPostsCollection();
  94. const cursor = collection
  95. .find({ visibility: { $in: ["public", "anonymous"] } })
  96. .sort({ _id: -1 })
  97. .limit(200);
  98. const result = await cursor.toArray();
  99. await cursor.close();
  100. if (result.lenght === 0) {
  101. return null;
  102. }
  103. return result;
  104. }
  105. /**
  106. * Fuzzy searches posts from all users given a query
  107. *
  108. * @param {string} query
  109. */
  110. export async function searchPosts(query) {
  111. const collection = await getPostsCollection();
  112. const result = await collection
  113. .aggregate([
  114. {
  115. $search: {
  116. index: "default",
  117. text: {
  118. query,
  119. fuzzy: {
  120. maxExpansions: 10,
  121. prefixLength: 3,
  122. },
  123. path: "dream.text",
  124. },
  125. },
  126. },
  127. ])
  128. .toArray();
  129. return result;
  130. }