Skip to content

Commit 705c0ec

Browse files
committed
feat: 요구사항 변경
1 parent 3eb4c78 commit 705c0ec

21 files changed

Lines changed: 716 additions & 233 deletions

src/main/java/queuing/core/room/application/command/SyncPlaybackCommand.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/main/java/queuing/core/room/application/implement/RoomPlaybackWriter.java

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import queuing.core.room.application.service.RoomRedisKeys;
1010
import queuing.core.room.presentation.socket.response.PlaybackStatus;
1111
import queuing.core.room.presentation.socket.response.PlaybackStatusResponse;
12-
import queuing.core.room.presentation.socket.response.VideoResponse;
1312
import tools.jackson.databind.ObjectMapper;
1413

1514
@Component
@@ -26,39 +25,6 @@ public RoomPlaybackState getPlaybackState(String roomSlug) {
2625
return deserializePlaybackState(json);
2726
}
2827

29-
public PlaybackStatusResponse getPlaybackStatus(String roomSlug) {
30-
RoomPlaybackState state = getPlaybackState(roomSlug);
31-
return toPlaybackStatusResponse(state, System.currentTimeMillis());
32-
}
33-
34-
public void setCurrentVideo(String roomSlug, VideoResponse video) {
35-
String currentVideoKey = roomRedisKeys.currentVideoKey(roomSlug);
36-
try {
37-
if (video == null) {
38-
redisTemplate.delete(currentVideoKey);
39-
} else {
40-
redisTemplate.opsForValue().set(currentVideoKey, objectMapper.writeValueAsString(video));
41-
}
42-
} catch (RuntimeException e) {
43-
log.error("Failed to serialize current video", e);
44-
}
45-
}
46-
47-
public VideoResponse getCurrentVideo(String roomSlug) {
48-
String currentVideoKey = roomRedisKeys.currentVideoKey(roomSlug);
49-
String json = redisTemplate.opsForValue().get(currentVideoKey);
50-
return deserializeSafely(json, VideoResponse.class);
51-
}
52-
53-
public void clearPlayback(String roomSlug) {
54-
String playbackKey = roomRedisKeys.playbackKey(roomSlug);
55-
redisTemplate.delete(playbackKey);
56-
}
57-
58-
public PlaybackStatusResponse syncPlayback(String roomSlug, String videoId, PlaybackStatus status, long currentTimeMs) {
59-
return syncPlayback(roomSlug, videoId, status, currentTimeMs, System.currentTimeMillis());
60-
}
61-
6228
public PlaybackStatusResponse syncPlayback(String roomSlug, String videoId, PlaybackStatus status, long currentTimeMs, long clientTimestampMs) {
6329
long now = System.currentTimeMillis();
6430
long latencyMs = Math.max(0, now - clientTimestampMs);
@@ -74,7 +40,24 @@ public PlaybackStatusResponse syncPlayback(String roomSlug, String videoId, Play
7440
now
7541
);
7642
updatePlaybackStatus(roomSlug, input);
77-
return getPlaybackStatus(roomSlug);
43+
return toPlaybackStatusResponse(getPlaybackState(roomSlug), now);
44+
}
45+
46+
public void clearPlayback(String roomSlug) {
47+
String playbackKey = roomRedisKeys.playbackKey(roomSlug);
48+
redisTemplate.delete(playbackKey);
49+
}
50+
51+
public PlaybackStatusResponse startPlayback(String roomSlug, String videoId, long currentTimeMs) {
52+
long now = System.currentTimeMillis();
53+
PlaybackStatusResponse input = PlaybackStatusResponse.of(
54+
videoId,
55+
PlaybackStatus.PLAYING,
56+
Math.max(0, currentTimeMs),
57+
now
58+
);
59+
updatePlaybackStatus(roomSlug, input);
60+
return toPlaybackStatusResponse(getPlaybackState(roomSlug), now);
7861
}
7962

8063
public void updatePlaybackStatus(String roomSlug, PlaybackStatusResponse status) {

src/main/java/queuing/core/room/application/implement/RoomPresenceReader.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package queuing.core.room.application.implement;
22

3+
import java.nio.charset.StandardCharsets;
4+
import java.util.Map;
5+
36
import lombok.RequiredArgsConstructor;
47
import org.springframework.data.redis.core.StringRedisTemplate;
58
import org.springframework.stereotype.Component;
@@ -11,10 +14,50 @@
1114
public class RoomPresenceReader {
1215
private final StringRedisTemplate redisTemplate;
1316
private final RoomRedisKeys roomRedisKeys;
17+
private static final String GUEST_KEY_PREFIX = "guest:";
1418

1519
public long getActiveUsersCount(String roomSlug) {
1620
String participantsKey = roomRedisKeys.participantsKey(roomSlug);
17-
Long size = redisTemplate.opsForHash().size(participantsKey);
18-
return (size != null) ? size : 0L;
21+
Map<Object, Object> entries = redisTemplate.opsForHash().entries(participantsKey);
22+
if (entries == null || entries.isEmpty()) {
23+
return 0L;
24+
}
25+
26+
long activeCount = 0L;
27+
for (Object fieldObj : entries.keySet()) {
28+
String field = toStringSafe(fieldObj);
29+
if (field == null) {
30+
continue;
31+
}
32+
33+
if (!field.startsWith(GUEST_KEY_PREFIX)) {
34+
activeCount++;
35+
continue;
36+
}
37+
38+
String sessionId = field.substring(GUEST_KEY_PREFIX.length());
39+
Boolean exists = redisTemplate.hasKey(roomRedisKeys.sessionMappingKey(sessionId));
40+
if (Boolean.TRUE.equals(exists)) {
41+
activeCount++;
42+
continue;
43+
}
44+
45+
redisTemplate.opsForHash().delete(participantsKey, field);
46+
}
47+
48+
return activeCount;
49+
}
50+
51+
private String toStringSafe(Object obj) {
52+
if (obj == null) {
53+
return null;
54+
}
55+
if (obj instanceof String str) {
56+
return str;
57+
}
58+
if (obj instanceof byte[] bytes) {
59+
return new String(bytes, StandardCharsets.UTF_8);
60+
}
61+
return null;
1962
}
2063
}

src/main/java/queuing/core/room/application/implement/RoomQueueWriter.java

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ public long nextOrder(String roomSlug, long fallback) {
3232
public void enqueue(String roomSlug, QueueEntry entry) {
3333
String entryKey = roomRedisKeys.queueEntryKey(roomSlug, entry.entryId());
3434
String pendingKey = roomRedisKeys.queueKey(roomSlug);
35-
String playlistKey = roomRedisKeys.playlistKey(roomSlug);
3635

3736
try {
3837
String json = objectMapper.writeValueAsString(entry);
39-
List<Object> execResult = executeEnqueueTransaction(entryKey, pendingKey, playlistKey, entry.entryId(), json);
38+
List<Object> execResult = executeEnqueueTransaction(entryKey, pendingKey, entry.entryId(), json);
4039
if (execResult == null) {
4140
log.warn("Failed to enqueue queue entry due to transaction failure");
4241
}
@@ -58,10 +57,6 @@ public List<String> getPendingEntryIds(String roomSlug) {
5857
return (ids != null) ? ids : List.of();
5958
}
6059

61-
public List<String> getPlaylistEntryIds(String roomSlug, long offset, int size) {
62-
return rangeIds(roomRedisKeys.playlistKey(roomSlug), offset, size);
63-
}
64-
6560
public QueueEntry getEntry(String roomSlug, String entryId) {
6661
if (entryId == null) return null;
6762
String json = redisTemplate.opsForValue().get(roomRedisKeys.queueEntryKey(roomSlug, entryId));
@@ -85,7 +80,6 @@ public List<Object> execute(RedisOperations operations) {
8580
operations.multi();
8681
for (String entryId : entryIds) {
8782
operations.opsForList().remove(roomRedisKeys.queueKey(roomSlug), 0, entryId);
88-
operations.opsForList().remove(roomRedisKeys.playlistKey(roomSlug), 0, entryId);
8983
operations.delete(roomRedisKeys.queueEntryKey(roomSlug, entryId));
9084
}
9185
return operations.exec();
@@ -94,25 +88,16 @@ public List<Object> execute(RedisOperations operations) {
9488
}
9589

9690
public void replacePendingOrder(String roomSlug, List<String> orderedPendingEntryIds) {
97-
String currentEntryId = getCurrentEntryId(roomSlug);
9891
redisTemplate.execute(new SessionCallback<List<Object>>() {
9992
@Override
10093
public List<Object> execute(RedisOperations operations) {
10194
String pendingKey = roomRedisKeys.queueKey(roomSlug);
102-
String playlistKey = roomRedisKeys.playlistKey(roomSlug);
10395

10496
operations.multi();
10597
operations.delete(pendingKey);
106-
operations.delete(playlistKey);
10798
if (!orderedPendingEntryIds.isEmpty()) {
10899
operations.opsForList().rightPushAll(pendingKey, orderedPendingEntryIds);
109100
}
110-
if (currentEntryId != null) {
111-
operations.opsForList().rightPush(playlistKey, currentEntryId);
112-
}
113-
if (!orderedPendingEntryIds.isEmpty()) {
114-
operations.opsForList().rightPushAll(playlistKey, orderedPendingEntryIds);
115-
}
116101
return operations.exec();
117102
}
118103
});
@@ -203,14 +188,13 @@ private List<String> rangeIds(String key, long offset, int size) {
203188
return (ids != null) ? ids : List.of();
204189
}
205190

206-
private List<Object> executeEnqueueTransaction(String entryKey, String pendingKey, String playlistKey, String entryId, String json) {
191+
private List<Object> executeEnqueueTransaction(String entryKey, String pendingKey, String entryId, String json) {
207192
return redisTemplate.execute(new SessionCallback<List<Object>>() {
208193
@Override
209194
public List<Object> execute(RedisOperations operations) {
210195
operations.multi();
211196
operations.opsForValue().set(entryKey, json);
212197
operations.opsForList().rightPush(pendingKey, entryId);
213-
operations.opsForList().rightPush(playlistKey, entryId);
214198
return operations.exec();
215199
}
216200
});

src/main/java/queuing/core/room/application/query/GetRoomPlaybackQuery.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/main/java/queuing/core/room/application/service/RoomPlaybackOrchestrator.java

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import queuing.core.room.application.command.QueueAddedCommand;
1010
import queuing.core.room.application.command.StartNextTrackCommand;
11-
import queuing.core.room.application.command.SyncPlaybackCommand;
1211
import queuing.core.room.application.implement.RoomPlaybackWriter;
1312
import queuing.core.room.application.implement.RoomQueueWriter;
1413
import queuing.core.room.application.model.QueueEntry;
@@ -21,7 +20,6 @@
2120
import queuing.core.room.presentation.socket.event.payload.TrackStartedEventData;
2221
import queuing.core.room.presentation.socket.response.PlaybackStatus;
2322
import queuing.core.room.presentation.socket.response.PlaybackStatusResponse;
24-
import queuing.core.room.presentation.socket.response.VideoResponse;
2523

2624
@Service
2725
@Slf4j
@@ -35,23 +33,6 @@ public class RoomPlaybackOrchestrator {
3533
private final RoomTrackEndStore roomTrackEndStore;
3634
private final RoomQueueHistoryService roomQueueHistoryService;
3735

38-
public PlaybackStatusResponse handlePlaybackSync(SyncPlaybackCommand command) {
39-
PlaybackStatusResponse updated = roomPlaybackImplement.syncPlayback(
40-
command.roomSlug(),
41-
command.videoId(),
42-
command.status(),
43-
command.currentTimeMs(),
44-
command.clientTimestampMs()
45-
);
46-
roomEventPublisher.publish(command.roomSlug(), RoomEventType.PLAYBACK_SYNC, updated);
47-
if (command.status() == PlaybackStatus.ENDED) {
48-
startNextTrack(StartNextTrackCommand.of(command.roomSlug(), TrackTransitionReason.AUTO_ENDED.name()));
49-
return updated;
50-
}
51-
rescheduleTrackEnd(command.roomSlug());
52-
return updated;
53-
}
54-
5536
public void handleQueueAdded(QueueAddedCommand command) {
5637
QueueEntry entry = command.entry();
5738
roomQueueImplement.enqueue(command.roomSlug(), entry);
@@ -129,7 +110,6 @@ private void startNextTrackLocked(String roomSlug, TrackTransitionReason reason)
129110
roomEventPublisher.publish(roomSlug, RoomEventType.TRACK_ENDED, TrackEndedEventData.of(resolved, skipped));
130111
}
131112
roomQueueImplement.setCurrentEntryId(roomSlug, null);
132-
roomPlaybackImplement.setCurrentVideo(roomSlug, null);
133113
roomPlaybackImplement.clearPlayback(roomSlug);
134114
return;
135115
}
@@ -146,7 +126,6 @@ private void startNextTrackLocked(String roomSlug, TrackTransitionReason reason)
146126
QueueEntry nextEntry = roomQueueImplement.getEntry(roomSlug, nextEntryId);
147127
if (nextEntry == null) {
148128
roomQueueImplement.setCurrentEntryId(roomSlug, null);
149-
roomPlaybackImplement.setCurrentVideo(roomSlug, null);
150129
roomPlaybackImplement.clearPlayback(roomSlug);
151130
return;
152131
}
@@ -156,17 +135,7 @@ private void startNextTrackLocked(String roomSlug, TrackTransitionReason reason)
156135
roomQueueImplement.setCurrentEntryId(roomSlug, nextEntry.entryId());
157136
QueueEntry started = roomQueueImplement.markStarted(roomSlug, nextEntry.entryId());
158137

159-
VideoResponse currentVideo = VideoResponse.of(
160-
nextEntry.track().videoId(),
161-
nextEntry.track().title(),
162-
nextEntry.track().provider().name(),
163-
"PT0S",
164-
nextEntry.track().thumbnailUrl(),
165-
null
166-
);
167-
roomPlaybackImplement.setCurrentVideo(roomSlug, currentVideo);
168-
169-
PlaybackStatusResponse playback = roomPlaybackImplement.syncPlayback(roomSlug, nextEntry.track().videoId(), PlaybackStatus.PLAYING, 0);
138+
PlaybackStatusResponse playback = roomPlaybackImplement.startPlayback(roomSlug, nextEntry.track().videoId(), 0);
170139
roomEventPublisher.publish(roomSlug, RoomEventType.TRACK_STARTED, TrackStartedEventData.of(started != null ? started : nextEntry, playback));
171140

172141
rescheduleTrackEnd(roomSlug);

0 commit comments

Comments
 (0)