2019-09-25 17:26:19 +09:00
|
|
|
import { createReducer, on } from '@ngrx/store';
|
2019-10-11 11:58:03 +09:00
|
|
|
import {
|
|
|
|
initialState,
|
|
|
|
adapterBuddy2,
|
|
|
|
adapterGroup2,
|
|
|
|
adapterRoom,
|
|
|
|
adapterRoomUser,
|
|
|
|
adapterRoomUserShort
|
|
|
|
} from './state';
|
2019-10-10 15:44:39 +09:00
|
|
|
import {
|
|
|
|
buddy2Success,
|
|
|
|
group2Success,
|
|
|
|
roomSuccess,
|
2019-10-16 10:21:26 +09:00
|
|
|
updateRoomForNewEventMessage,
|
2019-10-16 14:52:31 +09:00
|
|
|
refreshRoomSuccess,
|
2019-10-18 16:48:53 +09:00
|
|
|
updateUnreadCount,
|
|
|
|
createGroupSuccess,
|
2019-10-21 13:10:19 +09:00
|
|
|
addBuddySuccess,
|
2019-10-21 16:11:33 +09:00
|
|
|
delBuddySuccess,
|
|
|
|
delGroupSuccess
|
2019-10-10 15:44:39 +09:00
|
|
|
} from './actions';
|
2019-10-11 11:58:03 +09:00
|
|
|
import {
|
|
|
|
RoomUserDetailData,
|
2019-10-18 16:48:53 +09:00
|
|
|
RoomUserData,
|
|
|
|
GroupDetailData
|
2019-10-11 11:58:03 +09:00
|
|
|
} from '@ucap-webmessenger/protocol-sync';
|
2019-10-10 14:50:58 +09:00
|
|
|
|
2019-10-11 13:11:48 +09:00
|
|
|
import * as AuthenticationStore from '@app/store/account/authentication';
|
2019-10-11 15:55:27 +09:00
|
|
|
import * as RoomStore from '@app/store/messenger/room';
|
|
|
|
import { RoomInfo } from '@ucap-webmessenger/protocol-room';
|
2019-10-11 13:11:48 +09:00
|
|
|
|
2019-09-25 17:26:19 +09:00
|
|
|
export const reducer = createReducer(
|
|
|
|
initialState,
|
|
|
|
on(buddy2Success, (state, action) => {
|
|
|
|
return {
|
|
|
|
...state,
|
2019-10-18 16:48:53 +09:00
|
|
|
buddy2: adapterBuddy2.upsertMany(action.buddyList, {
|
2019-10-11 11:58:03 +09:00
|
|
|
...state.buddy2,
|
|
|
|
syncDate: action.syncDate
|
|
|
|
})
|
2019-09-25 18:08:50 +09:00
|
|
|
};
|
|
|
|
}),
|
2019-10-02 14:34:17 +09:00
|
|
|
|
2019-09-25 18:08:50 +09:00
|
|
|
on(group2Success, (state, action) => {
|
|
|
|
return {
|
|
|
|
...state,
|
2019-10-18 16:48:53 +09:00
|
|
|
group2: adapterGroup2.upsertMany(action.groupList, {
|
2019-10-11 11:58:03 +09:00
|
|
|
...state.group2,
|
|
|
|
syncDate: action.syncDate
|
|
|
|
})
|
2019-09-25 18:08:50 +09:00
|
|
|
};
|
2019-10-02 15:49:25 +09:00
|
|
|
}),
|
|
|
|
|
|
|
|
on(roomSuccess, (state, action) => {
|
2019-10-11 11:58:03 +09:00
|
|
|
const roomUserList: RoomUserDetailData[] = [];
|
|
|
|
const roomUserShortList: RoomUserData[] = [];
|
|
|
|
|
|
|
|
for (const key in action.roomUserInfoMap) {
|
|
|
|
if (action.roomUserInfoMap.hasOwnProperty(key)) {
|
|
|
|
const element = action.roomUserInfoMap[key];
|
|
|
|
if (!!element.userInfoList && 0 < element.userInfoList.length) {
|
|
|
|
roomUserList.push({ roomSeq: key, userInfos: element.userInfoList });
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
!!element.userInfoShortList &&
|
|
|
|
0 < element.userInfoShortList.length
|
|
|
|
) {
|
|
|
|
roomUserShortList.push({
|
|
|
|
roomSeq: key,
|
|
|
|
userInfos: element.userInfoShortList
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-17 15:06:38 +09:00
|
|
|
let unReadCount = 0;
|
2019-10-18 13:34:24 +09:00
|
|
|
/** SYNC ROOM 으로 정보 수집시 증분값을 받는다면 기존 state 의 정보를 뒤져 noReadCount 를 계산 후 중분 값에 대한 noReadCount 를 더한다. */
|
|
|
|
// tslint:disable-next-line: forin
|
|
|
|
for (const key in state.room.entities) {
|
|
|
|
const value = state.room.entities[key];
|
|
|
|
unReadCount += value.noReadCnt;
|
|
|
|
}
|
2019-10-17 15:06:38 +09:00
|
|
|
action.roomList.map(item => (unReadCount += item.noReadCnt));
|
|
|
|
|
2019-10-02 15:49:25 +09:00
|
|
|
return {
|
|
|
|
...state,
|
2019-10-15 14:40:44 +09:00
|
|
|
room: adapterRoom.upsertMany(action.roomList, {
|
2019-10-11 11:58:03 +09:00
|
|
|
...state.room,
|
|
|
|
syncDate: action.syncDate
|
|
|
|
}),
|
2019-10-15 14:40:44 +09:00
|
|
|
roomUser: adapterRoomUser.upsertMany(roomUserList, {
|
2019-10-11 11:58:03 +09:00
|
|
|
...state.roomUser
|
|
|
|
}),
|
2019-10-15 14:40:44 +09:00
|
|
|
roomUserShort: adapterRoomUserShort.upsertMany(roomUserShortList, {
|
2019-10-11 11:58:03 +09:00
|
|
|
...state.roomUserShort
|
2019-10-17 15:06:38 +09:00
|
|
|
}),
|
|
|
|
chatUnreadCount: unReadCount
|
2019-10-02 15:49:25 +09:00
|
|
|
};
|
2019-10-10 14:50:58 +09:00
|
|
|
}),
|
|
|
|
|
2019-10-10 15:44:39 +09:00
|
|
|
on(updateRoomForNewEventMessage, (state, action) => {
|
2019-10-11 11:58:03 +09:00
|
|
|
const roomInfo = {
|
|
|
|
...state.room.entities[action.roomSeq],
|
|
|
|
finalEventDate: action.info.sendDate,
|
|
|
|
finalEventMessage: action.info.sentMessage
|
|
|
|
};
|
2019-10-10 14:50:58 +09:00
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
2019-10-11 11:58:03 +09:00
|
|
|
room: adapterRoom.updateOne(
|
|
|
|
{ id: action.roomSeq, changes: roomInfo },
|
|
|
|
{ ...state.room }
|
|
|
|
)
|
2019-10-10 14:50:58 +09:00
|
|
|
};
|
2019-10-11 13:11:48 +09:00
|
|
|
}),
|
|
|
|
|
2019-10-11 15:55:27 +09:00
|
|
|
on(RoomStore.updateSuccess, (state, action) => {
|
|
|
|
const roomInfo: RoomInfo = {
|
|
|
|
...state.room.entities[action.res.roomSeq],
|
|
|
|
roomName: action.res.roomName,
|
|
|
|
receiveAlarm: action.res.receiveAlarm
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
room: adapterRoom.updateOne(
|
|
|
|
{ id: action.res.roomSeq, changes: roomInfo },
|
|
|
|
{ ...state.room }
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
|
2019-10-16 10:21:26 +09:00
|
|
|
on(refreshRoomSuccess, (state, action) => {
|
|
|
|
const roomUserList: RoomUserDetailData[] = [];
|
|
|
|
const roomUserShortList: RoomUserData[] = [];
|
|
|
|
|
|
|
|
if (action.userInfoList) {
|
|
|
|
roomUserList.push({
|
|
|
|
roomSeq: action.roomInfo.roomSeq,
|
|
|
|
userInfos: action.userInfoList
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (action.userInfoShortList) {
|
|
|
|
roomUserShortList.push({
|
|
|
|
roomSeq: action.roomInfo.roomSeq,
|
|
|
|
userInfos: action.userInfoShortList
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
room: adapterRoom.upsertOne(action.roomInfo, {
|
|
|
|
...state.room
|
|
|
|
}),
|
|
|
|
roomUser: adapterRoomUser.upsertMany(roomUserList, {
|
|
|
|
...state.roomUser
|
|
|
|
}),
|
|
|
|
roomUserShort: adapterRoomUserShort.upsertMany(roomUserShortList, {
|
|
|
|
...state.roomUserShort
|
|
|
|
})
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
|
2019-10-16 14:52:31 +09:00
|
|
|
on(updateUnreadCount, (state, action) => {
|
|
|
|
const roomInfo: RoomInfo = {
|
|
|
|
...state.room.entities[action.roomSeq],
|
|
|
|
noReadCnt: action.noReadCnt
|
|
|
|
};
|
|
|
|
|
2019-10-17 15:06:38 +09:00
|
|
|
let unReadCount = 0;
|
|
|
|
if (action.noReadCnt === 0) {
|
|
|
|
// tslint:disable-next-line: forin
|
|
|
|
for (const key in state.room.entities) {
|
|
|
|
if (key !== action.roomSeq) {
|
|
|
|
const value = state.room.entities[key];
|
|
|
|
unReadCount += value.noReadCnt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unReadCount = state.chatUnreadCount + 1;
|
|
|
|
}
|
|
|
|
|
2019-10-16 14:52:31 +09:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
room: adapterRoom.updateOne(
|
|
|
|
{ id: action.roomSeq, changes: roomInfo },
|
|
|
|
{ ...state.room }
|
2019-10-17 15:06:38 +09:00
|
|
|
),
|
|
|
|
chatUnreadCount: unReadCount
|
2019-10-16 14:52:31 +09:00
|
|
|
};
|
|
|
|
}),
|
|
|
|
|
2019-10-18 16:48:53 +09:00
|
|
|
/** 새 그룹 추가. */
|
|
|
|
on(createGroupSuccess, (state, action) => {
|
|
|
|
const groupInfo: GroupDetailData = {
|
|
|
|
seq: action.groupSeq,
|
|
|
|
name: action.groupName,
|
|
|
|
isActive: true,
|
|
|
|
userSeqs: []
|
|
|
|
};
|
2019-10-11 13:11:48 +09:00
|
|
|
return {
|
2019-10-18 16:48:53 +09:00
|
|
|
...state,
|
|
|
|
group2: adapterGroup2.addOne(groupInfo, {
|
|
|
|
...state.group2
|
|
|
|
})
|
2019-10-11 13:11:48 +09:00
|
|
|
};
|
2019-10-21 13:10:19 +09:00
|
|
|
}),
|
2019-10-21 16:11:33 +09:00
|
|
|
/** 새 그룹 삭제 */
|
|
|
|
on(delGroupSuccess, (state, action) => {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
group2: adapterGroup2.removeOne(action.groupSeq, {
|
|
|
|
...state.group2
|
|
|
|
})
|
|
|
|
};
|
|
|
|
}),
|
2019-10-21 13:10:19 +09:00
|
|
|
|
|
|
|
on(delBuddySuccess, (state, action) => {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
buddy2: adapterBuddy2.removeMany(action.userSeqs, {
|
|
|
|
...state.buddy2
|
|
|
|
})
|
|
|
|
};
|
2019-09-25 17:26:19 +09:00
|
|
|
})
|
|
|
|
);
|