git ssb

10+

Matt McKegg / patchwork



Tree: f096230a58e0b7b3a85a61e839a6088a8fe434c2

Files: f096230a58e0b7b3a85a61e839a6088a8fe434c2 / modules / notifications.js

3759 bytesRaw
1var pull = require('pull-stream')
2var paramap = require('pull-paramap')
3var plugs = require('patchbay/plugs')
4var cont = require('cont')
5var ref = require('ssb-ref')
6
7var sbot_log = plugs.first(exports.sbot_log = [])
8var sbot_get = plugs.first(exports.sbot_get = [])
9var sbot_user_feed = plugs.first(exports.sbot_user_feed = [])
10var message_unbox = plugs.first(exports.message_unbox = [])
11var get_id = plugs.first(exports.get_id = [])
12var feed_summary = plugs.first(exports.feed_summary = [])
13
14exports.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
53function 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
64function 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
135function getFirstMessage (feedId, cb) {
136 sbot_user_feed({id: feedId, gte: 0, limit: 1})(null, cb)
137}
138

Built with git-ssb-web