trending_model.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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', 'event', 'task', 'votes', 'report', '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 (c.opinions) {
  37. if (tombstoned.has(k)) continue;
  38. if (c.replaces) replaces.set(c.replaces, k);
  39. itemsById.set(k, m);
  40. }
  41. }
  42. for (const replacedId of replaces.keys()) {
  43. itemsById.delete(replacedId);
  44. }
  45. let items = Array.from(itemsById.values());
  46. if (filter === 'MINE') {
  47. items = items.filter(m => m.value.author === userId);
  48. } else if (filter === 'RECENT') {
  49. const now = Date.now();
  50. items = items.filter(m => now - m.value.timestamp < 24 * 60 * 60 * 1000);
  51. }
  52. if (types.includes(filter)) {
  53. items = items.filter(m => m.value.content.type === filter);
  54. }
  55. if (filter !== 'ALL') {
  56. items = items.filter(m => (m.value.content.opinions_inhabitants || []).length > 0);
  57. }
  58. if (filter === 'TOP') {
  59. items.sort((a, b) => {
  60. const aLen = (a.value.content.opinions_inhabitants || []).length;
  61. const bLen = (b.value.content.opinions_inhabitants || []).length;
  62. if (bLen !== aLen) return bLen - aLen;
  63. return b.value.timestamp - a.value.timestamp;
  64. });
  65. } else {
  66. items.sort((a, b) => {
  67. const aLen = (a.value.content.opinions_inhabitants || []).length;
  68. const bLen = (b.value.content.opinions_inhabitants || []).length;
  69. return bLen - aLen;
  70. });
  71. }
  72. return { filtered: items };
  73. };
  74. const getMessageById = async id => {
  75. const ssbClient = await openSsb();
  76. return new Promise((res, rej) => {
  77. ssbClient.get(id, (err, msg) => {
  78. if (err) rej(err);
  79. else res(msg);
  80. });
  81. });
  82. };
  83. const createVote = async (contentId, category) => {
  84. const ssbClient = await openSsb();
  85. const userId = ssbClient.id;
  86. if (!categories.includes(category)) throw new Error('Invalid voting category');
  87. const msg = await getMessageById(contentId);
  88. if (!msg || !msg.content) throw new Error('Content not found');
  89. const type = msg.content.type;
  90. if (!types.includes(type)) throw new Error('Invalid content type for voting');
  91. if (msg.content.opinions_inhabitants?.includes(userId)) throw new Error('Already voted');
  92. const tombstone = {
  93. type: 'tombstone',
  94. target: contentId,
  95. deletedAt: new Date().toISOString()
  96. };
  97. const updated = {
  98. ...msg.content,
  99. opinions: {
  100. ...msg.content.opinions,
  101. [category]: (msg.content.opinions?.[category] || 0) + 1
  102. },
  103. opinions_inhabitants: [...(msg.content.opinions_inhabitants || []), userId],
  104. updatedAt: new Date().toISOString(),
  105. replaces: contentId
  106. };
  107. await new Promise((res, rej) => {
  108. ssbClient.publish(tombstone, (err) => err ? rej(err) : res());
  109. });
  110. return new Promise((res, rej) => {
  111. ssbClient.publish(updated, (err, result) => err ? rej(err) : res(result));
  112. });
  113. };
  114. return { listTrending, getMessageById, createVote, types, categories };
  115. };