git ssb

10+

Matt McKegg / patchwork



Tree: 719c20c66b72e2b795bcbf2367d4ec4e85bb0d1a

Files: 719c20c66b72e2b795bcbf2367d4ec4e85bb0d1a / modules / message / html / backlinks.js

1928 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 'intl.sync.i18n': 'first',
13})
14
15exports.gives = nest('message.html.backlinks')
16
17exports.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
49function onlyReferences (backlinks, msg) {
50 return backlinks.filter(link => link.root !== msg.key && !includeOrEqual(link.branch, msg.key))
51}
52
53function 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
57function 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