fix: deprecate chats.leave

isekai-main
Julian Lam 4 years ago
parent fe17c94c35
commit 16ba16cdc4

@ -65,8 +65,8 @@ post:
delete: delete:
tags: tags:
- chats - chats
summary: remove users from chat room summary: leave/remove users from chat room
description: This operation removes (kicks) a user from a chat room description: This operation removes (kicks) multiple user from a chat room, or leaves the chat room if the requested user is the same as the calling user.
parameters: parameters:
- in: path - in: path
name: roomId name: roomId

@ -1,7 +1,7 @@
delete: delete:
tags: tags:
- chats - chats
summary: remove one user from chat room summary: leave/remove one user from chat room
description: This operation removes (kicks) a single user from a chat room description: This operation removes (kicks) a single user from a chat room
parameters: parameters:
- in: path - in: path

@ -278,11 +278,7 @@ define('forum/chats', [
message: '<p>[[modules:chat.leave-prompt]]</p><p class="help-block">[[modules:chat.leave-help]]</p>', message: '<p>[[modules:chat.leave-prompt]]</p><p class="help-block">[[modules:chat.leave-help]]</p>',
callback: function (ok) { callback: function (ok) {
if (ok) { if (ok) {
socket.emit('modules.chats.leave', roomId, function (err) { api.delete(`/chats/${roomId}/users/${app.user.uid}`, {}).then(() => {
if (err) {
alerts.error(err);
}
// Return user to chats page. If modal, close modal. // Return user to chats page. If modal, close modal.
const modal = buttonEl.parents('.chat-modal'); const modal = buttonEl.parents('.chat-modal');
if (modal.length) { if (modal.length) {
@ -290,7 +286,7 @@ define('forum/chats', [
} else { } else {
ajaxify.go('chats'); ajaxify.go('chats');
} }
}); }).catch(alerts.error);
} }
}, },
}); });
@ -384,10 +380,7 @@ define('forum/chats', [
Chats.leave = function (el) { Chats.leave = function (el) {
const roomId = el.attr('data-roomid'); const roomId = el.attr('data-roomid');
socket.emit('modules.chats.leave', roomId, function (err) { api.delete(`/chats/${roomId}/users/${app.user.uid}`, {}).then(() => {
if (err) {
return alerts.error(err);
}
if (parseInt(roomId, 10) === parseInt(ajaxify.data.roomId, 10)) { if (parseInt(roomId, 10) === parseInt(ajaxify.data.roomId, 10)) {
ajaxify.go('user/' + ajaxify.data.userslug + '/chats'); ajaxify.go('user/' + ajaxify.data.userslug + '/chats');
} else { } else {
@ -398,7 +391,7 @@ define('forum/chats', [
if (modal.length) { if (modal.length) {
chatModule.close(modal); chatModule.close(modal);
} }
}); }).catch(alerts.error);
}; };
Chats.switchChat = function (roomid) { Chats.switchChat = function (roomid) {

@ -108,7 +108,12 @@ chatsAPI.kick = async (caller, data) => {
throw new Error('[[error:no-user]]'); throw new Error('[[error:no-user]]');
} }
// Additional checks if kicking vs leaving
if (data.uids.length === 1 && parseInt(data.uids[0], 10) === caller.uid) {
await messaging.leaveRoom([caller.uid], data.roomId);
} else {
await messaging.removeUsersFromRoom(caller.uid, data.uids, data.roomId); await messaging.removeUsersFromRoom(caller.uid, data.uids, data.roomId);
}
delete data.uids; delete data.uids;
return chatsAPI.users(caller, data); return chatsAPI.users(caller, data);

@ -131,6 +131,8 @@ SocketModules.chats.removeUserFromRoom = async function (socket, data) {
}; };
SocketModules.chats.leave = async function (socket, roomid) { SocketModules.chats.leave = async function (socket, roomid) {
sockets.warnDeprecated(socket, 'DELETE /api/v3/chats/:roomId/users OR DELETE /api/v3/chats/:roomId/users/:uid');
if (!socket.uid || !roomid) { if (!socket.uid || !roomid) {
throw new Error('[[error:invalid-data]]'); throw new Error('[[error:invalid-data]]');
} }

@ -213,29 +213,22 @@ describe('Messaging Library', () => {
assert.strictEqual(body.status.message, await translator.translate('[[error:cant-chat-with-yourself]]')); assert.strictEqual(body.status.message, await translator.translate('[[error:cant-chat-with-yourself]]'));
}); });
it('should fail to leave room with invalid data', (done) => { it('should fail to leave room with invalid data', async () => {
socketModules.chats.leave({ uid: null }, roomId, (err) => { let { statusCode, body } = await callv3API('delete', `/chats/${roomId}/users`, {}, 'foo');
assert.equal(err.message, '[[error:invalid-data]]'); assert.strictEqual(statusCode, 400);
socketModules.chats.leave({ uid: mocks.users.foo.uid }, null, (err) => { assert.strictEqual(body.status.message, await translator.translate('[[error:required-parameters-missing, uids]]'));
assert.equal(err.message, '[[error:invalid-data]]');
done(); ({ statusCode, body } = await callv3API('delete', `/chats/${roomId}/users`, { uids: [98237423] }, 'foo'));
}); assert.strictEqual(statusCode, 400);
}); assert.strictEqual(body.status.message, await translator.translate('[[error:no-user]]'));
}); });
it('should leave the chat room', (done) => { it('should leave the chat room', async () => {
socketModules.chats.leave({ uid: mocks.users.baz.uid }, roomId, (err) => { await callv3API('delete', `/chats/${roomId}/users/${mocks.users.baz.uid}`, {}, 'baz');
assert.ifError(err); const isUserInRoom = await Messaging.isUserInRoom(mocks.users.baz.uid, roomId);
Messaging.isUserInRoom(mocks.users.baz.uid, roomId, (err, isUserInRoom) => {
assert.ifError(err);
assert.equal(isUserInRoom, false); assert.equal(isUserInRoom, false);
Messaging.getRoomData(roomId, (err, data) => { const data = await Messaging.getRoomData(roomId);
assert.ifError(err);
assert.equal(data.owner, mocks.users.foo.uid); assert.equal(data.owner, mocks.users.foo.uid);
done();
});
});
});
}); });
it('should send a user-leave system message when a user leaves the chat room', (done) => { it('should send a user-leave system message when a user leaves the chat room', (done) => {
@ -253,16 +246,21 @@ describe('Messaging Library', () => {
); );
}); });
it('should send not a user-leave system message when a user tries to leave a room they are not in', async () => { it('should not send a user-leave system message when a user tries to leave a room they are not in', async () => {
await socketModules.chats.leave({ uid: mocks.users.baz.uid }, roomId); await callv3API('delete', `/chats/${roomId}/users/${mocks.users.baz.uid}`, {}, 'baz');
const messages = await socketModules.chats.getMessages( const messages = await socketModules.chats.getMessages(
{ uid: mocks.users.foo.uid }, { uid: mocks.users.foo.uid },
{ uid: mocks.users.foo.uid, roomId: roomId, start: 0 } { uid: mocks.users.foo.uid, roomId: roomId, start: 0 }
); );
assert.equal(messages.length, 4); assert.equal(messages.length, 4);
const message = messages.pop(); let message = messages.pop();
assert.strictEqual(message.system, true); assert.strictEqual(message.system, true);
assert.strictEqual(message.content, 'user-leave'); assert.strictEqual(message.content, 'user-leave');
// The message before should still be a user-join
message = messages.pop();
assert.strictEqual(message.system, true);
assert.strictEqual(message.content, 'user-join');
}); });
it('should change owner when owner leaves room', async () => { it('should change owner when owner leaves room', async () => {
@ -605,7 +603,7 @@ describe('Messaging Library', () => {
}); });
after(async () => { after(async () => {
await socketModules.chats.leave({ uid: mocks.users.baz.uid }, roomId); await callv3API('delete', `/chats/${roomId}/users/${mocks.users.baz.uid}`, {}, 'baz');
}); });
it('should fail to edit message with invalid data', async () => { it('should fail to edit message with invalid data', async () => {

Loading…
Cancel
Save