git ssb

0+

mixmix / ssb-client-basic-tutorial



Commit cf524354e56763eb8ba2d0ab07b1fe49066baec6

drop helpers/ in favour of source/

mixmix committed on 10/17/2018, 11:42:33 AM
Parent: a6d1c96847d89a3d559508ed9c4443a8730a7145

Files changed

v03.jschanged
helpers/days-posts.jsdeleted
source/days-posts.jsadded
v03.jsView
@@ -1,8 +1,8 @@
11 const Connection = require('ssb-client')
22 const pull = require('pull-stream')
33 pull.paraMap = require('pull-paramap')
4-const daysPosts = require('./helpers/days-posts')
4 +const daysPosts = require('./source/days-posts')
55
66 console.log('Connecting')
77
88 Connection((err, server) => {
helpers/days-posts.jsView
@@ -1,45 +1,0 @@
1-// Note this is just extracted from v02.js and gussied up a little to make is more useable and to make v03.js less cluttered
2-
3-module.exports = function (server) {
4- if (!server) throw new Error('day-posts helper requires a server!')
5- if (!server.query) throw new Error('day-posts helper requires a server with the ssb-query installed!')
6-
7- return function daysPosts (day = new Date()) {
8- const opts = {
9- reverse: true,
10- query: [
11- {
12- $filter: {
13- value: {
14- content: { type: 'post' },
15- timestamp: {
16- $gte: Number(startOfDay(day)),
17- $lt: Number(startOfDay(day, +1))
18- }
19- }
20- }
21- }, {
22- $map: {
23- author: ['value', 'author'],
24- timestamp: ['value', 'timestamp'],
25- text: ['value', 'content', 'text'],
26- root: ['value', 'content', 'root'] // the root messages of a thread, this is present if this post is a reply to another message
27- }
28- }
29- ]
30- }
31-
32- return server.query.read(opts)
33- // returns a source stream
34- // will not start delivering data until it's connected with a sink
35- }
36-}
37-
38-function startOfDay (time = new Date(), dayOffset = 0) {
39- // dayOffset = 0 means if this argument is not supplied to set it to default to 0
40-
41- const year = time.getFullYear()
42- const month = time.getMonth()
43- const date = time.getDate() + dayOffset
44- return new Date(year, month, date, 0, 0, 0) // 0 hours, 0 minutes, 0 secords
45-}
source/days-posts.jsView
@@ -1,0 +1,45 @@
1 +// Note this is just extracted from v02.js and gussied up a little to make is more useable and to make v03.js less cluttered
2 +
3 +module.exports = function (server) {
4 + if (!server) throw new Error('day-posts helper requires a server!')
5 + if (!server.query) throw new Error('day-posts helper requires a server with the ssb-query installed!')
6 +
7 + return function daysPosts (day = new Date()) {
8 + const opts = {
9 + reverse: true,
10 + query: [
11 + {
12 + $filter: {
13 + value: {
14 + content: { type: 'post' },
15 + timestamp: {
16 + $gte: Number(startOfDay(day)),
17 + $lt: Number(startOfDay(day, +1))
18 + }
19 + }
20 + }
21 + }, {
22 + $map: {
23 + author: ['value', 'author'],
24 + timestamp: ['value', 'timestamp'],
25 + text: ['value', 'content', 'text'],
26 + root: ['value', 'content', 'root'] // the root messages of a thread, this is present if this post is a reply to another message
27 + }
28 + }
29 + ]
30 + }
31 +
32 + return server.query.read(opts)
33 + // returns a source stream
34 + // will not start delivering data until it's connected with a sink
35 + }
36 +}
37 +
38 +function startOfDay (time = new Date(), dayOffset = 0) {
39 + // dayOffset = 0 means if this argument is not supplied to set it to default to 0
40 +
41 + const year = time.getFullYear()
42 + const month = time.getMonth()
43 + const date = time.getDate() + dayOffset
44 + return new Date(year, month, date, 0, 0, 0) // 0 hours, 0 minutes, 0 secords
45 +}

Built with git-ssb-web