Skip to content

Node.js based forum software built for the modern web

feat: `POST /chats/:roomId/:mid` and `DELETE /chats/:roomId/:mid`

Julian Lamd5fd098ecf76
diff --git a/public/openapi/components/schemas/Chats.yaml b/public/openapi/components/schemas/Chats.yaml
index 5d3a8865f7..85de6dd23c 100644
--- a/public/openapi/components/schemas/Chats.yaml
+++ b/public/openapi/components/schemas/Chats.yaml
@@ -52,8 +52,16 @@ MessageObject:
example: dragon-fruit
picture:
type: string
+ nullable: true
description: A URL pointing to a picture to be used as the user's avatar
example: 'https://images.unsplash.com/photo-1560070094-e1f2ddec4337?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=256&h=256&q=80'
+ status:
+ type: string
+ enum:
+ - online
+ - offline
+ - dnd
+ - away
banned:
type: boolean
description: Whether a user is banned or not
@@ -80,6 +88,12 @@ MessageObject:
example: Not Banned
deleted:
type: boolean
+ self:
+ type: number
+ newSet:
+ type: boolean
+ cleanedContent:
+ type: string
RoomObjectFull:
# Messaging.loadRoom
allOf:
diff --git a/public/openapi/write.yaml b/public/openapi/write.yaml
index 9d7319bd80..d7a7a55d55 100644
--- a/public/openapi/write.yaml
+++ b/public/openapi/write.yaml
@@ -140,6 +140,8 @@ paths:
$ref: 'write/chats.yaml'
/chats/{roomId}:
$ref: 'write/chats/roomId.yaml'
+ /chats/{roomId}/{mid}:
+ $ref: 'write/chats/roomId/mid.yaml'
/flags/:
$ref: 'write/flags.yaml'
/flags/{flagId}:
diff --git a/public/openapi/write/chats/roomId/mid.yaml b/public/openapi/write/chats/roomId/mid.yaml
new file mode 100644
index 0000000000..769d650c48
--- /dev/null
+++ b/public/openapi/write/chats/roomId/mid.yaml
@@ -0,0 +1,141 @@
+get:
+ tags:
+ - chats
+ summary: get a chat message
+ description: This operation retrieves a single chat room message, by its id
+ parameters:
+ - in: path
+ name: roomId
+ schema:
+ type: number
+ required: true
+ description: a valid chat room id
+ example: 1
+ - in: path
+ name: mid
+ schema:
+ type: number
+ required: true
+ description: a valid message id
+ example: 1
+ responses:
+ '200':
+ description: Message successfully retrieved
+ content:
+ application/json:
+ schema:
+ type: object
+ properties:
+ status:
+ $ref: ../../../components/schemas/Status.yaml#/Status
+ response:
+ $ref: ../../../components/schemas/Chats.yaml#/MessageObject
+put:
+ tags:
+ - chats
+ summary: edit a chat message
+ description: This operation edits a chat message.
+ parameters:
+ - in: path
+ name: roomId
+ schema:
+ type: number
+ required: true
+ description: a valid chat room id
+ example: 1
+ - in: path
+ name: mid
+ schema:
+ type: number
+ required: true
+ description: a valid message id
+ example: 5
+ requestBody:
+ required: true
+ content:
+ application/json:
+ schema:
+ type: object
+ properties:
+ message:
+ type: string
+ description: message content
+ example: 'edited message'
+ responses:
+ '200':
+ description: Message successfully edited
+ content:
+ application/json:
+ schema:
+ type: object
+ properties:
+ status:
+ $ref: ../../../components/schemas/Status.yaml#/Status
+ response:
+ $ref: ../../../components/schemas/Chats.yaml#/MessageObject
+delete:
+ tags:
+ - chats
+ summary: delete a chat message
+ description: This operation deletes a chat message
+ parameters:
+ - in: path
+ name: roomId
+ schema:
+ type: number
+ required: true
+ description: a valid chat room id
+ example: 1
+ - in: path
+ name: mid
+ schema:
+ type: number
+ required: true
+ description: a valid message id
+ example: 5
+ responses:
+ '200':
+ description: Message successfully deleted
+ content:
+ application/json:
+ schema:
+ type: object
+ properties:
+ status:
+ $ref: ../../../components/schemas/Status.yaml#/Status
+ response:
+ type: object
+ properties: {}
+post:
+ tags:
+ - chats
+ summary: restore a chat message
+ description: This operation restores a delete chat message
+ parameters:
+ - in: path
+ name: roomId
+ schema:
+ type: number
+ required: true
+ description: a valid chat room id
+ example: 1
+ - in: path
+ name: mid
+ schema:
+ type: number
+ required: true
+ description: a valid message id
+ example: 5
+ responses:
+ '200':
+ description: message successfully restored
+ content:
+ application/json:
+ schema:
+ type: object
+ properties:
+ status:
+ $ref: ../../../components/schemas/Status.yaml#/Status
+ response:
+ type: object
+ properties: {}
\ No newline at end of file
diff --git a/public/src/client/chats/messages.js b/public/src/client/chats/messages.js
index 7850a30568..191805b18c 100644
--- a/public/src/client/chats/messages.js
+++ b/public/src/client/chats/messages.js
@@ -190,31 +190,17 @@ define('forum/chats/messages', [
return;
}
- socket.emit('modules.chats.delete', {
- messageId: messageId,
- roomId: roomId,
- }, function (err) {
- if (err) {
- return alerts.error(err);
- }
-
+ api.delete(`/chats/${roomId}/${messageId}`, {}).then(() => {
components.get('chat/message', messageId).toggleClass('deleted', true);
- });
+ }).catch(alerts.error);
});
});
};
messages.restore = function (messageId, roomId) {
- socket.emit('modules.chats.restore', {
- messageId: messageId,
- roomId: roomId,
- }, function (err) {
- if (err) {
- return alerts.error(err);
- }
-
+ api.post(`/chats/${roomId}/${messageId}`, {}).then(() => {
components.get('chat/message', messageId).toggleClass('deleted', false);
- });
+ }).catch(alerts.error);
};
return messages;
diff --git a/src/controllers/write/chats.js b/src/controllers/write/chats.js
index a4733f4045..076fd9be3a 100644
--- a/src/controllers/write/chats.js
+++ b/src/controllers/write/chats.js
@@ -80,5 +80,15 @@ Chats.messages.edit = async (req, res) => {
};
Chats.messages.delete = async (req, res) => {
- // ...
+ await messaging.canDelete(req.params.mid, req.uid);
+ await messaging.deleteMessage(req.params.mid, req.uid);
+
+ helpers.formatApiResponse(200, res);
+};
+
+Chats.messages.restore = async (req, res) => {
+ await messaging.canDelete(req.params.mid, req.uid);
+ await messaging.restoreMessage(req.params.mid, req.uid);
+
+ helpers.formatApiResponse(200, res);
};
diff --git a/src/routes/write/chats.js b/src/routes/write/chats.js
index 3ec6d5c3c7..597a7829bf 100644
--- a/src/routes/write/chats.js
+++ b/src/routes/write/chats.js
@@ -25,7 +25,8 @@ module.exports = function () {
setupApiRoute(router, 'get', '/:roomId/:mid', [...middlewares, middleware.assert.room, middleware.assert.message], controllers.write.chats.messages.get);
setupApiRoute(router, 'put', '/:roomId/:mid', [...middlewares, middleware.assert.room, middleware.assert.message], controllers.write.chats.messages.edit);
- // setupApiRoute(router, 'delete', '/:roomId/:mid', [...middlewares, middleware.assert.room], controllers.write.chats.messages.delete);
+ setupApiRoute(router, 'post', '/:roomId/:mid', [...middlewares, middleware.assert.room, middleware.assert.message], controllers.write.chats.messages.restore);
+ setupApiRoute(router, 'delete', '/:roomId/:mid', [...middlewares, middleware.assert.room, middleware.assert.message], controllers.write.chats.messages.delete);
return router;
};
diff --git a/src/socket.io/modules.js b/src/socket.io/modules.js
index 709ccbdfc1..5d62ce566f 100644
--- a/src/socket.io/modules.js
+++ b/src/socket.io/modules.js
@@ -156,6 +156,8 @@ SocketModules.chats.edit = async function (socket, data) {
};
SocketModules.chats.delete = async function (socket, data) {
+ sockets.warnDeprecated(socket, 'DELETE /api/v3/chats/:roomId/:mid');
+
if (!data || !data.roomId || !data.messageId) {
throw new Error('[[error:invalid-data]]');
}
@@ -164,6 +166,8 @@ SocketModules.chats.delete = async function (socket, data) {
};
SocketModules.chats.restore = async function (socket, data) {
+ sockets.warnDeprecated(socket, 'POST /api/v3/chats/:roomId/:mid');
+
if (!data || !data.roomId || !data.messageId) {
throw new Error('[[error:invalid-data]]');
}