git ssb

10+

Matt McKegg / patchwork



Tree: 5bc8a8f290ca35dec4add0a2960c251a539967f3

Files: 5bc8a8f290ca35dec4add0a2960c251a539967f3 / modules / message / html / backlinks.js

1811 bytesRaw
1var nest = require('depnest')
2var ref = require('ssb-ref')
3var { h, map, computed } = require('mutant')
4
5exports.needs = nest({
6 'message.obs': {
7 backlinks: 'first',
8 name: 'first',
9 author: 'first'
10 },
11 'profile.html.person': 'first'
12})
13
14exports.gives = nest('message.html.backlinks')
15
16exports.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
47function onlyReferences (backlinks, msg) {
48 return backlinks.filter(link => link.root !== msg.key && !includeOrEqual(link.branch, msg.key))
49}
50
51function onlyForks (backlinks, msg) {
52 return backlinks.filter(link => link.root === msg.key && msg.value.content && msg.value.content.root)
53}
54
55function 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