-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
index.js
205 lines (174 loc) · 4.77 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
//console.log(`Environment : ${process.env.NODE_ENV}`);
const express = require("express");
const app = express();
app.disable("x-powered-by");
const port = 3000;
const cors = require("cors");
const { upload } = require("./s3");
const { preference, post, feedback } = require("./db/models");
const { Op } = require("sequelize");
const { registerAnonymousUser, resetUser } = require("./controller-auth");
const { sendEmail } = require("./email");
const {
sendArchiveEmail,
sendAskFriendsForHelpEmail,
} = require("./controller-email");
const { authentication } = require("./middlewares");
const { encrypt, decrypt } = require("./encryption");
const { classify } = require("./service-classifier");
const { text } = require("express");
app.use(cors());
app.use(express.json());
app.options("*", cors());
app.use(authentication);
console.log(`Environment : ${process.env.NODE_ENV}`);
app.get("/auth/register", async (req, res) => {
try {
const newUser = await registerAnonymousUser();
res.send({ user: newUser });
} catch (err) {
res.status(501).send();
}
});
app.post("/preference/", async (req, res) => {
console.log("POST preferences");
const { id, language, slurList, email } = req.body;
const user = req.user;
const encryptedEmail = email ? encrypt(email) : null;
const result = await preference.upsert({
id,
userId: user.id,
email: encryptedEmail,
language,
slurList,
});
let plainResult = result[0].get({ plain: true });
console.log({ plainResult });
res.send({
...plainResult,
email,
});
// res.send({ ...result[0].get({ plain: true }) });
});
// Feedback code from here
app.post("/feedback", async (req, res) => {
console.log("POST feedback");
try {
const data = req.body;
await feedback.create({
userId: data.user_id,
tweetText: data.tweet_text,
sentiment: data.tweet_sentiment,
confidence: data.tweet_confidence
});
res.send({ msg: "Feedback Sent" });
}
catch (err) {
//console.log(err);
res.status(501).send({ msg: "Error sending feedback" });
}
});
// Feedback code ends here
app.get("/preference/", async (req, res) => {
const user = req.user;
const result = await preference.findOne({
where: {
userId: user.id,
},
});
if (result == null) {
res.status(404).send();
} else {
let plainResult = result.get({ plain: true });
// console.log({ plainResult });
res.send({
...plainResult,
email: decrypt(plainResult.email),
});
}
});
app.get("/post", async (req, res) => {
const user = req.user;
const result = post.findAll({
where: {
userId: user.id,
},
limit: 20,
});
res.send({ posts: result.map((res) => res.get({ plain: true })) });
});
app.post("/archive", upload.single("screenshot"), async (req, res) => {
console.log("archive POST request");
try {
const fileName = req.file.key;
const s3URL = req.file.location;
const { url } = req.body;
const user = req.user;
await post.create({
userId: user.id,
sourceUrl: url,
permanentUrl: null,
tags: null,
screenshot: fileName,
});
const result = await preference.findOne({
where: {
userId: user.id,
},
});
resultPlain = result.get({ plain: true });
resultPlain = { ...resultPlain, email: decrypt(resultPlain.email) };
console.log({ resultPlain });
if (
(result != null && resultPlain.email != undefined) ||
resultPlain.email != null
) {
await sendArchiveEmail(
resultPlain.email,
url,
`https://uli-media.tattle.co.in/${fileName}`
);
}
res.send({ msg: "Tweet Archived" });
} catch (err) {
res.status(501).send({ msg: "Error archiving tweet" });
}
});
app.get("/archive", async (req, res) => {
const user = req.user;
const { rows, count } = await post.findAndCountAll({
where: {
userId: user.id,
},
limit: 20,
});
const archive = rows.map((row) => row.get({ plain: true }));
res.send({ archive, count });
});
app.post("/predict", async (req, res) => {
const { text } = req.body;
try {
const label = await classify(text);
res.send(label);
} catch (err) {
console.log("Error : could not classify tweet");
console.log(err);
res.status(501).send("Could not label this tweet");
}
});
app.post("/reset", async (req, res) => {
const { user } = req;
const userId = user.id;
console.log(`reset request for ${userId}`);
try {
await resetUser(userId);
res.send({ message: "Account reset" });
} catch (err) {
console.log(`Error : unable to reset account`);
console.log(err);
res.status(501).send("Could not reset account");
}
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});