This commit is contained in:
Troplo 2022-07-30 18:41:18 +10:00
parent ad48cce336
commit ef4345c53f
4 changed files with 165 additions and 153 deletions

View file

@ -38,7 +38,8 @@ let Errors = {
"Registrations are currently disabled on this instance. Please try again later.", "Registrations are currently disabled on this instance. Please try again later.",
400 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) { function processErrors(errorName) {

View file

@ -2,153 +2,155 @@ const express = require("express")
const router = express.Router() const router = express.Router()
const Errors = require("../lib/errors.js") const Errors = require("../lib/errors.js")
const auth = require("../lib/authorize.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( router.delete("/:id/:associationId", auth, async (req, res, next) => {
"/association/:id/:associationId", try {
auth, const io = req.app.get("io")
async (req, res, next) => { const chat = await ChatAssociation.findOne({
try { where: {
const io = req.app.get("io") id: req.params.id,
const chat = await ChatAssociation.findOne({ userId: req.user.id,
where: { rank: "admin"
id: req.params.id, },
userId: req.user.id, include: [
rank: "admin" {
}, model: Chat,
include: [ as: "chat",
{ include: [
model: Chat, {
as: "chat", model: User,
include: [ as: "users",
{ attributes: ["id", "username", "createdAt", "updatedAt"]
model: User, }
as: "users", ]
attributes: ["id", "username", "createdAt", "updatedAt"] }
} ]
] })
} const association = await ChatAssociation.findOne({
] where: {
}) id: req.params.associationId,
const association = await ChatAssociation.findOne({ chatId: chat.chat.id
where: { },
id: req.params.associationId, include: [
chatId: chat.chat.id {
}, model: User,
include: [ as: "user",
{ attributes: ["id", "username", "createdAt", "updatedAt"]
model: User, }
as: "user", ]
attributes: ["id", "username", "createdAt", "updatedAt"] })
} if (!chat) {
] throw Errors.chatNotFoundOrNotAdmin
})
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)
} }
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 { try {
const chat = await ChatAssociation.findOne({ const chat = await ChatAssociation.findOne({
where: { 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 { try {
const io = req.app.get("io") const io = req.app.get("io")
const chat = await ChatAssociation.findOne({ 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 { try {
const io = req.app.get("io") const io = req.app.get("io")
const chat = await ChatAssociation.findOne({ const chat = await ChatAssociation.findOne({
where: { where: {
userId: req.user.id, userId: req.user.id,
id: req.params.id id: req.params.id
} },
include: [
{
model: Chat,
as: "chat"
}
]
}) })
if (chat) { if (chat) {
if (chat.chat.type === "direct") {
throw Errors.leavingDirectChat
}
await chat.destroy() await chat.destroy()
res.sendStatus(204) res.sendStatus(204)
const message = await Message.create({ const message = await Message.create({

View file

@ -1,6 +1,6 @@
{ {
"name": "colubrina-chat", "name": "colubrina-chat",
"version": "1.0.6", "version": "1.0.7",
"private": true, "private": true,
"author": "Troplo <troplo@troplo.com>", "author": "Troplo <troplo@troplo.com>",
"license": "GPL-3.0", "license": "GPL-3.0",

View file

@ -743,7 +743,7 @@ export default {
}, },
removeUserFromGroup(user) { removeUserFromGroup(user) {
this.axios this.axios
.delete("/api/v1/association/" + this.settings.item.id + "/" + user.id) .delete("/api/v1/associations/" + this.settings.item.id + "/" + user.id)
.then(() => { .then(() => {
this.$toast.success("User has been removed from the group.") this.$toast.success("User has been removed from the group.")
}) })
@ -753,7 +753,7 @@ export default {
}, },
giveUserAdmin(user) { giveUserAdmin(user) {
this.axios this.axios
.put("/api/v1/association/" + this.settings.item.id + "/" + user.id, { .put("/api/v1/associations/" + this.settings.item.id + "/" + user.id, {
rank: "admin" rank: "admin"
}) })
.then(() => { .then(() => {
@ -788,7 +788,7 @@ export default {
}, },
addMembersToGroup() { addMembersToGroup() {
this.axios 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 users: this.settings.addMembers.users
}) })
.then(() => { .then(() => {
@ -805,7 +805,7 @@ export default {
}, },
leaveGroup() { leaveGroup() {
this.axios this.axios
.delete("/api/v1/association/" + this.leave.item.id) .delete("/api/v1/associations/" + this.leave.item.id)
.then(() => { .then(() => {
this.leave.dialog = false this.leave.dialog = false
this.$store.state.chats = this.$store.state.chats.filter( this.$store.state.chats = this.$store.state.chats.filter(