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