This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
forked from dyc3/opentogethertube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.js
243 lines (237 loc) · 8.12 KB
/
storage.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
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
const _ = require("lodash");
const moment = require("moment");
const { Room, CachedVideo } = require("./models");
const Sequelize = require("sequelize");
module.exports = {
getRoomByName(roomName) {
return Room.findOne({
where: { name: roomName },
}).then(room => {
if (!room) {
console.error("Room", roomName, "does not exist in db.");
return null;
}
return {
name: room.name,
title: room.title,
description: room.description,
visibility: room.visibility,
};
}).catch(err => {
console.error("Failed to get room by name:", err);
});
},
saveRoom(room) {
return Room.create({
name: room.name,
title: room.title,
description: room.description,
visibility: room.visibility,
}).then(result => {
console.log("Saved room to db: id", result.dataValues.id);
return true;
}).catch(err => {
console.error("Failed to save room to storage:", err);
return false;
});
},
/**
* Gets cached video information from the database. If cached information
* is invalid, it will be omitted from the returned video object.
* @param {string} service The service that hosts the source video.
* @param {string} id The id of the video on the given service.
* @return {Object} Video object, but it may contain missing properties.
*/
getVideoInfo(service, id) {
return CachedVideo.findOne({ where: { service: service, serviceId: id } }).then(cachedVideo => {
if (cachedVideo === null) {
console.log("Cache missed:", service, id);
return { service, id };
}
const origCreatedAt = moment(cachedVideo.createdAt);
const lastUpdatedAt = moment(cachedVideo.updatedAt);
const today = moment();
// We check for changes every at an interval of 30 days, unless the original cache date was
// less than 7 days ago, then the interval is 7 days. The reason for this is that the uploader
// is unlikely to change the video info after a week of the original upload. Since we don't store
// the upload date, we pretend the original cache date is the upload date. This is potentially an
// over optimization.
const isCachedInfoValid = lastUpdatedAt.diff(today, "days") <= (origCreatedAt.diff(today, "days") <= 7) ? 7 : 30;
let video = {
service: cachedVideo.service,
id: cachedVideo.serviceId,
};
// We only invalidate the title and description because those are the only ones that can change.
if (cachedVideo.title !== null && isCachedInfoValid) {
video.title = cachedVideo.title;
}
if (cachedVideo.description !== null && isCachedInfoValid) {
video.description = cachedVideo.description;
}
if (cachedVideo.thumbnail !== null) {
video.thumbnail = cachedVideo.thumbnail;
}
if (cachedVideo.length !== null) {
video.length = cachedVideo.length;
}
return video;
}).catch(err => {
console.warn("Cache failure", err);
return { service, id };
});
},
/**
* Gets cached video information from the database. If cached information
* is invalid, it will be omitted from the returned video object.
* Does not guarantee order will be maintained.
* @param {Array.<Video|Object>} videos The videos to find in the cache.
* @return {Promise.<Object>} Video object, but it may contain missing properties.
*/
getManyVideoInfo(videos) {
const { or, and } = Sequelize.Op;
videos = videos.map(video => {
video = _.cloneDeep(video);
video.serviceId = video.id;
delete video.id;
return video;
});
return CachedVideo.findAll({
where: {
[or]: videos.map(video => {
return {
[and]: [
{ service: video.service },
{ serviceId: video.serviceId },
],
};
}),
},
}).then(foundVideos => {
if (videos.length !== foundVideos.length) {
for (let video of videos) {
if (!_.find(foundVideos, video)) {
foundVideos.push(video);
}
}
}
return foundVideos.map(cachedVideo => {
const origCreatedAt = moment(cachedVideo.createdAt);
const lastUpdatedAt = moment(cachedVideo.updatedAt);
const today = moment();
// We check for changes every at an interval of 30 days, unless the original cache date was
// less than 7 days ago, then the interval is 7 days. The reason for this is that the uploader
// is unlikely to change the video info after a week of the original upload. Since we don't store
// the upload date, we pretend the original cache date is the upload date. This is potentially an
// over optimization.
const isCachedInfoValid = lastUpdatedAt.diff(today, "days") <= (origCreatedAt.diff(today, "days") <= 7) ? 7 : 30;
let video = {
service: cachedVideo.service,
id: cachedVideo.serviceId,
};
// We only invalidate the title and description because those are the only ones that can change.
if (cachedVideo.title && isCachedInfoValid) {
video.title = cachedVideo.title;
}
if (cachedVideo.description && isCachedInfoValid) {
video.description = cachedVideo.description;
}
if (cachedVideo.thumbnail) {
video.thumbnail = cachedVideo.thumbnail;
}
if (cachedVideo.length) {
video.length = cachedVideo.length;
}
return video;
});
}).catch(err => {
console.warn("Cache failure", err);
return videos;
});
},
/**
* Updates the database with the given video. If the video exists in
* the database, it is overwritten. Omitted properties will not be
* overwritten. If the video does not exist in the database, it will be
* created.
* @param {Video|Object} video Video object to store
*/
updateVideoInfo(video) {
video = _.cloneDeep(video);
video.serviceId = video.id;
delete video.id;
return CachedVideo.findOne({ where: { service: video.service, serviceId: video.serviceId } }).then(cachedVideo => {
console.log(`Found video ${video.service}:${video.serviceId} in cache`);
CachedVideo.update(video, { where: { id: cachedVideo.id } }).then(rowsUpdated => {
console.log("Updated database records, updated", rowsUpdated, "rows");
return true;
}).catch(err => {
console.error("Failed to cache video info", err);
return false;
});
}).catch(() => {
return CachedVideo.create(video).then(() => {
console.log(`Stored video info for ${video.service}:${video.serviceId} in cache`);
return true;
}).catch(err => {
console.error("Failed to cache video info", err);
return false;
});
});
},
/**
* Updates the database for all the videos in the given list. If a video
* exists in the database, it is overwritten. Omitted properties will not
* be overwritten. If the video does not exist in the database, it will be
* created.
*
* This also minimizes the number of database queries made by doing bulk
* queries instead of a query for each video.
* @param {Array} videos List of videos to store.
*/
updateManyVideoInfo(videos) {
const { or, and } = Sequelize.Op;
videos = videos.map(video => {
video = _.cloneDeep(video);
video.serviceId = video.id;
delete video.id;
return video;
});
return CachedVideo.findAll({
where: {
[or]: videos.map(video => {
return {
[and]: [
{ service: video.service },
{ serviceId: video.serviceId },
],
};
}),
},
}).then(foundVideos => {
let [
toUpdate,
toCreate,
] = _.partition(videos, video => _.find(foundVideos, { service: video.service, serviceId: video.serviceId }));
console.log("bulk cache: should update", toUpdate.length, "rows, create", toCreate.length, "rows");
toCreate = toCreate.map(video => CachedVideo.build(video));
toUpdate = toUpdate.map(video => Object.assign(_.find(foundVideos, { service: video.service, serviceId: video.serviceId }), video));
return CachedVideo.bulkCreate(_.concat(toCreate, toUpdate)).then(cachedVideos => {
console.log("bulk cache: created/updated", cachedVideos.length, "rows");
return true;
}).catch(err => {
console.error("Failed to bulk update video cache:", err);
return false;
});
});
},
getVideoInfoFields() {
let fields = [];
for (let column in CachedVideo.rawAttributes) {
if (column === "id" || column === "createdAt" || column === "updatedAt" || column === "serviceId") {
continue;
}
fields.push(column);
}
return fields;
},
};