Node.js based forum software built for the modern web
feat: `PUT /api/v3/chats/:roomId`
Julian Lam9a4fd5dc7ec0
diff --git a/public/openapi/components/schemas/Chats.yaml b/public/openapi/components/schemas/Chats.yamlindex 7dc9ac320e..5d3a8865f7 100644--- a/public/openapi/components/schemas/Chats.yaml+++ b/public/openapi/components/schemas/Chats.yaml@@ -79,4 +79,61 @@ MessageObject:description: An ISO 8601 formatted date string representing the moment a ban will be lifted, or the words "Not Banned"example: Not Banneddeleted:+ type: boolean+RoomObjectFull:+ # Messaging.loadRoom+ allOf:+ - $ref: '#/RoomObject'+ - $ref: '#/MessageObject'+ - type: object+ properties:+ isOwner:+ type: boolean+ users:+ type: array+ items:+ type: object+ properties:+ uid:+ type: number+ description: A user identifier+ username:+ type: string+ description: A friendly name for a given user account+ picture:+ nullable: true+ type: string+ status:+ type: string+ displayname:+ type: string+ description: This is either username or fullname depending on forum and user settings+ icon:text:+ type: string+ description: A single-letter representation of a username. This is used in the+ auto-generated icon given to users+ without an avatar+ icon:bgColor:+ type: string+ description: A six-character hexadecimal colour code assigned to the user. This+ value is used in conjunction with+ `icon:text` for the user's+ auto-generated icon+ example: "#f44336"+ isOwner:+ type: boolean+ canReply:+ type: boolean+ groupChat:+ type: boolean+ usernames:+ type: string+ description: User-friendly depiction of the users within the chat room+ maximumUsersInChatRoom:+ type: number+ maximumChatMessageLength:+ type: number+ showUserInput:+ type: boolean+ isAdminOrGlobalMod:type: boolean\ No newline at end of filediff --git a/public/openapi/write/chats/roomId.yaml b/public/openapi/write/chats/roomId.yamlindex 143eaf2b6e..3473c92e1b 100644--- a/public/openapi/write/chats/roomId.yaml+++ b/public/openapi/write/chats/roomId.yaml@@ -39,61 +39,7 @@ get:status:$ref: ../../components/schemas/Status.yaml#/Statusresponse:- allOf:- - $ref: ../../components/schemas/Chats.yaml#/RoomObject- - $ref: ../../components/schemas/Chats.yaml#/MessageObject- - type: object- properties:- isOwner:- type: boolean- users:- type: array- items:- type: object- properties:- uid:- type: number- description: A user identifier- username:- type: string- description: A friendly name for a given user account- picture:- nullable: true- type: string- status:- type: string- displayname:- type: string- description: This is either username or fullname depending on forum and user settings- icon:text:- type: string- description: A single-letter representation of a username. This is used in the- auto-generated icon given to users- without an avatar- icon:bgColor:- type: string- description: A six-character hexadecimal colour code assigned to the user. This- value is used in conjunction with- `icon:text` for the user's- auto-generated icon- example: "#f44336"- isOwner:- type: boolean- canReply:- type: boolean- groupChat:- type: boolean- usernames:- type: string- description: User-friendly depiction of the users within the chat room- maximumUsersInChatRoom:- type: number- maximumChatMessageLength:- type: number- showUserInput:- type: boolean- isAdminOrGlobalMod:- type: boolean+ $ref: ../../components/schemas/Chats.yaml#/RoomObjectFullpost:tags:- chats@@ -143,4 +89,40 @@ post:cleanedContent:type: stringmid:- type: number\ No newline at end of file+ type: number+put:+ tags:+ - chats+ summary: rename a chat room+ description: This operation renames a chat room.+ parameters:+ - in: path+ name: roomId+ schema:+ type: number+ required: true+ description: a valid room id+ example: 1+ requestBody:+ required: true+ content:+ application/json:+ schema:+ type: object+ properties:+ name:+ type: string+ description: the new name of the room+ example: 'casper the friendly room'+ responses:+ '200':+ description: Chat room renamed+ content:+ application/json:+ schema:+ type: object+ properties:+ status:+ $ref: ../../components/schemas/Status.yaml#/Status+ response:+ $ref: ../../components/schemas/Chats.yaml#/RoomObjectFull\ No newline at end of filediff --git a/public/src/client/chats.js b/public/src/client/chats.jsindex b3f7445942..1a6f1d638f 100644--- a/public/src/client/chats.js+++ b/public/src/client/chats.js@@ -13,10 +13,12 @@ define('forum/chats', ['bootbox','alerts','chat',+ 'api',], function (components, translator, mousetrap,recentChats, search, messages,- autocomplete, hooks, bootbox, alerts, chatModule+ autocomplete, hooks, bootbox, alerts, chatModule,+ api) {const Chats = {initialised: false,@@ -345,14 +347,9 @@ define('forum/chats', [});function submit() {- socket.emit('modules.chats.renameRoom', {- roomId: roomId,- newName: modal.find('#roomName').val(),- }, function (err) {- if (err) {- alerts.error(err);- }- });+ api.put(`/chats/${roomId}`, {+ name: modal.find('#roomName').val(),+ }).catch(alerts.error);}};diff --git a/src/api/chats.js b/src/api/chats.jsindex edcd86bee7..c71f272b1a 100644--- a/src/api/chats.js+++ b/src/api/chats.js@@ -1,12 +1,14 @@'use strict';+const validator = require('validator');+const user = require('../user');const meta = require('../meta');const messaging = require('../messaging');const plugins = require('../plugins');// const websockets = require('../socket.io');-// const socketHelpers = require('../socket.io/helpers');+const socketHelpers = require('../socket.io/helpers');const chatsAPI = module.exports;@@ -59,3 +61,14 @@ chatsAPI.post = async (caller, data) => {return message;};++chatsAPI.rename = async (caller, data) => {+ await messaging.renameRoom(caller.uid, data.roomId, data.name);+ const uids = await messaging.getUidsInRoom(data.roomId, 0, -1);+ const eventData = { roomId: data.roomId, newName: validator.escape(String(data.name)) };++ socketHelpers.emitToUids('event:chats.roomRename', eventData, uids);+ return messaging.loadRoom(caller.uid, {+ roomId: data.roomId,+ });+};diff --git a/src/controllers/write/chats.js b/src/controllers/write/chats.jsindex 0dccc7ecdd..c716b1de1d 100644--- a/src/controllers/write/chats.js+++ b/src/controllers/write/chats.js@@ -44,6 +44,15 @@ Chats.post = async (req, res) => {helpers.formatApiResponse(200, res, messageObj);};+Chats.rename = async (req, res) => {+ const roomObj = await api.chats.rename(req, {+ ...req.body,+ roomId: req.params.roomId,+ });++ helpers.formatApiResponse(200, res, roomObj);+};+Chats.users = async (req, res) => {// ...};diff --git a/src/routes/write/chats.js b/src/routes/write/chats.jsindex bcd9195898..9dbed92e50 100644--- a/src/routes/write/chats.js+++ b/src/routes/write/chats.js@@ -16,7 +16,8 @@ module.exports = function () {setupApiRoute(router, 'head', '/:roomId', [...middlewares, middleware.assert.room], controllers.write.chats.exists);setupApiRoute(router, 'get', '/:roomId', [...middlewares, middleware.assert.room], controllers.write.chats.get);setupApiRoute(router, 'post', '/:roomId', [...middlewares, middleware.assert.room, middleware.checkRequired.bind(null, ['message'])], controllers.write.chats.post);- // // no route for room deletion, reserved just in case...+ setupApiRoute(router, 'put', '/:roomId', [...middlewares, middleware.assert.room, middleware.checkRequired.bind(null, ['name'])], controllers.write.chats.rename);+ // no route for room deletion, noted here just in case...// setupApiRoute(router, 'get', '/:roomId/users', [...middlewares, middleware.assert.room], controllers.write.chats.users);// setupApiRoute(router, 'put', '/:roomId/users', [...middlewares, middleware.assert.room, middleware.checkRequired.bind(null, ['uids'])], controllers.write.chats.invite);diff --git a/src/socket.io/modules.js b/src/socket.io/modules.jsindex a5129904a2..144889c9e7 100644--- a/src/socket.io/modules.js+++ b/src/socket.io/modules.js@@ -203,15 +203,15 @@ SocketModules.chats.markAllRead = async function (socket) {};SocketModules.chats.renameRoom = async function (socket, data) {+ sockets.warnDeprecated(socket, 'PUT /api/v3/chats/:roomId');+if (!data || !data.roomId || !data.newName) {throw new Error('[[error:invalid-data]]');}- await Messaging.renameRoom(socket.uid, data.roomId, data.newName);- const uids = await Messaging.getUidsInRoom(data.roomId, 0, -1);- const eventData = { roomId: data.roomId, newName: validator.escape(String(data.newName)) };- uids.forEach((uid) => {- server.in(`uid_${uid}`).emit('event:chats.roomRename', eventData);- });++ data.name = data.newName;+ delete data.newName;+ await api.chats.rename(socket, data);};SocketModules.chats.getRecentChats = async function (socket, data) {