feat: `POST /api/v3/chats`, chat room creation, plus openAPI docs update
parent
94bead71fe
commit
40b4544e70
@ -0,0 +1,14 @@
|
|||||||
|
RoomObject:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
owner:
|
||||||
|
type: number
|
||||||
|
description: the uid of the chat room owner (usually the user who created the room initially)
|
||||||
|
roomId:
|
||||||
|
type: string
|
||||||
|
description: unique identifier for the chat room
|
||||||
|
roomName:
|
||||||
|
type: string
|
||||||
|
groupChat:
|
||||||
|
type: boolean
|
||||||
|
description: whether the chat room is a group chat or not
|
@ -0,0 +1,29 @@
|
|||||||
|
post:
|
||||||
|
tags:
|
||||||
|
- chats
|
||||||
|
summary: create a chat room
|
||||||
|
description: This operation creates a new chat room and adds users to the room, if provided.
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
uids:
|
||||||
|
type: array
|
||||||
|
example: [2, 3]
|
||||||
|
required:
|
||||||
|
- uids
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: chat room successfully created
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
status:
|
||||||
|
$ref: ../components/schemas/Status.yaml#/Status
|
||||||
|
response:
|
||||||
|
$ref: ../components/schemas/Chats.yaml#/RoomObject
|
@ -0,0 +1,37 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const meta = require('../meta');
|
||||||
|
const privileges = require('../privileges');
|
||||||
|
const messaging = require('../messaging');
|
||||||
|
|
||||||
|
|
||||||
|
const websockets = require('../socket.io');
|
||||||
|
const socketHelpers = require('../socket.io/helpers');
|
||||||
|
|
||||||
|
const chatsAPI = module.exports;
|
||||||
|
|
||||||
|
function rateLimitExceeded(caller) {
|
||||||
|
const session = caller.request ? caller.request.session : caller.session; // socket vs req
|
||||||
|
const now = Date.now();
|
||||||
|
session.lastChatMessageTime = session.lastChatMessageTime || 0;
|
||||||
|
if (now - session.lastChatMessageTime < meta.config.chatMessageDelay) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
session.lastChatMessageTime = now;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
chatsAPI.create = async function (caller, data) {
|
||||||
|
if (rateLimitExceeded(caller)) {
|
||||||
|
throw new Error('[[error:too-many-messages]]');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!data.uids || !Array.isArray(data.uids)) {
|
||||||
|
throw new Error(`[[error:array-expected, uids, ${typeof data.uids}]]`);
|
||||||
|
}
|
||||||
|
|
||||||
|
await Promise.all(data.uids.map(async uid => messaging.canMessageUser(caller.uid, uid)));
|
||||||
|
const roomId = await messaging.newRoom(caller.uid, data.uids);
|
||||||
|
|
||||||
|
return await messaging.getRoomData(roomId);
|
||||||
|
};
|
Loading…
Reference in New Issue