Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions src/Constants/Renames.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const RENAMES = {
ROOM: 'room',
USER: 'user',
}

export default RENAMES;

2 changes: 2 additions & 0 deletions src/Constants/SocketEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const SOCKET_EVENTS = {
VIDEO_DURATION_CHANGED: 'video-duration-changed',
SKIP_VIDEO: 'skip-video',
VIDEO_SKIPPED: 'video-skipped',
CHANGE_NAME: 'change-name',
NAME_CHANGED: 'name-changed',
};

export default SOCKET_EVENTS;
2 changes: 2 additions & 0 deletions src/Constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import SOCKET_EVENTS from './SocketEvents';
import PLAYLIST_STATUS from './PlaylistStatus';
import VIDEO_STATUS from './VideoStatus';
import { USER_TYPES, ACTIONS } from './Room';
import RENAMES from './Renames';

export {
SOCKET_EVENTS,
PLAYLIST_STATUS,
VIDEO_STATUS,
USER_TYPES,
ACTIONS,
RENAMES,
};
12 changes: 12 additions & 0 deletions src/Events/Room/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,21 @@ export const joinRoom = async ({ id, userId, username }, { socket, io }) => {
io.to(id).emit(SOCKET_EVENTS.RECEIVE_MESSAGE, { message });
};

export const changeName = async ({
id, type, name
}, { socket, io }) => {
await RoomService.changeName({id, socketId: socket.id, type, name});

io.to(id).emit(SOCKET_EVENTS.NAME_CHANGED, { name, type });
};

export default [
{
event: SOCKET_EVENTS.JOIN_ROOM,
handler: joinRoom,
},
{
event: SOCKET_EVENTS.CHANGE_NAME,
handler: changeName,
},
];
24 changes: 22 additions & 2 deletions src/Services/Room/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Model from 'metronom';
import axios from 'axios';
import MessageService from '../Message';
import generateId from '../../Utilities/GenerateId';
import { USER_TYPES, VIDEO_STATUS } from '../../Constants';
import { USER_TYPES, VIDEO_STATUS, RENAMES } from '../../Constants';
import { MongoRoomModel, RedisRoomModel, UserModel } from '../../Models';
import CustomError from '../../Exceptions/CustomError';

Expand Down Expand Up @@ -305,6 +304,26 @@ const addSocketId = async (id, socketId) => {
return user;
};

const changeName = async ({id, socketId, type, name}) => {
let willBeRenamed;
switch (type) {
case RENAMES.USER:
willBeRenamed = await findUserWithSocketId(socketId);
willBeRenamed.username = name;
break;
case RENAMES.ROOM:
willBeRenamed = await findMongoRoom(id);
willBeRenamed.name = name;
break;
default:
break;
}

willBeRenamed.save();

return willBeRenamed;
};

const RoomService = {
createRoom,
joinRoom,
Expand All @@ -325,6 +344,7 @@ const RoomService = {
findUserWithId,
findUserWithSocketId,
addSocketId,
changeName,
};

export default RoomService;