Skip to content

Commit 341e248

Browse files
author
juanpms2
committed
Merge branch 'master' into feature/#122-reposition-video
2 parents 3b879a9 + 346eb3d commit 341e248

32 files changed

Lines changed: 419 additions & 365 deletions

‎back/.env‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ API_URL=/api
55
CORS_ORIGIN=http://localhost:8080
66
MONGODB_URI=mongodb://localhost:27017/code-paster
77
STATIC_FILES_PATH="../../front/dist"
8+
MONGO_EXPIRATION_TIME='2d'
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
export const envConstants = {
2-
NODE_ENV: process.env.NODE_ENV,
3-
PORT: process.env.PORT,
4-
isMockRepository: process.env.MOCK_REPOSITORY === 'true',
5-
MONGODB_URI: process.env.MONGODB_URI,
6-
API_URL: process.env.API_URL,
7-
CORS_ORIGIN: process.env.CORS_ORIGIN,
8-
isProduction: process.env.NODE_ENV === 'production',
9-
STATIC_FILES_PATH: process.env.STATIC_FILES_PATH
10-
};
11-
12-
2+
NODE_ENV: process.env.NODE_ENV,
3+
PORT: process.env.PORT,
4+
isMockRepository: process.env.MOCK_REPOSITORY === 'true',
5+
MONGODB_URI: process.env.MONGODB_URI,
6+
API_URL: process.env.API_URL,
7+
CORS_ORIGIN: process.env.CORS_ORIGIN,
8+
isProduction: process.env.NODE_ENV === 'production',
9+
STATIC_FILES_PATH: process.env.STATIC_FILES_PATH,
10+
MONGO_EXPIRATION_TIME: process.env.MONGO_EXPIRATION_TIME,
11+
};
Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
import { RoomInfo, RoomContext } from 'dals'
1+
import { RoomInfo, RoomContext } from 'dals';
22

33
export const isRoomAvailable = async (room: string): Promise<boolean> => {
4-
const roomAvailable = !(await RoomContext.exists({ room }));
5-
return roomAvailable;
6-
}
7-
8-
export const saveRoomInfo = async (newRoomInfo: RoomInfo, setFullText: boolean): Promise<void> => {
9-
await isRoomAvailable(newRoomInfo.room) ? await insertRoom(newRoomInfo) : await updateRoomContent(newRoomInfo, setFullText);
4+
return (await RoomContext.countDocuments({ room: room })) === 0;
5+
};
6+
7+
export const saveRoomInfo = async (
8+
newRoomInfo: RoomInfo,
9+
setFullText: boolean
10+
): Promise<void> => {
11+
(await isRoomAvailable(newRoomInfo.room))
12+
? await insertRoom(newRoomInfo)
13+
: await updateRoomContent(newRoomInfo, setFullText);
1014
};
1115

1216
export const getRoomContent = async (room: string): Promise<string> => {
@@ -15,19 +19,35 @@ export const getRoomContent = async (room: string): Promise<string> => {
1519
};
1620

1721
const getRoomByName = async (room: string): Promise<RoomInfo> => {
18-
return (await RoomContext.findOne({room: room}).lean());
22+
return await RoomContext.findOne({ room: room }).lean();
1923
};
2024

21-
const updateRoomContent = async (newRoomInfo: RoomInfo, setFullText:boolean): Promise<void> => {
25+
const appendRoomContent = async (newRoomInfo: RoomInfo): Promise<void> => {
2226
const room: RoomInfo = await getRoomByName(newRoomInfo.room);
23-
let newContent = "";
24-
if(room && Boolean(room.content) && !setFullText) {
25-
newContent = room.content + '\n';
27+
28+
if (room) {
29+
const newContent = `${room.content}\n${newRoomInfo.content}`;
30+
await RoomContext.updateOne(
31+
{ room: newRoomInfo.room },
32+
{ content: newContent }
33+
);
2634
}
27-
newContent = setFullText ? newRoomInfo.content : (newContent + newRoomInfo.content);
28-
await RoomContext.updateOne({room: newRoomInfo.room}, {content: newContent});
2935
};
30-
36+
37+
const replaceRoomContent = async (newRoomInfo: RoomInfo): Promise<void> =>
38+
await RoomContext.updateOne(
39+
{ room: newRoomInfo.room },
40+
{ content: newRoomInfo.content }
41+
);
42+
43+
const updateRoomContent = async (
44+
newRoomInfo: RoomInfo,
45+
setFullText: boolean
46+
): Promise<void> =>
47+
setFullText
48+
? replaceRoomContent(newRoomInfo)
49+
: appendRoomContent(newRoomInfo);
50+
3151
const insertRoom = async (newRoomInfo: RoomInfo): Promise<void> => {
3252
await RoomContext.create(newRoomInfo);
3353
};

‎back/src/dals/room/room.context.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import { Document, model, Schema } from 'mongoose';
22
import { RoomInfo } from 'dals';
3+
import { envConstants } from 'core';
34

45
const roomSchema = new Schema({
56
room: Schema.Types.String,
6-
content: Schema.Types.String
7+
content: Schema.Types.String,
8+
expireAt: {
9+
type: Schema.Types.Date,
10+
default: Date.now,
11+
index: {
12+
expires: envConstants.MONGO_EXPIRATION_TIME,
13+
},
14+
},
715
});
816

9-
export const RoomContext = model<RoomInfo & Document>(
10-
'RoomInfo',
11-
roomSchema
12-
);
17+
export const RoomContext = model<RoomInfo & Document>('RoomInfo', roomSchema);

‎back/src/dals/session/session.context.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import { Document, model, Schema } from 'mongoose';
22
import { UserSession } from 'dals/session';
3+
import { envConstants } from 'core/constants';
34

45
const userSessionSchema = new Schema({
56
room: Schema.Types.String,
67
trainerToken: Schema.Types.String,
78
isTrainer: Schema.Types.Boolean,
8-
connectionId: Schema.Types.String
9+
connectionId: Schema.Types.String,
10+
expireAt: {
11+
type: Schema.Types.Date,
12+
default: Date.now,
13+
index: {
14+
expires: envConstants.MONGO_EXPIRATION_TIME,
15+
},
16+
},
917
});
1018

1119
export const UserSessionContext = model<UserSession & Document>(

‎back/src/pods/messages/messages.consts.ts‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@ export const InputMessageTypes = {
44
TRAINER_APPEND_TEXT: 'TRAINER_APPEND_TEXT',
55
TRAINER_SET_FULL_TEXT: 'TRAINER_SET_FULL_TEXT',
66
TRAINER_SAVE_CONTENT: 'TRAINER_SAVE_CONTENT',
7-
TRAINER_REQUEST_FULL_CONTENT: 'TRAINER_REQUEST_FULL_CONTENT',
8-
STUDENT_REQUEST_FULL_CONTENT: 'STUDENT_REQUEST_FULL_CONTENT',
97
};
108

119
export const OutputMessageTypes = {
1210
CONNECTION_ESTABLISHED_TRAINER: 'CONNECTION_ESTABLISHED_TRAINER',
1311
CONNECTION_ESTABLISHED_STUDENT: 'CONNECTION_ESTABLISHED_STUDENT',
1412
ERROR_MULTI_TRAINER_NOT_IMPLEMENTED_YET: 'ERROR_MULTI_TRAINER_NOT_IMPLEMENTED_YET',
1513
APPEND_TEXT: 'APPEND_TEXT',
16-
TRAINER_SEND_FULL_CONTENT: 'TRAINER_SEND_FULL_CONTENT',
17-
STUDENT_SEND_FULL_CONTENT: 'STUDENT_SEND_FULL_CONTENT',
1814
UPDATE_FULL_CONTENT: 'UPDATE_FULL_CONTENT',
1915
};
2016

‎back/src/pods/messages/messages.socket-events.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import SocketIOClient, { Socket } from 'socket.io';
22
import { Action, SocketInfo } from './messages.model';
3-
import { processInputMessage, processOutputMessageCollection } from './processors';
3+
import {
4+
processInputMessage,
5+
processOutputMessageCollection,
6+
} from './processors';
47
import { InputMessageTypes } from './messages.consts';
58

69
export const messageSocketEvents = async (
@@ -41,10 +44,10 @@ export const messageSocketEvents = async (
4144
});
4245
}
4346

44-
processOutputMessageCollection(socketInfo, outputMessageCollection);
47+
await processOutputMessageCollection(socketInfo, outputMessageCollection);
4548

4649
socket.on('message', async function (message: any) {
47-
console.log("socket.on 'message':: " + message);
50+
console.log(`socket.on 'message': ${JSON.stringify(message)}`);
4851
if (message && message.type) {
4952
const outputMessageCollection: Action[] = await processInputMessage(
5053
socketInfo,
@@ -54,5 +57,4 @@ export const messageSocketEvents = async (
5457
await processOutputMessageCollection(socketInfo, outputMessageCollection);
5558
}
5659
});
57-
5860
};
Lines changed: 71 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
import { sessionRepository, roomRepository, RoomInfo } from 'dals';
12
import { InputMessageTypes, OutputMessageTypes } from '../messages.consts';
2-
import { Action, InputEstablishConnectionTrainer, SocketInfo } from '../messages.model';
3-
import {sessionRepository, roomRepository } from 'dals';
4-
const {addNewUser, getRoomFromConnectionId, isExistingConnection, isTrainerUser } = sessionRepository;
5-
const { isRoomAvailable, saveRoomInfo} = roomRepository;
3+
import {
4+
Action,
5+
InputEstablishConnectionTrainer,
6+
SocketInfo,
7+
} from '../messages.model';
68

7-
export const processInputMessage = async (socketInfo: SocketInfo, action: Action): Promise<Action[]> => {
9+
export const processInputMessage = async (
10+
socketInfo: SocketInfo,
11+
action: Action
12+
): Promise<Action[]> => {
813
let outputActionCollection: Action[] = [];
914
switch (action.type) {
1015
case InputMessageTypes.ESTABLISH_CONNECTION_TRAINER:
@@ -36,76 +41,91 @@ export const processInputMessage = async (socketInfo: SocketInfo, action: Action
3641
InputMessageTypes.TRAINER_SET_FULL_TEXT
3742
);
3843
break;
39-
case InputMessageTypes.STUDENT_REQUEST_FULL_CONTENT:
40-
outputActionCollection = await handleRequestGetStudentContent(socketInfo);
41-
break;
42-
case InputMessageTypes.TRAINER_REQUEST_FULL_CONTENT:
43-
outputActionCollection = await handleRequestGetTrainerContent(socketInfo);
44-
break;
4544
default:
4645
break;
4746
}
4847

4948
return outputActionCollection;
5049
};
5150

52-
const handleRequestGetStudentContent = async (socketInfo: SocketInfo): Promise<Action[]> => {
53-
return [{ type: OutputMessageTypes.STUDENT_SEND_FULL_CONTENT }];
54-
};
51+
const handleEstablishConnectionTrainer = async (
52+
socketInfo: SocketInfo,
53+
room: string,
54+
trainerToken: string
55+
): Promise<Action[]> => {
56+
if (!trainerToken || !room) {
57+
// Ignore
58+
return [];
59+
}
5560

56-
const handleRequestGetTrainerContent = async (socketInfo: SocketInfo): Promise<Action[]> => {
57-
return [{ type: OutputMessageTypes.TRAINER_SEND_FULL_CONTENT }];
58-
};
61+
const roomAvailable: boolean = await roomRepository.isRoomAvailable(room);
5962

60-
const handleTrainerSendText = async (socketInfo: SocketInfo, text: string, action: string): Promise<Action[]> => {
61-
if (!isTrainerUser(socketInfo.connectionId)) {
62-
return [];
63+
if (roomAvailable) {
64+
roomRepository.saveRoomInfo({ room, content: '' }, false);
6365
}
64-
const room = await getRoomFromConnectionId(socketInfo.connectionId);
65-
const roomInfo = { room, content: text };
66-
67-
switch(action){
68-
case InputMessageTypes.TRAINER_APPEND_TEXT:
69-
saveRoomInfo(roomInfo, false);
70-
return [{ type: OutputMessageTypes.APPEND_TEXT, payload: text }];
71-
case InputMessageTypes.TRAINER_SET_FULL_TEXT:
72-
saveRoomInfo(roomInfo, true);
73-
return [{ type: OutputMessageTypes.UPDATE_FULL_CONTENT}];
74-
default:
75-
return [];
76-
};
66+
67+
if (
68+
roomAvailable ||
69+
!(await sessionRepository.isExistingConnection(socketInfo.connectionId))
70+
) {
71+
await sessionRepository.addNewUser({
72+
connectionId: socketInfo.connectionId,
73+
room,
74+
trainerToken,
75+
isTrainer: !!trainerToken,
76+
});
77+
socketInfo.socket.join(room);
78+
}
79+
return [{ type: OutputMessageTypes.CONNECTION_ESTABLISHED_TRAINER }];
7780
};
7881

79-
const handleEstablishConnectionStudent = async (socketInfo: SocketInfo, room: string): Promise<Action[]> => {
82+
const handleEstablishConnectionStudent = async (
83+
socketInfo: SocketInfo,
84+
room: string
85+
): Promise<Action[]> => {
8086
if (!room) {
8187
// Ignore
8288
return [];
8389
}
8490

85-
const roomAvailable: boolean = await isRoomAvailable(room);
91+
const roomAvailable: boolean = await roomRepository.isRoomAvailable(room);
8692

87-
if (roomAvailable || !(await isExistingConnection(socketInfo.connectionId))) {
88-
await addNewUser({connectionId: socketInfo.connectionId, room, trainerToken: '', isTrainer: false});
93+
if (
94+
roomAvailable ||
95+
!(await sessionRepository.isExistingConnection(socketInfo.connectionId))
96+
) {
97+
await sessionRepository.addNewUser({
98+
connectionId: socketInfo.connectionId,
99+
room,
100+
trainerToken: '',
101+
isTrainer: false,
102+
});
89103
socketInfo.socket.join(room);
90104
}
91105
return [{ type: OutputMessageTypes.CONNECTION_ESTABLISHED_STUDENT }];
92106
};
93107

94-
const handleEstablishConnectionTrainer = async (socketInfo: SocketInfo, room: string, trainerToken: string): Promise<Action[]> => {
95-
if (!trainerToken || !room) {
96-
// Ignore
108+
const handleTrainerSendText = async (
109+
socketInfo: SocketInfo,
110+
text: string,
111+
action: string
112+
): Promise<Action[]> => {
113+
if (!sessionRepository.isTrainerUser(socketInfo.connectionId)) {
97114
return [];
98115
}
116+
const room = await sessionRepository.getRoomFromConnectionId(
117+
socketInfo.connectionId
118+
);
119+
const roomInfo: RoomInfo = { room, content: text };
99120

100-
const roomAvailable: boolean = await isRoomAvailable(room);
101-
102-
if(roomAvailable) {
103-
saveRoomInfo({room, content:""}, false);
104-
}
105-
106-
if (roomAvailable || !(await isExistingConnection(socketInfo.connectionId))) {
107-
await addNewUser({ connectionId: socketInfo.connectionId, room, trainerToken, isTrainer: !!trainerToken });
108-
socketInfo.socket.join(room);
121+
switch (action) {
122+
case InputMessageTypes.TRAINER_APPEND_TEXT:
123+
await roomRepository.saveRoomInfo(roomInfo, false);
124+
return [{ type: OutputMessageTypes.APPEND_TEXT, payload: text }];
125+
case InputMessageTypes.TRAINER_SET_FULL_TEXT:
126+
await roomRepository.saveRoomInfo(roomInfo, true);
127+
return [{ type: OutputMessageTypes.UPDATE_FULL_CONTENT }];
128+
default:
129+
return [];
109130
}
110-
return [{ type: OutputMessageTypes.CONNECTION_ESTABLISHED_TRAINER }];
111-
};
131+
};

0 commit comments

Comments
 (0)