Files: fd559dbf257d9992c5048911d4e56c6de2b21442 / feed / obs / thread.js
3182 bytesRaw
1 | var nest = require('depnest') |
2 | var sort = require('ssb-sort') |
3 | var ref = require('ssb-ref') |
4 | var { Array: MutantArray, Value, map, concat, computed } = require('mutant') |
5 | |
6 | exports.needs = nest({ |
7 | 'backlinks.obs.for': 'first', |
8 | 'sbot.async.get': 'first', |
9 | 'message.sync.unbox': 'first', |
10 | 'message.sync.root': 'first', |
11 | 'message.sync.isBlocked': 'first' |
12 | }) |
13 | |
14 | exports.gives = nest('feed.obs.thread') |
15 | |
16 | exports.create = function (api) { |
17 | return nest('feed.obs.thread', thread) |
18 | |
19 | function thread (rootId, { branch } = {}) { |
20 | if (!ref.isLink(rootId)) throw new Error('an id must be specified') |
21 | var sync = Value(false) |
22 | var { isBlocked, root } = api.message.sync |
23 | |
24 | var prepend = MutantArray() |
25 | api.sbot.async.get(rootId, (err, value) => { |
26 | sync.set(true) |
27 | if (!err) { |
28 | var msg = unboxIfNeeded({key: rootId, value}) |
29 | if (isBlocked(msg)) msg.isBlocked = true |
30 | prepend.push(Value(msg)) |
31 | } |
32 | }) |
33 | |
34 | var backlinks = api.backlinks.obs.for(rootId) |
35 | var replies = map(computed(backlinks, (msgs) => { |
36 | return msgs.filter(msg => { |
37 | const { type, branch } = msg.value.content |
38 | return type !== 'vote' && !isBlocked(msg) && (root(msg) === rootId || matchAny(branch, rootId)) |
39 | }) |
40 | }), x => Value(x), { |
41 | // avoid refresh of entire list when items added |
42 | comparer: (a, b) => a === b |
43 | }) |
44 | |
45 | var messages = concat([prepend, replies]) |
46 | |
47 | var result = { |
48 | messages, |
49 | lastId: computed(messages, (messages) => { |
50 | var branches = sort.heads(messages) |
51 | if (branches.length <= 1) { |
52 | branches = branches[0] |
53 | } |
54 | return branches |
55 | }), |
56 | rootId: computed(messages, (messages) => { |
57 | if (branch && messages.length) { |
58 | return messages[0].value.content.root |
59 | } else { |
60 | return rootId |
61 | } |
62 | }), |
63 | branchId: computed(messages, (messages) => { |
64 | if (branch) return rootId |
65 | }), |
66 | previousKey: function (msg) { |
67 | return PreviousKey(result.messages, msg) |
68 | }, |
69 | isPrivate: computed(messages, msgs => { |
70 | if (!msgs[0]) return false |
71 | |
72 | return msgs[0].value.private || false |
73 | }), |
74 | channel: computed(messages, msgs => { |
75 | if (!msgs[0]) return undefined |
76 | |
77 | return msgs[0].value.content.channel |
78 | }), |
79 | recps: computed(messages, msgs => { |
80 | if (!msgs[0]) return undefined |
81 | |
82 | return msgs[0].value.content.recps |
83 | }) |
84 | } |
85 | |
86 | result.sync = computed([backlinks.sync, sync], (a, b) => a && b, {idle: true}) |
87 | return result |
88 | } |
89 | |
90 | function unboxIfNeeded (msg) { |
91 | if (msg.value && typeof msg.value.content === 'string') { |
92 | return api.message.sync.unbox(msg) || msg |
93 | } else { |
94 | return msg |
95 | } |
96 | } |
97 | } |
98 | |
99 | function PreviousKey (collection, item) { |
100 | return computed(collection, (c) => { |
101 | var index = collection.indexOf(item) |
102 | if (~index) { |
103 | var previous = c[index - 1] |
104 | if (previous) { |
105 | return previous.key |
106 | } |
107 | } |
108 | }) |
109 | } |
110 | |
111 | function matchAny (valueOrArray, compare) { |
112 | if (valueOrArray === compare) { |
113 | return true |
114 | } else if (Array.isArray(valueOrArray)) { |
115 | return valueOrArray.includes(compare) |
116 | } |
117 | } |
118 |
Built with git-ssb-web