git ssb

7+

dinoworm 🐛 / patchcore



Commit d586656fe9c7044534f06d6fe4d390aac3d49118

add naive about observable reducer (obs_about_name, obs_about_image)

currently only uses self asserted messages
Matt McKegg committed on 2/12/2017, 3:11:45 PM
Parent: 5adda321ba346df2f7f08c633b873a70704fc195

Files changed

components/message/author.jschanged
example.jschanged
package.jsonchanged
sbot.jschanged
observables/about.jsadded
components/message/author.jsView
@@ -1,6 +1,10 @@
11 const h = require('mutant/h')
22
3 +exports.needs = {
4 + obs_about_name: 'first'
5 +}
6 +
37 exports.gives = {
48 message_author: true
59 }
610
@@ -9,7 +13,9 @@
913 message_author
1014 }
1115
1216 function message_author (msg) {
13- return h('div', {}, [msg.value.author.slice(0, 10)])
17 + return h('div', {}, [
18 + api.obs_about_name(msg.value.author)
19 + ])
1420 }
1521 }
example.jsView
@@ -14,10 +14,14 @@
1414
1515 require('insert-css')(`
1616 .Message {
1717 padding: 20px;
18- border-bottom: 1px solid #EEE
18 + border-bottom: 1px solid #EEE;
1919 }
20 +
21 + .Message > header.author {
22 + font-weight: bold;
23 + }
2024 `)
2125
2226 var app = h('div.App', [
2327 api.render_feed(api.feeds.public)
package.jsonView
@@ -28,8 +28,9 @@
2828 "bulkify": "^1.4.2",
2929 "emoji-named-characters": "^1.0.2",
3030 "es2040": "^1.2.4",
3131 "mutant": "^3.13.1",
32 + "pull-abortable": "^4.1.0",
3233 "pull-reconnect": "0.0.3",
3334 "pull-stream": "^3.5.0",
3435 "ssb-client": "^4.4.0",
3536 "ssb-config": "^2.2.0",
sbot.jsView
@@ -14,8 +14,9 @@
1414
1515 exports.gives = {
1616 sbot_log: true,
1717 sbot_get: true,
18 + sbot_user_feed: true,
1819 sbot_query: true,
1920 sbot_publish: true,
2021 connection_status: true
2122 }
observables/about.jsView
@@ -1,0 +1,74 @@
1 +var {Value, Struct} = require('mutant')
2 +var Abortable = require('pull-abortable')
3 +var pull = require('pull-stream')
4 +
5 +exports.needs = {
6 + sbot_user_feed: 'first'
7 +}
8 +exports.gives = {
9 + obs_about_name: true,
10 + obs_about_image: true
11 +}
12 +
13 +exports.create = function (api) {
14 + var cache = {}
15 +
16 + return {
17 + obs_about_name: (id) => get(id).displayName,
18 + obs_about_image: (id) => get(id).image
19 + }
20 +
21 + function get (id) {
22 + if (!cache[id]) {
23 + cache[id] = About(api, id)
24 + }
25 + return cache[id]
26 + }
27 +}
28 +
29 +function About (api, id) {
30 + // naive about that only looks at what a feed asserts about itself
31 +
32 + var obs = Struct({
33 + displayName: Value(id.slice(0, 10)),
34 + image: Value()
35 + })
36 +
37 + var hasName = false
38 +
39 + var abortable = Abortable()
40 +
41 + // search history
42 + pull(
43 + api.sbot_user_feed({reverse: true, id}),
44 + abortable,
45 + pull.drain(function (item) {
46 + update(item)
47 + if (hasName && obs.image()) {
48 + abortable.abort()
49 + }
50 + })
51 + )
52 +
53 + // get live changes
54 + pull(
55 + api.sbot_user_feed({tail: true, id}),
56 + pull.drain(update)
57 + )
58 +
59 + return obs
60 +
61 + // scoped
62 +
63 + function update (item) {
64 + if (item.value && item.value.content.type === 'about' && item.value.content.about === id) {
65 + if (item.value.content.name) {
66 + hasName = true
67 + obs.displayName.set(item.value.content.name)
68 + }
69 + if (item.value.content.image) {
70 + obs.image.set(item.value.content.image)
71 + }
72 + }
73 + }
74 +}

Built with git-ssb-web