secure-scuttlebot classic
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')
7var keys = require('../keys')
8
9function map(ary, iter) {
10 if(Array.isArray(ary)) return ary.map(iter)
11}
12
13exports.needs = {
14 message_render: 'first',
15 message_compose: 'first',
16 message_unbox: 'first',
17 sbot_log: 'first',
18 avatar_image_link: 'first',
19 emoji_url: 'first'
20}
21
22exports.gives = {
23 builtin_tabs: true,
24 screen_view: true,
25 message_meta: true,
26 message_content_mini: true
27}
28
29exports.create = function (api) {
30 var id = keys.id
31
32 function privateStream (opts) {
33 return pull(
34 u.next(api.sbot_log, opts),
35 pull.map(function (msg) {
36 if(!msg || !msg.value || 'string' != typeof msg.value.content) return null
37 var unboxed = api.message_unbox(msg)
38 if(unboxed) {
39 unboxed.value.private = true
40 return unboxed
41 }
42 msg.value.private = true
43 return msg
44 }),
45 pull.filter(Boolean)
46 )
47 }
48
49 return {
50 builtin_tabs: function () {
51 return ['/private']
52 },
53
54 screen_view: function (path) {
55 if(path !== '/private') return
56
57 var div = h('div.column.scroller',
58 {style: {'overflow':'auto'}})
59
60 var compose = api.message_compose(
61 {type: 'post', recps: [], private: true},
62 {
63 prepublish: function (msg) {
64 msg.recps = [id].concat(msg.mentions).filter(function (e) {
65 return ref.isFeed('string' === typeof e ? e : e.link)
66 })
67 if(!msg.recps.length)
68 throw new Error('cannot make private message without recipients - just mention the user in an at reply in the message you send')
69 return msg
70 },
71 placeholder: 'Write a private message'
72 }
73 )
74
75 var content = h('div.column.scroller__content')
76 div.appendChild(h('div.scroller__wrapper', compose, content))
77
78 pull(
79 privateStream({old: false, limit: 100}),
80 Scroller(div, content, api.message_render, true, false)
81 )
82
83 pull(
84 privateStream({reverse: true, limit: 1000}),
85 Scroller(div, content, api.message_render, false, false, function (err) {
86 if(err) throw err
87 })
88 )
89
90 return div
91 },
92
93 message_meta: function (msg) {
94 var content = msg.value.content
95 var recps = content && content.recps
96 var isEncryptedString = 'string' === typeof content
97 var isPrivate = msg.value.private ||
98 (content && content.private) ||
99 (Array.isArray(recps) && recps.length) ||
100 isEncryptedString
101
102 if(!isPrivate) return
103
104 var icon = api.emoji_url('lock')
105 var label = icon
106 ? h('img', {className: 'emoji', src: icon})
107 : 'PRIVATE'
108
109 return h('span.row', map(recps, function (id) {
110 return api.avatar_image_link('string' == typeof id ? id : id.link, 'thumbnail')
111 }))
112 },
113
114 message_content_mini: function (msg, sbot) {
115 var isEncrypted = typeof msg.value.content === 'string'
116 if(!msg.value.private && !isEncrypted) return
117 if(msg.value.content && typeof msg.value.content === 'object' && msg.value.content.text)
118 return
119 var icon = api.emoji_url('lock')
120 return icon
121 ? h('img', {className: 'emoji', src: icon})
122 : 'PRIVATE'
123 }
124 }
125
126}