This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
IRCLikeMessageItem.js
176 lines (169 loc) · 4.23 KB
/
IRCLikeMessageItem.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { createTextVNode } from "inferno";
import "./IRCLikeMessageItem.css";
import {
mxcMediaToHttp,
bytesToHigherScale,
msToHigherScale,
} from "../../utils";
function IRCLikeMessageItemText({ date, text, sender, isFocused }) {
return (
<div className={"ircmsg" + (isFocused ? "--focused" : "")} tabIndex={0}>
<p>
<b $HasTextChildren>{date}</b>
<b $HasTextChildren>{`<${sender}>`}</b>
{createTextVNode(" " + text)}
</p>
</div>
);
}
function IRCLikeMessageItemNotice({ date, isFocused, text, sender }) {
return (
<div className={"ircmsg" + (isFocused ? "--focused" : "")} tabIndex={0}>
<p>
<i>
<b $HasTextChildren>{date}</b>
<b $HasTextChildren>{`<${sender}>`}</b>
{createTextVNode(" " + text)}
</i>
</p>
</div>
);
}
function IRCLikeMessageItemImage({
date,
sender,
text,
width,
height,
size,
url,
isFocused,
}) {
while (height > (192 * 2) / 3) {
height *= 0.75;
width *= 0.75;
}
while (width > (238 * 2) / 3) {
height *= 0.75;
width *= 0.75;
}
url = window.mClient.mxcUrlToHttp(url, width, height, "scale", true);
return (
<div className={"ircmsg" + (isFocused ? "--focused" : "")} tabIndex={0}>
<p>
<b $HasTextChildren>{date}</b>
<b $HasTextChildren>{`<${sender}>`}</b>
{createTextVNode(text)}
<img width={width} height={height} src={url} alt={text} />
</p>
</div>
);
}
function IRCLikeMessageItemAudio({
date,
isFocused,
sender,
size,
duration,
url,
text,
}) {
const hsUrl = window.mClient.getHomeserverUrl();
url = mxcMediaToHttp(hsUrl, url);
return (
<div className={"ircmsg" + (isFocused ? "--focused" : "")} tabIndex={0}>
<p>
<b $HasTextChildren>{date}</b>
<b $HasTextChildren>{`${sender} has sent an audio clip.`}</b>
<br />
Title: {createTextVNode(text)}
<br />
Duration: {createTextVNode(msToHigherScale(duration))}
<br />
Size: {createTextVNode(bytesToHigherScale(size))}
<audio src={url} autoplay={false} />
</p>
</div>
);
}
function IRCLikeMessageItemUnknown({ date, isFocused, sender }) {
return (
<div className={"ircmsg" + (isFocused ? "--focused" : "")} tabIndex={0}>
<b $HasTextChildren>{date}</b>
<p $HasTextChildren>Unsupported message type was sent from {sender}</p>
</div>
);
}
function IRCLikeMessageItem({ date, sender, content, isFocused }) {
const userId = sender.userId;
let userObj = window.mClient.getUser(userId);
let displayName = (userObj && userObj.displayName) || userId;
// In matrix-js-sdk 15.1.1 sometimes getUser(...) returns null. This is a temporary workaround.
let h = date.getHours().toString();
let m = date.getMinutes().toString();
if (h.length === 1) {
h = "0" + h;
}
if (m.length === 1) {
m = "0" + m;
}
let d = `[${h}:${m}] `;
switch (content.msgtype) {
case "m.text":
return (
<IRCLikeMessageItemText
date={d}
sender={displayName}
text={content.body}
isFocused={isFocused}
/>
);
case "m.notice":
return (
<IRCLikeMessageItemNotice
date={d}
sender={displayName}
text={content.body}
isFocused={isFocused}
/>
);
case "m.image":
return (
<IRCLikeMessageItemImage
date={d}
sender={displayName}
text={content.body}
width={content.info.w}
height={content.info.h}
size={content.info.size}
url={content.url}
isFocused={isFocused}
/>
);
case "m.audio":
return (
<IRCLikeMessageItemAudio
date={d}
sender={displayName}
text={content.body}
duration={content.info.duration}
size={content.info.size}
url={content.url}
isFocused={isFocused}
/>
);
case "m.emote":
case "m.video":
case "m.location":
case "m.file":
default:
return (
<IRCLikeMessageItemUnknown
date={d}
sender={sender.userId}
isFocsued={isFocused}
/>
);
}
}
export default IRCLikeMessageItem;