git ssb

0+

Dominic / yap-patch



Tree: 634a3e47dc6133cc55dae03d47dc86880af6282c

Files: 634a3e47dc6133cc55dae03d47dc86880af6282c / public.js

3311 bytesRaw
1var pull = require('pull-stream')
2var ref = require('ssb-ref')
3var Append = require('pull-append')
4
5var sort = require('ssb-sort')
6
7var u = require('yap-util')
8
9toUrl = u.toUrl
10
11//var render = require('./message').render
12var niceAgo = require('nice-ago')
13
14function merge() {
15 return Object.assign.apply(null, [{}].concat([].slice.call(arguments)))
16}
17
18module.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
40 pull(
41 sbot.query.read(_opts),
42 pull.filter(function (v) {
43 return v.value.content.type === 'post'
44 }),
45 pull.collect(function (err, ary) {
46 if(err) return cb(err)
47 ary = ary.sort(function (a, b) {
48 return b.value.timestamp - a.value.timestamp
49 }).map(function (data) {
50 min = Math.min(data.value.timestamp, min)
51 max = Math.max(data.value.timestamp, max)
52 return apply('message', data)
53 })
54 var nav_opts = {}
55 if(opts.author) nav_opts.author = opts.author
56 if(opts.channel) nav_opts.channel = opts.channel
57 if(opts.private) nav_opts.private = opts.private
58
59 var nav = ['span',
60 opts.author ?
61 apply('avatar', {
62 id: opts.author,
63 name: true,
64 image: false,
65 href: toUrl('patch/friends', {id: opts.author})
66 }): '',
67 ' ',
68
69 //load previous from a url, so that it can be updated by coherence
70 apply('more', Object.assign({
71 href: toUrl(type, Object.assign({}, nav_opts, { gt: max })),
72 label: '<< '+ niceAgo(Date.now(), max),
73 title: 'after '+ new Date(max).toString()
74 }, nav_opts, {gt: max })),
75 ' + ',
76 apply('more', Object.assign({
77 href: toUrl(type, Object.assign({}, nav_opts, { lt: min })),
78 label: niceAgo(Date.now(), min) + ' >>',
79 title: 'before '+ new Date(min).toString()
80 }, nav_opts, {lt: min })), ' ',
81 ['a',
82 {
83 href: toUrl('compose', {private: opts.private, content: {channel: opts.channel}}),
84 title: new Date(max).toString()
85 },
86 tr('Compose')
87 ]
88 ]
89 ary.unshift(nav)
90 ary.push(nav)
91 cb(null, ['div.' + Type, ['title', Type], ary])
92 })
93 )
94 }
95 }
96}
97

Built with git-ssb-web