git ssb

10+

Matt McKegg / patchwork



Tree: a196de0da06518f5565c1980ce469c1752b019af

Files: a196de0da06518f5565c1980ce469c1752b019af / modules / message / html / backlinks.js

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

Built with git-ssb-web