git ssb

10+

Matt McKegg / patchwork



Tree: 9612ad288ce92ed027371e3b3e1c005116464264

Files: 9612ad288ce92ed027371e3b3e1c005116464264 / modules / private.js

2474 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_cache = plugs.first(exports.update_cache = [])
11var h = require('../lib/h')
12
13exports.screen_view = function (path, sbot) {
14 if (path === '/private') {
15 var id = get_id()
16
17 return feed_summary((opts) => {
18 return pull(
19 sbot_log(opts),
20 loosen(10), // release tight loops if they continue too long (avoid scroll jank)
21 unbox(),
22 pull.through((item) => {
23 if (item.value) {
24 update_cache(item)
25 }
26 })
27 )
28 }, [
29 message_compose({type: 'post', recps: [], private: true}, {
30 prepublish: function (msg) {
31 msg.recps = [id].concat(msg.mentions).filter(function (e) {
32 return ref.isFeed(typeof e === 'string' ? e : e.link)
33 })
34 if (!msg.recps.length) {
35 throw new Error('cannot make private message without recipients - just mention the user in an at reply in the message you send')
36 }
37 return msg
38 },
39 placeholder: `Write a private message \n\n\n\nThis can only be read by yourself and people you have @mentioned.`
40 })
41 ], {
42 windowSize: 1000
43 })
44 }
45}
46
47exports.message_meta = function (msg) {
48 if (msg.value.content.recps || msg.value.private) {
49 return h('span.private', [
50 map(msg.value.content.recps, function (id) {
51 return avatar_image_link(typeof id === 'string' ? id : id.link, 'thumbnail')
52 })
53 ])
54 }
55}
56
57function unbox () {
58 return pull(
59 pull.filter(function (msg) {
60 return typeof msg.value.content === 'string'
61 }),
62 pull.map(function (msg) {
63 return message_unbox(msg) || { timestamp: msg.timestamp }
64 })
65 )
66}
67
68function map (ary, iter) {
69 if (Array.isArray(ary)) return ary.map(iter)
70}
71
72function loosen (max) {
73 var lastRelease = Date.now()
74 return pull.asyncMap(function (item, cb) {
75 if (Date.now() - lastRelease > max) {
76 setImmediate(() => {
77 lastRelease = Date.now()
78 cb(null, item)
79 })
80 } else {
81 cb(null, item)
82 }
83 })
84}
85

Built with git-ssb-web