Files: 5cb4510e7266ce92c57a4468efce4479ec64d714 / modules_basic / message.js
3346 bytesRaw
1 | var h = require('hyperscript') |
2 | var u = require('../util') |
3 | var pull = require('pull-stream') |
4 | |
5 | //var plugs = require('../plugs') |
6 | // |
7 | //var message_content = plugs.first(exports.message_content = []) |
8 | //var message_content_mini = plugs.first(exports.message_content_mini = []) |
9 | // |
10 | //var avatar = plugs.first(exports.avatar = []) |
11 | //var avatar_name = plugs.first(exports.avatar_name = []) |
12 | //var avatar_link = plugs.first(exports.avatar_link = []) |
13 | //var message_meta = plugs.map(exports.message_meta = []) |
14 | //var message_action = plugs.map(exports.message_action = []) |
15 | //var message_link = plugs.first(exports.message_link = []) |
16 | // |
17 | //var sbot_links = plugs.first(exports.sbot_links = []) |
18 | |
19 | exports.needs = { |
20 | message_content: 'first', |
21 | message_content_mini: 'first', |
22 | avatar: 'first', |
23 | avatar_name: 'first', |
24 | avatar_link: 'first', |
25 | message_meta: 'map', |
26 | message_action: 'map', |
27 | message_link: 'first', |
28 | // sbot_links: 'first' |
29 | } |
30 | |
31 | exports.gives = 'message_render' |
32 | |
33 | function message_content_mini_fallback(msg) { |
34 | return h('code', msg.value.content.type) |
35 | } |
36 | |
37 | exports.create = function (api) { |
38 | |
39 | function mini(msg, el) { |
40 | var div = h('div.message.message--mini', |
41 | h('div.row', |
42 | h('div', |
43 | api.avatar_link(msg.value.author, api.avatar_name(msg.value.author)), |
44 | h('span.message_content', el)), |
45 | h('div.message_meta.row', api.message_meta(msg)) |
46 | ) |
47 | ) |
48 | div.setAttribute('tabindex', '0') |
49 | return div |
50 | } |
51 | |
52 | return function (msg, sbot) { |
53 | var el = api.message_content_mini(msg) |
54 | if(el) return mini(msg, el) |
55 | |
56 | var el = api.message_content(msg) |
57 | if(!el) return mini(msg, message_content_mini_fallback(msg)) |
58 | |
59 | var links = [] |
60 | for(var k in CACHE) { |
61 | var _msg = CACHE[k] |
62 | if(Array.isArray(_msg.content.mentions)) { |
63 | for(var i = 0; i < _msg.content.mentions.length; i++) |
64 | if(_msg.content.mentions[i].link == msg.key) |
65 | links.push(k) |
66 | } |
67 | } |
68 | |
69 | var backlinks = h('div.backlinks') |
70 | if(links.length) |
71 | backlinks.appendChild(h('label', 'backlinks:', |
72 | h('div', links.map(function (key) { |
73 | return api.message_link(key) |
74 | })) |
75 | )) |
76 | |
77 | |
78 | // pull( |
79 | // sbot_links({dest: msg.key, rel: 'mentions', keys: true}), |
80 | // pull.collect(function (err, links) { |
81 | // if(links.length) |
82 | // backlinks.appendChild(h('label', 'backlinks:', |
83 | // h('div', links.map(function (link) { |
84 | // return message_link(link.key) |
85 | // })) |
86 | // )) |
87 | // }) |
88 | // ) |
89 | |
90 | var msg = h('div.message', |
91 | h('div.title.row', |
92 | h('div.avatar', api.avatar(msg.value.author, 'thumbnail')), |
93 | h('div.message_meta.row', api.message_meta(msg)) |
94 | ), |
95 | h('div.message_content', el), |
96 | h('div.message_actions.row', |
97 | h('div.actions', api.message_action(msg), |
98 | h('a', {href: '#' + msg.key}, 'Reply') |
99 | ) |
100 | ), |
101 | backlinks, |
102 | {onkeydown: function (ev) { |
103 | //on enter, hit first meta. |
104 | if(ev.keyCode == 13) { |
105 | |
106 | // unless in an input |
107 | if (ev.target.nodeName === 'INPUT' |
108 | || ev.target.nodeName === 'TEXTAREA') return |
109 | |
110 | msg.querySelector('.enter').click() |
111 | } |
112 | }} |
113 | ) |
114 | |
115 | // ); hyperscript does not seem to set attributes correctly. |
116 | msg.setAttribute('tabindex', '0') |
117 | |
118 | return msg |
119 | } |
120 | } |
121 | |
122 | |
123 |
Built with git-ssb-web