git ssb

16+

Dominic / patchbay



Tree: 2c6e20555336c47eae846c5db7b268cf2a85c159

Files: 2c6e20555336c47eae846c5db7b268cf2a85c159 / modules_extra / notifications.js

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

Built with git-ssb-web