git ssb

16+

Dominic / patchbay



Tree: 5840ace3e723ac68ca995db5a3598cb2ac264c83

Files: 5840ace3e723ac68ca995db5a3598cb2ac264c83 / modules_basic / private.js

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

Built with git-ssb-web