app/html/thread.jsView |
---|
8 | 8 | |
9 | 9 | exports.needs = nest({ |
10 | 10 | 'about.html.image': 'first', |
11 | 11 | 'app.sync.goTo': 'first', |
| 12 | + 'feed.obs.thread': 'first', |
12 | 13 | 'message.html.markdown': 'first' |
13 | 14 | }) |
14 | 15 | |
15 | 16 | exports.create = (api) => { |
16 | 17 | return nest('app.html.thread', thread) |
17 | 18 | |
18 | | - function thread (source) { |
| 19 | + function thread (id) { |
19 | 20 | |
20 | 21 | |
21 | | - const chunkedThread = buildChunkedThreadObs(source) |
22 | 22 | |
23 | 23 | var myId = '@EMovhfIrFk4NihAKnRNhrfRaqIhBv1Wj8pTxJNgvCCY=.ed25519' |
24 | 24 | |
25 | 25 | |
| 26 | + const thread = api.feed.obs.thread(id) |
| 27 | + const chunkedMessages = buildChunkedMessages(thread.messages) |
| 28 | + |
26 | 29 | const { goTo } = api.app.sync |
27 | 30 | const threadView = h('Thread', |
28 | | - map(chunkedThread, chunk => { |
| 31 | + map(chunkedMessages, chunk => { |
| 32 | + const author = get(chunk, '[0].value.author') |
29 | 33 | |
30 | | - return computed(chunk, chunk => { |
31 | | - const author = get(chunk, '[0].value.author') |
32 | | - if (author === myId) { |
33 | | - return h('div.my-chunk', [ |
| 34 | + return author === myId |
| 35 | + ? h('div.my-chunk', [ |
34 | 36 | h('div.avatar'), |
35 | 37 | h('div.msgs', map(chunk, msg => { |
36 | 38 | return h('div.msg-row', [ |
37 | 39 | h('div.spacer'), |
38 | 40 | message(msg) |
39 | 41 | ]) |
40 | 42 | })) |
41 | 43 | ]) |
42 | | - } else { |
43 | | - return h('div.other-chunk', [ |
44 | | - h('div.avatar', api.about.html.image(author)), |
| 44 | + : h('div.other-chunk', [ |
| 45 | + h('div.avatar', when(author, api.about.html.image(author))), |
45 | 46 | h('div.msgs', map(chunk, msg => { |
46 | 47 | return h('div.msg-row', [ |
47 | 48 | message(msg), |
48 | 49 | h('div.spacer') |
49 | 50 | ]) |
50 | 51 | })) |
51 | 52 | ]) |
52 | | - } |
53 | | - }) |
54 | 53 | }) |
55 | 54 | ) |
56 | 55 | |
57 | 56 | function message (msg) { |
63 | 62 | return threadView |
64 | 63 | } |
65 | 64 | } |
66 | 65 | |
67 | | -function buildChunkedThreadObs (source) { |
68 | | - var chunkedThread = MutantArray() |
| 66 | +function buildChunkedMessages (messagesObs) { |
| 67 | + return computed(messagesObs, msgs => { |
| 68 | + var chunkedMessages = MutantArray() |
69 | 69 | |
70 | | - var _chunk = null |
71 | | - var _lastMsg = null |
72 | | - |
73 | | - pull( |
74 | | - source, |
75 | | - pull.drain(msg => { |
| 70 | + var _chunk = null |
| 71 | + var _lastMsg = null |
| 72 | + |
| 73 | + msgs.forEach(msg => { |
76 | 74 | if (!_lastMsg || !isSameAuthor(_lastMsg, msg)) |
77 | 75 | createNewChunk(msg) |
78 | 76 | else |
79 | 77 | _chunk.push(msg) |
80 | 78 | |
81 | 79 | _lastMsg = msg |
82 | 80 | }) |
83 | | - ) |
84 | 81 | |
85 | | - function createNewChunk (msg) { |
86 | | - const newChunk = MutantArray() |
87 | | - newChunk.push(msg) |
88 | | - chunkedThread.push(newChunk) |
89 | | - _chunk = newChunk |
90 | | - } |
| 82 | + function createNewChunk (msg) { |
| 83 | + const newChunk = MutantArray() |
| 84 | + newChunk.push(msg) |
| 85 | + chunkedMessages.push(newChunk) |
| 86 | + _chunk = newChunk |
| 87 | + } |
91 | 88 | |
92 | | - return chunkedThread |
| 89 | + return chunkedMessages |
| 90 | + }) |
93 | 91 | } |
94 | 92 | |
95 | 93 | function isSameAuthor (msgA, msgB) { |
96 | 94 | |