git ssb

7+

dinoworm 🐛 / patchcore



Commit f6852d8a65ff7eebb26e615d8b3da4bbb9f45cdd

add message.obs.backlinks and message.obs.name

Matt McKegg committed on 2/17/2017, 4:01:38 PM
Parent: 48b0a33541f9be71bcf6682aece9d89314e79272

Files changed

message/html/backlinks.jschanged
message/obs/backlinks.jsadded
message/obs/name.jsadded
message/html/backlinks.jsView
@@ -1,44 +1,36 @@
11 const h = require('mutant/h')
2 +const map = require('mutant/map')
3 +const computed = require('mutant/computed')
4 +const when = require('mutant/when')
25 const nest = require('depnest')
36
47 exports.needs = nest({
8 + 'message.obs.backlinks': 'first',
9 + 'message.obs.name': 'first',
510 'message.async.name': 'first',
611 'sbot.sync.cache': 'first'
712 })
813
914 exports.gives = nest('message.html.backlinks')
1015
1116 exports.create = function (api) {
1217 return nest('message.html.backlinks', function (msg) {
13- var cache = api.sbot.sync.cache()
14- var links = []
15- for (var k in cache) {
16- var _msg = cache[k]
17- var mentions = _msg.content.mentions
18-
19- if (Array.isArray(mentions)) {
20- for (var i = 0; i < mentions.length; i++) {
21- if (mentions[i].link === msg.key) {
22- links.push(k)
23- }
24- }
25- }
26- }
27-
28- if (links.length === 0) return null
29-
30- var hrefList = h('ul')
31- links.forEach(link => {
32- api.message.async.name(link, (err, name) => {
33- if (err) throw err
34- hrefList.appendChild(h('li', [
35- h('a -backlink', { href: link }, name)
36- ]))
37- })
38- })
39- return h('MessageBacklinks', [
40- h('header', 'backlinks:'),
41- hrefList
42- ])
18 + var backlinks = api.message.obs.backlinks(msg.key)
19 + return when(computed(backlinks, hasItems),
20 + h('MessageBacklinks', [
21 + h('header', 'backlinks:'),
22 + h('ul', [
23 + map(backlinks, (link) => {
24 + return h('li', [
25 + h('a -backlink', { href: link, title: link }, api.message.obs.name(link))
26 + ])
27 + })
28 + ])
29 + ])
30 + )
4331 })
4432 }
33 +
34 +function hasItems (items) {
35 + return (items && items.length)
36 +}
message/obs/backlinks.jsView
@@ -1,0 +1,36 @@
1 +var nest = require('depnest')
2 +var MutantSet = require('mutant/set')
3 +
4 +exports.needs = nest({
5 + 'message.sync.unbox': 'first'
6 +})
7 +
8 +exports.gives = nest({
9 + 'sbot.hook.feed': true,
10 + 'message.obs.backlinks': true
11 +})
12 +
13 +exports.create = function (api) {
14 + var mentionedLookup = {}
15 + return nest({
16 + 'sbot.hook.feed': (msg) => {
17 + if (msg.value && typeof msg.value.content === 'string') {
18 + msg = api.message.sync.unbox(msg)
19 + }
20 + if (msg && msg.value && msg.value.content && Array.isArray(msg.value.content.mentions)) {
21 + msg.value.content.mentions.forEach(mention => {
22 + var link = typeof mention === 'object' ? mention.link : mention
23 + get(link).add(msg.key)
24 + })
25 + }
26 + },
27 + 'message.obs.backlinks': (id) => get(id)
28 + })
29 +
30 + function get (id) {
31 + if (!mentionedLookup[id]) {
32 + mentionedLookup[id] = MutantSet()
33 + }
34 + return mentionedLookup[id]
35 + }
36 +}
message/obs/name.jsView
@@ -1,0 +1,23 @@
1 +var Value = require('mutant/value')
2 +var ref = require('ssb-ref')
3 +var nest = require('depnest')
4 +
5 +exports.needs = nest('message.async.name', 'first')
6 +
7 +exports.gives = nest('message.obs.name')
8 +
9 +exports.create = function (api) {
10 + return nest('message.obs.name', function (id) {
11 + if (typeof id !== 'string') { throw new Error('link must be to message id') }
12 + var value = Value(id.substring(0, 10) + '...')
13 +
14 + if (ref.isMsg(id)) {
15 + api.message.async.name(id, function (err, name) {
16 + if (err) console.error(err)
17 + else value.set(name)
18 + })
19 + }
20 +
21 + return value
22 + })
23 +}

Built with git-ssb-web