diff --git a/projects/ucap-webmessenger-app/src/app/store/messenger/sync/actions.ts b/projects/ucap-webmessenger-app/src/app/store/messenger/sync/actions.ts index 646c0439..cba55fd5 100644 --- a/projects/ucap-webmessenger-app/src/app/store/messenger/sync/actions.ts +++ b/projects/ucap-webmessenger-app/src/app/store/messenger/sync/actions.ts @@ -4,8 +4,14 @@ import { GroupRequest, GroupDetailData, GroupResponse, - UserInfo + UserInfo, + RoomRequest } from '@ucap-webmessenger/protocol-sync'; +import { + RoomInfo, + UserInfoShort, + UserInfo as RoomUserInfo +} from '@ucap-webmessenger/protocol-room'; export const buddy2 = createAction( '[Messenger::Sync] Buddy2', @@ -36,3 +42,27 @@ export const group2Failure = createAction( '[Messenger::Sync] Group2 Failure', props<{ error: any }>() ); + +export const room = createAction( + '[Messenger::Sync] Room', + props() +); + +export const roomSuccess = createAction( + '[Messenger::Sync] Room Success', + props<{ + roomList: RoomInfo[]; + roomUserInfoMap: { + [param: string]: { + userInfoShortList: UserInfoShort[]; + userInfoList: RoomUserInfo[]; + }; + }; + syncDate: string; + }>() +); + +export const roomFailure = createAction( + '[Messenger::Sync] Room Failure', + props<{ error: any }>() +); diff --git a/projects/ucap-webmessenger-app/src/app/store/messenger/sync/effects.ts b/projects/ucap-webmessenger-app/src/app/store/messenger/sync/effects.ts index 1ca2f009..b938e874 100644 --- a/projects/ucap-webmessenger-app/src/app/store/messenger/sync/effects.ts +++ b/projects/ucap-webmessenger-app/src/app/store/messenger/sync/effects.ts @@ -22,7 +22,10 @@ import { buddy2Failure, group2, group2Success, - group2Failure + group2Failure, + room, + roomFailure, + roomSuccess } from './actions'; import { SessionStorageService } from '@ucap-webmessenger/web-storage'; @@ -36,9 +39,23 @@ import { GroupResponse, UserInfo, SSVC_TYPE_SYNC_BUDDY2_RES, - SSVC_TYPE_SYNC_GROUP_RES2 + SSVC_TYPE_SYNC_GROUP_RES2, + SSVC_TYPE_SYNC_ROOM_DATA, + SSVC_TYPE_SYNC_ROOM_USER, + SSVC_TYPE_SYNC_ROOM_USER2, + SSVC_TYPE_SYNC_ROOM_RES, + RoomData, + RoomUserData, + RoomUserDetailData, + RoomResponse } from '@ucap-webmessenger/protocol-sync'; import { regViewSuccess } from '@app/store/setting/option'; +import { + RoomInfo, + UserInfoShort, + UserInfo as RoomUserInfo +} from '@ucap-webmessenger/protocol-room'; +import { LoginInfo, KEY_LOGIN_INFO } from '@app/types'; @Injectable() export class Effects { @@ -110,6 +127,107 @@ export class Effects { { dispatch: false } ); + buddy2SuccessPostRoom$ = createEffect(() => + this.actions$.pipe( + ofType(buddy2Success), + map(action => { + const loginInfo = this.sessionStorageService.get( + KEY_LOGIN_INFO + ); + + return loginInfo.localeCode; + }), + withLatestFrom( + this.store.pipe( + select(state => { + return state.messenger.sync.roomSyncDate as string; + }) + ) + ), + map(([localeCode, roomSyncDate]) => + room({ syncDate: roomSyncDate, localeCode }) + ) + ) + ); + + room$ = createEffect( + () => { + let roomList: RoomInfo[]; + let roomUserInfoMap: { + [param: string]: { + userInfoShortList: UserInfoShort[]; + userInfoList: RoomUserInfo[]; + }; + }; + + return this.actions$.pipe( + ofType(room), + tap(() => { + roomList = []; + roomUserInfoMap = {}; + }), + switchMap(req => { + return this.syncProtocolService.room(req).pipe( + map(res => { + switch (res.Type) { + case SSVC_TYPE_SYNC_ROOM_DATA: + roomList.push(...(res as RoomData).roomInfos); + break; + case SSVC_TYPE_SYNC_ROOM_USER: + { + const roomUserData = res as RoomUserData; + if (!roomUserInfoMap[roomUserData.roomSeq]) { + roomUserInfoMap[roomUserData.roomSeq] = { + userInfoList: [], + userInfoShortList: [] + }; + } + roomUserInfoMap[ + roomUserData.roomSeq + ].userInfoShortList.push(...roomUserData.userInfos); + } + break; + case SSVC_TYPE_SYNC_ROOM_USER2: + { + const roomUserDetailData = res as RoomUserDetailData; + if (!roomUserInfoMap[roomUserDetailData.roomSeq]) { + roomUserInfoMap[roomUserDetailData.roomSeq] = { + userInfoList: [], + userInfoShortList: [] + }; + } + roomUserInfoMap[ + roomUserDetailData.roomSeq + ].userInfoList.push(...roomUserDetailData.userInfos); + } + break; + case SSVC_TYPE_SYNC_ROOM_RES: + { + this.logger.debug( + 'roomList', + roomList, + 'roomUserInfoMap', + roomUserInfoMap + ); + this.store.dispatch( + roomSuccess({ + roomList, + roomUserInfoMap, + syncDate: (res as RoomResponse).syncDate + }) + ); + } + break; + } + }), + catchError(error => of(roomFailure({ error }))) + ); + }) + ); + }, + { dispatch: false } + ); + constructor( private actions$: Actions, private store: Store, diff --git a/projects/ucap-webmessenger-app/src/app/store/messenger/sync/reducers.ts b/projects/ucap-webmessenger-app/src/app/store/messenger/sync/reducers.ts index eab7680b..9f8670b0 100644 --- a/projects/ucap-webmessenger-app/src/app/store/messenger/sync/reducers.ts +++ b/projects/ucap-webmessenger-app/src/app/store/messenger/sync/reducers.ts @@ -1,6 +1,6 @@ import { createReducer, on } from '@ngrx/store'; import { initialState } from './state'; -import { buddy2Success, group2Success } from './actions'; +import { buddy2Success, group2Success, roomSuccess } from './actions'; export const reducer = createReducer( initialState, @@ -18,5 +18,14 @@ export const reducer = createReducer( groupList: action.groupList, group2SyncDate: action.syncDate }; + }), + + on(roomSuccess, (state, action) => { + return { + ...state, + roomList: action.roomList, + roomUserInfoMap: action.roomUserInfoMap, + roomSyncDate: action.syncDate + }; }) ); diff --git a/projects/ucap-webmessenger-app/src/app/store/messenger/sync/state.ts b/projects/ucap-webmessenger-app/src/app/store/messenger/sync/state.ts index 73e5fbc2..65b5068c 100644 --- a/projects/ucap-webmessenger-app/src/app/store/messenger/sync/state.ts +++ b/projects/ucap-webmessenger-app/src/app/store/messenger/sync/state.ts @@ -1,5 +1,10 @@ import { Selector, createSelector } from '@ngrx/store'; import { UserInfo, GroupDetailData } from '@ucap-webmessenger/protocol-sync'; +import { + RoomInfo, + UserInfoShort, + UserInfo as RoomUserInfo +} from '@ucap-webmessenger/protocol-room'; export interface State { buddyInfoList: UserInfo[]; @@ -7,13 +12,25 @@ export interface State { groupList: GroupDetailData[]; group2SyncDate: string; + + roomList: RoomInfo[]; + roomUserInfoMap: { + [param: string]: { + userInfoShortList: UserInfoShort[]; + userInfoList: RoomUserInfo[]; + }; + }; + roomSyncDate: string; } export const initialState: State = { buddyInfoList: [], buddy2SyncDate: '', groupList: [], - group2SyncDate: '' + group2SyncDate: '', + roomList: [], + roomUserInfoMap: {}, + roomSyncDate: '' }; export function selectors(selector: Selector) { @@ -42,6 +59,14 @@ export function selectors(selector: Selector) { buddyList: state.buddyInfoList }; } + ), + roomList: createSelector( + selector, + (state: State) => state.roomList + ), + roomSyncDate: createSelector( + selector, + (state: State) => state.roomSyncDate ) }; } diff --git a/projects/ucap-webmessenger-protocol-sync/src/lib/models/room.ts b/projects/ucap-webmessenger-protocol-sync/src/lib/models/room.ts index 4410721d..fad89480 100644 --- a/projects/ucap-webmessenger-protocol-sync/src/lib/models/room.ts +++ b/projects/ucap-webmessenger-protocol-sync/src/lib/models/room.ts @@ -70,8 +70,9 @@ export const decodeRoomData: ProtocolDecoder = ( message: ProtocolMessage ) => { const roomInfos: RoomInfo[] = []; - if (message.bodyList.length > 1) { - const info = message.bodyList[1].split(BodyStringDivider); + + for (const body of message.bodyList) { + const info = body.split(BodyStringDivider); if (info.length > 11) { roomInfos.push({ roomSeq: info[0], diff --git a/projects/ucap-webmessenger-protocol-sync/src/public-api.ts b/projects/ucap-webmessenger-protocol-sync/src/public-api.ts index e3b19528..b8e35a3f 100644 --- a/projects/ucap-webmessenger-protocol-sync/src/public-api.ts +++ b/projects/ucap-webmessenger-protocol-sync/src/public-api.ts @@ -3,6 +3,7 @@ */ export * from './lib/models/buddy'; export * from './lib/models/group'; +export * from './lib/models/room'; export * from './lib/services/sync-protocol.service';