Skip to content

Commit

Permalink
refactor : Refactor API endpoints into individual files. fixes #559
Browse files Browse the repository at this point in the history
  • Loading branch information
hardik-pratap-singh authored May 15, 2024
1 parent 0dceb96 commit a84df6e
Show file tree
Hide file tree
Showing 19 changed files with 591 additions and 385 deletions.
73 changes: 73 additions & 0 deletions browser-extension/api-server/controllers/archive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//console.log(`Environment : ${process.env.NODE_ENV}`);
const {
preference,
post
} = require("../db/models");


const {
sendArchiveEmail,
} = require("../controller-email");
const { encrypt, decrypt } = require("../encryption");


const archiveTweet = 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" });
}
};

const getArchive = 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 });
};



module.exports = { getArchive , archiveTweet } ;
19 changes: 19 additions & 0 deletions browser-extension/api-server/controllers/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//console.log(`Environment : ${process.env.NODE_ENV}`);


const { registerAnonymousUser } = require("../controller-auth");


const register = async (req, res) => {
try {
const newUser = await registerAnonymousUser();
res.send({ user: newUser });
} catch (err) {
console.log(err);
res.status(501).send();
}
};



module.exports = {register} ;
29 changes: 29 additions & 0 deletions browser-extension/api-server/controllers/feedback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//console.log(`Environment : ${process.env.NODE_ENV}`);

const {
feedback
} = require("../db/models");


const addFeedback = 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" });
}
};



module.exports = {addFeedback} ;
19 changes: 19 additions & 0 deletions browser-extension/api-server/controllers/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//console.log(`Environment : ${process.env.NODE_ENV}`);

const {
post
} = require("../db/models");


const getPost = 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 })) });
};

module.exports = {getPost} ;
20 changes: 20 additions & 0 deletions browser-extension/api-server/controllers/predict.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//console.log(`Environment : ${process.env.NODE_ENV}`);

const { classify } = require("../service-classifier");


const 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");
}
};



module.exports = {predict} ;
57 changes: 57 additions & 0 deletions browser-extension/api-server/controllers/preference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//console.log(`Environment : ${process.env.NODE_ENV}`);

const {
preference
} = require("../db/models");

const { encrypt, decrypt } = require("../encryption");


const addPreference = 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 }) });
};



const getPreference = 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),
});
}
};




module.exports = {addPreference , getPreference} ;
20 changes: 20 additions & 0 deletions browser-extension/api-server/controllers/reset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//console.log(`Environment : ${process.env.NODE_ENV}`);

const { resetUser } = require("../controller-auth");


const resetAccount = 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");
}
};

module.exports = {resetAccount} ;
Loading

0 comments on commit a84df6e

Please sign in to comment.