git ssb

16+

Dominic / patchbay



Tree: 103c9cb09aa9eb239d03b8d79511054d4391d90b

Files: 103c9cb09aa9eb239d03b8d79511054d4391d90b / modules_basic / private.js

3101 bytesRaw
1'use strict'
2var h = require('hyperscript')
3var u = require('../util')
4var pull = require('pull-stream')
5var Scroller = require('pull-scroll')
6var ref = require('ssb-ref')
7
8function map(ary, iter) {
9 if(Array.isArray(ary)) return ary.map(iter)
10}
11
12exports.needs = {
13 build_scroller: 'first',
14 message_render: 'first',
15 message_compose: 'first',
16 message_unbox: 'first',
17 sbot_log: 'first',
18 sbot_whoami: 'first',
19 avatar_image_link: 'first',
20 emoji_url: 'first'
21}
22
23exports.gives = {
24 builtin_tabs: true,
25 screen_view: true,
26 message_meta: true,
27 message_content_mini: true
28}
29
30exports.create = function (api) {
31
32 function unbox () {
33 return pull(
34 pull.filter(function (msg) {
35 return 'string' == typeof msg.value.content
36 }),
37 pull.map(function (msg) {
38 return api.message_unbox(msg)
39 }),
40 pull.filter(Boolean)
41 )
42 }
43
44 return {
45 builtin_tabs: function () {
46 return ['/private']
47 },
48
49 screen_view: function (path) {
50 if(path !== '/private') return
51
52 var composer = api.message_compose(
53 {type: 'post', recps: [], private: true},
54 {
55 prepublish: function (msg) {
56 msg.recps = [id].concat(msg.mentions).filter(function (e) {
57 return ref.isFeed('string' === typeof e ? e : e.link)
58 })
59 if(!msg.recps.length)
60 throw new Error('cannot make private message without recipients - just mention the user in an at reply in the message you send')
61 return msg
62 },
63 placeholder: 'Write a private message'
64 }
65 )
66 var { container, content } = api.build_scroller({ prepend: composer })
67
68 // if local id is different from sbot id, sbot won't have indexes of
69 // private threads
70 //TODO: put all private indexes client side.
71 var id = require('../keys').id
72 api.sbot_whoami(function (err, feed) {
73 if (err) return console.error(err)
74 if(id !== feed.id)
75 return container.appendChild(h('h4',
76 'Private messages are not supported in the lite client.'))
77
78 pull(
79 u.next(api.sbot_log, {old: false, limit: 100}),
80 unbox(),
81 Scroller(container, content, api.message_render, true, false)
82 )
83
84 pull(
85 u.next(api.sbot_log, {reverse: true, limit: 1000}),
86 unbox(),
87 Scroller(container, content, api.message_render, false, false, function (err) {
88 if(err) throw err
89 })
90 )
91 })
92
93 return container
94 },
95
96 message_meta: function (msg) {
97 if(msg.value.content.recps || msg.value.private)
98 return h('span.row', 'PRIVATE', map(msg.value.content.recps, function (id) {
99 return api.avatar_image_link('string' == typeof id ? id : id.link, 'thumbnail')
100 }))
101 },
102
103 message_content_mini: function (msg, sbot) {
104 if (typeof msg.value.content === 'string') {
105 var icon = api.emoji_url('lock')
106 return icon
107 ? h('img', {className: 'emoji', src: icon})
108 : 'PRIVATE'
109 }
110 }
111 }
112
113}
114
115

Built with git-ssb-web