-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscr-functions.js
117 lines (109 loc) · 4.4 KB
/
scr-functions.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
exports.makeGetPastebinId = (makeNewPastebin, SERVER_TIMESTAMP)=>((req, res) => {
const pastebinResponse = {
pastebinId: null,
error: null
};
try {
const pastebinRef = makeNewPastebin(() => {
pastebinResponse.pastebinId = pastebinRef.key;
if (!pastebinResponse.isSent) {
res.status(200).send(pastebinResponse);
pastebinResponse.isSent = true;
}
},
SERVER_TIMESTAMP);
} catch (error) {
pastebinResponse.error = '[Server Error]: Internal error.';
if (!pastebinResponse.isSent) {
res.status(500).send(pastebinResponse);
pastebinResponse.isSent = true;
}
console.log(pastebinResponse.error, error);
}
});
exports.makeGetPastebin = (getDatabaseRootRef)=>((req, res) => {
const pastebinResponse = {
pastebinId: req.query.pastebinId,
initialEditorsTexts: {},
error: null
};
try {
if (pastebinResponse.pastebinId) {
const firebasePastebinRef =
getDatabaseRootRef().child(`${pastebinResponse.pastebinId}`);
firebasePastebinRef.once('value').then(snapshot => {
if (snapshot.exists()) {
res.status(200).send(pastebinResponse);
//slows down requests
// sendExistingContent(res, pastebinResponse, firebasePastebinRef);
} else {
pastebinResponse.error = '[Client Error]: Pastebin does not exist.' +
' Custom pastebinIds are not allowed.';
res.status(400).send(pastebinResponse);
console.log(pastebinResponse.error, pastebinResponse);
}
}).catch(error => {
pastebinResponse.error = '[Server Error]: Internal error.';
res.status(500).send(pastebinResponse);
console.log(pastebinResponse.error, error);
});
} else {
pastebinResponse.error = '[Client Error]: PastebinId was not provided.';
res.status(400).send(pastebinResponse);
console.log(pastebinResponse.error, pastebinResponse);
}
} catch (error) {
pastebinResponse.error = '[Server Error]: Internal error.';
res.status(400).send(pastebinResponse);
console.log(pastebinResponse.error, pastebinResponse, error);
}
});
exports.makeCopyPastebin=((copyPastebinById, SERVER_TIMESTAMP)=>(req, res) => {
const pastebinResponse = {
sourcePastebinId: req.query.sourcePastebinId,
copyUsers: req.query.copyUsers,
copyChat: req.query.copyChat,
pastebinId: null,
error: null
};
try {
if (pastebinResponse.sourcePastebinId) {
copyPastebinById(pastebinResponse.sourcePastebinId, res, pastebinResponse, SERVER_TIMESTAMP);
} else {
pastebinResponse.error = '[Client Error]: No Pastebin ID was provided.' +
' Please add pastebinId="a_value" to the URL.';
res.status(400).send(pastebinResponse);
console.log(pastebinResponse.error, pastebinResponse);
}
} catch (error) {
pastebinResponse.error = '[Server Error]: Internal Error.';
res.status(500).send(pastebinResponse);
console.log(pastebinResponse.error, pastebinResponse);
}
});
exports.makeGetPastebinToken = (createCustomToken)=>((req, res) => {
const pastebinResponse = {
pastebinToken: null,
error: null
};
const uid = req.query.pastebinId;
if (uid) {
createCustomToken(uid)
.then(customToken => {
pastebinResponse.pastebinToken = customToken;
res.status(200).send(pastebinResponse);
})
.catch(error => {
pastebinResponse.error = '[Server Error]: Authentication failed ' +
'while accessing pastebin. Please inform admin to get renew ' +
'Firebase service account.';
res.status(500).send(pastebinResponse);
console.log(pastebinResponse.error, error);
});
} else {
pastebinResponse.error = '[Client Error]: No Pastebin ID was provided. ' +
'Please add pastebinId="a_value" to the URL';
res.status(400).send(pastebinResponse);
console.log(pastebinResponse.error, error);
}
});