git ssb

0+

ev / microbay



forked from Dominic / patchbay

Tree: b8c0ac9c32b89a9bd44ff026a5f5c5cd4368ffbe

Files: b8c0ac9c32b89a9bd44ff026a5f5c5cd4368ffbe / modules / notifications.js

3424 bytesRaw
1var h = require('hyperscript')
2var u = require('../util')
3var pull = require('pull-stream')
4var Scroller = require('pull-scroll')
5var paramap = require('pull-paramap')
6var plugs = require('../plugs')
7var cont = require('cont')
8
9var message_render = plugs.first(exports.message_render = [])
10var sbot_log = plugs.first(exports.sbot_log = [])
11var sbot_whoami = plugs.first(exports.sbot_whoami = [])
12var sbot_get = plugs.first(exports.sbot_get = [])
13var sbot_user_feed = plugs.first(exports.sbot_user_feed = [])
14var message_unbox = plugs.first(exports.message_unbox = [])
15
16function unbox() {
17 return pull(
18 pull.map(function (msg) {
19 return msg.value && 'string' === typeof msg.value.content ?
20 message_unbox(msg) : msg
21 }),
22 pull.filter(Boolean)
23 )
24}
25
26function notifications(ourIds) {
27
28 function linksToUs(link) {
29 return link && link.link in ourIds
30 }
31
32 function isOurMsg(id, cb) {
33 sbot_get(id, function (err, msg) {
34 if (err && err.name == 'NotFoundError') cb(null, false)
35 else if (err) cb(err)
36 else cb(err, msg.author in ourIds)
37 })
38 }
39
40 return paramap(function (msg, cb) {
41 var c = msg.value && msg.value.content
42 if (!c || typeof c !== 'object') return cb()
43 if (msg.value.author in ourIds) return cb()
44
45 if (c.mentions && Array.isArray(c.mentions) && c.mentions.some(linksToUs))
46 return cb(null, msg)
47
48 if (msg.private)
49 return cb(null, msg)
50
51 switch (c.type) {
52 case 'post':
53 if (c.branch || c.root)
54 cont.para([].concat(c.branch, c.root).map(function (id) {
55 return function (cb) { isOurMsg(id, cb) }
56 }))
57 (function (err, results) {
58 if (err) cb(err)
59 else if (results.some(Boolean)) cb(null, msg)
60 else cb()
61 })
62 else return cb()
63
64 case 'contact':
65 return cb(null, c.contact in ourIds ? msg : null)
66
67 case 'vote':
68 if (c.vote && c.vote.link)
69 return isOurMsg(c.vote.link, function (err, isOurs) {
70 cb(err, isOurs ? msg : null)
71 })
72 else return cb()
73
74 default:
75 cb()
76 }
77 }, 4)
78}
79
80function getFirstMessage(feedId, cb) {
81 sbot_user_feed({id: feedId, gte: 0, limit: 1})(null, cb)
82}
83
84exports.screen_view = function (path) {
85 if(path === '/notifications') {
86 var ids = {}
87 var oldest
88
89 sbot_whoami(function (err, me) {
90 if (err) return console.error(err)
91 ids[me.id] = true
92 getFirstMessage(me.id, function (err, msg) {
93 if (err) return console.error(err)
94 if (!oldest || msg.value.timestamp < oldest) {
95 oldest = msg.value.timestamp
96 }
97 })
98 })
99
100 var content = h('div.column.scroller__content')
101 var div = h('div.column.scroller',
102 {style: {'overflow':'auto'}},
103 h('div.scroller__wrapper',
104 content
105 )
106 )
107
108 pull(
109 sbot_log({old: false}),
110 unbox(),
111 notifications(ids),
112 pull.filter(),
113 Scroller(div, content, message_render, true, false)
114 )
115
116 pull(
117 u.next(sbot_log, {reverse: true, limit: 100, live: false}),
118 unbox(),
119 notifications(ids),
120 pull.filter(),
121 pull.take(function (msg) {
122 // abort stream after we pass the oldest messages of our feeds
123 return !oldest || msg.value.timestamp > oldest
124 }),
125 Scroller(div, content, message_render, false, false)
126 )
127
128 return div
129 }
130}
131
132

Built with git-ssb-web