git ssb

10+

Matt McKegg / patchwork



Tree: 33a48d4a8febf6f3e24bfec414ddd8c70db320e0

Files: 33a48d4a8febf6f3e24bfec414ddd8c70db320e0 / modules / private.js

2327 bytesRaw
1var pull = require('pull-stream')
2var ref = require('ssb-ref')
3var plugs = require('patchbay/plugs')
4var message_compose = plugs.first(exports.message_compose = [])
5var sbot_log = plugs.first(exports.sbot_log = [])
6var feed_summary = plugs.first(exports.feed_summary = [])
7var message_unbox = plugs.first(exports.message_unbox = [])
8var get_id = plugs.first(exports.get_id = [])
9var avatar_image_link = plugs.first(exports.avatar_image_link = [])
10var update_likes = plugs.first(exports.update_likes = [])
11
12exports.screen_view = function (path, sbot) {
13 if (path === '/private') {
14 var id = get_id()
15
16 return feed_summary((opts) => {
17 return pull(
18 sbot_log(opts),
19 loosen(10), // release tight loops if they continue too long (avoid scroll jank)
20 unbox(),
21 pull.through((item) => {
22 if (item.value) {
23 update_likes(item)
24 }
25 })
26 )
27 }, [
28 message_compose({type: 'post', recps: [], private: true}, {
29 prepublish: function (msg) {
30 msg.recps = [id].concat(msg.mentions).filter(function (e) {
31 return ref.isFeed(typeof e === 'string' ? e : e.link)
32 })
33 if (!msg.recps.length) {
34 throw new Error('cannot make private message without recipients - just mention the user in an at reply in the message you send')
35 }
36 return msg
37 },
38 placeholder: 'Write a private message'
39 })
40 ])
41 }
42}
43
44exports.message_meta = function (msg) {
45 if(msg.value.content.recps || msg.value.private) {
46 return h('span.row', 'PRIVATE', map(msg.value.content.recps, function (id) {
47 return avatar_image_link('string' == typeof id ? id : id.link, 'thumbnail')
48 }))
49 }
50}
51
52function unbox () {
53 return pull(
54 pull.filter(function (msg) {
55 return typeof msg.value.content === 'string'
56 }),
57 pull.map(function (msg) {
58 return message_unbox(msg) || { timestamp: msg.timestamp }
59 })
60 )
61}
62
63function map (ary, iter) {
64 if (Array.isArray(ary)) return ary.map(iter)
65}
66
67function loosen (max) {
68 var lastRelease = Date.now()
69 return pull.asyncMap(function (item, cb) {
70 if (Date.now() - lastRelease > max) {
71 setImmediate(() => {
72 lastRelease = Date.now()
73 cb(null, item)
74 })
75 } else {
76 cb(null, item)
77 }
78 })
79}
80

Built with git-ssb-web