Skip to content

Commit 43ba911

Browse files
committed
Resolve Conflict
2 parents 524c010 + adcbfad commit 43ba911

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

core/data/src/main/java/com/idle/data/repository/ChatRepositoryImpl.kt

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ class ChatRepositoryImpl @Inject constructor(
4848

4949
return response.map { dto ->
5050
val chatRoom = dto.toVO()
51-
52-
// 로컬에 채팅방이 존재하지 않으면 채팅방을 개설
5351
if (!localChatDataSource.isChatRoomExist(chatRoom.id, userId)) {
5452
localChatDataSource.insertChatRoom(
5553
myId = userId,
@@ -70,11 +68,13 @@ class ChatRepositoryImpl @Inject constructor(
7068
roomId: String,
7169
myId: String,
7270
messageId: String?
73-
): List<ChatMessage> = localChatDataSource.getMessages(
74-
roomId = roomId,
75-
myId = myId,
76-
lastMessageId = messageId
77-
)
71+
): List<ChatMessage> {
72+
return localChatDataSource.getMessages(
73+
roomId = roomId,
74+
myId = myId,
75+
lastMessageId = messageId
76+
)
77+
}
7878

7979
override suspend fun getChatRoomMessages(
8080
userType: UserType,
@@ -83,22 +83,24 @@ class ChatRepositoryImpl @Inject constructor(
8383
messageId: String?,
8484
unReadMessageCount: Int?
8585
): List<ChatMessage> {
86-
// Paging형식으로 동작, MessageId가 Null이라는 것은 가장 첫번째 페이징 호출이므로 서버 먼저 호출
87-
// 서버에서 읽지않은 메세지를 받아오는데 만약 로컬에 해당 메시지가 이미 저장되어 있다면 로컬에서 가져옴
8886
if (messageId == null || !localChatDataSource.isMessageExist(roomId, myId, messageId)) {
8987
val response = if (userType == UserType.WORKER) {
9088
chatDataSource.getWorkerChatRoomMessages(roomId, messageId)
9189
} else {
9290
chatDataSource.getCenterChatRoomMessages(roomId, messageId)
9391
}
9492

95-
val messages = response.chatMessageInfos
93+
val allMessages = response.chatMessageInfos
9694
.sortedBy { it.sequence }
9795
.map { it.toVO() }
9896

97+
val messagesToUse = unReadMessageCount?.let { count ->
98+
allMessages.takeLast(count)
99+
} ?: allMessages
100+
99101
val maxSeq = localChatDataSource.getMaxLocalSequence(roomId, myId) ?: Int.MIN_VALUE
100-
messages.forEach { message ->
101-
// 만약 메시지를 저장할 채팅방이 존재하지 않으면 채팅방을 먼저 로컬에 생성
102+
103+
messagesToUse.forEach { message ->
102104
if (!localChatDataSource.isChatRoomExist(message.roomId, myId)) {
103105
localChatDataSource.insertChatRoom(
104106
myId = myId,
@@ -112,20 +114,20 @@ class ChatRepositoryImpl @Inject constructor(
112114
)
113115
}
114116

115-
// 받아온 메시지 Sequence가 현재 로컬에 저장된 메시지 Sequence보다 클 경우, 로컬에 메시지 삽입
116117
if (message.sequence > maxSeq) {
117118
localChatDataSource.insertMessage(message, myId)
118119
}
119120
}
120121

121-
// 메시지의 마지막 SequnceNumber까지 모두 읽음 처리
122-
messages.lastOrNull()?.let {
123-
localChatDataSource.readMessages(
124-
roomId = roomId,
125-
myId = myId,
126-
senderId = it.senderId,
127-
sequence = response.sequence,
128-
)
122+
response.sequence.takeIf { it >= 0 }?.let { seq ->
123+
allMessages.lastOrNull()?.let {
124+
localChatDataSource.readMessages(
125+
roomId = roomId,
126+
myId = myId,
127+
senderId = it.senderId,
128+
sequence = seq
129+
)
130+
}
129131
}
130132
}
131133

@@ -152,10 +154,8 @@ class ChatRepositoryImpl @Inject constructor(
152154
return chatDataSource.subscribeChatMessage(userId)
153155
.map { response ->
154156
val message = response.toVO()
155-
156157
when (message) {
157158
is ChatMessage -> {
158-
// 만약 채팅 메시지를 수신했다면,
159159
if (!localChatDataSource.isChatRoomExist(
160160
roomId = message.roomId,
161161
myId = userId
@@ -172,15 +172,11 @@ class ChatRepositoryImpl @Inject constructor(
172172
)
173173
)
174174
}
175-
176-
// 로컬에 해당 메시지를 저장
177175
localChatDataSource.insertMessage(message, userId)
178176
}
179177

180178
is ReadMessage -> {
181-
// 상대방이 읽었다는 메시지를 수신했다면,
182179
if (message.opponentId != userId) {
183-
// 해당 메시지를 읽음 처리
184180
localChatDataSource.readMessages(
185181
roomId = message.chatroomId,
186182
myId = userId,
@@ -191,8 +187,8 @@ class ChatRepositoryImpl @Inject constructor(
191187
}
192188
}
193189
message
194-
}.retryWhen { cause, attempt ->
195-
// 최대 5번까지 네트워크 연결 재시도
190+
}
191+
.retryWhen { cause, attempt ->
196192
if (cause is IOException && attempt < MAX_RETRY_ATTEMPTS) {
197193
connectWebSocket()
198194
delay(calculateRetryTime(attempt.toInt()))

0 commit comments

Comments
 (0)