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