git ssb

0+

ev / microbay



forked from Dominic / patchbay

Tree: c76c3ea7deaa59d8cb641d653848825e7c5818c6

Files: c76c3ea7deaa59d8cb641d653848825e7c5818c6 / modules / notifications.js

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

Built with git-ssb-web