Files: 7a4602b4d8ba03a08fa8dc8623644a1673938ce4 / public.js
3436 bytesRaw
1 | var pull = require('pull-stream') |
2 | var ref = require('ssb-ref') |
3 | var Append = require('pull-append') |
4 | |
5 | var sort = require('ssb-sort') |
6 | |
7 | var u = require('yap-util') |
8 | |
9 | toUrl = u.toUrl |
10 | |
11 | //var render = require('./message').render |
12 | var niceAgo = require('nice-ago') |
13 | |
14 | function merge() { |
15 | return Object.assign.apply(null, [{}].concat([].slice.call(arguments))) |
16 | } |
17 | |
18 | module.exports = function (sbot) { |
19 | return function (opts, apply, req) { |
20 | var tr = require('./translations')(req.cookies.lang) |
21 | var self = this |
22 | opts.reverse = opts.reverse != 'false' |
23 | // var min = !isNaN(+opts.lt) ? +opts.lt : Date.now() |
24 | // var max = !isNaN(+opts.gt) ? +opts.gt : 0 |
25 | |
26 | var min = Date.now() |
27 | var max = 0 |
28 | |
29 | var type = 'patch/'+(opts.private ? 'private' : 'public') |
30 | var Type = (opts.private ? 'Private' : 'Public') |
31 | |
32 | return function (cb) { |
33 | //grab a handle on the since value _before_ we make the |
34 | //query, because then we avoid the race condition |
35 | //where something isn't included in the query but |
36 | //is in the since. |
37 | var since = self.since |
38 | var _opts = u.createQuery(opts, {limit: 20, reverse: opts.reverse}) |
39 | var _type = opts.type |
40 | if(opts.channel) { |
41 | delete _opts.query[0].$filter.value.content.type |
42 | } |
43 | pull( |
44 | sbot.query.read(_opts), |
45 | pull.filter(function (v) { |
46 | return v.value.content.type === 'post' |
47 | }), |
48 | pull.collect(function (err, ary) { |
49 | if(err) return cb(err) |
50 | ary = ary.sort(function (a, b) { |
51 | return b.value.timestamp - a.value.timestamp |
52 | }).map(function (data) { |
53 | min = Math.min(data.value.timestamp, min) |
54 | max = Math.max(data.value.timestamp, max) |
55 | return apply('message', data) |
56 | }) |
57 | var nav_opts = {} |
58 | if(opts.author) nav_opts.author = opts.author |
59 | if(opts.channel) nav_opts.channel = opts.channel |
60 | if(opts.private) nav_opts.private = opts.private |
61 | |
62 | var nav = ['span', |
63 | opts.author ? |
64 | apply('avatar', { |
65 | id: opts.author, |
66 | name: true, |
67 | image: false, |
68 | href: toUrl('patch/friends', {id: opts.author}) |
69 | }): '', |
70 | ' ', |
71 | |
72 | //load previous from a url, so that it can be updated by coherence |
73 | apply('more', Object.assign({ |
74 | href: toUrl(type, Object.assign({}, nav_opts, { gt: max })), |
75 | label: '<< '+ niceAgo(Date.now(), max), |
76 | title: 'after '+ new Date(max).toString() |
77 | }, nav_opts, {gt: max })), |
78 | ' + ', |
79 | apply('more', Object.assign({ |
80 | href: toUrl(type, Object.assign({}, nav_opts, { lt: min })), |
81 | label: niceAgo(Date.now(), min) + ' >>', |
82 | title: 'before '+ new Date(min).toString() |
83 | }, nav_opts, {lt: min })), ' ', |
84 | ['a', |
85 | { |
86 | href: toUrl('compose', {private: opts.private, content: {channel: opts.channel}}), |
87 | title: new Date(max).toString() |
88 | }, |
89 | tr('Compose') |
90 | ] |
91 | ] |
92 | ary.unshift(nav) |
93 | ary.push(nav) |
94 | cb(null, ['div.' + Type, ['title', Type], ary]) |
95 | }) |
96 | ) |
97 | } |
98 | } |
99 | } |
100 |
Built with git-ssb-web