1+ import { sessionRepository , roomRepository , RoomInfo } from 'dals' ;
12import { 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