diff --git a/lib/services/audio/rtc_manager.dart b/lib/services/audio/rtc_manager.dart index 6e00c7d..f1fe40d 100644 --- a/lib/services/audio/rtc_manager.dart +++ b/lib/services/audio/rtc_manager.dart @@ -523,6 +523,30 @@ class RealTimeCommunicationManager extends ChangeNotifier { return currentUserId.isNotEmpty && currentUserId == userId.trim(); } + Set _currentUserIdentityKeys() { + return _userProfileIdentityKeys( + AccountStorage().getCurrentUser()?.userProfile, + ); + } + + Set _userProfileIdentityKeys(SocialChatUserProfile? user) { + return { + user?.id?.trim() ?? "", + user?.account?.trim() ?? "", + user?.getID().trim() ?? "", + }..removeWhere((item) => item.isEmpty); + } + + bool _userProfileMatchesAnyIdentity( + SocialChatUserProfile? user, + Set identityKeys, + ) { + if (identityKeys.isEmpty) { + return false; + } + return _userProfileIdentityKeys(user).any(identityKeys.contains); + } + ///房间红包列表 List redPacketList = []; @@ -5418,14 +5442,15 @@ class RealTimeCommunicationManager extends ChangeNotifier { ///自己是否在麦上 bool isOnMai() { - final currentUserId = - (AccountStorage().getCurrentUser()?.userProfile?.id ?? "").trim(); - if (currentUserId.isEmpty) { + final currentUserKeys = _currentUserIdentityKeys(); + if (currentUserKeys.isEmpty) { return false; } for (final entry in roomWheatMap.entries) { - if ((micAtIndexForDisplay(entry.key)?.user?.id ?? "").trim() == - currentUserId) { + if (_userProfileMatchesAnyIdentity( + micAtIndexForDisplay(entry.key)?.user, + currentUserKeys, + )) { return true; } } @@ -5434,25 +5459,39 @@ class RealTimeCommunicationManager extends ChangeNotifier { ///自己是否在指定的麦上 bool isOnMaiInIndex(num index) { - return (micAtIndexForDisplay(index)?.user?.id ?? "").trim() == - (AccountStorage().getCurrentUser()?.userProfile?.id ?? "").trim(); + return _userProfileMatchesAnyIdentity( + micAtIndexForDisplay(index)?.user, + _currentUserIdentityKeys(), + ); } ///点击的用户在哪个麦上 num userOnMaiInIndex(String userId) { + return _findUserOnMaiInIndex(userId, useDisplaySeatForCurrentUser: true); + } + + num _findUserOnMaiInIndex( + String userId, { + required bool useDisplaySeatForCurrentUser, + }) { final normalizedUserId = userId.trim(); if (normalizedUserId.isEmpty) { return -1; } num index = -1; + final currentUserKeys = _currentUserIdentityKeys(); + final isCurrentUserQuery = currentUserKeys.contains(normalizedUserId); + final lookupKeys = {normalizedUserId}; + if (isCurrentUserQuery) { + lookupKeys.addAll(currentUserKeys); + } + lookupKeys.removeWhere((item) => item.isEmpty); roomWheatMap.forEach((k, value) { - final visibleSeat = - normalizedUserId == - (AccountStorage().getCurrentUser()?.userProfile?.id ?? "") - .trim() + final candidateSeat = + useDisplaySeatForCurrentUser && isCurrentUserQuery ? micAtIndexForDisplay(k) : value; - if ((visibleSeat?.user?.id ?? "").trim() == normalizedUserId) { + if (_userProfileMatchesAnyIdentity(candidateSeat?.user, lookupKeys)) { index = k; } }); diff --git a/test/rtc_self_mic_snapshot_test.dart b/test/rtc_self_mic_snapshot_test.dart index e767f29..0f473af 100644 --- a/test/rtc_self_mic_snapshot_test.dart +++ b/test/rtc_self_mic_snapshot_test.dart @@ -30,4 +30,33 @@ void main() { expect(rtcProvider.roomWheatMap[3]?.user?.id, 'user-1'); expect(rtcProvider.isOnMai(), isTrue); }); + + test('self mic lookup matches account when mic user id differs', () { + SharedPreferences.setMockInitialValues({}); + AccountStorage().setCurrentUser( + SocialChatLoginRes( + userProfile: SocialChatUserProfile( + id: 'local-user-id', + account: 'account-1', + userNickname: 'Me', + ), + ), + ); + final rtcProvider = + RealTimeCommunicationManager() + ..roomWheatMap = { + 2: MicRes( + micIndex: 2, + user: SocialChatUserProfile( + id: 'mic-user-id', + account: 'account-1', + userNickname: 'Me', + ), + ), + }; + + expect(rtcProvider.isOnMai(), isTrue); + expect(rtcProvider.isOnMaiInIndex(2), isTrue); + expect(rtcProvider.userOnMaiInIndex('local-user-id'), 2); + }); }