/
LinkPreviewRepository.js
178 lines (159 loc) · 5.82 KB
/
LinkPreviewRepository.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
177
178
/*
* Wire
* Copyright (C) 2018 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*/
import Logger from 'utils/Logger';
window.z = window.z || {};
window.z.links = z.links || {};
z.links.LinkPreviewRepository = class LinkPreviewRepository {
constructor(assetService, propertiesRepository) {
this.getLinkPreviewFromString = this.getLinkPreviewFromString.bind(this);
this.updatedSendPreference = this.updatedSendPreference.bind(this);
this.assetService = assetService;
this.logger = new Logger('z.links.LinkPreviewRepository', z.config.LOGGER.OPTIONS);
this.shouldSendPreviews = propertiesRepository.getPreference(z.properties.PROPERTIES_TYPE.PREVIEWS.SEND);
amplify.subscribe(z.event.WebApp.PROPERTIES.UPDATE.PREVIEWS.SEND, this.updatedSendPreference);
amplify.subscribe(z.event.WebApp.PROPERTIES.UPDATED, properties => {
this.updatedSendPreference(properties.settings.previews.send);
});
}
/**
* Searches for url in given string and creates a link preview.
*
* @note This will already upload the associated image as asset
* @param {string} string - Input text to generate preview for
* @returns {Promise} Resolves with link preview proto message
*/
getLinkPreviewFromString(string) {
if (this.shouldSendPreviews && z.util.Environment.desktop) {
return Promise.resolve().then(() => {
const linkData = z.links.LinkPreviewHelpers.getFirstLinkWithOffset(string);
if (linkData) {
return this.getLinkPreview(linkData.url, linkData.offset).catch(error => {
const isLinkPreviewError = error instanceof z.error.LinkPreviewError;
if (!isLinkPreviewError) {
throw error;
}
});
}
});
}
return Promise.resolve();
}
/**
* Creates link preview for given link. This will upload associated image as asset and will
* resolve with a LinkPreview instance
*
* @param {string} url - URL found to generate link preview from
* @param {number} [offset=0] - starting index of the link
* @returns {Promise} Resolves with a link preview if generated
*/
getLinkPreview(url, offset = 0) {
let openGraphData;
return Promise.resolve()
.then(() => {
if (z.links.LinkPreviewBlackList.isBlacklisted(url)) {
throw new z.error.LinkPreviewError(z.error.LinkPreviewError.TYPE.BLACKLISTED);
}
if (window.openGraph) {
return this._fetchOpenGraphData(url);
}
throw new z.error.LinkPreviewError(z.error.LinkPreviewError.TYPE.NOT_SUPPORTED);
})
.then(fetchedData => {
if (fetchedData) {
openGraphData = fetchedData;
return z.links.LinkPreviewProtoBuilder.buildFromOpenGraphData(openGraphData, url, offset);
}
throw new z.error.LinkPreviewError(z.error.LinkPreviewError.TYPE.NO_DATA_AVAILABLE);
})
.then(linkPreview => {
if (linkPreview) {
return this._fetchPreviewImage(linkPreview, openGraphData.image);
}
throw new z.error.LinkPreviewError(z.error.LinkPreviewError.TYPE.UNSUPPORTED_TYPE);
});
}
/**
* Update the send link preview preference
* @param {boolean} sendPreviewsPreference - Updated preference
* @returns {undefined} No return value
*/
updatedSendPreference(sendPreviewsPreference) {
this.shouldSendPreviews = sendPreviewsPreference;
}
/**
* Fetch and upload open graph images.
*
* @private
* @param {LinkPreview} linkPreview - Link preview proto message
* @param {Object} [openGraphImage={}] - Open graph image URL
* @returns {Promise} Resolves with the link preview proto message
*/
_fetchPreviewImage(linkPreview, openGraphImage = {}) {
if (openGraphImage.data) {
return this._uploadPreviewImage(openGraphImage.data)
.then(asset => {
linkPreview.article[z.cryptography.PROTO_MESSAGE_TYPE.LINK_PREVIEW_IMAGE] = asset; // deprecated
linkPreview.image[z.cryptography.PROTO_MESSAGE_TYPE.LINK_PREVIEW_IMAGE] = asset;
return linkPreview;
})
.catch(error => {
this.logger.error(error);
return linkPreview;
});
}
return Promise.resolve(linkPreview);
}
/**
* Fetch open graph data.
*
* @private
* @param {string} link - Link to fetch open graph data from
* @returns {Promise} Resolves with the retrieved open graph data
*/
_fetchOpenGraphData(link) {
return new Promise(resolve => {
return window
.openGraph(link, (error, data) => {
if (error) {
resolve();
}
if (data) {
data = Object.entries(data).reduce((filteredData, [key, value]) => {
filteredData[key] = Array.isArray(value) ? value[0] : value;
return filteredData;
}, {});
}
resolve(data);
})
.catch(resolve);
});
}
/**
* Upload open graph image as asset
*
* @private
* @param {string} dataUri - image data as base64 encoded data URI
* @returns {Promise} Resolves with the uploaded asset
*/
_uploadPreviewImage(dataUri) {
return Promise.resolve(z.util.base64ToBlob(dataUri)).then(blob =>
this.assetService.uploadImageAsset(blob, {public: true})
);
}
};