402 lines
11 KiB
TypeScript
Raw Normal View History

2019-09-25 17:26:19 +09:00
import { Injectable } from '@angular/core';
import { Actions, ofType, createEffect } from '@ngrx/effects';
import { of } from 'rxjs';
2019-10-02 14:34:17 +09:00
import {
catchError,
exhaustMap,
map,
withLatestFrom,
switchMap,
tap
} from 'rxjs/operators';
2019-09-25 17:26:19 +09:00
2019-09-25 18:08:50 +09:00
import { Store, select } from '@ngrx/store';
2019-09-26 11:11:22 +09:00
import { NGXLogger } from 'ngx-logger';
2019-09-25 18:08:50 +09:00
import {
buddy2,
buddy2Success,
buddy2Failure,
group2,
group2Success,
2019-10-02 15:49:25 +09:00
group2Failure,
room,
roomFailure,
2019-10-10 15:44:39 +09:00
roomSuccess,
updateRoomForNewEventMessage,
refreshRoom,
refreshRoomFailure,
refreshRoomSuccess
2019-09-25 18:08:50 +09:00
} from './actions';
2019-09-25 17:26:19 +09:00
import { SessionStorageService } from '@ucap-webmessenger/web-storage';
import {
SyncProtocolService,
SSVC_TYPE_SYNC_BUDDY2_DATA,
BuddyResponse,
2019-09-25 18:08:50 +09:00
BuddyDetailData,
SSVC_TYPE_SYNC_GROUP_DATA2,
GroupDetailData,
2019-10-02 14:34:17 +09:00
GroupResponse,
UserInfo,
SSVC_TYPE_SYNC_BUDDY2_RES,
2019-10-02 15:49:25 +09:00
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
2019-09-25 17:26:19 +09:00
} from '@ucap-webmessenger/protocol-sync';
2019-10-04 13:45:02 +09:00
import { regViewSuccess } from '@app/store/messenger/option';
2019-10-02 15:49:25 +09:00
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
2019-10-02 15:49:25 +09:00
} from '@ucap-webmessenger/protocol-room';
import { LoginInfo, KEY_LOGIN_INFO } from '@app/types';
2019-09-25 17:26:19 +09:00
2019-10-10 15:44:39 +09:00
import * as ChatStore from '@app/store/messenger/chat';
2019-10-16 14:53:53 +09:00
import * as RoomStore from '@app/store/messenger/room';
import { Dictionary } from '@ngrx/entity';
import { LoginResponse } from '@ucap-webmessenger/protocol-authentication';
2019-10-10 15:44:39 +09:00
2019-09-25 17:26:19 +09:00
@Injectable()
export class Effects {
2019-10-02 14:34:17 +09:00
buddy2$ = createEffect(
() => {
let buddyList: UserInfo[];
2019-09-25 17:26:19 +09:00
2019-10-02 14:34:17 +09:00
return this.actions$.pipe(
ofType(buddy2),
tap(() => {
buddyList = [];
}),
switchMap(req => {
return this.syncProtocolService.buddy2(req).pipe(
map(res => {
2019-10-10 12:14:01 +09:00
switch (res.SSVC_TYPE) {
2019-10-02 14:34:17 +09:00
case SSVC_TYPE_SYNC_BUDDY2_DATA:
buddyList.push(
...(res as BuddyDetailData).buddyInfos.filter(
v => v.isBuddy && v.isActive
)
);
2019-10-02 14:34:17 +09:00
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 }
2019-09-25 18:08:50 +09:00
);
2019-10-02 14:34:17 +09:00
group2$ = createEffect(
() => {
let groupList: GroupDetailData[];
2019-09-25 18:08:50 +09:00
2019-10-02 14:34:17 +09:00
return this.actions$.pipe(
ofType(group2),
tap(() => {
groupList = [];
}),
switchMap(req => {
return this.syncProtocolService.group2(req).pipe(
map(res => {
2019-10-10 12:14:01 +09:00
switch (res.SSVC_TYPE) {
2019-10-02 14:34:17 +09:00
case SSVC_TYPE_SYNC_GROUP_DATA2:
if ((res as GroupDetailData).isActive) {
groupList.push(res as GroupDetailData);
}
2019-10-02 14:34:17 +09:00
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 }
2019-09-25 18:08:50 +09:00
);
2019-10-02 15:49:25 +09:00
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 => {
2019-10-10 12:14:01 +09:00
switch (res.SSVC_TYPE) {
2019-10-02 15:49:25 +09:00
case SSVC_TYPE_SYNC_ROOM_DATA:
roomList.push(
...(res as RoomData).roomInfos.filter(v => v.isJoinRoom)
);
2019-10-02 15:49:25 +09:00
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 }
);
2019-10-10 15:44:39 +09:00
newEventMessageForRoomInfoList$ = createEffect(
() =>
this.actions$.pipe(
ofType(ChatStore.newEventMessage),
withLatestFrom(
this.store.pipe(
select((state: any) => state.messenger.sync.room.ids as string[])
2019-10-10 15:44:39 +09:00
),
this.store.pipe(
select((state: any) => state.messenger.sync.room.syncDate as string)
2019-10-10 15:44:39 +09:00
)
),
tap(([action, roomSeqList, roomSyncDate]) => {
const index = roomSeqList.findIndex(
(roomSeq, i) => roomSeq === action.roomSeq
2019-10-10 15:44:39 +09:00
);
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 }
);
2019-10-16 14:53:53 +09:00
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 }
);
2019-09-25 17:26:19 +09:00
constructor(
private actions$: Actions,
2019-09-25 18:08:50 +09:00
private store: Store<any>,
2019-09-25 17:26:19 +09:00
private syncProtocolService: SyncProtocolService,
private roomProtocolService: RoomProtocolService,
2019-09-26 11:11:22 +09:00
private sessionStorageService: SessionStorageService,
private logger: NGXLogger
2019-09-25 17:26:19 +09:00
) {}
}