git ssb

16+

Dominic / patchbay



Tree: 010c28ba45ecc8a029b813a3cd9e7edc8e098059

Files: 010c28ba45ecc8a029b813a3cd9e7edc8e098059 / modules_extra / notifications.js

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

Built with git-ssb-web