Files: f3a07a62ed183316f08c7c0cf24dcb767afd29b1 / message / html / subject.js
1301 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 | if (msg === undefined) debugger |
21 | // test if it's a message ref, or a full message object |
22 | // a message ref is generally passed in if we're fetching the subject of a root message |
23 | if (isMsg(msg)) { |
24 | if (subjectCache[msg]) return subjectCache[msg] |
25 | |
26 | var subject = Value() |
27 | |
28 | api.sbot.async.get(msg, (err, value) => { |
29 | if (err) throw err |
30 | |
31 | var _subject = getMsgSubject({ key: msg, value: api.message.sync.unbox(value) }) |
32 | subject.set(_subject) |
33 | subjectCache[msg] = _subject |
34 | }) |
35 | |
36 | return subject |
37 | } else { return getMsgSubject(msg) } |
38 | } |
39 | |
40 | function getMsgSubject (msg) { |
41 | const { subject, text } = msg.value.content |
42 | if (!(subject || text)) return |
43 | |
44 | return subject |
45 | ? api.message.html.markdown(subject) |
46 | : api.message.html.markdown(title(text)) |
47 | } |
48 | } |
49 |
Built with git-ssb-web