git ssb

10+

Matt McKegg / patchwork



Tree: 9612ad288ce92ed027371e3b3e1c005116464264

Files: 9612ad288ce92ed027371e3b3e1c005116464264 / modules / notifications.js

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

Built with git-ssb-web