Commit 8b497fa65516218c6325255df066d9421cbef349
1 parent
530d3b3a
✨ feat(im): 完善 rtc 的后端代码逻辑,各种代码风格的优化
Showing
14 changed files
with
368 additions
and
99 deletions
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/collection/CollectionUtils.java
| @@ -124,6 +124,22 @@ public class CollectionUtils { | @@ -124,6 +124,22 @@ public class CollectionUtils { | ||
| 124 | return from.stream().filter(filter).map(func).filter(Objects::nonNull).collect(Collectors.toSet()); | 124 | return from.stream().filter(filter).map(func).filter(Objects::nonNull).collect(Collectors.toSet()); |
| 125 | } | 125 | } |
| 126 | 126 | ||
| 127 | + public static <T, U> Set<U> convertLinkedSet(Collection<T> from, Function<T, U> func) { | ||
| 128 | + if (CollUtil.isEmpty(from)) { | ||
| 129 | + return new LinkedHashSet<>(); | ||
| 130 | + } | ||
| 131 | + return from.stream().map(func).filter(Objects::nonNull) | ||
| 132 | + .collect(Collectors.toCollection(LinkedHashSet::new)); | ||
| 133 | + } | ||
| 134 | + | ||
| 135 | + public static <T, U> Set<U> convertLinkedSet(Collection<T> from, Function<T, U> func, Predicate<T> filter) { | ||
| 136 | + if (CollUtil.isEmpty(from)) { | ||
| 137 | + return new LinkedHashSet<>(); | ||
| 138 | + } | ||
| 139 | + return from.stream().filter(filter).map(func).filter(Objects::nonNull) | ||
| 140 | + .collect(Collectors.toCollection(LinkedHashSet::new)); | ||
| 141 | + } | ||
| 142 | + | ||
| 127 | public static <T, K> Map<K, T> convertMapByFilter(Collection<T> from, Predicate<T> filter, Function<T, K> keyFunc) { | 143 | public static <T, K> Map<K, T> convertMapByFilter(Collection<T> from, Predicate<T> filter, Function<T, K> keyFunc) { |
| 128 | if (CollUtil.isEmpty(from)) { | 144 | if (CollUtil.isEmpty(from)) { |
| 129 | return new HashMap<>(); | 145 | return new HashMap<>(); |
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/http/HttpUtils.java
| @@ -206,4 +206,14 @@ public class HttpUtils { | @@ -206,4 +206,14 @@ public class HttpUtils { | ||
| 206 | } | 206 | } |
| 207 | } | 207 | } |
| 208 | 208 | ||
| 209 | + /** | ||
| 210 | + * WebSocket URL 切换成 HTTP URL:ws:// → http://;wss:// → https://;其它格式原样保留 | ||
| 211 | + * | ||
| 212 | + * @param url 原始 URL | ||
| 213 | + * @return 切换协议后的 URL | ||
| 214 | + */ | ||
| 215 | + public static String wsUrlToHttp(String url) { | ||
| 216 | + return StrUtil.startWithIgnoreCase(url, "ws") ? "http" + url.substring(2) : url; | ||
| 217 | + } | ||
| 218 | + | ||
| 209 | } | 219 | } |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/rtc/ImRtcCallController.java
| 1 | package cn.iocoder.yudao.module.im.controller.admin.rtc; | 1 | package cn.iocoder.yudao.module.im.controller.admin.rtc; |
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; | 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 4 | +import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | ||
| 4 | import cn.iocoder.yudao.module.im.controller.admin.rtc.vo.ImRtcCallInviteMoreReqVO; | 5 | import cn.iocoder.yudao.module.im.controller.admin.rtc.vo.ImRtcCallInviteMoreReqVO; |
| 5 | import cn.iocoder.yudao.module.im.controller.admin.rtc.vo.ImRtcCallInviteReqVO; | 6 | import cn.iocoder.yudao.module.im.controller.admin.rtc.vo.ImRtcCallInviteReqVO; |
| 6 | import cn.iocoder.yudao.module.im.controller.admin.rtc.vo.ImRtcCallRespVO; | 7 | import cn.iocoder.yudao.module.im.controller.admin.rtc.vo.ImRtcCallRespVO; |
| 7 | import cn.iocoder.yudao.module.im.controller.admin.rtc.vo.ImRtcGroupCallRespVO; | 8 | import cn.iocoder.yudao.module.im.controller.admin.rtc.vo.ImRtcGroupCallRespVO; |
| 9 | +import cn.iocoder.yudao.module.im.dal.dataobject.rtc.ImRtcCallDO; | ||
| 10 | +import cn.iocoder.yudao.module.im.dal.dataobject.rtc.ImRtcParticipantDO; | ||
| 11 | +import cn.iocoder.yudao.module.im.enums.rtc.ImRtcParticipantStatusEnum; | ||
| 12 | +import cn.iocoder.yudao.module.im.framework.config.ImProperties; | ||
| 8 | import cn.iocoder.yudao.module.im.service.rtc.ImRtcCallService; | 13 | import cn.iocoder.yudao.module.im.service.rtc.ImRtcCallService; |
| 9 | import io.swagger.v3.oas.annotations.Operation; | 14 | import io.swagger.v3.oas.annotations.Operation; |
| 10 | import io.swagger.v3.oas.annotations.Parameter; | 15 | import io.swagger.v3.oas.annotations.Parameter; |
| @@ -14,6 +19,10 @@ import jakarta.validation.Valid; | @@ -14,6 +19,10 @@ import jakarta.validation.Valid; | ||
| 14 | import org.springframework.validation.annotation.Validated; | 19 | import org.springframework.validation.annotation.Validated; |
| 15 | import org.springframework.web.bind.annotation.*; | 20 | import org.springframework.web.bind.annotation.*; |
| 16 | 21 | ||
| 22 | +import java.util.List; | ||
| 23 | +import java.util.Objects; | ||
| 24 | +import java.util.Set; | ||
| 25 | + | ||
| 17 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | 26 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| 18 | import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; | 27 | import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; |
| 19 | 28 | ||
| @@ -25,17 +34,28 @@ public class ImRtcCallController { | @@ -25,17 +34,28 @@ public class ImRtcCallController { | ||
| 25 | 34 | ||
| 26 | @Resource | 35 | @Resource |
| 27 | private ImRtcCallService rtcCallService; | 36 | private ImRtcCallService rtcCallService; |
| 37 | + @Resource | ||
| 38 | + private ImProperties imProperties; | ||
| 28 | 39 | ||
| 29 | @PostMapping("/invite") | 40 | @PostMapping("/invite") |
| 30 | @Operation(summary = "发起通话;私聊或群聊根据 scene 区分") | 41 | @Operation(summary = "发起通话;私聊或群聊根据 scene 区分") |
| 31 | public CommonResult<ImRtcCallRespVO> invite(@Valid @RequestBody ImRtcCallInviteReqVO reqVO) { | 42 | public CommonResult<ImRtcCallRespVO> invite(@Valid @RequestBody ImRtcCallInviteReqVO reqVO) { |
| 32 | - return success(rtcCallService.invite(getLoginUserId(), reqVO)); | 43 | + ImRtcCallDO call = rtcCallService.inviteCall(getLoginUserId(), reqVO); |
| 44 | + return success(buildCallResp(call, getLoginUserId())); | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + @PostMapping("/join") | ||
| 48 | + @Operation(summary = "加入已有群通话;用于胶囊条「加入」按钮") | ||
| 49 | + @Parameter(name = "roomName", description = "LiveKit 房间名", required = true, example = "im_rtc_call_xxxx") | ||
| 50 | + public CommonResult<ImRtcCallRespVO> joinCall(@RequestParam("roomName") String roomName) { | ||
| 51 | + ImRtcCallDO call = rtcCallService.joinCall(getLoginUserId(), roomName); | ||
| 52 | + return success(buildCallResp(call, getLoginUserId())); | ||
| 33 | } | 53 | } |
| 34 | 54 | ||
| 35 | @PostMapping("/invite-more") | 55 | @PostMapping("/invite-more") |
| 36 | @Operation(summary = "通话中添加成员;仅群通话可用") | 56 | @Operation(summary = "通话中添加成员;仅群通话可用") |
| 37 | public CommonResult<Boolean> inviteMore(@Valid @RequestBody ImRtcCallInviteMoreReqVO reqVO) { | 57 | public CommonResult<Boolean> inviteMore(@Valid @RequestBody ImRtcCallInviteMoreReqVO reqVO) { |
| 38 | - rtcCallService.inviteMore(getLoginUserId(), reqVO); | 58 | + rtcCallService.inviteMoreCall(getLoginUserId(), reqVO); |
| 39 | return success(true); | 59 | return success(true); |
| 40 | } | 60 | } |
| 41 | 61 | ||
| @@ -43,14 +63,15 @@ public class ImRtcCallController { | @@ -43,14 +63,15 @@ public class ImRtcCallController { | ||
| 43 | @Operation(summary = "接听通话") | 63 | @Operation(summary = "接听通话") |
| 44 | @Parameter(name = "roomName", description = "LiveKit 房间名", required = true, example = "call_friend_1_2") | 64 | @Parameter(name = "roomName", description = "LiveKit 房间名", required = true, example = "call_friend_1_2") |
| 45 | public CommonResult<ImRtcCallRespVO> accept(@RequestParam("roomName") String roomName) { | 65 | public CommonResult<ImRtcCallRespVO> accept(@RequestParam("roomName") String roomName) { |
| 46 | - return success(rtcCallService.accept(getLoginUserId(), roomName)); | 66 | + ImRtcCallDO call = rtcCallService.acceptCall(getLoginUserId(), roomName); |
| 67 | + return success(buildCallResp(call, getLoginUserId())); | ||
| 47 | } | 68 | } |
| 48 | 69 | ||
| 49 | @PostMapping("/reject") | 70 | @PostMapping("/reject") |
| 50 | @Operation(summary = "拒绝通话") | 71 | @Operation(summary = "拒绝通话") |
| 51 | @Parameter(name = "roomName", description = "LiveKit 房间名", required = true, example = "call_friend_1_2") | 72 | @Parameter(name = "roomName", description = "LiveKit 房间名", required = true, example = "call_friend_1_2") |
| 52 | public CommonResult<Boolean> reject(@RequestParam("roomName") String roomName) { | 73 | public CommonResult<Boolean> reject(@RequestParam("roomName") String roomName) { |
| 53 | - rtcCallService.reject(getLoginUserId(), roomName); | 74 | + rtcCallService.rejectCall(getLoginUserId(), roomName); |
| 54 | return success(true); | 75 | return success(true); |
| 55 | } | 76 | } |
| 56 | 77 | ||
| @@ -58,7 +79,7 @@ public class ImRtcCallController { | @@ -58,7 +79,7 @@ public class ImRtcCallController { | ||
| 58 | @Operation(summary = "取消邀请;主叫接通前调用") | 79 | @Operation(summary = "取消邀请;主叫接通前调用") |
| 59 | @Parameter(name = "roomName", description = "LiveKit 房间名", required = true, example = "call_friend_1_2") | 80 | @Parameter(name = "roomName", description = "LiveKit 房间名", required = true, example = "call_friend_1_2") |
| 60 | public CommonResult<Boolean> cancel(@RequestParam("roomName") String roomName) { | 81 | public CommonResult<Boolean> cancel(@RequestParam("roomName") String roomName) { |
| 61 | - rtcCallService.cancel(getLoginUserId(), roomName); | 82 | + rtcCallService.cancelCall(getLoginUserId(), roomName); |
| 62 | return success(true); | 83 | return success(true); |
| 63 | } | 84 | } |
| 64 | 85 | ||
| @@ -66,7 +87,7 @@ public class ImRtcCallController { | @@ -66,7 +87,7 @@ public class ImRtcCallController { | ||
| 66 | @Operation(summary = "离开通话;接通后调用") | 87 | @Operation(summary = "离开通话;接通后调用") |
| 67 | @Parameter(name = "roomName", description = "LiveKit 房间名", required = true, example = "call_friend_1_2") | 88 | @Parameter(name = "roomName", description = "LiveKit 房间名", required = true, example = "call_friend_1_2") |
| 68 | public CommonResult<Boolean> leave(@RequestParam("roomName") String roomName) { | 89 | public CommonResult<Boolean> leave(@RequestParam("roomName") String roomName) { |
| 69 | - rtcCallService.leave(getLoginUserId(), roomName); | 90 | + rtcCallService.leaveCall(getLoginUserId(), roomName); |
| 70 | return success(true); | 91 | return success(true); |
| 71 | } | 92 | } |
| 72 | 93 | ||
| @@ -74,14 +95,56 @@ public class ImRtcCallController { | @@ -74,14 +95,56 @@ public class ImRtcCallController { | ||
| 74 | @Operation(summary = "重新签发 Token;客户端重连或 Token 过期续期") | 95 | @Operation(summary = "重新签发 Token;客户端重连或 Token 过期续期") |
| 75 | @Parameter(name = "roomName", description = "LiveKit 房间名", required = true, example = "call_friend_1_2") | 96 | @Parameter(name = "roomName", description = "LiveKit 房间名", required = true, example = "call_friend_1_2") |
| 76 | public CommonResult<ImRtcCallRespVO> refreshToken(@RequestParam("roomName") String roomName) { | 97 | public CommonResult<ImRtcCallRespVO> refreshToken(@RequestParam("roomName") String roomName) { |
| 77 | - return success(rtcCallService.refreshToken(getLoginUserId(), roomName)); | 98 | + ImRtcCallDO call = rtcCallService.refreshCallToken(getLoginUserId(), roomName); |
| 99 | + return success(buildCallResp(call, getLoginUserId())); | ||
| 78 | } | 100 | } |
| 79 | 101 | ||
| 80 | - @GetMapping("/group-active-call") | ||
| 81 | - @Operation(summary = "查询群当前进行中的通话;用于群聊顶部「N 人正在通话」胶囊条") | 102 | + @GetMapping("/get-active-call") |
| 103 | + @Operation(summary = "查询当前进行中的通话;用于群聊顶部「N 人正在通话」胶囊条") | ||
| 82 | @Parameter(name = "groupId", description = "群编号", required = true, example = "2048") | 104 | @Parameter(name = "groupId", description = "群编号", required = true, example = "2048") |
| 83 | - public CommonResult<ImRtcGroupCallRespVO> getGroupActiveCall(@RequestParam("groupId") Long groupId) { | ||
| 84 | - return success(rtcCallService.getGroupActiveCall(getLoginUserId(), groupId)); | 105 | + public CommonResult<ImRtcGroupCallRespVO> getActiveCall(@RequestParam("groupId") Long groupId) { |
| 106 | + ImRtcCallDO call = rtcCallService.getActiveCall(getLoginUserId(), groupId); | ||
| 107 | + return success(buildGroupActiveResp(call)); | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + // ========== VO 拼装 ========== | ||
| 111 | + | ||
| 112 | + private ImRtcCallRespVO buildCallResp(ImRtcCallDO call, Long userId) { | ||
| 113 | + if (call == null) { | ||
| 114 | + return null; | ||
| 115 | + } | ||
| 116 | + List<ImRtcParticipantDO> participants = rtcCallService.getCallParticipants(call.getCallId()); | ||
| 117 | + return new ImRtcCallRespVO() | ||
| 118 | + .setCallId(call.getCallId()).setRoomName(call.getRoomName()) | ||
| 119 | + .setLivekitUrl(imProperties.getRtc().getLivekitUrl()) | ||
| 120 | + .setToken(rtcCallService.signCallToken(userId, call.getRoomName())) | ||
| 121 | + .setScene(call.getConversationType()).setMediaType(call.getMediaType()) | ||
| 122 | + .setStatus(call.getStatus()).setInviterId(call.getInviterUserId()) | ||
| 123 | + .setGroupId(call.getGroupId()) | ||
| 124 | + .setInviteeIds(filterUserIds(participants, ImRtcParticipantStatusEnum.INVITING)) | ||
| 125 | + .setJoinedUserIds(filterUserIds(participants, ImRtcParticipantStatusEnum.JOINED)); | ||
| 126 | + } | ||
| 127 | + | ||
| 128 | + private ImRtcGroupCallRespVO buildGroupActiveResp(ImRtcCallDO call) { | ||
| 129 | + if (call == null) { | ||
| 130 | + return null; | ||
| 131 | + } | ||
| 132 | + List<ImRtcParticipantDO> participants = rtcCallService.getCallParticipants(call.getCallId()); | ||
| 133 | + return new ImRtcGroupCallRespVO() | ||
| 134 | + .setCallId(call.getCallId()).setRoomName(call.getRoomName()) | ||
| 135 | + .setGroupId(call.getGroupId()).setMediaType(call.getMediaType()) | ||
| 136 | + .setInviterId(call.getInviterUserId()) | ||
| 137 | + .setJoinedUserIds(filterUserIds(participants, ImRtcParticipantStatusEnum.JOINED)) | ||
| 138 | + .setInviteeIds(filterUserIds(participants, ImRtcParticipantStatusEnum.INVITING)); | ||
| 139 | + } | ||
| 140 | + | ||
| 141 | + /** | ||
| 142 | + * 按状态过滤参与者 userId;用 LinkedHashSet 保留前端展示顺序 | ||
| 143 | + */ | ||
| 144 | + private static Set<Long> filterUserIds(List<ImRtcParticipantDO> participants, | ||
| 145 | + ImRtcParticipantStatusEnum status) { | ||
| 146 | + return CollectionUtils.convertLinkedSet(participants, ImRtcParticipantDO::getUserId, | ||
| 147 | + p -> Objects.equals(p.getStatus(), status.getStatus())); | ||
| 85 | } | 148 | } |
| 86 | 149 | ||
| 87 | } | 150 | } |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/rtc/ImRtcWebhookController.java renamed to yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/rtc/ImRtcLiveKitController.java
| 1 | package cn.iocoder.yudao.module.im.controller.admin.rtc; | 1 | package cn.iocoder.yudao.module.im.controller.admin.rtc; |
| 2 | 2 | ||
| 3 | -import cn.hutool.core.codec.Base64; | ||
| 4 | import cn.hutool.core.util.StrUtil; | 3 | import cn.hutool.core.util.StrUtil; |
| 5 | -import cn.hutool.jwt.JWT; | ||
| 6 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; | 4 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 7 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | 5 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; |
| 8 | -import cn.iocoder.yudao.module.im.framework.config.ImProperties; | 6 | +import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore; |
| 7 | +import cn.iocoder.yudao.module.im.framework.rtc.core.LiveKitClient; | ||
| 9 | import cn.iocoder.yudao.module.im.service.rtc.ImRtcCallService; | 8 | import cn.iocoder.yudao.module.im.service.rtc.ImRtcCallService; |
| 10 | -import cn.iocoder.yudao.module.im.service.rtc.dto.LiveKitWebhookEventDTO; | 9 | +import cn.iocoder.yudao.module.im.framework.rtc.core.LiveKitWebhookEventDTO; |
| 11 | import io.swagger.v3.oas.annotations.Operation; | 10 | import io.swagger.v3.oas.annotations.Operation; |
| 12 | import io.swagger.v3.oas.annotations.tags.Tag; | 11 | import io.swagger.v3.oas.annotations.tags.Tag; |
| 13 | import jakarta.annotation.Resource; | 12 | import jakarta.annotation.Resource; |
| @@ -20,15 +19,10 @@ import org.springframework.web.bind.annotation.RequestBody; | @@ -20,15 +19,10 @@ import org.springframework.web.bind.annotation.RequestBody; | ||
| 20 | import org.springframework.web.bind.annotation.RequestMapping; | 19 | import org.springframework.web.bind.annotation.RequestMapping; |
| 21 | import org.springframework.web.bind.annotation.RestController; | 20 | import org.springframework.web.bind.annotation.RestController; |
| 22 | 21 | ||
| 23 | -import java.nio.charset.StandardCharsets; | ||
| 24 | -import java.security.MessageDigest; | ||
| 25 | -import java.util.Objects; | ||
| 26 | - | ||
| 27 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | 22 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| 28 | 23 | ||
| 29 | -// TODO @AI:是不是改成 ImRtcLiveKitController 更合适??? | ||
| 30 | /** | 24 | /** |
| 31 | - * LiveKit Webhook 入口 | 25 | + * LiveKit 厂商对接入口(Webhook 回调) |
| 32 | * <p> | 26 | * <p> |
| 33 | * 安全由请求签名(JWT + body sha256)保证,不走登录鉴权;伪造请求会被签名校验直接拒绝 | 27 | * 安全由请求签名(JWT + body sha256)保证,不走登录鉴权;伪造请求会被签名校验直接拒绝 |
| 34 | * | 28 | * |
| @@ -38,70 +32,39 @@ import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | @@ -38,70 +32,39 @@ import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | ||
| 38 | @RestController | 32 | @RestController |
| 39 | @RequestMapping("/im/livekit") | 33 | @RequestMapping("/im/livekit") |
| 40 | @Slf4j | 34 | @Slf4j |
| 41 | -public class ImRtcWebhookController { | 35 | +public class ImRtcLiveKitController { |
| 42 | 36 | ||
| 43 | @Resource | 37 | @Resource |
| 44 | - private ImProperties imProperties; | 38 | + private LiveKitClient liveKitClient; |
| 45 | @Resource | 39 | @Resource |
| 46 | private ImRtcCallService rtcCallService; | 40 | private ImRtcCallService rtcCallService; |
| 47 | 41 | ||
| 48 | @PostMapping(value = "/webhook", consumes = MediaType.APPLICATION_JSON_VALUE) | 42 | @PostMapping(value = "/webhook", consumes = MediaType.APPLICATION_JSON_VALUE) |
| 49 | @Operation(summary = "LiveKit Webhook 回调;接收成员离开 / 房间结束等事件做业务态兜底清理") | 43 | @Operation(summary = "LiveKit Webhook 回调;接收成员离开 / 房间结束等事件做业务态兜底清理") |
| 50 | @PermitAll | 44 | @PermitAll |
| 45 | + @TenantIgnore | ||
| 51 | public CommonResult<Boolean> webhook(HttpServletRequest request, @RequestBody String rawBody) { | 46 | public CommonResult<Boolean> webhook(HttpServletRequest request, @RequestBody String rawBody) { |
| 52 | - // TODO @AI:1.1 1.2 这种 | ||
| 53 | - // 1. 校验签名;伪造请求直接 200 但忽略,避免给攻击者反馈 | ||
| 54 | - if (!verifySignature(request, rawBody)) { | ||
| 55 | - log.warn("[livekit-webhook][签名校验失败 ip={} body={}]", request.getRemoteAddr(), rawBody); | 47 | + // 1.1 校验签名;伪造请求直接 200 但忽略,避免给攻击者反馈 |
| 48 | + if (!liveKitClient.verifyWebhookSignature(request.getHeader("Authorization"), rawBody)) { | ||
| 49 | + log.warn("[webhook][签名校验失败 ip={} bodyLength={}]", | ||
| 50 | + request.getRemoteAddr(), rawBody == null ? 0 : rawBody.length()); | ||
| 56 | return success(false); | 51 | return success(false); |
| 57 | } | 52 | } |
| 58 | - // 2. 解析事件并交给 service 处理;幂等由 service 自己保证 | 53 | + // 1.2 解析事件载荷;非法 / 空 event 直接忽略 |
| 59 | LiveKitWebhookEventDTO event = JsonUtils.parseObject(rawBody, LiveKitWebhookEventDTO.class); | 54 | LiveKitWebhookEventDTO event = JsonUtils.parseObject(rawBody, LiveKitWebhookEventDTO.class); |
| 60 | if (event == null || StrUtil.isBlank(event.getEvent())) { | 55 | if (event == null || StrUtil.isBlank(event.getEvent())) { |
| 61 | return success(false); | 56 | return success(false); |
| 62 | } | 57 | } |
| 63 | 58 | ||
| 64 | - // TODO @AI:2. ;这里是不是要打个 logger info? | 59 | + // 2. 交给 service 处理;幂等由 service 自己保证 |
| 60 | + log.info("[webhook][事件处理 event={} roomName={}]", event.getEvent(), | ||
| 61 | + event.getRoom() == null ? null : event.getRoom().getName()); | ||
| 65 | try { | 62 | try { |
| 66 | rtcCallService.handleLiveKitEvent(event); | 63 | rtcCallService.handleLiveKitEvent(event); |
| 67 | } catch (Exception e) { | 64 | } catch (Exception e) { |
| 68 | - log.error("[livekit-webhook][事件处理失败 event={} body={}]", event.getEvent(), rawBody, e); | 65 | + log.error("[webhook][事件处理失败 event={} body={}]", event.getEvent(), rawBody, e); |
| 69 | } | 66 | } |
| 70 | return success(true); | 67 | return success(true); |
| 71 | } | 68 | } |
| 72 | 69 | ||
| 73 | - // TODO @AI:是不是给 LiveKitClient 校验? | ||
| 74 | - /** | ||
| 75 | - * 校验 LiveKit Webhook 签名;流程参见 | ||
| 76 | - * https://docs.livekit.io/home/server/webhook/#receiving-webhooks | ||
| 77 | - */ | ||
| 78 | - private boolean verifySignature(HttpServletRequest request, String rawBody) { | ||
| 79 | - String authHeader = request.getHeader("Authorization"); | ||
| 80 | - if (StrUtil.isBlank(authHeader)) { | ||
| 81 | - return false; | ||
| 82 | - } | ||
| 83 | - String token = StrUtil.removePrefix(authHeader, "Bearer ").trim(); | ||
| 84 | - ImProperties.Rtc cfg = imProperties.getRtc(); | ||
| 85 | - try { | ||
| 86 | - JWT jwt = JWT.of(token); | ||
| 87 | - // 1. JWT HS256 签名;密钥使用 LiveKit API Secret | ||
| 88 | - if (!jwt.setKey(cfg.getApiSecret().getBytes(StandardCharsets.UTF_8)).verify()) { | ||
| 89 | - return false; | ||
| 90 | - } | ||
| 91 | - // 2. body sha256 一致性校验;防止抓到 token 后篡改 body | ||
| 92 | - Object expectedSha = jwt.getPayload("sha256"); | ||
| 93 | - if (expectedSha == null) { | ||
| 94 | - return false; | ||
| 95 | - } | ||
| 96 | - byte[] digest = MessageDigest.getInstance("SHA-256") | ||
| 97 | - .digest(rawBody.getBytes(StandardCharsets.UTF_8)); | ||
| 98 | - String actualSha = Base64.encode(digest); | ||
| 99 | - return Objects.equals(expectedSha.toString(), actualSha); | ||
| 100 | - } catch (Exception e) { | ||
| 101 | - // TODO @AI:是不是要加个什么 request 的参数,方便溯源? | ||
| 102 | - log.warn("[livekit-webhook][签名解析异常]", e); | ||
| 103 | - return false; | ||
| 104 | - } | ||
| 105 | - } | ||
| 106 | - | ||
| 107 | } | 70 | } |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/rtc/vo/ImRtcCallRespVO.java
| @@ -42,7 +42,4 @@ public class ImRtcCallRespVO { | @@ -42,7 +42,4 @@ public class ImRtcCallRespVO { | ||
| 42 | @Schema(description = "已加入房间的成员编号集合") | 42 | @Schema(description = "已加入房间的成员编号集合") |
| 43 | private Set<Long> joinedUserIds; | 43 | private Set<Long> joinedUserIds; |
| 44 | 44 | ||
| 45 | - @Schema(description = "是否本次新建;true 主叫弹「等待对方接受」;false 直接进通话中(已有房间)") | ||
| 46 | - private Boolean newCreated; | ||
| 47 | - | ||
| 48 | } | 45 | } |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/dal/dataobject/rtc/ImRtcCallDO.java
| @@ -31,6 +31,11 @@ import java.time.LocalDateTime; | @@ -31,6 +31,11 @@ import java.time.LocalDateTime; | ||
| 31 | public class ImRtcCallDO extends BaseDO { | 31 | public class ImRtcCallDO extends BaseDO { |
| 32 | 32 | ||
| 33 | /** | 33 | /** |
| 34 | + * LiveKit 房间名前缀;roomName 格式固定为 {@code ROOM_NAME_PREFIX + callId} | ||
| 35 | + */ | ||
| 36 | + public static final String ROOM_NAME_PREFIX = "im_rtc_call_"; | ||
| 37 | + | ||
| 38 | + /** | ||
| 34 | * 编号 | 39 | * 编号 |
| 35 | */ | 40 | */ |
| 36 | @TableId | 41 | @TableId |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/dal/mysql/rtc/ImRtcParticipantMapper.java
| @@ -33,7 +33,7 @@ public interface ImRtcParticipantMapper extends BaseMapperX<ImRtcParticipantDO> | @@ -33,7 +33,7 @@ public interface ImRtcParticipantMapper extends BaseMapperX<ImRtcParticipantDO> | ||
| 33 | .eq(ImRtcParticipantDO::getStatus, status)); | 33 | .eq(ImRtcParticipantDO::getStatus, status)); |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | - default ImRtcParticipantDO selectLastByUserIdAndStatusIn(Long userId, Collection<Integer> statuses) { | 36 | + default ImRtcParticipantDO selectLastByUserIdAndStatus(Long userId, Collection<Integer> statuses) { |
| 37 | return selectLast(new LambdaQueryWrapperX<ImRtcParticipantDO>() | 37 | return selectLast(new LambdaQueryWrapperX<ImRtcParticipantDO>() |
| 38 | .eq(ImRtcParticipantDO::getUserId, userId) | 38 | .eq(ImRtcParticipantDO::getUserId, userId) |
| 39 | .in(ImRtcParticipantDO::getStatus, statuses)); | 39 | .in(ImRtcParticipantDO::getStatus, statuses)); |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/enums/ErrorCodeConstants.java
| @@ -90,5 +90,6 @@ public interface ErrorCodeConstants { | @@ -90,5 +90,6 @@ public interface ErrorCodeConstants { | ||
| 90 | ErrorCode RTC_PRIVATE_INVITEE_REQUIRED = new ErrorCode(1_040_900_006, "私聊通话必须指定对方"); | 90 | ErrorCode RTC_PRIVATE_INVITEE_REQUIRED = new ErrorCode(1_040_900_006, "私聊通话必须指定对方"); |
| 91 | ErrorCode RTC_GROUP_REQUIRED = new ErrorCode(1_040_900_007, "群聊通话必须指定群编号"); | 91 | ErrorCode RTC_GROUP_REQUIRED = new ErrorCode(1_040_900_007, "群聊通话必须指定群编号"); |
| 92 | ErrorCode RTC_INVITE_BUSY = new ErrorCode(1_040_900_008, "通话发起繁忙,请稍后再试"); | 92 | ErrorCode RTC_INVITE_BUSY = new ErrorCode(1_040_900_008, "通话发起繁忙,请稍后再试"); |
| 93 | + ErrorCode RTC_GROUP_CALL_ACTIVE = new ErrorCode(1_040_900_009, "该群已有进行中通话,请通过胶囊条加入"); | ||
| 93 | 94 | ||
| 94 | } | 95 | } |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/enums/message/ImMessageTypeEnum.java
| @@ -82,7 +82,7 @@ public enum ImMessageTypeEnum implements ArrayValuable<Integer> { | @@ -82,7 +82,7 @@ public enum ImMessageTypeEnum implements ArrayValuable<Integer> { | ||
| 82 | /** | 82 | /** |
| 83 | * 对应 OpenIM:SignalingNotification 1601(通话信令统一入口) | 83 | * 对应 OpenIM:SignalingNotification 1601(通话信令统一入口) |
| 84 | * 对应自己的类:ImRtcCallNotification | 84 | * 对应自己的类:ImRtcCallNotification |
| 85 | - * 场景:通话信令;不入库,走 imWebSocketService 仅推参与方;signalingType 区分 INVITE / REJECT(预留 ACCEPT / CANCEL / HUNGUP) | 85 | + * 场景:通话信令;不入库,走 imWebSocketService 仅推参与方;status 复用参与者状态枚举区分 INVITING / JOINED / REJECTED / NO_ANSWER / LEFT |
| 86 | */ | 86 | */ |
| 87 | RTC_CALL(1601, "通话信令", false, false), | 87 | RTC_CALL(1601, "通话信令", false, false), |
| 88 | /** | 88 | /** |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/framework/rtc/core/LiveKitClient.java
| 1 | package cn.iocoder.yudao.module.im.framework.rtc.core; | 1 | package cn.iocoder.yudao.module.im.framework.rtc.core; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.codec.Base64; | ||
| 3 | import cn.hutool.core.collection.CollUtil; | 4 | import cn.hutool.core.collection.CollUtil; |
| 4 | import cn.hutool.core.lang.Assert; | 5 | import cn.hutool.core.lang.Assert; |
| 5 | import cn.hutool.core.util.StrUtil; | 6 | import cn.hutool.core.util.StrUtil; |
| 7 | +import cn.hutool.crypto.digest.DigestUtil; | ||
| 6 | import cn.hutool.http.HttpRequest; | 8 | import cn.hutool.http.HttpRequest; |
| 7 | import cn.hutool.http.HttpResponse; | 9 | import cn.hutool.http.HttpResponse; |
| 8 | import cn.hutool.json.JSONArray; | 10 | import cn.hutool.json.JSONArray; |
| @@ -13,11 +15,13 @@ import cn.iocoder.yudao.framework.common.util.http.HttpUtils; | @@ -13,11 +15,13 @@ import cn.iocoder.yudao.framework.common.util.http.HttpUtils; | ||
| 13 | import cn.iocoder.yudao.module.im.framework.config.ImProperties; | 15 | import cn.iocoder.yudao.module.im.framework.config.ImProperties; |
| 14 | import lombok.extern.slf4j.Slf4j; | 16 | import lombok.extern.slf4j.Slf4j; |
| 15 | 17 | ||
| 18 | +import java.nio.charset.StandardCharsets; | ||
| 16 | import java.time.Duration; | 19 | import java.time.Duration; |
| 17 | import java.time.Instant; | 20 | import java.time.Instant; |
| 18 | import java.util.Date; | 21 | import java.util.Date; |
| 19 | import java.util.HashMap; | 22 | import java.util.HashMap; |
| 20 | import java.util.Map; | 23 | import java.util.Map; |
| 24 | +import java.util.Objects; | ||
| 21 | 25 | ||
| 22 | /** | 26 | /** |
| 23 | * LiveKit 客户端 | 27 | * LiveKit 客户端 |
| @@ -159,6 +163,45 @@ public class LiveKitClient { | @@ -159,6 +163,45 @@ public class LiveKitClient { | ||
| 159 | } | 163 | } |
| 160 | 164 | ||
| 161 | /** | 165 | /** |
| 166 | + * 校验 LiveKit Webhook 签名;流程参见 | ||
| 167 | + * <a href="https://docs.livekit.io/home/server/webhook/#receiving-webhooks">webhook 文档</a> | ||
| 168 | + * <p> | ||
| 169 | + * 校验两步: | ||
| 170 | + * 1.1 JWT HS256 签名验证;密钥使用 LiveKit API Secret | ||
| 171 | + * 1.2 body 的 sha256 与 JWT 内 claim 一致;防止抓到 token 后篡改 body | ||
| 172 | + * | ||
| 173 | + * @param authHeader 请求头 Authorization 原值(含 "Bearer " 前缀) | ||
| 174 | + * @param rawBody 请求原始 body | ||
| 175 | + * @return 是否通过;签名异常一律视为不通过 | ||
| 176 | + */ | ||
| 177 | + public boolean verifyWebhookSignature(String authHeader, String rawBody) { | ||
| 178 | + if (StrUtil.isBlank(authHeader)) { | ||
| 179 | + return false; | ||
| 180 | + } | ||
| 181 | + String token = StrUtil.removePrefix(authHeader, "Bearer ").trim(); | ||
| 182 | + ImProperties.Rtc cfg = imProperties.getRtc(); | ||
| 183 | + try { | ||
| 184 | + JWT jwt = JWT.of(token); | ||
| 185 | + // JWT HS256 签名验证 | ||
| 186 | + if (!jwt.setKey(cfg.getApiSecret().getBytes(StandardCharsets.UTF_8)).verify()) { | ||
| 187 | + return false; | ||
| 188 | + } | ||
| 189 | + // body sha256 一致性校验 | ||
| 190 | + Object expectedSha = jwt.getPayload("sha256"); | ||
| 191 | + if (expectedSha == null) { | ||
| 192 | + return false; | ||
| 193 | + } | ||
| 194 | + // 计算 body 的 sha256,并与 JWT 内 claim 对比 | ||
| 195 | + String actualSha = Base64.encode(DigestUtil.sha256(rawBody)); | ||
| 196 | + return Objects.equals(expectedSha.toString(), actualSha); | ||
| 197 | + } catch (Exception e) { | ||
| 198 | + log.warn("[verifyWebhookSignature][签名解析异常 bodyLength={}]", | ||
| 199 | + rawBody == null ? 0 : rawBody.length(), e); | ||
| 200 | + return false; | ||
| 201 | + } | ||
| 202 | + } | ||
| 203 | + | ||
| 204 | + /** | ||
| 162 | * 签发管理 Token;用于调 Server API(DeleteRoom / ListParticipants / RemoveParticipant 等) | 205 | * 签发管理 Token;用于调 Server API(DeleteRoom / ListParticipants / RemoveParticipant 等) |
| 163 | * | 206 | * |
| 164 | * @return JWT 字符串 | 207 | * @return JWT 字符串 |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/job/rtc/ImRtcCallCleanupJob.java
| @@ -10,7 +10,7 @@ import lombok.extern.slf4j.Slf4j; | @@ -10,7 +10,7 @@ import lombok.extern.slf4j.Slf4j; | ||
| 10 | import org.springframework.stereotype.Component; | 10 | import org.springframework.stereotype.Component; |
| 11 | 11 | ||
| 12 | /** | 12 | /** |
| 13 | - * 僵尸通话清理 Job:兜底 LiveKit Webhook 丢失 / 客户端异常关闭等未调 leave 等场景 | 13 | + * 僵尸通话清理 Job:兜底 LiveKit Webhook 丢失 / 客户端异常关闭等未调 leave 的场景 |
| 14 | * | 14 | * |
| 15 | * @author 芋道源码 | 15 | * @author 芋道源码 |
| 16 | */ | 16 | */ |
| @@ -18,23 +18,38 @@ import org.springframework.stereotype.Component; | @@ -18,23 +18,38 @@ import org.springframework.stereotype.Component; | ||
| 18 | @Slf4j | 18 | @Slf4j |
| 19 | public class ImRtcCallCleanupJob implements JobHandler { | 19 | public class ImRtcCallCleanupJob implements JobHandler { |
| 20 | 20 | ||
| 21 | + /** | ||
| 22 | + * 默认扫描阈值(分钟);通话创建超过此值仍未结束才纳入扫描,避开「刚发起还在响铃」的合理零人态 | ||
| 23 | + */ | ||
| 24 | + private static final int DEFAULT_THRESHOLD_MINUTES = 5; | ||
| 25 | + | ||
| 21 | @Resource | 26 | @Resource |
| 22 | private ImRtcCallService rtcCallService; | 27 | private ImRtcCallService rtcCallService; |
| 23 | 28 | ||
| 24 | /** | 29 | /** |
| 25 | * 执行清理 | 30 | * 执行清理 |
| 26 | * | 31 | * |
| 27 | - * @param param 阈值(分钟);为空走 Service 默认(5 分钟) | 32 | + * @param param 阈值(分钟);为空 / 非法走默认 {@link #DEFAULT_THRESHOLD_MINUTES} |
| 28 | * @return 清理数量描述 | 33 | * @return 清理数量描述 |
| 29 | */ | 34 | */ |
| 30 | @Override | 35 | @Override |
| 31 | @TenantJob | 36 | @TenantJob |
| 32 | public String execute(String param) { | 37 | public String execute(String param) { |
| 33 | - Integer thresholdMinutes = StrUtil.isNotBlank(param) && NumberUtil.isInteger(param) | ||
| 34 | - ? Integer.parseInt(param) : null; | 38 | + int thresholdMinutes = resolveThresholdMinutes(param); |
| 35 | int cleaned = rtcCallService.cleanupZombieCalls(thresholdMinutes); | 39 | int cleaned = rtcCallService.cleanupZombieCalls(thresholdMinutes); |
| 36 | log.info("[execute][清理僵尸通话数量 ({}) 个]", cleaned); | 40 | log.info("[execute][清理僵尸通话数量 ({}) 个]", cleaned); |
| 37 | return String.format("清理僵尸通话 %s 个", cleaned); | 41 | return String.format("清理僵尸通话 %s 个", cleaned); |
| 38 | } | 42 | } |
| 39 | 43 | ||
| 44 | + /** | ||
| 45 | + * 解析 quartz param 为分钟阈值;非法 / 非正数走默认值 | ||
| 46 | + */ | ||
| 47 | + private static int resolveThresholdMinutes(String param) { | ||
| 48 | + if (StrUtil.isBlank(param) || !NumberUtil.isInteger(param)) { | ||
| 49 | + return DEFAULT_THRESHOLD_MINUTES; | ||
| 50 | + } | ||
| 51 | + int minutes = Integer.parseInt(param); | ||
| 52 | + return minutes > 0 ? minutes : DEFAULT_THRESHOLD_MINUTES; | ||
| 53 | + } | ||
| 54 | + | ||
| 40 | } | 55 | } |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/service/rtc/ImRtcCallService.java
| @@ -2,77 +2,103 @@ package cn.iocoder.yudao.module.im.service.rtc; | @@ -2,77 +2,103 @@ package cn.iocoder.yudao.module.im.service.rtc; | ||
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.module.im.controller.admin.rtc.vo.ImRtcCallInviteMoreReqVO; | 3 | import cn.iocoder.yudao.module.im.controller.admin.rtc.vo.ImRtcCallInviteMoreReqVO; |
| 4 | import cn.iocoder.yudao.module.im.controller.admin.rtc.vo.ImRtcCallInviteReqVO; | 4 | import cn.iocoder.yudao.module.im.controller.admin.rtc.vo.ImRtcCallInviteReqVO; |
| 5 | -import cn.iocoder.yudao.module.im.controller.admin.rtc.vo.ImRtcCallRespVO; | ||
| 6 | -import cn.iocoder.yudao.module.im.controller.admin.rtc.vo.ImRtcGroupCallRespVO; | 5 | +import cn.iocoder.yudao.module.im.dal.dataobject.rtc.ImRtcCallDO; |
| 6 | +import cn.iocoder.yudao.module.im.dal.dataobject.rtc.ImRtcParticipantDO; | ||
| 7 | 7 | ||
| 8 | import java.util.List; | 8 | import java.util.List; |
| 9 | 9 | ||
| 10 | -// TODO @AI:一个好友,只能一个通话;一个群,只能一个通话;(同一时间); | ||
| 11 | /** | 10 | /** |
| 12 | * IM 实时通话 Service | 11 | * IM 实时通话 Service |
| 13 | * <p> | 12 | * <p> |
| 14 | - * 业务模型:一个好友对 / 一个群同时只能有一个进行中的通话;invite 兼具「创建」与「加入已有」两种语义。 | ||
| 15 | - * 媒体协商完全交给 LiveKit;后端只做:会话状态机、Token 签发、来电信令推送、通话历史落消息流。 | 13 | + * 业务模型:一个好友对 / 一个群同时只能有一个进行中的通话;inviteCall 仅负责「创建新通话」,加入已有群通话走 {@link #joinCall} |
| 16 | * | 14 | * |
| 17 | * @author 芋道源码 | 15 | * @author 芋道源码 |
| 18 | */ | 16 | */ |
| 19 | public interface ImRtcCallService { | 17 | public interface ImRtcCallService { |
| 20 | 18 | ||
| 21 | - // TODO @AI:是不是风格上,还是有 create 和 invite;这样两种逻辑上更好理解; | ||
| 22 | /** | 19 | /** |
| 23 | - * 发起通话;同一 roomName 已有进行中通话时返回该会话并标记 newCreated=false | 20 | + * 发起新通话;同好友对 / 同群已有进行中通话直接抛错(群通话场景应改走 {@link #joinCall}) |
| 24 | * | 21 | * |
| 25 | * @param userId 操作人编号;通常是当前登录用户 | 22 | * @param userId 操作人编号;通常是当前登录用户 |
| 26 | * @param reqVO 请求参数(scene / mediaType / peerUserId 或 groupId) | 23 | * @param reqVO 请求参数(scene / mediaType / peerUserId 或 groupId) |
| 27 | - * @return 包含 token / roomName / livekitUrl 的会话信息 | 24 | + * @return 通话主表 |
| 28 | */ | 25 | */ |
| 29 | - ImRtcCallRespVO invite(Long userId, ImRtcCallInviteReqVO reqVO); | 26 | + ImRtcCallDO inviteCall(Long userId, ImRtcCallInviteReqVO reqVO); |
| 30 | 27 | ||
| 31 | /** | 28 | /** |
| 32 | - * 通话中追加成员;仅群通话场景可用;给新邀请人推 RTC_INVITE | 29 | + * 加入已有群通话:用于群胶囊条「加入」按钮;旁观者作为 ACTIVE_JOIN 加入,邀请池内成员转 JOINED |
| 30 | + * | ||
| 31 | + * @param userId 加入者用户编号 | ||
| 32 | + * @param roomName LiveKit 房间名;从胶囊条 activeCall 拿 | ||
| 33 | + * @return 通话主表 | ||
| 34 | + */ | ||
| 35 | + ImRtcCallDO joinCall(Long userId, String roomName); | ||
| 36 | + | ||
| 37 | + /** | ||
| 38 | + * 通话中追加成员:仅群通话场景可用;给新邀请人推 RTC_CALL(INVITE) | ||
| 33 | * | 39 | * |
| 34 | * @param userId 操作人编号;必须是当前会话参与者 | 40 | * @param userId 操作人编号;必须是当前会话参与者 |
| 35 | * @param reqVO 房间名 + 新邀请的用户编号集合 | 41 | * @param reqVO 房间名 + 新邀请的用户编号集合 |
| 36 | */ | 42 | */ |
| 37 | - void inviteMore(Long userId, ImRtcCallInviteMoreReqVO reqVO); | 43 | + void inviteMoreCall(Long userId, ImRtcCallInviteMoreReqVO reqVO); |
| 38 | 44 | ||
| 39 | /** | 45 | /** |
| 40 | - * 接听通话;私聊状态切换 INVITING → ONGOING;群通话直接加入已有 ONGOING 房间 | 46 | + * 接听通话:私聊状态切换 INVITING → RUNNING;群通话直接加入已有 RUNNING 房间 |
| 47 | + * | ||
| 48 | + * @return 通话主表 | ||
| 41 | */ | 49 | */ |
| 42 | - ImRtcCallRespVO accept(Long userId, String roomName); | 50 | + ImRtcCallDO acceptCall(Long userId, String roomName); |
| 43 | 51 | ||
| 44 | /** | 52 | /** |
| 45 | * 拒绝通话;仅 INVITING 状态可拒;群通话拒绝等同于不参与,房间仍存在 | 53 | * 拒绝通话;仅 INVITING 状态可拒;群通话拒绝等同于不参与,房间仍存在 |
| 46 | */ | 54 | */ |
| 47 | - void reject(Long userId, String roomName); | 55 | + void rejectCall(Long userId, String roomName); |
| 48 | 56 | ||
| 49 | /** | 57 | /** |
| 50 | * 取消邀请;主叫在 INVITING 状态主动取消 | 58 | * 取消邀请;主叫在 INVITING 状态主动取消 |
| 51 | */ | 59 | */ |
| 52 | - void cancel(Long userId, String roomName); | 60 | + void cancelCall(Long userId, String roomName); |
| 53 | 61 | ||
| 54 | /** | 62 | /** |
| 55 | - * 离开通话;ONGOING 状态下离开;私聊任一方离开 = 通话结束;群通话最后一人离开才结束 | 63 | + * 离开通话;RUNNING 状态下离开;私聊任一方离开 = 通话结束;群通话最后一人离开才结束 |
| 64 | + */ | ||
| 65 | + void leaveCall(Long userId, String roomName); | ||
| 66 | + | ||
| 67 | + /** | ||
| 68 | + * 校验用户仍是该通话的参与者,用于客户端重连或 Token 过期续期前的合法性检查; | ||
| 69 | + * 签发新 Token 由 Controller 调 {@link #signCallToken} 完成 | ||
| 70 | + * | ||
| 71 | + * @return 通话主表 | ||
| 56 | */ | 72 | */ |
| 57 | - void leave(Long userId, String roomName); | 73 | + ImRtcCallDO refreshCallToken(Long userId, String roomName); |
| 58 | 74 | ||
| 59 | /** | 75 | /** |
| 60 | - * 重新签发 Token;用于客户端重连或 Token 过期续期 | 76 | + * 查询当前正在进行的通话;目前仅群聊场景(胶囊条),私聊未来扩展再补 peerUserId 参数 |
| 77 | + * <p> | ||
| 78 | + * 鉴权:仅群活跃成员可查;防止任意用户探测群通话状态 / 拿到 inviter / inviteeIds 等敏感信息 | ||
| 79 | + * | ||
| 80 | + * @param userId 操作人编号;通常是当前登录用户 | ||
| 81 | + * @param groupId 群编号 | ||
| 82 | + * @return 通话主表;不存在返回 null | ||
| 61 | */ | 83 | */ |
| 62 | - ImRtcCallRespVO refreshToken(Long userId, String roomName); | 84 | + ImRtcCallDO getActiveCall(Long userId, Long groupId); |
| 63 | 85 | ||
| 64 | /** | 86 | /** |
| 65 | - * 列出用户当前的活跃通话;用于 App 冷启动 / 推送点开恢复 | 87 | + * 查询某通话的全部参与者明细;交给 Controller 拼装 inviteeIds / joinedUserIds |
| 88 | + * | ||
| 89 | + * @param callId 业务通话编号 | ||
| 90 | + * @return 参与者明细列表 | ||
| 66 | */ | 91 | */ |
| 67 | - List<ImRtcCallRespVO> getActiveSessions(Long userId); | 92 | + List<ImRtcParticipantDO> getCallParticipants(String callId); |
| 68 | 93 | ||
| 69 | /** | 94 | /** |
| 70 | - * 查询群当前正在进行的通话;用于群聊顶部「N 人正在通话」胶囊条 | 95 | + * 签发指定用户进入该通话的 LiveKit Token;供 Controller 拼接到响应 VO |
| 71 | * | 96 | * |
| 72 | - * @param groupId 群编号 | ||
| 73 | - * @return 群活跃通话;不存在返回 null;不含 token,仅展示用 | 97 | + * @param userId 进房用户编号;token 内 displayName 取该用户昵称 |
| 98 | + * @param roomName LiveKit 房间名 | ||
| 99 | + * @return JWT 字符串 | ||
| 74 | */ | 100 | */ |
| 75 | - ImRtcGroupCallRespVO getGroupActiveCall(Long groupId); | 101 | + String signCallToken(Long userId, String roomName); |
| 76 | 102 | ||
| 77 | /** | 103 | /** |
| 78 | * 处理 LiveKit Webhook 事件;用于关 tab / 强杀 / 网络断开等异常退出场景的兜底清理 | 104 | * 处理 LiveKit Webhook 事件;用于关 tab / 强杀 / 网络断开等异常退出场景的兜底清理 |
| @@ -80,6 +106,14 @@ public interface ImRtcCallService { | @@ -80,6 +106,14 @@ public interface ImRtcCallService { | ||
| 80 | * 关键事件:participant_left(成员离开) / room_finished(房间结束)。前端正常 leave 时 | 106 | * 关键事件:participant_left(成员离开) / room_finished(房间结束)。前端正常 leave 时 |
| 81 | * 也会触发同样的 LiveKit 事件;此处需做幂等处理,session 已被业务接口移除时直接忽略。 | 107 | * 也会触发同样的 LiveKit 事件;此处需做幂等处理,session 已被业务接口移除时直接忽略。 |
| 82 | */ | 108 | */ |
| 83 | - void handleLiveKitEvent(cn.iocoder.yudao.module.im.service.rtc.dto.LiveKitWebhookEventDTO event); | 109 | + void handleLiveKitEvent(cn.iocoder.yudao.module.im.framework.rtc.core.LiveKitWebhookEventDTO event); |
| 110 | + | ||
| 111 | + /** | ||
| 112 | + * 【定时任务调用】清理僵尸通话 | ||
| 113 | + * | ||
| 114 | + * @param thresholdMinutes 通话创建超过此分钟数才纳入扫描;调用方保证 > 0 | ||
| 115 | + * @return 清理数量 | ||
| 116 | + */ | ||
| 117 | + int cleanupZombieCalls(int thresholdMinutes); | ||
| 84 | 118 | ||
| 85 | } | 119 | } |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/service/websocket/dto/notification/rtc/ImRtcParticipantConnectedNotification.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.im.service.websocket.dto.notification.rtc; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.module.im.dal.dataobject.rtc.ImRtcCallDO; | ||
| 4 | +import lombok.Data; | ||
| 5 | + | ||
| 6 | +/** | ||
| 7 | + * RTC_PARTICIPANT_CONNECTED 通话参与者加入通知 | ||
| 8 | + * <p> | ||
| 9 | + * 不入库;LiveKit webhook participant_joined 触发;私聊推 peer 多端 + inviter 多端,群聊全群广播 | ||
| 10 | + * <p> | ||
| 11 | + * 前端 callStore 把 userId 追加进 joinedUserIds;胶囊条人数 +1;群聊场景首条 1602 携带通话元信息以便 首次填充胶囊条 | ||
| 12 | + * | ||
| 13 | + * @author 芋道源码 | ||
| 14 | + */ | ||
| 15 | +@Data | ||
| 16 | +public class ImRtcParticipantConnectedNotification { | ||
| 17 | + | ||
| 18 | + /** | ||
| 19 | + * 业务通话编号 | ||
| 20 | + */ | ||
| 21 | + private String callId; | ||
| 22 | + /** | ||
| 23 | + * LiveKit 房间名 | ||
| 24 | + */ | ||
| 25 | + private String roomName; | ||
| 26 | + /** | ||
| 27 | + * 加入的参与者用户编号 | ||
| 28 | + */ | ||
| 29 | + private Long userId; | ||
| 30 | + /** | ||
| 31 | + * 会话类型 | ||
| 32 | + */ | ||
| 33 | + private Integer conversationType; | ||
| 34 | + /** | ||
| 35 | + * 群编号;群通话场景必填 | ||
| 36 | + */ | ||
| 37 | + private Long groupId; | ||
| 38 | + /** | ||
| 39 | + * 媒体类型;群聊场景的非邀请成员靠这个字段 首次填充胶囊条 | ||
| 40 | + */ | ||
| 41 | + private Integer mediaType; | ||
| 42 | + /** | ||
| 43 | + * 发起人用户编号;群聊场景的非邀请成员靠这个字段 首次填充胶囊条 | ||
| 44 | + */ | ||
| 45 | + private Long inviterUserId; | ||
| 46 | + | ||
| 47 | + /** | ||
| 48 | + * 构造参与者加入通知;按 {@link ImRtcCallDO} 抽通话上下文,仅 userId 是变量 | ||
| 49 | + * | ||
| 50 | + * @param call 通话主表 | ||
| 51 | + * @param userId 加入的参与者用户编号 | ||
| 52 | + * @return 通知载荷 | ||
| 53 | + */ | ||
| 54 | + public static ImRtcParticipantConnectedNotification of(ImRtcCallDO call, Long userId) { | ||
| 55 | + ImRtcParticipantConnectedNotification notification = new ImRtcParticipantConnectedNotification(); | ||
| 56 | + notification.callId = call.getCallId(); | ||
| 57 | + notification.roomName = call.getRoomName(); | ||
| 58 | + notification.userId = userId; | ||
| 59 | + notification.conversationType = call.getConversationType(); | ||
| 60 | + notification.groupId = call.getGroupId(); | ||
| 61 | + notification.mediaType = call.getMediaType(); | ||
| 62 | + notification.inviterUserId = call.getInviterUserId(); | ||
| 63 | + return notification; | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | +} |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/service/websocket/dto/notification/rtc/ImRtcParticipantDisconnectedNotification.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.im.service.websocket.dto.notification.rtc; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.module.im.dal.dataobject.rtc.ImRtcCallDO; | ||
| 4 | +import lombok.Data; | ||
| 5 | + | ||
| 6 | +/** | ||
| 7 | + * RTC_PARTICIPANT_DISCONNECTED 通话参与者离开通知 | ||
| 8 | + * <p> | ||
| 9 | + * 不入库;LiveKit webhook participant_left 触发;推送范围同 {@link ImRtcParticipantConnectedNotification} | ||
| 10 | + * <p> | ||
| 11 | + * 前端 callStore 把 userId 从 joinedUserIds 移除;胶囊条人数 -1 | ||
| 12 | + * | ||
| 13 | + * @author 芋道源码 | ||
| 14 | + */ | ||
| 15 | +@Data | ||
| 16 | +public class ImRtcParticipantDisconnectedNotification { | ||
| 17 | + | ||
| 18 | + /** | ||
| 19 | + * 业务通话编号 | ||
| 20 | + */ | ||
| 21 | + private String callId; | ||
| 22 | + /** | ||
| 23 | + * LiveKit 房间名 | ||
| 24 | + */ | ||
| 25 | + private String roomName; | ||
| 26 | + /** | ||
| 27 | + * 离开的参与者用户编号 | ||
| 28 | + */ | ||
| 29 | + private Long userId; | ||
| 30 | + /** | ||
| 31 | + * 会话类型 | ||
| 32 | + */ | ||
| 33 | + private Integer conversationType; | ||
| 34 | + /** | ||
| 35 | + * 群编号;群通话场景必填 | ||
| 36 | + */ | ||
| 37 | + private Long groupId; | ||
| 38 | + | ||
| 39 | + /** | ||
| 40 | + * 构造参与者离开通知;按 {@link ImRtcCallDO} 抽通话上下文,仅 userId 是变量 | ||
| 41 | + * | ||
| 42 | + * @param call 通话主表 | ||
| 43 | + * @param userId 离开的参与者用户编号 | ||
| 44 | + * @return 通知载荷 | ||
| 45 | + */ | ||
| 46 | + public static ImRtcParticipantDisconnectedNotification of(ImRtcCallDO call, Long userId) { | ||
| 47 | + ImRtcParticipantDisconnectedNotification notification = new ImRtcParticipantDisconnectedNotification(); | ||
| 48 | + notification.callId = call.getCallId(); | ||
| 49 | + notification.roomName = call.getRoomName(); | ||
| 50 | + notification.userId = userId; | ||
| 51 | + notification.conversationType = call.getConversationType(); | ||
| 52 | + notification.groupId = call.getGroupId(); | ||
| 53 | + return notification; | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | +} |