Skip to content

Commit

Permalink
[v1.0.8] 배포 , 채팅방인원제한 제거, 알림 400명씩 병렬요청
Browse files Browse the repository at this point in the history
[v1.0.8] 배포 , 채팅방인원제한 제거, 알림 400명씩 병렬요청
  • Loading branch information
ImNM authored Jul 31, 2022
2 parents adccd15 + ec7035b commit 645d774
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 142 deletions.
41 changes: 22 additions & 19 deletions src/apis/alarm/pushAlarm.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { instanceToPlain, plainToInstance } from 'class-transformer';
import { PUSH_ALARM, PUSH_ALARM_TYPE } from 'src/common/consts/enum';
import { RoomIdDto } from 'src/common/dtos/RoomId.dto';
import { UserIdDto } from 'src/common/dtos/UserId.dto';
import { chunksArray } from 'src/common/funcs/chunkArray';
import { FcmService } from 'src/fcm/fcm.service';
import { FCM_ADMIN } from 'src/fcm/fcmAdminProvider';
import { RoomRepository } from 'src/repositories/room.repository';
Expand All @@ -19,27 +20,27 @@ export class PushAlarmProcessor {
constructor(
private fcmService: FcmService,
private userRepository: UserRepository,
private roomRepository: RoomRepository,
private roomRepository: RoomRepository
) {}

private async sendPushAlarm(sendPushAlarmDto: SendPushAlarmSubDto) {
const findUserFcmToken = await this.userRepository.findUserFcmToken(
sendPushAlarmDto.receivers ? sendPushAlarmDto.receivers : [],
sendPushAlarmDto.receivers ? sendPushAlarmDto.receivers : []
);
console.log(findUserFcmToken);

const TokenArray = findUserFcmToken
.filter((e) => {
.filter(e => {
const checkAppAlarmOn = e.appAlarm;
const checkFCMTokenValid = e.FCMToken.length === 0 ? false : true;
return checkAppAlarmOn && checkFCMTokenValid;
})
.map((e) => e.FCMToken);
.map(e => e.FCMToken);
console.log(TokenArray);
if (TokenArray.length) {
const result = await this.fcmService.sendNotification(
TokenArray,
sendPushAlarmDto,
sendPushAlarmDto
);
console.log(result);
}
Expand Down Expand Up @@ -77,11 +78,9 @@ export class PushAlarmProcessor {

const [room, userInfo] = await Promise.all([
this.roomRepository.getUserAlarmInfoInRoom(
new RoomIdDto(chatAlarmSubDto.roomId),
),
this.userRepository.findOneByUserId(
new UserIdDto(chatAlarmSubDto.sender),
new RoomIdDto(chatAlarmSubDto.roomId)
),
this.userRepository.findOneByUserId(new UserIdDto(chatAlarmSubDto.sender))
]);

if (!room) {
Expand All @@ -104,13 +103,13 @@ export class PushAlarmProcessor {
: [];
const roomNameAndUserAlarmInfoArray = {
userFcmInfoList,
roomName: room.name,
roomName: room.name
};

const TokenArray = roomNameAndUserAlarmInfoArray.userFcmInfoList
.filter((e) => {
const checkIfIBlockUser = userInfo.opBlockedUsers.find((user) =>
user._id.equals(e._id),
.filter(e => {
const checkIfIBlockUser = userInfo.opBlockedUsers.find(user =>
user._id.equals(e._id)
)
? false
: true;
Expand All @@ -128,23 +127,27 @@ export class PushAlarmProcessor {
checkFCMTokenValid
);
})
.map((e) => e.FCMToken);
.map(e => e.FCMToken);

const sendPushAlarmObj: SendPushAlarmPubDto = {
nickname: chatAlarmSubDto.nickname,
content: chatAlarmSubDto.content,
pushAlarmType: PUSH_ALARM_TYPE.CHAT,
chatId: chatAlarmSubDto.chatId,
roomId: chatAlarmSubDto.roomId.toString(),
roomName: roomNameAndUserAlarmInfoArray.roomName,
roomName: roomNameAndUserAlarmInfoArray.roomName
};

if (TokenArray.length) {
const result = await this.fcmService.sendNotification(
TokenArray,
plainToInstance(SendPushAlarmSubDto, sendPushAlarmObj),
const chunckedArray = chunksArray<string>(TokenArray, 400);
await Promise.all(
[...chunckedArray].map(() => {
return this.fcmService.sendNotification(
TokenArray,
plainToInstance(SendPushAlarmSubDto, sendPushAlarmObj)
);
})
);
console.log(result);
}
}

Expand Down
82 changes: 41 additions & 41 deletions src/apis/rooms/rooms.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
BadRequestException,
Injectable,
InternalServerErrorException,
Type,
Type
} from '@nestjs/common';
import { plainToInstance } from 'class-transformer';
import { ObjectId, Types } from 'mongoose';
Expand Down Expand Up @@ -34,15 +34,15 @@ export class RoomsService {
constructor(
private readonly roomRepository: RoomRepository,
private readonly userRepository: UserRepository,
private readonly chatService: ChatService,
private readonly chatService: ChatService
) {}

private filterRemoveBlockedUserFromUserList(
userList: UserProfileDto[],
blockedUserDto: BlockedUserDto,
blockedUserDto: BlockedUserDto
) {
return userList.filter(
(user) => !blockedUserDto.blockedUsers.find((e) => e.equals(user._id)),
user => !blockedUserDto.blockedUsers.find(e => e.equals(user._id))
);
}

Expand Down Expand Up @@ -72,11 +72,11 @@ export class RoomsService {
// @returnValueToDto(ResFindRoomDto)
async findRoom(
findRoomDto: FindRoomDto,
userId: UserIdDto,
userId: UserIdDto
): Promise<ResFindRoomDto[]> {
//
const isCATEGORY_TYPE = Object.values<string>(CATEGORY_TYPE).includes(
findRoomDto.filter,
findRoomDto.filter
);
const user = await this.userRepository.findOneByUserId(userId);
if (!user) {
Expand All @@ -86,7 +86,7 @@ export class RoomsService {
if (isCATEGORY_TYPE) {
// 카테고리 타입인 경우
rooms = await this.roomRepository.findRoomsByCoordinatesWithFilter(
findRoomDto,
findRoomDto
);
} else if (findRoomDto.filter === FIND_ROOM_FILTER_TYPE.ALL) {
//전체 찾기 인경우
Expand All @@ -95,13 +95,13 @@ export class RoomsService {
// 내가 즐겨 찾기 한경우
rooms = await this.roomRepository.findFavoirteRoomsByCoordinates(
findRoomDto,
user.favoriteRoomList,
user.favoriteRoomList
);
}

const result = rooms.map((element: Room) => {
const iFavorite = user.favoriteRoomList.find((room) =>
room._id.equals(element._id),
const iFavorite = user.favoriteRoomList.find(room =>
room._id.equals(element._id)
)
? true
: false;
Expand All @@ -121,7 +121,7 @@ export class RoomsService {
// return new ResFindRoomDto(room, iFavorite, iJoin);
// });
return plainToInstance(ResFindRoomDto, result, {
excludeExtraneousValues: true,
excludeExtraneousValues: true
}) as unknown as Array<ResFindRoomDto>;
}

Expand All @@ -136,7 +136,7 @@ export class RoomsService {
async addUserToRoom(
roomIdDto: RoomIdDto,
userIdDto: UserIdDto,
blockUserListDto: BlockedUserDto,
blockUserListDto: BlockedUserDto
): Promise<ResFindOneRoomDto> {
// 이전 룸에서 빼주는 로직 추가해야함
const user = await this.userRepository.findOneByUserId(userIdDto);
Expand All @@ -153,8 +153,8 @@ export class RoomsService {
const myRoomId = user.myRoom._id;
if (roomIdDto.roomId.equals(myRoomId)) {
// 룸이 같을경우 룸의 정보를 리턴
const iFavorite = user.favoriteRoomList.find((room) =>
room._id.equals(myRoomId),
const iFavorite = user.favoriteRoomList.find(room =>
room._id.equals(myRoomId)
)
? true
: false;
Expand All @@ -163,66 +163,66 @@ export class RoomsService {
// 차단 유저아웃
room.userList = this.filterRemoveBlockedUserFromUserList(
room.userList,
blockUserListDto,
blockUserListDto
);
const result = { ...room, iFavorite, iAlarm: user.chatAlarm };

return plainToInstance(ResFindOneRoomDto, result, {
excludeExtraneousValues: true,
excludeExtraneousValues: true
});
} else {
// 다른 룸일 경우 다른룸에서 해당 유저를 빼줌
// 300명인지 체크하는 로직추가
if (room.userCount >= 300) {
throw new BadRequestException('유저수가 300명이 넘었습니다.');
}
// if (room.userCount >= 300) {
// throw new BadRequestException('유저수가 300명이 넘었습니다.');
// }

await this.roomRepository.pullUserFromRoom(
new RoomIdDto(user.myRoom._id),
userIdDto,
userIdDto
);
}
}
// 300명인지 체크하는 로직추가
if (room.userCount >= 300) {
throw new BadRequestException('유저수가 300명이 넘었습니다.');
}
// if (room.userCount >= 300) {
// throw new BadRequestException('유저수가 300명이 넘었습니다.');
// }
// 룸에 새로 들어갈때,,,?
await this.userRepository.setMyRoom(userIdDto, roomIdDto);
await this.userRepository.turnOnChatAlarm(userIdDto);
const newRoom = await this.roomRepository.addUserToRoom(
roomIdDto,
userIdDto,
userIdDto
);
//check
const iFavorite = user.favoriteRoomList.find((room) =>
room._id.equals(roomIdDto.roomId),
const iFavorite = user.favoriteRoomList.find(room =>
room._id.equals(roomIdDto.roomId)
)
? true
: false;

// 차단 유저아웃
newRoom.userList = this.filterRemoveBlockedUserFromUserList(
newRoom.userList,
blockUserListDto,
blockUserListDto
);

const result = { ...newRoom, iFavorite, iAlarm: true };
// console.log(result);
return plainToInstance(ResFindOneRoomDto, result, {
excludeExtraneousValues: true,
excludeExtraneousValues: true
});
}

@returnValueToDto(LeftRoomResultResDto)
async pullUserFromRoom(
roomIdDto: RoomIdDto,
userIdDto: UserIdDto,
userIdDto: UserIdDto
): Promise<LeftRoomResultResDto> {
await this.userRepository.setMyRoom(userIdDto, null);
const result = await this.roomRepository.pullUserFromRoom(
roomIdDto,
userIdDto,
userIdDto
);
return { leftSuccess: result ? true : false };
}
Expand All @@ -236,22 +236,22 @@ export class RoomsService {

async toggleRoomToUserFavoriteList(
roomIdDto: RoomIdDto,
userIdDto: UserIdDto,
userIdDto: UserIdDto
): Promise<ResFavoriteToggleDto> {
const user = await this.userRepository.findOneByUserId(userIdDto);
if (!user) {
throw new InternalServerErrorException('잘못된 접근');
}
const isFavoritRoom = user.favoriteRoomList.find((room) =>
roomIdDto.roomId.equals(room._id),
const isFavoritRoom = user.favoriteRoomList.find(room =>
roomIdDto.roomId.equals(room._id)
);
console.log('asdfasdf', isFavoritRoom);
let iFavoritRoom: boolean;
if (isFavoritRoom) {
//내가 이미 즐겨찾기해놨으면
iFavoritRoom = await this.userRepository.pullRoomToFavoriteList(
userIdDto,
roomIdDto,
roomIdDto
);
} else {
// 즐겨찾기 안해놨으면
Expand All @@ -260,11 +260,11 @@ export class RoomsService {
}
iFavoritRoom = await this.userRepository.pushRoomToFavoriteList(
userIdDto,
roomIdDto,
roomIdDto
);
}
return plainToInstance(ResFavoriteToggleDto, {
iFavoritRoom: iFavoritRoom,
iFavoritRoom: iFavoritRoom
});
}

Expand Down Expand Up @@ -293,7 +293,7 @@ export class RoomsService {
if (recentChatInfo.lastChat) {
const recentChatSenderId = recentChatInfo.lastChat.sender._id;
let iBlock = false;
if (user.iBlockUsers.find((e) => e._id.equals(recentChatSenderId))) {
if (user.iBlockUsers.find(e => e._id.equals(recentChatSenderId))) {
iBlock = true;
}

Expand All @@ -314,7 +314,7 @@ export class RoomsService {
const isChatAlarmOn = await this.userRepository.toggleChatAlarm(userId);

return plainToInstance(ResChatAlarmToggleDto, {
isChatAlarmOn: isChatAlarmOn,
isChatAlarmOn: isChatAlarmOn
});
}

Expand All @@ -327,7 +327,7 @@ export class RoomsService {
@returnValueToDto(RoomUserListDto)
async findRoomInUserList(
roomIdDto: RoomIdDto,
user: User,
user: User
// userIdDto: UserIdDto,
// blockUserListDto: BlockedUserDto,
) {
Expand All @@ -336,14 +336,14 @@ export class RoomsService {
}
if (!user.myRoom._id.equals(roomIdDto.roomId)) {
throw new BadRequestException(
'유저가 들어간 방정보와 파라미터가 일치하지 않음',
'유저가 들어간 방정보와 파라미터가 일치하지 않음'
);
}
const room = await this.roomRepository.findOneByRoomId(roomIdDto);

const filteredUserList = this.filterRemoveBlockedUserFromUserList(
room.userList,
user.blockedUserDto,
user.blockedUserDto
);
return { userList: filteredUserList, userCount: filteredUserList.length };
}
Expand Down
Loading

0 comments on commit 645d774

Please sign in to comment.