feat: `PUT /api/v3/chats/:roomId`

isekai-main
Julian Lam 3 years ago
parent eeffb9d978
commit 9a4fd5dc7e

@ -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 Banned
deleted:
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

@ -39,61 +39,7 @@ get:
status:
$ref: ../../components/schemas/Status.yaml#/Status
response:
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#/RoomObjectFull
post:
tags:
- chats
@ -143,4 +89,40 @@ post:
cleanedContent:
type: string
mid:
type: number
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

@ -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);
}
};

@ -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,
});
};

@ -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) => {
// ...
};

@ -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);

@ -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) {

Loading…
Cancel
Save