diff --git a/backend/lib/errors.js b/backend/lib/errors.js index b18d78d..3b61afc 100644 --- a/backend/lib/errors.js +++ b/backend/lib/errors.js @@ -38,7 +38,8 @@ let Errors = { "Registrations are currently disabled on this instance. Please try again later.", 400 ], - banned: ["You are banned from this instance.", 400] + banned: ["You are banned from this instance.", 400], + leavingDirectChat: ["You cannot leave a direct message.", 400] } function processErrors(errorName) { diff --git a/backend/routes/associations.js b/backend/routes/associations.js index efdd2fe..06bafb4 100644 --- a/backend/routes/associations.js +++ b/backend/routes/associations.js @@ -2,153 +2,155 @@ const express = require("express") const router = express.Router() const Errors = require("../lib/errors.js") const auth = require("../lib/authorize.js") -const { User, Message, ChatAssociation, Chat, Attachment, Friend} = require("../models") +const { + User, + Message, + ChatAssociation, + Chat, + Attachment, + Friend +} = require("../models") -router.delete( - "/association/:id/:associationId", - auth, - async (req, res, next) => { - try { - const io = req.app.get("io") - const chat = await ChatAssociation.findOne({ - where: { - id: req.params.id, - userId: req.user.id, - rank: "admin" - }, - include: [ - { - model: Chat, - as: "chat", - include: [ - { - model: User, - as: "users", - attributes: ["id", "username", "createdAt", "updatedAt"] - } - ] - } - ] - }) - const association = await ChatAssociation.findOne({ - where: { - id: req.params.associationId, - chatId: chat.chat.id - }, - include: [ - { - model: User, - as: "user", - attributes: ["id", "username", "createdAt", "updatedAt"] - } - ] - }) - if (!chat) { - throw Errors.chatNotFoundOrNotAdmin - } - if (!association) { - throw Errors.chatNotFoundOrNotAdmin - } - if(association.chat) - await association.destroy() - res.sendStatus(204) - const message = await Message.create({ - userId: 0, - chatId: chat.chat.id, - content: `${association.user.username} has been removed by ${req.user.username}.`, - type: "leave" - }) - const associations = await ChatAssociation.findAll({ - where: { - chatId: chat.chat.id - }, - include: [ - { - model: User, - as: "user", - attributes: [ - "username", - "name", - "avatar", - "id", - "createdAt", - "updatedAt" - ] - } - ] - }) - const messageLookup = await Message.findOne({ - where: { - id: message.id - }, - include: [ - { - model: Attachment, - as: "attachments" - }, - { - model: Message, - as: "reply", - include: [ - { - model: User, - as: "user", - attributes: [ - "username", - "name", - "avatar", - "id", - "createdAt", - "updatedAt" - ] - } - ] - }, - { - model: Chat, - as: "chat", - include: [ - { - model: User, - as: "users", - attributes: [ - "username", - "name", - "avatar", - "id", - "createdAt", - "updatedAt" - ] - } - ] - }, - { - model: User, - as: "user", - attributes: [ - "username", - "name", - "avatar", - "id", - "createdAt", - "updatedAt" - ] - } - ] - }) - associations.forEach((association) => { - io.to(association.userId).emit("message", { - ...messageLookup.dataValues, - associationId: association.id, - keyId: `${message.id}-${message.updatedAt.toISOString()}` - }) - }) - } catch (err) { - next(err) +router.delete("/:id/:associationId", auth, async (req, res, next) => { + try { + const io = req.app.get("io") + const chat = await ChatAssociation.findOne({ + where: { + id: req.params.id, + userId: req.user.id, + rank: "admin" + }, + include: [ + { + model: Chat, + as: "chat", + include: [ + { + model: User, + as: "users", + attributes: ["id", "username", "createdAt", "updatedAt"] + } + ] + } + ] + }) + const association = await ChatAssociation.findOne({ + where: { + id: req.params.associationId, + chatId: chat.chat.id + }, + include: [ + { + model: User, + as: "user", + attributes: ["id", "username", "createdAt", "updatedAt"] + } + ] + }) + if (!chat) { + throw Errors.chatNotFoundOrNotAdmin } + if (!association) { + throw Errors.chatNotFoundOrNotAdmin + } + if (association.chat) await association.destroy() + res.sendStatus(204) + const message = await Message.create({ + userId: 0, + chatId: chat.chat.id, + content: `${association.user.username} has been removed by ${req.user.username}.`, + type: "leave" + }) + const associations = await ChatAssociation.findAll({ + where: { + chatId: chat.chat.id + }, + include: [ + { + model: User, + as: "user", + attributes: [ + "username", + "name", + "avatar", + "id", + "createdAt", + "updatedAt" + ] + } + ] + }) + const messageLookup = await Message.findOne({ + where: { + id: message.id + }, + include: [ + { + model: Attachment, + as: "attachments" + }, + { + model: Message, + as: "reply", + include: [ + { + model: User, + as: "user", + attributes: [ + "username", + "name", + "avatar", + "id", + "createdAt", + "updatedAt" + ] + } + ] + }, + { + model: Chat, + as: "chat", + include: [ + { + model: User, + as: "users", + attributes: [ + "username", + "name", + "avatar", + "id", + "createdAt", + "updatedAt" + ] + } + ] + }, + { + model: User, + as: "user", + attributes: [ + "username", + "name", + "avatar", + "id", + "createdAt", + "updatedAt" + ] + } + ] + }) + associations.forEach((association) => { + io.to(association.userId).emit("message", { + ...messageLookup.dataValues, + associationId: association.id, + keyId: `${message.id}-${message.updatedAt.toISOString()}` + }) + }) + } catch (err) { + next(err) } -) -router.put("/association/:id/:associationId", auth, async (req, res, next) => { +}) +router.put("/:id/:associationId", auth, async (req, res, next) => { try { const chat = await ChatAssociation.findOne({ where: { @@ -194,7 +196,7 @@ router.put("/association/:id/:associationId", auth, async (req, res, next) => { } }) -router.post("/association/:id", auth, async (req, res, next) => { +router.post("/:id", auth, async (req, res, next) => { try { const io = req.app.get("io") const chat = await ChatAssociation.findOne({ @@ -400,16 +402,25 @@ router.post("/association/:id", auth, async (req, res, next) => { } }) -router.delete("/association/:id", auth, async (req, res, next) => { +router.delete("/:id", auth, async (req, res, next) => { try { const io = req.app.get("io") const chat = await ChatAssociation.findOne({ where: { userId: req.user.id, id: req.params.id - } + }, + include: [ + { + model: Chat, + as: "chat" + } + ] }) if (chat) { + if (chat.chat.type === "direct") { + throw Errors.leavingDirectChat + } await chat.destroy() res.sendStatus(204) const message = await Message.create({ diff --git a/frontend/package.json b/frontend/package.json index bb19687..0ca995b 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -1,6 +1,6 @@ { "name": "colubrina-chat", - "version": "1.0.6", + "version": "1.0.7", "private": true, "author": "Troplo ", "license": "GPL-3.0", diff --git a/frontend/src/components/Header.vue b/frontend/src/components/Header.vue index 9453ffe..b77646e 100644 --- a/frontend/src/components/Header.vue +++ b/frontend/src/components/Header.vue @@ -743,7 +743,7 @@ export default { }, removeUserFromGroup(user) { this.axios - .delete("/api/v1/association/" + this.settings.item.id + "/" + user.id) + .delete("/api/v1/associations/" + this.settings.item.id + "/" + user.id) .then(() => { this.$toast.success("User has been removed from the group.") }) @@ -753,7 +753,7 @@ export default { }, giveUserAdmin(user) { this.axios - .put("/api/v1/association/" + this.settings.item.id + "/" + user.id, { + .put("/api/v1/associations/" + this.settings.item.id + "/" + user.id, { rank: "admin" }) .then(() => { @@ -788,7 +788,7 @@ export default { }, addMembersToGroup() { this.axios - .post("/api/v1/association/" + this.settings.item.chat.id, { + .post("/api/v1/associations/" + this.settings.item.chat.id, { users: this.settings.addMembers.users }) .then(() => { @@ -805,7 +805,7 @@ export default { }, leaveGroup() { this.axios - .delete("/api/v1/association/" + this.leave.item.id) + .delete("/api/v1/associations/" + this.leave.item.id) .then(() => { this.leave.dialog = false this.$store.state.chats = this.$store.state.chats.filter(