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