forked from mattermost/mattermost-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post_body_additional_content.jsx
357 lines (300 loc) · 10.5 KB
/
post_body_additional_content.jsx
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import PropTypes from 'prop-types';
import React from 'react';
import * as Utils from 'utils/utils.jsx';
import YoutubeVideo from 'components/youtube_video';
import ViewImageModal from 'components/view_image';
import Constants from 'utils/constants';
import * as PostUtils from 'utils/post_utils.jsx';
import MessageAttachmentList from '../message_attachments/message_attachment_list.jsx';
import PostAttachmentOpenGraph from '../post_attachment_opengraph';
import PostImage from '../post_image';
export default class PostBodyAdditionalContent extends React.PureComponent {
static propTypes = {
/**
* The post to render the content of
*/
post: PropTypes.object.isRequired,
/**
* The post's message
*/
children: PropTypes.element.isRequired,
/**
* User's preference to link previews
*/
previewEnabled: PropTypes.bool,
/**
* Flag passed down to PostBodyAdditionalContent for determining if post embed is visible
*/
isEmbedVisible: PropTypes.bool,
/**
* Whether link previews are enabled in the first place.
*/
enableLinkPreviews: PropTypes.bool.isRequired,
/**
* If an image proxy is enabled.
*/
hasImageProxy: PropTypes.bool.isRequired,
/**
* Options specific to text formatting
*/
options: PropTypes.object,
actions: PropTypes.shape({
getRedirectLocation: PropTypes.func.isRequired,
toggleEmbedVisibility: PropTypes.func.isRequired,
}).isRequired,
}
static defaultProps = {
previewEnabled: false,
}
constructor(props) {
super(props);
this.getSlackAttachment = this.getSlackAttachment.bind(this);
this.generateToggleableEmbed = this.generateToggleableEmbed.bind(this);
this.generateStaticEmbed = this.generateStaticEmbed.bind(this);
this.isLinkToggleable = this.isLinkToggleable.bind(this);
this.handleLinkLoadError = this.handleLinkLoadError.bind(this);
this.handleLinkLoaded = this.handleLinkLoaded.bind(this);
this.state = {
link: Utils.extractFirstLink(props.post.message),
linkLoadError: false,
linkLoaded: false,
};
}
componentDidMount() {
// check the availability of the image rendered(if any) in the first render.
this.loadShortenedImageLink();
this.preCheckImageLink();
this.mounted = true;
}
componentWillUnmount() {
this.mounted = false;
}
async loadShortenedImageLink() {
if (!this.isLinkImage(this.state.link) && !YoutubeVideo.isYoutubeLink(this.state.link) && this.props.enableLinkPreviews) {
const {data} = await this.props.actions.getRedirectLocation(this.state.link);
const {link} = this.state;
if (data && data.location && this.mounted) {
this.setState((state) => {
if (state.link !== link) {
return {};
}
return {link: data.location};
});
this.preCheckImageLink();
}
}
}
UNSAFE_componentWillReceiveProps(nextProps) { // eslint-disable-line camelcase
if (nextProps.post.message !== this.props.post.message) {
this.setState({
link: Utils.extractFirstLink(nextProps.post.message),
}, () => {
// check the availability of the image link
this.loadShortenedImageLink();
this.preCheckImageLink();
});
}
}
toggleEmbedVisibility = () => {
this.props.actions.toggleEmbedVisibility(this.props.post.id);
}
getSlackAttachment() {
let attachments = [];
if (this.props.post.props && this.props.post.props.attachments) {
attachments = this.props.post.props.attachments;
}
return (
<MessageAttachmentList
attachments={attachments}
postId={this.props.post.id}
key={this.props.post.id}
options={this.props.options}
imagesMetadata={this.props.post.metadata.images}
/>
);
}
// when image links are collapsed, check if the link is a valid image url and it is available
preCheckImageLink() {
// check only if embedVisible is false i.e the image are by default hidden/collapsed
// if embedVisible is true, the image is rendered, during which image load error is captured
if (!this.props.isEmbedVisible && this.isLinkImage(this.state.link)) {
const image = new Image();
image.src = PostUtils.getImageSrc(this.state.link, this.props.hasImageProxy);
image.onload = () => {
this.handleLinkLoaded();
};
image.onerror = () => {
this.handleLinkLoadError();
};
}
}
isLinkImage(link) {
let linkWithoutQuery = link.toLowerCase();
if (link.indexOf('?') !== -1) {
linkWithoutQuery = linkWithoutQuery.split('?')[0];
}
for (let i = 0; i < Constants.IMAGE_TYPES.length; i++) {
const imageType = Constants.IMAGE_TYPES[i];
if (linkWithoutQuery.endsWith('.' + imageType) || linkWithoutQuery.endsWith('=' + imageType)) {
return true;
}
}
return false;
}
isLinkToggleable() {
const link = this.state.link;
if (!link) {
return false;
}
if (YoutubeVideo.isYoutubeLink(link)) {
return true;
}
if (this.isLinkImage(link)) {
return true;
}
return false;
}
handleLinkLoadError() {
this.setState({
linkLoadError: true,
});
}
handleLinkLoaded() {
this.setState({
linkLoaded: true,
});
}
handleImageClick = () => {
this.setState({
showPreviewModal: true,
});
};
generateToggleableEmbed() {
const link = this.state.link;
if (!link) {
return null;
}
if (YoutubeVideo.isYoutubeLink(link)) {
return (
<YoutubeVideo
channelId={this.props.post.channel_id}
link={link}
show={this.props.isEmbedVisible}
onLinkLoaded={this.handleLinkLoaded}
/>
);
}
if (this.isLinkImage(link)) {
const {metadata} = this.props.post;
return (
<PostImage
channelId={this.props.post.channel_id}
link={link}
onLinkLoadError={this.handleLinkLoadError}
onLinkLoaded={this.handleLinkLoaded}
handleImageClick={this.handleImageClick}
dimensions={metadata && metadata.images && metadata.images[link]}
/>
);
}
return null;
}
generateStaticEmbed() {
if (this.props.post.props && this.props.post.props.attachments) {
return this.getSlackAttachment();
}
const link = Utils.extractFirstLink(this.props.post.message);
if (link && this.props.enableLinkPreviews && this.props.previewEnabled) {
return (
<PostAttachmentOpenGraph
link={link}
isEmbedVisible={this.props.isEmbedVisible}
post={this.props.post}
toggleEmbedVisibility={this.toggleEmbedVisibility}
/>
);
}
return null;
}
renderImagePreview() {
const link = this.state.link;
if (!link || !this.isLinkImage(link)) {
return null;
}
const captureExt = /(?:\.([^.]+))?$/;
if (!captureExt || captureExt.length <= 1) {
return null;
}
const ext = captureExt.exec(link)[1];
return (
<ViewImageModal
show={this.state.showPreviewModal}
onModalDismissed={() => this.setState({showPreviewModal: false})}
startIndex={0}
fileInfos={[{
has_preview_image: false,
link,
extension: ext,
}]}
/>
);
}
render() {
if (this.isLinkToggleable() && !this.state.linkLoadError) {
// if message has only one line and starts with a link place toggle in this only line
// else - place it in new line between message and embed
const prependToggle = (/^\s*https?:\/\/.*$/).test(this.props.post.message);
const toggle = (
<a
key='toggle'
className={`post__embed-visibility ${prependToggle ? 'pull-left' : ''}`}
data-expanded={this.props.isEmbedVisible}
aria-label='Toggle Embed Visibility'
onClick={this.toggleEmbedVisibility}
/>
);
const message = (
<div key='message'>
{this.props.children}
</div>
);
const contents = [message];
if (this.state.linkLoaded || YoutubeVideo.isYoutubeLink(this.state.link)) {
if (prependToggle) {
contents.unshift(toggle);
} else {
contents.push(toggle);
}
}
if (this.props.isEmbedVisible) {
contents.push(
<div
key='embed'
className='post__embed-container'
>
{this.generateToggleableEmbed()}
</div>
);
}
const imagePreview = this.renderImagePreview();
return (
<div>
{contents}
{imagePreview}
</div>
);
}
const staticEmbed = this.generateStaticEmbed();
if (staticEmbed) {
return (
<div>
{this.props.children}
{staticEmbed}
</div>
);
}
return this.props.children;
}
}