git ssb

16+

Dominic / patchbay



Tree: a41af216c01e66d28a8128ebe76646738b0bd3a0

Files: a41af216c01e66d28a8128ebe76646738b0bd3a0 / modules_basic / avatar / edit.js

5342 bytesRaw
1'use strict'
2const fs = require('fs')
3const dataurl = require('dataurl-')
4const hyperfile = require('hyperfile')
5const hypercrop = require('hypercrop')
6const hyperlightbox = require('hyperlightbox')
7const h = require('../../h')
8const {
9 Value, Array: MutantArray, Dict: MutantObject, Struct,
10 map, computed, when, dictToCollection
11} = require('@mmckegg/mutant')
12const pull = require('pull-stream')
13const getAvatar = require('ssb-avatar')
14const ref = require('ssb-ref')
15const visualize = require('visualize-buffer')
16const self_id = require('../../keys').id
17
18function crop (d, cb) {
19 var canvas = hypercrop(h('img', {src: d}))
20
21 return h('div.column.avatar_pic', [
22 canvas,
23 //canvas.selection,
24 h('div.row.avatar_pic__controls', [
25 h('button', {'ev-click': () => cb(null, canvas.selection.toDataURL()) }, 'okay'),
26 h('button', {'ev-click': () => cb(new Error('canceled')) }, 'cancel')
27 ])
28 ])
29}
30
31exports.needs = {
32 message_confirm: 'first',
33 sbot_blobs_add: 'first',
34 blob_url: 'first',
35 sbot_links: 'first',
36 avatar_name: 'first'
37}
38
39exports.gives = {
40 avatar_edit: true,
41 mcss: true
42}
43
44exports.create = function (api) {
45 return {
46 avatar_edit,
47 mcss: () => fs.readFileSync(__filename.replace(/js$/, 'mcss'), 'utf8')
48 }
49
50 function avatar_edit (id) {
51
52 var avatar = Struct({
53 original: Value(visualize(new Buffer(id.substring(1), 'base64'), 256).src),
54 new: MutantObject()
55 })
56
57 getAvatar({links: api.sbot_links}, self_id, id, (err, _avatar) => {
58 if (err) return console.error(err)
59 //don't show user has already selected an avatar.
60 if(ref.isBlob(_avatar.image))
61 avatar.original.set(api.blob_url(_avatar.image))
62 })
63
64 var name = Struct({
65 original: Value(api.avatar_name(id)),
66 new: Value()
67 })
68
69 var images = MutantArray()
70 pull(
71 api.sbot_links({dest: id, rel: 'about', values: true}),
72 pull.map(e => e.value.content.image),
73 pull.filter(e => e && 'string' == typeof e.link),
74 pull.unique('link'),
75 pull.drain(image => images.push(image) )
76 )
77
78 var namesRecord = MutantObject()
79 pull(
80 api.sbot_links({dest: id, rel: 'about', values: true}),
81 pull.map(e => e.value.content.name),
82 pull.filter(Boolean),
83 pull.drain(name => {
84 var n = namesRecord.get(name) || 0
85 namesRecord.put(name, n+1)
86 })
87 )
88 var names = dictToCollection(namesRecord)
89
90 var lb = hyperlightbox()
91
92
93 var description = '' //TODO load this in, make this editable
94
95 var isPossibleUpdate = computed([name.new, avatar.new], (name, avatar) => {
96 return name || avatar.link
97 })
98
99 var avatarSrc = computed([avatar], avatar => {
100 if (avatar.new.link) return api.blob_url(avatar.new.link)
101 else return avatar.original
102 })
103
104 var displayedName = computed([name], name => {
105 if (name.new) return '@'+name.new
106 else return name.original
107 })
108
109 return h('ProfileEdit', [
110 h('section.lightbox', lb),
111 h('section.avatar', [
112 h('section', [
113 h('img', { src: avatarSrc }),
114 ]),
115 h('footer', displayedName),
116 ]),
117 h('section.description', description),
118 h('section.aliases', [
119 h('header', 'Aliases'),
120 h('section.avatars', [
121 h('header', 'Avatars'),
122 map(images, image => h('img', {
123 'src': api.blob_url(image),
124 'ev-click': () => avatar.new.set(image)
125 })),
126 h('div.file-upload', [
127 hyperfile.asDataURL(dataUrlCallback)
128 ])
129 ]),
130 h('section.names', [
131 h('header', 'Names'),
132 h('section', [
133 map(names, n => h('div', { 'ev-click': () => name.new.set(n.key()) }, [
134 h('div.name', n.key),
135 h('div.count', n.value)
136 ])),
137 h('input', {
138 placeholder: ' + another name',
139 'ev-keyup': e => name.new.set(e.target.value)
140 })
141 ])
142 ]),
143 when(isPossibleUpdate, h('section.action', [
144 h('button.cancel', { 'ev-click': handleCancelClick }, 'cancel'),
145 h('button.confirm', { 'ev-click': handleUpdateClick }, 'confirm changes')
146 ]))
147 ])
148 ])
149
150 function dataUrlCallback (data) {
151 var el = crop(data, (err, data) => {
152 if(data) {
153 var _data = dataurl.parse(data)
154 pull(
155 pull.once(_data.data),
156 api.sbot_blobs_add((err, hash) => {
157 //TODO. Alerts are EVIL.
158 //I use them only in a moment of weakness.
159
160 if(err) return alert(err.stack)
161 avatar.new.set({
162 link: hash,
163 size: _data.data.length,
164 type: _data.mimetype,
165 width: 512,
166 height: 512
167 })
168 })
169 )
170 }
171 lb.close()
172 })
173 lb.show(el)
174 }
175
176 function handleCancelClick () {
177 name.new.set(null)
178 avatar.new.set({})
179 }
180
181 function handleUpdateClick () {
182 const newName = name.new()
183 const newAvatar = avatar.new()
184
185 const msg = {
186 type: 'about',
187 about: id
188 }
189
190 if (newName) msg.name = newName
191 if (newAvatar.link) msg.image = newAvatar
192
193 api.message_confirm(msg)
194 }
195 }
196
197}
198
199

Built with git-ssb-web