Files: f9490b57823c88d2d4e1cdc8495b8ab60d63862f / message / html / render / about-subjective.js
1774 bytesRaw
1 | const { h, Value, computed } = require('mutant') |
2 | var nest = require('depnest') |
3 | var extend = require('xtend') |
4 | |
5 | exports.needs = nest({ |
6 | 'about.obs.latestValue': 'first', |
7 | 'sbot.async.get': 'first', |
8 | 'book.html.simpleEmoji': 'first', |
9 | 'message.html': { |
10 | decorate: 'reduce', |
11 | layout: 'first', |
12 | markdown: 'first' |
13 | } |
14 | }) |
15 | |
16 | exports.gives = nest('message.html.render') |
17 | |
18 | exports.create = function (api) { |
19 | return nest('message.html.render', subjective) |
20 | |
21 | function subjective(msg, opts) { |
22 | if (!msg.value || !msg.value.content) |
23 | return |
24 | |
25 | const content = msg.value.content |
26 | |
27 | if (content.type == 'about' && content.rating && content.ratingType) |
28 | { |
29 | const element = api.message.html.layout(msg, extend({ |
30 | content: renderSubjective(msg), |
31 | layout: content.review ? 'default' : 'mini' |
32 | }, opts)) |
33 | |
34 | return api.message.html.decorate(element, { msg }) |
35 | } |
36 | } |
37 | |
38 | function bookTitle(msg) { |
39 | const name = Value('') |
40 | const bookKey = msg.value.content.about |
41 | api.sbot.async.get(bookKey, (err, msg) => { |
42 | let originalTitle = msg.content.title |
43 | let latestTitle = api.about.obs.latestValue(bookKey, 'title')() |
44 | name.set(latestTitle || originalTitle) |
45 | }) |
46 | return name |
47 | } |
48 | |
49 | function renderSubjective(msg) { |
50 | let title = bookTitle(msg) |
51 | const content = msg.value.content |
52 | return ['Rated ', h('a', { href: msg.value.content.about }, title), |
53 | ' ' + content.rating + ' ', |
54 | content.ratingMax ? ' / ' + content.ratingMax : '', |
55 | h('span.text', { innerHTML: |
56 | computed(content.ratingType, |
57 | api.book.html.simpleEmoji) }), |
58 | computed(content.review, api.message.html.markdown) |
59 | ] |
60 | } |
61 | } |
62 |
Built with git-ssb-web