git ssb

16+

Dominic / patchbay



Tree: 6b419928e2e49bc866e8d0b72330f7c9aa29b015

Files: 6b419928e2e49bc866e8d0b72330f7c9aa29b015 / modules / notifications.js

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

Built with git-ssb-web