git ssb

16+

Dominic / patchbay



Tree: 240810ceba9281d2eae595465e01b2b58f3c7b24

Files: 240810ceba9281d2eae595465e01b2b58f3c7b24 / modules / notifications.js

2869 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')
7
8var message_render = plugs.first(exports.message_render = [])
9var sbot_log = plugs.first(exports.sbot_log = [])
10var sbot_whoami = plugs.first(exports.sbot_whoami = [])
11var sbot_get = plugs.first(exports.sbot_get = [])
12var message_unbox = plugs.first(exports.message_unbox = [])
13
14function unbox() {
15 return pull(
16 pull.map(function (msg) {
17 return msg.value && 'string' === typeof msg.value.content ?
18 message_unbox(msg) : msg
19 }),
20 pull.filter(Boolean)
21 )
22}
23
24function notifications(ourIds) {
25
26 function linksToUs(link) {
27 return link && link.link in ourIds
28 }
29
30 function isOurMsg(id, cb) {
31 sbot_get(id, function (err, msg) {
32 if (err && err.name == 'NotFoundError') cb(null, false)
33 else if (err) cb(err)
34 else cb(err, msg.author in ourIds)
35 })
36 }
37
38 return paramap(function (msg, cb) {
39 var c = msg.value && msg.value.content
40 if (!c || typeof c !== 'object') return cb()
41 if (msg.value.author in ourIds) return cb()
42
43 if (c.mentions && Array.isArray(c.mentions) && c.mentions.some(linksToUs))
44 return cb(null, msg)
45
46 if (msg.private)
47 return cb(null, msg)
48
49 switch (c.type) {
50 case 'post':
51 if (c.branch)
52 return isOurMsg(c.branch, function (err, isOurs) {
53 if (err) cb(err)
54 else if (isOurs) cb(null, msg)
55 else if (c.root) isOurMsg(c.root, function (err, isOurs) {
56 if (err) cb(err)
57 else if (isOurs) cb(null, msg)
58 else cb()
59 })
60 })
61 else return cb()
62
63 case 'contact':
64 return cb(null, c.contact in ourIds ? msg : null)
65
66 case 'vote':
67 if (c.vote && c.vote.link)
68 return isOurMsg(c.vote.link, function (err, isOurs) {
69 cb(err, isOurs ? msg : null)
70 })
71 else return cb()
72
73 default:
74 cb()
75 }
76 }, 4)
77}
78
79exports.screen_view = function (path) {
80 if(path === '/notifications') {
81 var ids = {}
82 sbot_whoami(function (err, me) {
83 if (err) return console.error(err)
84 ids[me.id] = true
85 })
86
87 var content = h('div.column.scroller__content')
88 var div = h('div.column.scroller',
89 {style: {'overflow':'auto'}},
90 h('div.scroller__wrapper',
91 content
92 )
93 )
94
95 pull(
96 sbot_log({old: false}),
97 unbox(),
98 notifications(ids),
99 pull.filter(),
100 Scroller(div, content, message_render, true, false)
101 )
102
103 pull(
104 u.next(sbot_log, {reverse: true, limit: 100, live: false}),
105 unbox(),
106 notifications(ids),
107 pull.filter(),
108 Scroller(div, content, message_render, false, false)
109 )
110
111 return div
112 }
113}
114
115

Built with git-ssb-web