-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.js
262 lines (224 loc) · 6.22 KB
/
index.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
const functions = require("firebase-functions");
const algoliasearch = require("algoliasearch");
const admin = require("firebase-admin");
admin.initializeApp();
// Algolia search support
const ALGOLIA_ID = functions.config().algolia.app_id;
const ALGOLIA_ADMIN_KEY = functions.config().algolia.api_key;
const ALGOLIA_SEARCH_KEY = functions.config().algolia.search_key;
const ALGOLIA_INDEX_NAME = "posts";
const ALGOLIA_USER_INDEX_NAME = "users";
const client = algoliasearch(ALGOLIA_ID, ALGOLIA_ADMIN_KEY);
function getFirebaseUser(req, res, next) {
if (
!req.headers.authorization ||
!req.headers.authorization.startsWith("Bearer ")
) {
console.error(
"No Firebase ID token was passed as a Bearer token in the Authorization header.",
"Make sure you authorize your request by providing the following HTTP header:",
"Authorization: Bearer <Firebase ID Token>"
);
res.status(403).send("Unauthorized");
return;
}
let idToken;
if (
req.headers.authorization &&
req.headers.authorization.startsWith("Bearer ")
) {
idToken = req.headers.authorization.split("Bearer ")[1];
}
admin
.auth()
.verifyIdToken(idToken)
.then(decodedIdToken => {
req.user = decodedIdToken;
next();
})
.catch(error => {
console.error("Error while verifying Firebase ID token:", error);
res.status(403).send("Unauthorized");
});
}
const app = require("express")();
app.use(require("cors")({ origin: true }));
/**
* Run a search query from the client side
*/
function getFilterStringFromUsers(userId, users = []) {
let base = `userId:${userId}`;
// include other users you are following
users.forEach(id => {
base += ` OR userId:${id}`;
});
return base;
}
function getRelationsForUserId(userId) {
return admin
.firestore()
.collection("relations")
.where("fromUserId", "==", userId)
.where("confirmed", "==", true)
.limit(1000)
.get()
.then(snapshot => {
const userIds = [];
snapshot.docs.forEach(relation => {
userIds.push(relation.data().toUserId);
});
return userIds;
});
}
app.get("/query", getFirebaseUser, (req, res) => {
const userId = req.user.user_id;
getRelationsForUserId(userId).then(userIds => {
const params = {
filters: getFilterStringFromUsers(req.user.user_id, userIds),
userToken: req.user.user_id
};
const key = client.generateSecuredApiKey(ALGOLIA_SEARCH_KEY, params);
res.json({ key });
});
});
/**
* Fetch a key to list all other users recipes
*/
app.get("/following", getFirebaseUser, (req, res) => {
const userId = req.user.user_id;
getRelationsForUserId(userId).then(userIds => {
const filterString = getFilterStringFromUsers(null, userIds);
const params = {
filters: filterString,
userToken: req.user.user_id
};
const key = client.generateSecuredApiKey(ALGOLIA_SEARCH_KEY, params);
res.json({ key });
});
});
/**
* Index our text on creation
*/
exports.onRecipeCreated = functions.firestore
.document("recipes/{recipeId}")
.onCreate(snap => {
return indexEntry(snap);
});
/**
* Update our algolia index on updates
*/
exports.onRecipeUpdated = functions.firestore
.document("recipes/{recipeId}")
.onUpdate(snap => {
return indexEntry(snap.after);
});
function indexEntry(entry) {
const post = entry.data();
post.objectID = entry.id;
const index = client.initIndex(ALGOLIA_INDEX_NAME);
index.setSettings({
attributesToSnippet: ["text:30"],
snippetEllipsisText: "…"
});
return index.saveObject(post).then(
p => {
console.log("saved post", p);
},
err => {
console.error(err);
}
);
}
/**
* Delete our algolia index on deletion
*/
exports.onRecipeDeleted = functions.firestore
.document("recipes/{recipeId}")
.onDelete(snap => {
const index = client.initIndex(ALGOLIA_INDEX_NAME);
return index.deleteObject(snap.id);
});
exports.api = functions.https.onRequest(app);
/**
* Save users to algolia for user searches
*/
exports.onUserCreated = functions.auth.user().onCreate(user => {
const index = client.initIndex(ALGOLIA_USER_INDEX_NAME);
const userToSave = Object.assign(
{},
{
email: user.email,
displayName: user.displayName,
photoURL: user.photoURL,
uid: user.uid
},
{
objectID: user.uid
}
);
return Promise.all([
index.saveObject(userToSave),
createUserProfile(user.uid, user)
]);
});
function createUserProfile(userId, user) {
return admin
.firestore()
.collection("users")
.doc(userId)
.set({
uid: userId,
displayName: user.displayName,
email: user.email,
photoURL: user.photoURL
});
}
const { Storage } = require("@google-cloud/storage");
const gcs = new Storage();
const os = require("os");
const path = require("path");
const fs = require("fs-extra");
const sharp = require("sharp");
exports.onFileChange = functions.storage.object().onFinalize(object => {
return Promise.all([
createThumnail(object),
createThumnail(object, "thumb-sm@", 150, 100)
]);
});
function createThumnail(object, prefix = "thumb@", width = 1000, height = 700) {
const bucket = gcs.bucket(object.bucket);
const filePath = object.name;
const fileName = filePath.split("/").pop();
const bucketDir = path.dirname(filePath);
const workingDir = path.join(os.tmpdir(), "thumbs");
const tmpFilePath = path.join(workingDir, "source.png");
if (
fileName.includes(prefix) ||
fileName.includes("thumb-sm@") ||
!object.contentType.includes("image")
) {
console.log("exiting function");
return false;
}
return fs
.ensureDir(workingDir)
.then(() => {
return bucket.file(filePath).download({ destination: tmpFilePath });
})
.then(() => {
const thumbName = `${prefix}${fileName}`;
const thumbPath = path.join(workingDir, thumbName);
return sharp(tmpFilePath)
.resize(width, height)
.toFile(thumbPath)
.then(() => {
console.log("creating thumb: ", thumbPath, thumbName);
return bucket.upload(thumbPath, {
destination: path.join(bucketDir, thumbName)
});
});
})
.then(() => {
return fs.remove(workingDir);
});
}