Files: 05c5b4f2078be5153b1a4ff2c0f7455a930242b1 / modules / notifications.js
3759 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 | } |
51 | } |
52 | |
53 | function unbox () { |
54 | return pull( |
55 | pull.map(function (msg) { |
56 | return msg.value && typeof msg.value.content === 'string' |
57 | ? message_unbox(msg) |
58 | : msg |
59 | }), |
60 | pull.filter(Boolean) |
61 | ) |
62 | } |
63 | |
64 | function notifications (ourIds) { |
65 | function linksToUs (link) { |
66 | return link && link.link in ourIds |
67 | } |
68 | |
69 | function isOurMsg (id, cb) { |
70 | if (!id) return cb(null, false) |
71 | if (typeof id === 'object' && typeof id.link === 'string') id = id.link |
72 | if (!ref.isMsg(id)) return cb(null, false) |
73 | sbot_get(id, function (err, msg) { |
74 | if (err && err.name === 'NotFoundError') cb(null, false) |
75 | else if (err) cb(err) |
76 | else if (msg.content.type === 'issue' || msg.content.type === 'pull-request') { |
77 | isOurMsg(msg.content.repo || msg.content.project, cb) |
78 | } else { |
79 | cb(err, msg.author in ourIds) |
80 | } |
81 | }) |
82 | } |
83 | |
84 | function isAnyOurMessage (msg, ids, cb) { |
85 | cont.para(ids.map(function (id) { |
86 | return function (cb) { isOurMsg(id, cb) } |
87 | }))(function (err, results) { |
88 | if (err) cb(err) |
89 | else if (results.some(Boolean)) cb(null, msg) |
90 | else cb() |
91 | }) |
92 | } |
93 | |
94 | return paramap(function (msg, cb) { |
95 | var c = msg.value && msg.value.content |
96 | if (!c || typeof c !== 'object') return cb() |
97 | if (msg.value.author in ourIds) return cb() |
98 | |
99 | if (c.mentions && Array.isArray(c.mentions) && c.mentions.some(linksToUs)) { |
100 | return cb(null, msg) |
101 | } |
102 | |
103 | if (msg.private) { |
104 | return cb(null, msg) |
105 | } |
106 | |
107 | switch (c.type) { |
108 | case 'post': |
109 | if (c.branch || c.root) { |
110 | return isAnyOurMessage(msg, [].concat(c.branch, c.root), cb) |
111 | } else { |
112 | return cb() |
113 | } |
114 | case 'contact': |
115 | return cb(null, c.contact in ourIds ? msg : null) |
116 | case 'vote': |
117 | if (c.vote && c.vote.link) |
118 | return isOurMsg(c.vote.link, function (err, isOurs) { |
119 | cb(err, isOurs ? msg : null) |
120 | }) |
121 | else return cb() |
122 | case 'issue': |
123 | case 'pull-request': |
124 | return isOurMsg(c.project || c.repo, function (err, isOurs) { |
125 | cb(err, isOurs ? msg : null) |
126 | }) |
127 | case 'issue-edit': |
128 | return isAnyOurMessage(msg, [c.issue].concat(c.issues), cb) |
129 | default: |
130 | cb() |
131 | } |
132 | }, 4) |
133 | } |
134 | |
135 | function getFirstMessage (feedId, cb) { |
136 | sbot_user_feed({id: feedId, gte: 0, limit: 1})(null, cb) |
137 | } |
138 |
Built with git-ssb-web