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