git ssb

16+

Dominic / patchbay



Tree: 3c9b18facfb73339b585ecdd2141a23586ab0095

Files: 3c9b18facfb73339b585ecdd2141a23586ab0095 / modules / notifications.js

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

Built with git-ssb-web