Files: 17e939d97aceac4619d4a5ad7698fea625d570c7 / message / html / subject.js
1355 bytesRaw
1 | const nest = require('depnest') |
2 | const { computed, Value } = require('mutant') |
3 | const { title } = require('markdown-summary') |
4 | const { isMsg } = require('ssb-ref') |
5 | |
6 | exports.gives = nest('message.html.subject') |
7 | |
8 | exports.needs = nest({ |
9 | 'message.html.markdown': 'first', |
10 | 'message.sync.unbox': 'first', |
11 | 'sbot.async.get': 'first' |
12 | }) |
13 | |
14 | exports.create = function (api) { |
15 | var subjectCache = {} |
16 | |
17 | return nest('message.html.subject', subject) |
18 | |
19 | function subject(msg) { |
20 | // test if it's a message ref, or a full message object |
21 | // a message ref is generally passed in if we're fetching the subject of a root message |
22 | if (isMsg(msg)) { |
23 | if (subjectCache[msg]) return subjectCache[msg] |
24 | |
25 | var subject = Value() |
26 | |
27 | api.sbot.async.get(msg, (err, value) => { |
28 | if (err) throw err |
29 | try { |
30 | var _subject = getMsgSubject({ key: msg, value: api.message.sync.unbox(value) }) |
31 | subject.set(_subject) |
32 | subjectCache[msg] = _subject |
33 | } catch (n) { |
34 | subject.set("broken message") |
35 | } |
36 | |
37 | }) |
38 | |
39 | return subject |
40 | } else { return getMsgSubject(msg) } |
41 | } |
42 | |
43 | function getMsgSubject(msg) { |
44 | const { subject, text } = msg.value.content |
45 | if (!(subject || text)) return |
46 | |
47 | return subject |
48 | ? api.message.html.markdown(subject) |
49 | : api.message.html.markdown(title(text)) |
50 | } |
51 | } |
52 |
Built with git-ssb-web