git ssb

2+

mixmix / ticktack



Commit bd34a8d22b29ba32048c072fecfaaa2d0fb0af0f

added external share. problem with opening external url. implements #117

andre alves garzia committed on 4/23/2018, 10:20:45 PM
Parent: 040b198afe9d5a0c14eae6014372adffb6c9091d

Files changed

app/page/blogShow.jschanged
message/html/shares.jschanged
message/html/webshares.jsadded
message/html/webshares.mcssadded
message/index.jschanged
message/obs/shares.jschanged
message/obs/webshares.jsadded
translations/en.jschanged
app/page/blogShow.jsView
@@ -11,8 +11,9 @@
1111 'app.html.sideNav': 'first',
1212 'contact.html.follow': 'first',
1313 'message.html.channel': 'first',
1414 'message.html.likes': 'first',
15+ 'message.html.webshares': 'first',
1516 'message.html.shares': 'first',
1617 'message.html.timeago': 'first',
1718 'feed.obs.thread': 'first',
1819 'blog.html.title': 'first',
@@ -47,9 +48,10 @@
4748 h('h1', title),
4849 timeago(blogMsg),
4950 channel(blogMsg),
5051 api.message.html.likes(blogMsg),
51- api.message.html.shares(blogMsg)
52+ api.message.html.shares(blogMsg),
53+ api.message.html.webshares(blogMsg)
5254 ]),
5355 h('div.author', [
5456 h('div.leftCol', api.about.html.avatar(author, 'medium')),
5557 h('div.rightCol', [
message/html/shares.jsView
@@ -36,9 +36,9 @@
3636 })
3737
3838 var confirmationDialog = h('div.dialog', [
3939 h('div.message', [
40- h('p', strings.share.dialogLabel),
40+ h('p', strings.share.shareLabel),
4141 ]),
4242 h('div.form', [
4343 captionInput
4444 ]),
message/html/webshares.jsView
@@ -1,0 +1,73 @@
1+var { h, computed, when, Value, resolve } = require('mutant')
2+var nest = require('depnest')
3+var { clipboard } = require('electron')
4+var { shell } = require('electron')
5+
6+exports.needs = nest({
7+ 'keys.sync.id': 'first',
8+ 'message.obs.webshares': 'first',
9+ 'sbot.async.publish': 'first',
10+ 'translations.sync.strings': 'first',
11+ 'app.html.lightbox': '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+
44+ return h('WebShares', { 'ev-click': () => isOpen.set(true) }, [
45+ h('i.fa', { className: when(iShared, 'fa-globe', 'fa-globe faint') }),
46+ h('div.count', count),
47+ lb
48+ ])
49+ })
50+
51+ function publishShare(msg, action) {
52+ var url = `http://share2.ticktack.im:8807/${msg.key}`
53+ var share = {
54+ type: 'share',
55+ share: { link: msg.key, content: "blog", url: url }
56+ }
57+ if (msg.value.content.recps) {
58+ share.recps = msg.value.content.recps.map(function (e) {
59+ return e && typeof e !== 'string' ? e.link : e
60+ })
61+ share.private = true
62+ }
63+ api.sbot.async.publish(share)
64+
65+ if (action == "copy") {
66+ console.log("copying to clipboard")
67+ clipboard.writeText(url)
68+ } else {
69+ console.log("opening external")
70+ shell.openExternal(url, err => console.log("error", err))
71+ }
72+ }
73+}
message/html/webshares.mcssView
@@ -1,0 +1,20 @@
1+WebShares {
2+ cursor: pointer
3+ min-width: 2.5rem
4+
5+ display: flex
6+ align-items: center
7+
8+ i {
9+ margin-right: .4rem
10+ }
11+ i.fa-globe {
12+ $colorFontPrimary
13+ }
14+
15+ i.faint {
16+ color: #b9b9b9
17+ }
18+
19+}
20+
message/index.jsView
@@ -6,14 +6,16 @@
66 channel: require('./html/channel'),
77 compose: require('./html/compose'),
88 likes: require('./html/likes'),
99 shares: require('./html/shares'),
10+ webshares: require('./html/webshares'),
1011 subject: require('./html/subject'),
1112 timeago: require('./html/timeago')
1213 },
1314 sync: {
1415 getParticipants: require('./sync/getParticipants')
1516 },
1617 obs: {
17- shares: require('./obs/shares')
18+ shares: require('./obs/shares'),
19+ webshares: require('./obs/webshares')
1820 }
1921 }
message/obs/shares.jsView
@@ -26,9 +26,9 @@
2626 }
2727
2828 var c = msg.value.content
2929 if (c.type !== 'share') return
30- if (!c.share || !c.share.link) return
30+ if (!c.share || !c.share.link || !c.share.text) return
3131
3232 activeShares.forEach((shares) => {
3333 if (shares.id === c.share.link) {
3434 shares.push(msg)
@@ -56,9 +56,9 @@
5656 var shares = computed([backlinks.sync, concat([backlinks, merge])], (sync, backlinks) => {
5757 if (sync) {
5858 return backlinks.reduce((result, msg) => {
5959 var c = msg.value.content
60- if (c.type === 'share' && c.share && c.share.link === id) {
60+ if (c.type === 'share' && c.share && c.share.text && c.share.link === id) {
6161 var value = result[msg.value.author]
6262 if (!value || value[0] < msg.value.timestamp) {
6363 result[msg.value.author] = [msg.value.timestamp, c.share.text]
6464 }
message/obs/webshares.jsView
@@ -1,0 +1,86 @@
1+var nest = require('depnest')
2+var ref = require('ssb-ref')
3+var MutantArray = require('mutant/array')
4+var concat = require('mutant/concat')
5+
6+var { computed } = require('mutant')
7+
8+exports.needs = nest({
9+ 'message.sync.unbox': 'first',
10+ 'backlinks.obs.for': 'first'
11+})
12+
13+exports.gives = nest({
14+ 'sbot.hook.publish': true,
15+ 'message.obs.webshares': true
16+})
17+
18+exports.create = function (api) {
19+ var activeShares = new Set()
20+ return nest({
21+ 'sbot.hook.publish': (msg) => {
22+ if (!(msg && msg.value && msg.value.content)) return
23+ if (typeof msg.value.content === 'string') {
24+ msg = api.message.sync.unbox(msg)
25+ if (!msg) return
26+ }
27+
28+ var c = msg.value.content
29+ if (c.type !== 'share') return
30+ if (!c.share || !c.share.link || !c.share.url) return
31+
32+ activeShares.forEach((shares) => {
33+ if (shares.id === c.share.link) {
34+ shares.push(msg)
35+ }
36+ })
37+ },
38+ 'message.obs.webshares': (id) => {
39+ if (!ref.isLink(id)) throw new Error('an id must be specified')
40+ var obs = get(id)
41+ obs.id = id
42+ var result = computed(obs, getShares, {
43+ // allow manual append for simulated realtime
44+ onListen: () => activeShares.add(obs),
45+ onUnlisten: () => activeShares.delete(obs)
46+ })
47+ result.sync = obs.sync
48+ return result
49+ }
50+ })
51+
52+ function get(id) {
53+ var backlinks = api.backlinks.obs.for(id)
54+ var merge = MutantArray()
55+
56+ var shares = computed([backlinks.sync, concat([backlinks, merge])], (sync, backlinks) => {
57+ if (sync) {
58+ return backlinks.reduce((result, msg) => {
59+ var c = msg.value.content
60+ if (c.type === 'share' && c.share && c.share.url && c.share.link === id) {
61+ var value = result[msg.value.author]
62+ if (!value || value[0] < msg.value.timestamp) {
63+ result[msg.value.author] = [msg.value.timestamp, c.share.url]
64+ }
65+ }
66+ return result
67+ }, {})
68+ } else {
69+ return {}
70+ }
71+ })
72+
73+ shares.push = merge.push
74+ shares.sync = backlinks.sync
75+ return shares
76+ }
77+}
78+
79+function getShares(shares) {
80+ return Object.keys(shares).reduce((result, id) => {
81+ if (shares[id].length >= 1) {
82+ result.push(id)
83+ }
84+ return result
85+ }, [])
86+}
translations/en.jsView
@@ -159,11 +159,14 @@
159159 }
160160 },
161161 share: {
162162 captionPlaceholder: 'Type an optional caption here',
163- dialogLabel: 'Do you to share this post with your followers?',
163+ shareLabel: 'Do you to share this post with your followers?',
164+ externalShareLabel: 'Do you to share this post on the Web?',
164165 action: {
165- share: 'Share'
166+ share: 'Share',
167+ copy: 'Copy external URL',
168+ open: 'Open external URL'
166169 }
167170 },
168171 languages: {
169172 en: 'English',

Built with git-ssb-web