Files: 9e3d2a27287664a8a12ac78c22384a9748102890 / modules / message / html / backlinks.js
1811 bytesRaw
1 | var nest = require('depnest') |
2 | var ref = require('ssb-ref') |
3 | var { h, map, computed } = require('mutant') |
4 | |
5 | exports.needs = nest({ |
6 | 'message.obs': { |
7 | backlinks: 'first', |
8 | name: 'first', |
9 | author: 'first' |
10 | }, |
11 | 'profile.html.person': 'first' |
12 | }) |
13 | |
14 | exports.gives = nest('message.html.backlinks') |
15 | |
16 | exports.create = function (api) { |
17 | return nest('message.html.backlinks', function (msg, {includeReferences = true, includeForks = true} = {}) { |
18 | if (!ref.type(msg.key)) return [] |
19 | var backlinks = api.message.obs.backlinks(msg.key) |
20 | var references = includeReferences ? computed([backlinks, msg], onlyReferences) : [] |
21 | var forks = (includeForks && msg.value.content.root) ? computed([backlinks, msg], onlyForks) : [] |
22 | return [ |
23 | map(forks, link => { |
24 | return h('a.backlink', { |
25 | href: link.id, title: link.id |
26 | }, [ |
27 | h('strong', [ |
28 | api.profile.html.person(link.author), ' forked this discussion:' |
29 | ]), ' ', |
30 | api.message.obs.name(link.id) |
31 | ]) |
32 | }), |
33 | map(references, link => { |
34 | return h('a.backlink', { |
35 | href: link.id, title: link.id |
36 | }, [ |
37 | h('strong', [ |
38 | api.profile.html.person(link.author), ' referenced this message:' |
39 | ]), ' ', |
40 | api.message.obs.name(link.id) |
41 | ]) |
42 | }) |
43 | ] |
44 | }) |
45 | } |
46 | |
47 | function onlyReferences (backlinks, msg) { |
48 | return backlinks.filter(link => link.root !== msg.key && !includeOrEqual(link.branch, msg.key)) |
49 | } |
50 | |
51 | function onlyForks (backlinks, msg) { |
52 | return backlinks.filter(link => link.root === msg.key && msg.value.content && msg.value.content.root) |
53 | } |
54 | |
55 | function includeOrEqual (valueOrArray, item) { |
56 | if (Array.isArray(valueOrArray)) { |
57 | return valueOrArray.includes(item) |
58 | } else { |
59 | return valueOrArray === item |
60 | } |
61 | } |
62 |
Built with git-ssb-web