git ssb

16+

Dominic / patchbay



Commit 143963d78f350d2500b880b06019e46b743433b2

refactor calendar getter to remove duplications..?

mixmix committed on 10/31/2018, 8:44:06 AM
Parent: a601d157ddcecfcae2a4c1f3088109dbe87ed7e8

Files changed

app/page/calendar.jschanged
app/page/calendar.jsView
@@ -2,8 +2,9 @@
22 const { h, Array: MutantArray, map, Struct, computed, watch, throttle, resolve } = require('mutant')
33 const Month = require('marama')
44
55 const pull = require('pull-stream')
6 +const paraMap = require('pull-paramap')
67 const { isMsg } = require('ssb-ref')
78
89 exports.gives = nest({
910 'app.page.calendar': true,
@@ -13,8 +14,9 @@
1314 exports.needs = nest({
1415 'app.sync.goTo': 'first',
1516 'keys.sync.id': 'first',
1617 'message.html.render': 'first',
18 + 'message.sync.unbox': 'first',
1719 'sbot.async.get': 'first',
1820 'sbot.pull.stream': 'first'
1921 })
2022
@@ -42,9 +44,9 @@
4244 lt: endOfDay(d)
4345 })
4446 })
4547
46- watch(state.year, year => getGatherings(year, state.events, api))
48 + watch(state.year, year => getGatherings(year, state.events, Query))
4749 watchAttending(state.attending, api)
4850
4951 const page = h('CalendarPage', { title: '/calendar' }, [
5052 Calendar(state),
@@ -54,8 +56,12 @@
5456 page.scroll = (i) => scroll(state.range, i)
5557
5658 return page
5759 }
60 +
61 + function Query (opts) {
62 + return api.sbot.pull.stream(server => server.query.read(opts))
63 + }
5864 }
5965
6066 function scroll (range, i) {
6167 const { gte, lt } = resolve(range)
@@ -102,9 +108,11 @@
102108 pull.values(keys),
103109 pull.asyncMap((key, cb) => {
104110 api.sbot.async.get(key, (err, value) => {
105111 if (err) return cb(err)
106- cb(null, {key, value})
112 +
113 + if (typeof value.content === 'object') cb(null, {key, value})
114 + else cb(null, api.message.sync.unbox({key, value}))
107115 })
108116 }),
109117 pull.drain(msg => gatherings.push(msg))
110118 )
@@ -148,9 +156,10 @@
148156 })
149157 )
150158 }
151159
152-function getGatherings (year, events, api) {
160 +
161 +function getGatherings (year, events, Query) {
153162 // gatherings specify times with `about` messages which have a startDateTime
154163 // NOTE - this gets a window of about messages around the current year but does not gaurentee
155164 // that we got all events in this year (e.g. something booked 6 months agead would be missed)
156165 const query = [{
@@ -162,34 +171,39 @@
162171 },
163172 content: {
164173 type: 'about',
165174 startDateTime: {
166- epoch: {$gt: 0}
175 + epoch: {$is: 'number'}
167176 }
168177 }
169178 }
170179 }
171180 }, {
172181 $map: {
173- key: ['value', 'content', 'about'], // gathering
174- date: ['value', 'content', 'startDateTime', 'epoch']
182 + key: ['value', 'content', 'about'],
183 + date: ['value', 'content', 'startDateTime', 'epoch'],
184 + ts: ['value', 'timestamp']
175185 }
176186 }]
177-
178187 const opts = { reverse: false, live: true, query }
179188
189 + var target
180190 pull(
181- api.sbot.pull.stream(server => server.query.read(opts)),
191 + Query(opts),
182192 pull.filter(m => !m.sync),
183- pull.filter(r => isMsg(r.key) && Number.isInteger(r.date)),
184- pull.map(r => {
185- return { key: r.key, date: new Date(r.date) }
193 + pull.filter(m => m.date > 0 && Number.isInteger(m.date)),
194 + pull.map(m => {
195 + m.date = new Date(m.date)
196 + return m
186197 }),
187- pull.drain(({ key, date }) => {
188- var target = events.find(ev => ev.data.key === key)
189- if (target && target.date <= date) events.delete(target)
198 + pull.drain(({ key, date, ts }) => {
199 + target = events.find(ev => ev.data.key === key)
200 + if (target && target.data.ts <= ts) events.delete(target)
201 + // TODO causally sorted about messages
202 + // could do this with a backlinks query, paramap'd
190203
191- events.push({ date, data: { key } })
204 +
205 + events.push({ date, data: { key, ts } })
192206 })
193207 )
194208 }
195209

Built with git-ssb-web