git ssb

1+

Daan Patchwork / patchwork



Tree: 4d9f238ff73f7136cd292da88f06c17fe1a8c445

Files: 4d9f238ff73f7136cd292da88f06c17fe1a8c445 / lib / depject / message / html / render / about.js

3023 bytesRaw
1const h = require('mutant/h')
2const computed = require('mutant/computed')
3const nest = require('depnest')
4const extend = require('xtend')
5const ref = require('ssb-ref')
6const addContextMenu = require('../../../../message/html/decorate/context-menu')
7
8exports.needs = nest({
9 'message.html': {
10 layout: 'first',
11 markdown: 'first'
12 },
13 'profile.html.person': 'first',
14 'about.obs.name': 'first',
15 'blob.sync.url': 'first',
16 'intl.sync.i18n': 'first'
17})
18
19exports.gives = nest('message.html', {
20 canRender: true,
21 render: true
22})
23
24exports.create = function (api) {
25 const i18n = api.intl.sync.i18n
26 return nest('message.html', {
27 canRender: isRenderable,
28 render: function (msg, opts) {
29 if (!isRenderable(msg)) return
30
31 const c = msg.value.content
32 const self = msg.value.author === c.about
33
34 const miniContent = []
35 const content = []
36
37 if (c.name) {
38 const target = api.profile.html.person(c.about, c.name)
39 miniContent.push(computed([self, api.about.obs.name(c.about), c.name], (self, a, b) => {
40 if (self) {
41 return [i18n('self identifies as '), '"', target, '"']
42 } else if (a === b) {
43 return [i18n('identified '), api.profile.html.person(c.about)]
44 } else {
45 return [i18n('identifies '), api.profile.html.person(c.about), i18n(' as "'), target, '"']
46 }
47 }))
48 }
49
50 if (c.image) {
51 if (!miniContent.length) {
52 const imageAction = self ? i18n('self assigned a display image') : [i18n('assigned a display image to '), api.profile.html.person(c.about)]
53 miniContent.push(imageAction)
54 }
55
56 content.push(h('a AboutImage', {
57 href: c.about
58 }, [
59 h('img', { src: api.blob.sync.url(c.image) })
60 ]))
61 }
62
63 const elements = []
64
65 if (miniContent.length) {
66 const element = api.message.html.layout(msg, extend({
67 showActions: true,
68 miniContent,
69 content,
70 layout: 'mini'
71 }, opts))
72 elements.push(addContextMenu(element, { msg }))
73 }
74
75 if (c.description) {
76 elements.push(addContextMenu(api.message.html.layout(msg, extend({
77 showActions: true,
78 miniContent: self ? i18n('self assigned a description') : [i18n('assigned a description to '), api.profile.html.person(c.about)],
79 content: api.message.html.markdown(c.description),
80 layout: 'mini'
81 }, opts)), { msg }))
82 }
83
84 return elements
85 }
86 })
87
88 function isRenderable (msg) {
89 if (msg.value.content.type !== 'about') return
90 if (!ref.isFeed(msg.value.content.about)) return
91 const c = msg.value.content
92 if (!c || (!c.description && !isBlobLink(c.image) && !c.name)) return
93 return true
94 }
95}
96
97function isBlobLink (link) {
98 if (link && typeof link.link === 'string') {
99 link = link.link
100 }
101 const parsed = ref.parseLink(link)
102 if (parsed && ref.isBlob(parsed.link)) {
103 return true
104 }
105}
106

Built with git-ssb-web