trending_model.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. const pull = require('../server/node_modules/pull-stream');
  2. module.exports = ({ cooler }) => {
  3. let ssb;
  4. const openSsb = async () => {
  5. if (!ssb) ssb = await cooler.open();
  6. return ssb;
  7. };
  8. const types = [
  9. 'bookmark', 'votes', 'feed',
  10. 'image', 'audio', 'video', 'document', 'transfer'
  11. ];
  12. const categories = [
  13. 'interesting', 'necessary', 'funny', 'disgusting', 'sensible',
  14. 'propaganda', 'adultOnly', 'boring', 'confusing', 'inspiring', 'spam'
  15. ];
  16. const listTrending = async (filter = 'ALL') => {
  17. const ssbClient = await openSsb();
  18. const userId = ssbClient.id;
  19. const messages = await new Promise((res, rej) => {
  20. pull(
  21. ssbClient.createLogStream(),
  22. pull.collect((err, xs) => err ? rej(err) : res(xs))
  23. );
  24. });
  25. const tombstoned = new Set();
  26. const replaces = new Map();
  27. const itemsById = new Map();
  28. for (const m of messages) {
  29. const k = m.key;
  30. const c = m.value?.content;
  31. if (!c) continue;
  32. if (c.type === 'tombstone' && c.target) {
  33. tombstoned.add(c.target);
  34. continue;
  35. }
  36. if (
  37. c.opinions &&
  38. !tombstoned.has(k) &&
  39. !['task', 'event', 'report'].includes(c.type)
  40. ) {
  41. if (c.replaces) replaces.set(c.replaces, k);
  42. itemsById.set(k, m);
  43. }
  44. }
  45. for (const replacedId of replaces.keys()) {
  46. itemsById.delete(replacedId);
  47. }
  48. let items = Array.from(itemsById.values());
  49. if (filter === 'MINE') {
  50. items = items.filter(m => m.value.author === userId);
  51. } else if (filter === 'RECENT') {
  52. const now = Date.now();
  53. items = items.filter(m => now - m.value.timestamp < 24 * 60 * 60 * 1000);
  54. }
  55. if (types.includes(filter)) {
  56. items = items.filter(m => m.value.content.type === filter);
  57. }
  58. if (filter !== 'ALL') {
  59. items = items.filter(m => (m.value.content.opinions_inhabitants || []).length > 0);
  60. }
  61. if (filter === 'TOP') {
  62. items.sort((a, b) => {
  63. const aLen = (a.value.content.opinions_inhabitants || []).length;
  64. const bLen = (b.value.content.opinions_inhabitants || []).length;
  65. if (bLen !== aLen) return bLen - aLen;
  66. return b.value.timestamp - a.value.timestamp;
  67. });
  68. } else {
  69. items.sort((a, b) => {
  70. const aLen = (a.value.content.opinions_inhabitants || []).length;
  71. const bLen = (b.value.content.opinions_inhabitants || []).length;
  72. return bLen - aLen;
  73. });
  74. }
  75. return { filtered: items };
  76. };
  77. const getMessageById = async id => {
  78. const ssbClient = await openSsb();
  79. return new Promise((res, rej) => {
  80. ssbClient.get(id, (err, msg) => {
  81. if (err) rej(err);
  82. else res(msg);
  83. });
  84. });
  85. };
  86. const createVote = async (contentId, category) => {
  87. const ssbClient = await openSsb();
  88. const userId = ssbClient.id;
  89. if (!categories.includes(category)) throw new Error('Invalid voting category');
  90. const msg = await getMessageById(contentId);
  91. if (!msg || !msg.content) throw new Error('Content not found');
  92. const type = msg.content.type;
  93. if (
  94. !types.includes(type) ||
  95. ['task', 'event', 'report'].includes(type)
  96. ) {
  97. throw new Error('Voting not allowed on this content type');
  98. }
  99. if (msg.content.opinions_inhabitants?.includes(userId)) throw new Error('Already voted');
  100. const tombstone = {
  101. type: 'tombstone',
  102. target: contentId,
  103. deletedAt: new Date().toISOString()
  104. };
  105. const updated = {
  106. ...msg.content,
  107. opinions: {
  108. ...msg.content.opinions,
  109. [category]: (msg.content.opinions?.[category] || 0) + 1
  110. },
  111. opinions_inhabitants: [...(msg.content.opinions_inhabitants || []), userId],
  112. updatedAt: new Date().toISOString(),
  113. replaces: contentId
  114. };
  115. await new Promise((res, rej) => {
  116. ssbClient.publish(tombstone, (err) => err ? rej(err) : res());
  117. });
  118. return new Promise((res, rej) => {
  119. ssbClient.publish(updated, (err, result) => err ? rej(err) : res(result));
  120. });
  121. };
  122. return { listTrending, getMessageById, createVote, types, categories };
  123. };