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