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