-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
101 lines (78 loc) · 2.81 KB
/
Copy pathindex.js
File metadata and controls
101 lines (78 loc) · 2.81 KB
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
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const shortid = require('shortid');
admin.initializeApp();
const db = admin.database();
function validateInput(input) {
if (input.fiddle === undefined)
throw new functions.https.HttpsError('invalid-argument', 'Empty fiddle');
}
function validateContext(context) { }
exports.createFiddle = functions.https.onCall(async (data, context) => {
// validateInput(data);
// validateContext(context);
const RETRIES = 10;
for (let i = 0; i < RETRIES; i++) {
let id = shortid.generate();
const dbData = await db.ref(`/fiddle/${id}`).once('value');
if (dbData.exists() !== true) {
const newRecord = {
id,
created: new Date().getTime(),
fiddle: data,
views: 1
}
db.ref(`/fiddle/${id}`).set(newRecord);
return newRecord;
}
}
});
exports.updateFiddle = functions.https.onCall(async (data, context) => {
if (!data.id)
throw new functions.https.HttpsError('invalid-argument', 'Invalid Id');
// validateInput(data);
// validateContext(context);
const { id, fiddle } = data;
const ref = db.ref(`/fiddle/${id}`);
const dbData = await ref.once('value');
if (dbData.exists() === true) {
const record = dbData.val();
await ref.update({
fiddle,
modified: new Date().getTime(),
});
return { ...data, viewCount: record.viewCount };
} else {
throw new functions.https.HttpsError('invalid-argument', 'Invalid Id');
}
});
exports.getFiddle = functions.https.onCall(async (data, context) => {
if (!data.id)
throw new functions.https.HttpsError('invalid-argument', 'Invalid Id');
const { id } = data;
const ref = db.ref(`/fiddle/${id}`);
const dbRecord = await ref.once('value');
if (dbRecord.exists() === true) {
let data = dbRecord.val();
// Update number of hits
ref.child('viewCount').transaction(function (hits) {
return (hits || 0) + 1;
});
return data;
} else {
throw new functions.https.HttpsError('not-found');
}
});
exports.fetchAd = functions.https.onCall(async (data, context) => {
const adRef = db.ref('/ads/');
const lastIndex = (await adRef.child('lastIndex').once('value')).val();
const campaignsRecord = await adRef.child('/campaigns').once('value');
const campaigns = campaignsRecord.val();
const newIndex = (lastIndex + 1) % campaigns.length;
adRef.update({ lastIndex: newIndex });
const adToServe = campaigns[newIndex];
adRef.child(`campaigns/${newIndex}/viewCount`).transaction(function (views) {
return (views || 0) + 1;
});
return adToServe;
});