Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored code in src/api/chats.js #35

Open
wants to merge 1 commit into
base: f24
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 37 additions & 18 deletions src/api/chats.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,12 @@ chatsAPI.post = async (caller, data) => {
};

chatsAPI.update = async (caller, data) => {
if (!data || !data.roomId) {
throw new Error('[[error:invalid-data]]');
}
validateData(data);

if (data.hasOwnProperty('name')) {
if (!data.name && data.name !== '') {
throw new Error('[[error:invalid-data]]');
}
await messaging.renameRoom(caller.uid, data.roomId, data.name);
await updateRoomName(caller, data);
}

const [roomData, isAdmin] = await Promise.all([
messaging.getRoomData(data.roomId),
user.isAdministrator(caller.uid),
Expand All @@ -155,9 +151,7 @@ chatsAPI.update = async (caller, data) => {
throw new Error('[[error:invalid-data]]');
}
if (data.hasOwnProperty('groups')) {
if (roomData.public && isAdmin) {
await db.setObjectField(`chat:room:${data.roomId}`, 'groups', JSON.stringify(data.groups));
}
await updateGroups(roomData, data, isAdmin);
}
if (data.hasOwnProperty('notificationSetting') && isAdmin) {
await db.setObjectField(`chat:room:${data.roomId}`, 'notificationSetting', data.notificationSetting);
Expand All @@ -166,18 +160,43 @@ chatsAPI.update = async (caller, data) => {
roomId: data.roomId,
});
if (data.hasOwnProperty('name')) {
const ioRoom = require('../socket.io').in(`chat_room_${data.roomId}`);
if (ioRoom) {
ioRoom.emit('event:chats.roomRename', {
roomId: data.roomId,
newName: validator.escape(String(data.name)),
chatWithMessage: loadedRoom.chatWithMessage,
});
}
emitRoomRenameEvent(data, loadedRoom);
}
return loadedRoom;
};

function validateData(data) {
if (!data || !data.roomId) {
throw new Error('[[error:invalid-data]]');
}
}

async function updateRoomName(caller, data) {
if (data.hasOwnProperty('name')) {
if (!data.name && data.name !== '') {
throw new Error('[[error:invalid-data]]');
}
await messaging.renameRoom(caller.uid, data.roomId, data.name);
}
}

async function updateGroups(roomData, data, isAdmin) {
if (roomData.public && isAdmin) {
await db.setObjectField(`chat:room:${data.roomId}`, 'groups', JSON.stringify(data.groups));
}
}

function emitRoomRenameEvent(data, loadedRoom) {
const ioRoom = require('../socket.io').in(`chat_room_${data.roomId}`);
if (ioRoom) {
ioRoom.emit('event:chats.roomRename', {
roomId: data.roomId,
newName: validator.escape(String(data.name)),
chatWithMessage: loadedRoom.chatWithMessage,
});
}
}

chatsAPI.rename = async (caller, data) => {
if (!data || !data.roomId || !data.name) {
throw new Error('[[error:invalid-data]]');
Expand Down