Skip to content

Commit

Permalink
Merge branch 'slur-crowdsource' of https://github.com/aatmanvaidya/Uli
Browse files Browse the repository at this point in the history
…into slur-crowdsource
  • Loading branch information
aatmanvaidya committed Sep 12, 2023
2 parents e2e3177 + cf2dede commit 5170b76
Show file tree
Hide file tree
Showing 16 changed files with 673 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('slurs', {
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
},
userId: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
references: {
model:'users',
key: 'id',
}
},
label: {
type: Sequelize.STRING,
allowNull: false,
},
labelMeaning: {
type: Sequelize.TEXT,
allowNull: false,
},
appropriated: {
type: Sequelize.BOOLEAN,
allowNull: false,
},
appropriationContext: {
type: Sequelize.STRING,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
},
});
},

down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('slurs');
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('categories', {
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
},
slurId: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
references: {
model: 'slurs',
key: 'id',
},
},
category: {
type: Sequelize.ENUM(['gender', 'religion', 'caste']),
allowNull: false,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
},
});
},

down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('categories');
},
};
36 changes: 36 additions & 0 deletions browser-extension/api-server/db/models/category.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"use strict";
const { Model } = require("sequelize");

module.exports = (sequelize, DataTypes) => {
class category extends Model {
static associate(models) {
// Define associations here if needed
}
}

category.init(
{
id: {
primaryKey: true,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
},
slurId: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
foreignKey: true,
allowNull: false,
},
category : {
type: DataTypes.ENUM(['gender', 'religion', 'caste']),
allowNull: false,
},
},
{
sequelize,
modelName: "category",
}
);

return category;
};
56 changes: 56 additions & 0 deletions browser-extension/api-server/db/models/slur.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"use strict";
const { Model } = require("sequelize");

module.exports = (sequelize, DataTypes) => {
class slur extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// Define associations here if needed
slur.hasMany(models.category, {
foreignKey: "slurId",
as: "categories",
});
}
}

slur.init(
{
id: {
primaryKey: true,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
},
userId:{
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
foreignKey: true,
allowNull: false,
},
label: {
type: DataTypes.STRING,
allowNull: false,
},
labelMeaning: {
type: DataTypes.TEXT,
allowNull: false,
},
appropriated: {
type: DataTypes.BOOLEAN,
allowNull: false,
},
appropriationContext: {
type: DataTypes.STRING,
},
},
{
sequelize,
modelName: "slur",
}
);

return slur;
};
1 change: 1 addition & 0 deletions browser-extension/api-server/db/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = (sequelize, DataTypes) => {
// define association here
user.hasOne(models.preference);
user.hasMany(models.post);
user.hasMany(models.slur);
}
}
user.init(
Expand Down
164 changes: 154 additions & 10 deletions browser-extension/api-server/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
//console.log(`Environment : ${process.env.NODE_ENV}`);

const express = require("express");
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 { preference, post, feedback, slur, category, sequelize } = require("./db/models");


const { registerAnonymousUser, resetUser } = require("./controller-auth");
const { sendEmail } = require("./email");
const {
Expand Down Expand Up @@ -61,23 +62,22 @@ app.post("/preference/", async (req, res) => {


// 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" });
Expand All @@ -86,7 +86,6 @@ app.post("/feedback", async (req, res) => {


// Feedback code ends here

app.get("/preference/", async (req, res) => {
const user = req.user;
const result = await preference.findOne({
Expand Down Expand Up @@ -200,6 +199,151 @@ app.post("/reset", async (req, res) => {
}
});

// GET request for Slur and Category
app.get("/slur", async (req, res) => {
const user = req.user;
try {
const results = await slur.findAll({
where: {
userId: user.id,
},
include: [
{
model: category,
as: "categories",
},
],
});

if (results) {
res.status(500).send({ error: "error finding slurs" });
} else {
res.json(results);
}
} catch (error) {
console.error(error);
res.status(500).send({ error: "server error" });
}
});

// POST request for slur and category
app.post("/slur/create", async (req, res) => {
const { userId, label, labelMeaning, appropriated, appropriationContext, categories } = req.body;
// const t = await Op.transaction();
const t = await sequelize.transaction()

try {
const newSlur = await slur.create(
{
userId,
label,
labelMeaning,
appropriated,
appropriationContext,
},
{ transaction: t }
);

const categoryPromises = categories.map(async (categoryData) => {
const newCategory = await category.create(
{
slurId: newSlur.id,
category: categoryData.category,
},
{ transaction: t }
);
return newCategory;
});

// https://stackoverflow.com/questions/48376479/executing-multiple-sequelize-js-model-query-methods-with-promises-node
// https://stackoverflow.com/questions/28897708/sequelize-save-in-multiple-tables
const createdCategories = await Promise.all(categoryPromises);

await t.commit();

res.send({
slur: newSlur,
categories: createdCategories,
});
} catch (error) {
// await t.rollback();
console.error(error);
res.status(500).send({ error: "server error" });
}
});

// PUT request for slur and category
// https://sequelize.org/docs/v6/core-concepts/model-querying-finders/
// https://sequelize.org/docs/v7/querying/update/
app.put("/slur/:id", async (req, res) => {
const slurId = req.params.id;
const { label, labelMeaning, appropriated, appropriationContext, categories } = req.body;

try {
const existingSlur = await slur.findByPk(slurId);
if (!existingSlur) {
res.status(404).send({ error: "Slur not found" });
return;
}
// Update the slur record
existingSlur.label = label;
existingSlur.labelMeaning = labelMeaning;
existingSlur.appropriated = appropriated;
existingSlur.appropriationContext = appropriationContext;
await existingSlur.save();

// Delete existing categories for this slur
await category.destroy({
where: {
slurId: existingSlur.id,
},
});

// Create new categories
const categoryPromises = categories.map(async (categoryData) => {
const newCategory = await category.create({
slurId: existingSlur.id,
category: categoryData.category,
});
return newCategory;
});

const updatedCategories = await Promise.all(categoryPromises);

res.send({
slur: existingSlur,
categories: updatedCategories,
});
} catch (error) {
console.error(error);
res.status(500).send({ error: "Server error" });
}
});

// DELETE request for slur and category
app.delete("/slur/:id", async (req, res) => {
const slurId = req.params.id;
try {
const slurToDelete = await slur.findByPk(slurId);
if (!slurToDelete) {
res.status(404).send({ error: "Slur not found" });
return;
}

await category.destroy({
where: {
slurId: slurToDelete.id,
},
});
await slurToDelete.destroy();

res.send();
} catch (error) {
console.error(error);
res.status(500).send({ error: "server error" });
}
});

app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
});
Loading

0 comments on commit 5170b76

Please sign in to comment.