2019-10-16 15:01:16 +09:00

396 lines
11 KiB
TypeScript

import { Injectable } from '@angular/core';
import { Actions, ofType, createEffect } from '@ngrx/effects';
import { of } from 'rxjs';
import {
catchError,
exhaustMap,
map,
withLatestFrom,
switchMap,
tap
} from 'rxjs/operators';
import { Store, select } from '@ngrx/store';
import { NGXLogger } from 'ngx-logger';
import {
buddy2,
buddy2Success,
buddy2Failure,
group2,
group2Success,
group2Failure,
room,
roomFailure,
roomSuccess,
updateRoomForNewEventMessage,
refreshRoom,
refreshRoomFailure,
refreshRoomSuccess
} from './actions';
import { SessionStorageService } from '@ucap-webmessenger/web-storage';
import {
SyncProtocolService,
SSVC_TYPE_SYNC_BUDDY2_DATA,
BuddyResponse,
BuddyDetailData,
SSVC_TYPE_SYNC_GROUP_DATA2,
GroupDetailData,
GroupResponse,
UserInfo,
SSVC_TYPE_SYNC_BUDDY2_RES,
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/messenger/option';
import {
RoomInfo,
UserInfoShort,
UserInfo as RoomUserInfo,
RoomProtocolService,
SSVC_TYPE_ROOM_INFO_ROOM,
SSVC_TYPE_ROOM_INFO_USER,
SSVC_TYPE_ROOM_INFO_USER2,
InfoData,
UserShortData,
UserData,
SSVC_TYPE_ROOM_INFO_RES
} from '@ucap-webmessenger/protocol-room';
import { LoginInfo, KEY_LOGIN_INFO } from '@app/types';
import * as ChatStore from '@app/store/messenger/chat';
import * as RoomStore from '@app/store/messenger/room';
import { Dictionary } from '@ngrx/entity';
import { LoginResponse } from '@ucap-webmessenger/protocol-authentication';
@Injectable()
export class Effects {
buddy2$ = createEffect(
() => {
let buddyList: UserInfo[];
return this.actions$.pipe(
ofType(buddy2),
tap(() => {
buddyList = [];
}),
switchMap(req => {
return this.syncProtocolService.buddy2(req).pipe(
map(res => {
switch (res.SSVC_TYPE) {
case SSVC_TYPE_SYNC_BUDDY2_DATA:
buddyList.push(...(res as BuddyDetailData).buddyInfos);
break;
case SSVC_TYPE_SYNC_BUDDY2_RES:
this.store.dispatch(
buddy2Success({
buddyList,
syncDate: (res as BuddyResponse).syncDate
})
);
break;
}
}),
catchError(error => of(buddy2Failure({ error })))
);
})
);
},
{ dispatch: false }
);
group2$ = createEffect(
() => {
let groupList: GroupDetailData[];
return this.actions$.pipe(
ofType(group2),
tap(() => {
groupList = [];
}),
switchMap(req => {
return this.syncProtocolService.group2(req).pipe(
map(res => {
switch (res.SSVC_TYPE) {
case SSVC_TYPE_SYNC_GROUP_DATA2:
groupList.push(res as GroupDetailData);
break;
case SSVC_TYPE_SYNC_GROUP_RES2:
this.store.dispatch(
group2Success({
groupList,
syncDate: (res as GroupResponse).syncDate
})
);
break;
}
}),
catchError(error => of(group2Failure({ error })))
);
})
);
},
{ dispatch: false }
);
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.SSVC_TYPE) {
case SSVC_TYPE_SYNC_ROOM_DATA:
roomList.push(
...(res as RoomData).roomInfos.filter(v => v.isJoinRoom)
);
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.store.dispatch(
roomSuccess({
roomList,
roomUserInfoMap,
syncDate: (res as RoomResponse).syncDate
})
);
}
break;
}
}),
catchError(error => of(roomFailure({ error })))
);
})
);
},
{ dispatch: false }
);
newEventMessageForRoomInfoList$ = createEffect(
() =>
this.actions$.pipe(
ofType(ChatStore.newEventMessage),
withLatestFrom(
this.store.pipe(
select((state: any) => state.messenger.sync.room.ids as string[])
),
this.store.pipe(
select((state: any) => state.messenger.sync.room.syncDate as string)
)
),
tap(([action, roomSeqList, roomSyncDate]) => {
const index = roomSeqList.findIndex(
(roomSeq, i) => roomSeq === action.roomSeq
);
if (-1 === index) {
const loginInfo = this.sessionStorageService.get<LoginInfo>(
KEY_LOGIN_INFO
);
this.store.dispatch(
room({
syncDate: roomSyncDate,
localeCode: loginInfo.localeCode
})
);
return;
}
this.store.dispatch(updateRoomForNewEventMessage(action));
})
),
{ dispatch: false }
);
openRoom$ = createEffect(
() =>
this.actions$.pipe(
ofType(ChatStore.openRoom),
withLatestFrom(
this.store.pipe(
select(
(state: any) =>
state.account.authentication.loginRes as LoginResponse
)
),
this.store.pipe(
select(
(state: any) =>
state.messenger.sync.roomUser.entities as Dictionary<
RoomUserDetailData
>
)
),
this.store.pipe(
select(
(state: any) =>
state.messenger.sync.roomUserShort.entities as Dictionary<
RoomUserData
>
)
)
),
tap(([action, loginRes, roomUsers, roomUserShorts]) => {
const userSeqList = [...action.userSeqList, loginRes.userSeq];
let roomSeq = null;
for (const key in roomUsers) {
if (roomUsers.hasOwnProperty(key)) {
const element = roomUsers[key];
if (userSeqList.length === element.userInfos.length) {
roomSeq = key;
for (const roomUserInfo of element.userInfos) {
if (-1 === userSeqList.indexOf(roomUserInfo.seq)) {
roomSeq = null;
break;
}
}
}
}
}
for (const key in roomUserShorts) {
if (roomUserShorts.hasOwnProperty(key)) {
const element = roomUserShorts[key];
if (userSeqList.length === element.userInfos.length) {
roomSeq = key;
for (const roomUserDetailData of element.userInfos) {
if (-1 === userSeqList.indexOf(roomUserDetailData.seq)) {
roomSeq = null;
break;
}
}
}
}
}
this.logger.debug(
'openRoom',
'userSeqList',
userSeqList,
'roomSeq',
roomSeq
);
if (!!roomSeq) {
this.store.dispatch(ChatStore.selectedRoom({ roomSeq }));
return;
}
this.store.dispatch(
RoomStore.open({
req: { divCd: 'DivCode', userSeqs: userSeqList }
})
);
})
),
{ dispatch: false }
);
refreshRoom$ = createEffect(
() => {
let roomInfo: RoomInfo;
let userInfoShortList: UserInfoShort[];
let userInfoList: RoomUserInfo[];
return this.actions$.pipe(
ofType(refreshRoom),
tap(() => {
roomInfo = null;
userInfoShortList = [];
userInfoList = [];
}),
switchMap(req => {
return this.roomProtocolService.info(req).pipe(
map(res => {
switch (res.SSVC_TYPE) {
case SSVC_TYPE_ROOM_INFO_ROOM:
roomInfo = (res as InfoData).roomInfo;
break;
case SSVC_TYPE_ROOM_INFO_USER:
userInfoShortList.push(...(res as UserShortData).userInfos);
break;
case SSVC_TYPE_ROOM_INFO_USER2:
userInfoList.push(...(res as UserData).userInfos);
break;
case SSVC_TYPE_ROOM_INFO_RES:
this.store.dispatch(
refreshRoomSuccess({
roomInfo,
userInfoShortList,
userInfoList
})
);
break;
}
}),
catchError(error => of(refreshRoomFailure({ error })))
);
})
);
},
{ dispatch: false }
);
constructor(
private actions$: Actions,
private store: Store<any>,
private syncProtocolService: SyncProtocolService,
private roomProtocolService: RoomProtocolService,
private sessionStorageService: SessionStorageService,
private logger: NGXLogger
) {}
}