git ssb

16+

Dominic / patchbay



Tree: e7fc925105dd9b5a36e85af534af3db99b13faed

Files: e7fc925105dd9b5a36e85af534af3db99b13faed / modules_extra / notifications.js

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

Built with git-ssb-web