Files: de80e702316a3f736dcb1f65f3840baf28f51ab9 / modules_extra / notifications.js
4198 bytesRaw
1 | |
2 | var h = require('hyperscript') |
3 | var u = require('../util') |
4 | var pull = require('pull-stream') |
5 | var Scroller = require('pull-scroll') |
6 | var paramap = require('pull-paramap') |
7 | var cont = require('cont') |
8 | var ref = require('ssb-ref') |
9 | |
10 | exports.needs = { |
11 | build_scroller: 'first', |
12 | message_render: 'first', |
13 | sbot_log: 'first', |
14 | sbot_get: 'first', |
15 | sbot_user_feed: 'first', |
16 | message_unbox: 'first' |
17 | } |
18 | |
19 | |
20 | exports.gives = { |
21 | builtin_tabs: true, |
22 | screen_view: true |
23 | } |
24 | |
25 | exports.create = function (api) { |
26 | function unbox() { |
27 | return pull( |
28 | pull.map(function (msg) { |
29 | return msg.value && 'string' === typeof msg.value.content ? |
30 | api.message_unbox(msg) : msg |
31 | }), |
32 | pull.filter(Boolean) |
33 | ) |
34 | } |
35 | |
36 | function notifications(ourIds) { |
37 | |
38 | function linksToUs(link) { |
39 | return link && link.link in ourIds |
40 | } |
41 | |
42 | function isOurMsg(id, cb) { |
43 | if (!id) return cb(null, false) |
44 | if (typeof id === 'object' && typeof id.link === 'string') id = id.link |
45 | if (!ref.isMsg(id)) return cb(null, false) |
46 | api.sbot_get(id, function (err, msg) { |
47 | if (err && err.name == 'NotFoundError') cb(null, false) |
48 | else if (err) cb(err) |
49 | else if (msg.content.type === 'issue' || msg.content.type === 'pull-request') |
50 | isOurMsg(msg.content.repo || msg.content.project, cb) |
51 | else cb(err, msg.author in ourIds) |
52 | }) |
53 | } |
54 | |
55 | function isAnyOurMessage(msg, ids, cb) { |
56 | cont.para(ids.map(function (id) { |
57 | return function (cb) { isOurMsg(id, cb) } |
58 | })) |
59 | (function (err, results) { |
60 | if (err) cb(err) |
61 | else if (results.some(Boolean)) cb(null, msg) |
62 | else cb() |
63 | }) |
64 | } |
65 | |
66 | return paramap(function (msg, cb) { |
67 | var c = msg.value && msg.value.content |
68 | if (!c || typeof c !== 'object') return cb() |
69 | if (msg.value.author in ourIds) return cb() |
70 | |
71 | if (c.mentions && Array.isArray(c.mentions) && c.mentions.some(linksToUs)) |
72 | return cb(null, msg) |
73 | |
74 | if (msg.private) |
75 | return cb(null, msg) |
76 | |
77 | switch (c.type) { |
78 | case 'post': |
79 | if (c.branch || c.root) |
80 | return isAnyOurMessage(msg, [].concat(c.branch, c.root), cb) |
81 | else return cb() |
82 | |
83 | case 'contact': |
84 | return cb(null, c.contact in ourIds ? msg : null) |
85 | |
86 | case 'vote': |
87 | if (c.vote && c.vote.link) |
88 | return isOurMsg(c.vote.link, function (err, isOurs) { |
89 | cb(err, isOurs ? msg : null) |
90 | }) |
91 | else return cb() |
92 | |
93 | case 'issue': |
94 | case 'pull-request': |
95 | return isOurMsg(c.project || c.repo, function (err, isOurs) { |
96 | cb(err, isOurs ? msg : null) |
97 | }) |
98 | |
99 | case 'issue-edit': |
100 | return isAnyOurMessage(msg, [c.issue].concat(c.issues), cb) |
101 | |
102 | default: |
103 | cb() |
104 | } |
105 | }, 4) |
106 | } |
107 | |
108 | function getFirstMessage(feedId, cb) { |
109 | api.sbot_user_feed({id: feedId, gte: 0, limit: 1})(null, cb) |
110 | } |
111 | |
112 | return { |
113 | builtin_tabs: function () { |
114 | return ['/notifications'] |
115 | }, |
116 | |
117 | screen_view: function (path) { |
118 | if(path === '/notifications') { |
119 | var ids = {} |
120 | var oldest |
121 | |
122 | var id = require('../keys').id |
123 | ids[id] = true |
124 | getFirstMessage(id, function (err, msg) { |
125 | if (err) return console.error(err) |
126 | if (!oldest || msg.value.timestamp < oldest) { |
127 | oldest = msg.value.timestamp |
128 | } |
129 | }) |
130 | |
131 | var { container, content } = api.build_scroller() |
132 | |
133 | pull( |
134 | u.next(api.sbot_log, {old: false, limit: 100}), |
135 | unbox(), |
136 | notifications(ids), |
137 | pull.filter(), |
138 | Scroller(container, content, api.message_render, true, false) |
139 | ) |
140 | |
141 | pull( |
142 | u.next(api.sbot_log, {reverse: true, limit: 100, live: false}), |
143 | unbox(), |
144 | notifications(ids), |
145 | pull.filter(), |
146 | pull.take(function (msg) { |
147 | // abort stream after we pass the oldest messages of our feeds |
148 | return !oldest ? true : msg.value.timestamp > oldest |
149 | }), |
150 | Scroller(container, content, api.message_render, false, false) |
151 | ) |
152 | |
153 | return container |
154 | } |
155 | } |
156 | } |
157 | } |
158 | |
159 |
Built with git-ssb-web