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