git ssb

16+

Dominic / patchbay



Tree: c14e98624b1ae5d90235af8556d3f3d82510130d

Files: c14e98624b1ae5d90235af8556d3f3d82510130d / modules_basic / avatar / edit.js

4484 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,
10 map, computed, when
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 var img = visualize(new Buffer(id.substring(1), 'base64'), 256)
52
53 var proposedAvatar = MutantObject()
54 getAvatar({links: api.sbot_links}, self_id, id, (err, avatar) => {
55 if (err) return console.error(err)
56 //don't show user has already selected an avatar.
57 if(proposedAvatar.keys().length) return
58 if(ref.isBlob(avatar.image))
59 img.src = api.blob_url(avatar.image)
60 })
61
62 var images = MutantArray()
63 pull(
64 api.sbot_links({dest: id, rel: 'about', values: true}),
65 pull.map(e => e.value.content.image),
66 pull.filter(e => e && 'string' == typeof e.link),
67 pull.unique('link'),
68 pull.drain(image => images.push(image) )
69 )
70
71 var lb = hyperlightbox()
72 var name = Value(api.avatar_name(id))
73 var proposedName = Value()
74 var names = [] //TODO load in name aliases
75 var name_input = h('input', {placeholder: ' + another name', 'ev-keyup': (e) => proposedName.set(e.target.value) })
76 var description = '' //TODO load this in, make this editable
77
78 var isPossibleUpdate = computed([proposedName, proposedAvatar], (name, image) => {
79 return name || Object.keys(image).length
80 })
81
82 return h('ProfileEdit', [
83 h('section.lightbox', lb),
84 h('section.avatar', [
85 h('section', img),
86 h('footer', name),
87 ]),
88 h('section.description', description),
89 h('section.aliases', [
90 h('header', 'Aliases'),
91 h('section.avatars', [
92 h('header', 'Avatars'),
93 map(images, image => h('img', {
94 'src': api.blob_url(image),
95 'ev-click': changeSelectedImage(image)
96 })),
97 h('div.file-upload', [
98 hyperfile.asDataURL(dataUrlCallback)
99 ])
100 ]),
101 h('section.names', [
102 h('header', 'Names'),
103 names,
104 name_input
105 ]),
106 when(isPossibleUpdate, h('button.confirm', { 'ev-click': handleUpdateClick }, 'Confirm changes'))
107 ])
108 ])
109
110 function changeSelectedImage (image) {
111 return () => {
112 proposedAvatar.set(image)
113 img.src = api.blob_url(image.link || image)
114 }
115 }
116
117 function dataUrlCallback (data) {
118 var el = crop(data, (err, data) => {
119 if(data) {
120 img.src = data
121 var _data = dataurl.parse(data)
122 pull(
123 pull.once(_data.data),
124 api.sbot_blobs_add((err, hash) => {
125 //TODO. Alerts are EVIL.
126 //I use them only in a moment of weakness.
127
128 if(err) return alert(err.stack)
129 proposedAvatar.set({
130 link: hash,
131 size: _data.data.length,
132 type: _data.mimetype,
133 width: 512,
134 height: 512
135 })
136 })
137 )
138 }
139 lb.close()
140 })
141 lb.show(el)
142 }
143
144 function handleUpdateClick () {
145 const name = proposedName()
146 const avatar = proposedAvatar()
147
148 const msg = {
149 type: 'about',
150 about: id
151 }
152
153 if (name) msg.name = name
154 if (Object.keys(avatar).length) msg.image = avatar
155
156 api.message_confirm(msg)
157
158 if (name) name.set('@'+name)
159 }
160 }
161
162}
163
164

Built with git-ssb-web