Files: ffabb0372683da2291d5b0fb3162cce7ba9c520c / message / html / webshares.js
2898 bytesRaw
1 | var { h, computed, when, Value, resolve } = require('mutant') |
2 | var nest = require('depnest') |
3 | var { clipboard, shell } = require('electron') |
4 | |
5 | exports.needs = nest({ |
6 | 'keys.sync.id': 'first', |
7 | 'message.obs.webshares': 'first', |
8 | 'sbot.async.publish': 'first', |
9 | 'translations.sync.strings': 'first', |
10 | 'app.html.lightbox': 'first', |
11 | 'settings.sync.get': 'first' |
12 | }) |
13 | |
14 | exports.gives = nest('message.html.webshares') |
15 | |
16 | exports.create = (api) => { |
17 | return nest('message.html.webshares', function shares (msg) { |
18 | var id = api.keys.sync.id() |
19 | var shares = api.message.obs.webshares(msg.key) |
20 | |
21 | var iShared = computed(shares, shares => shares.includes(id)) |
22 | var count = computed(shares, shares => shares.length ? shares.length : '') |
23 | var isOpen = Value(false) |
24 | var strings = api.translations.sync.strings() |
25 | var publishAndClose = (msg, action) => { |
26 | publishShare(msg, action) |
27 | isOpen.set(false) |
28 | } |
29 | var confirmationDialog = h('div.dialog', [ |
30 | h('div.message', [ |
31 | h('p', strings.share.externalShareLabel) |
32 | ]), |
33 | h('div.actions', [ |
34 | h('Button', { 'ev-click': () => isOpen.set(false) }, strings.userShow.action.cancel), |
35 | h('Button', { style: { 'margin-left': '10px', 'margin-right': '10px' }, 'ev-click': () => publishAndClose(msg, 'copy') }, strings.share.action.copy), |
36 | h('Button -primary', { 'ev-click': () => publishAndClose(msg, 'open') }, strings.share.action.open) |
37 | |
38 | ]) |
39 | ]) |
40 | |
41 | var lb = api.app.html.lightbox(confirmationDialog, isOpen) |
42 | |
43 | return h('WebShares', { 'ev-click': () => isOpen.set(true) }, [ |
44 | h('i.fa.fa-share-alt', { className: when(iShared, '', 'faint') }), |
45 | h('div.count', count), |
46 | lb |
47 | ]) |
48 | }) |
49 | |
50 | function publishShare (msg, action) { |
51 | var sharingScope = api.settings.sync.get('websharingmetrics') |
52 | var url = `http://share2.ticktack.im:8807/${encodeURIComponent(msg.key)}` |
53 | var share = { |
54 | type: 'share', |
55 | share: { link: msg.key, content: 'blog', url: url } |
56 | } |
57 | // if it was a private message, then share as private ... |
58 | if (msg.value.content.recps) { |
59 | share.recps = msg.value.content.recps.map(function (e) { |
60 | return e && typeof e !== 'string' ? e.link : e |
61 | }) |
62 | share.private = true |
63 | } else { |
64 | // ... else obey configuration settings. |
65 | switch (sharingScope) { |
66 | case 'private': |
67 | share.private = true |
68 | break |
69 | case 'author': |
70 | share.recps = [ |
71 | id, |
72 | msg.value.author |
73 | ] |
74 | share.private = true |
75 | break |
76 | case 'public': |
77 | default: |
78 | break |
79 | } |
80 | } |
81 | api.sbot.async.publish(share) |
82 | |
83 | if (action == 'copy') { |
84 | console.log('copying to clipboard') |
85 | clipboard.writeText(url) |
86 | } else { |
87 | console.log('opening external') |
88 | shell.openExternal(url, err => console.log('error', err)) |
89 | } |
90 | } |
91 | } |
92 |
Built with git-ssb-web