Files: 0eaf17392ff36c30135c6f4ef3eb9c8ee0c28faf / modules / notifications.js
2869 bytesRaw
1 | var h = require('hyperscript') |
2 | var u = require('../util') |
3 | var pull = require('pull-stream') |
4 | var Scroller = require('pull-scroll') |
5 | var paramap = require('pull-paramap') |
6 | var plugs = require('../plugs') |
7 | |
8 | var message_render = plugs.first(exports.message_render = []) |
9 | var sbot_log = plugs.first(exports.sbot_log = []) |
10 | var sbot_whoami = plugs.first(exports.sbot_whoami = []) |
11 | var sbot_get = plugs.first(exports.sbot_get = []) |
12 | var message_unbox = plugs.first(exports.message_unbox = []) |
13 | |
14 | function 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 | |
24 | function 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 | |
79 | exports.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