Fix group call participant order for 60 seconds

This commit is contained in:
Ali 2021-02-23 14:15:55 +04:00
parent 27e1fa1573
commit 89eb7756e3
3 changed files with 90 additions and 34 deletions

View File

@ -718,6 +718,7 @@ public final class PresentationGroupCallImpl: PresentationGroupCall {
jsonParams: nil,
joinTimestamp: strongSelf.temporaryJoinTimestamp,
activityTimestamp: nil,
activityRank: nil,
muteState: GroupCallParticipantsContext.Participant.MuteState(canUnmute: true, mutedByYou: false),
volume: nil
))

View File

@ -116,6 +116,7 @@ public func getCurrentGroupCall(account: Account, callId: Int64, accessHash: Int
jsonParams: jsonParams,
joinTimestamp: date,
activityTimestamp: activeDate.flatMap(Double.init),
activityRank: nil,
muteState: muteState,
volume: volume
))
@ -264,6 +265,7 @@ public func getGroupCallParticipants(account: Account, callId: Int64, accessHash
jsonParams: jsonParams,
joinTimestamp: date,
activityTimestamp: activeDate.flatMap(Double.init),
activityRank: nil,
muteState: muteState,
volume: volume
))
@ -271,6 +273,8 @@ public func getGroupCallParticipants(account: Account, callId: Int64, accessHash
}
}
parsedParticipants.sort()
return GroupCallParticipantsContext.State(
participants: parsedParticipants,
nextParticipantsFetchOffset: nextParticipantsFetchOffset,
@ -311,7 +315,6 @@ public func joinGroupCall(account: Account, peerId: PeerId, callId: Int64, acces
return .generic
}
|> mapToSignal { updates -> Signal<JoinGroupCallResult, JoinGroupCallError> in
let admins: Signal<(Set<PeerId>, [Api.User]), JoinGroupCallError>
if peerId.namespace == Namespaces.Peer.CloudChannel {
admins = account.postbox.transaction { transaction -> Api.InputChannel? in
@ -592,6 +595,7 @@ public final class GroupCallParticipantsContext {
public var jsonParams: String?
public var joinTimestamp: Int32
public var activityTimestamp: Double?
public var activityRank: Int?
public var muteState: MuteState?
public var volume: Int32?
@ -601,6 +605,7 @@ public final class GroupCallParticipantsContext {
jsonParams: String?,
joinTimestamp: Int32,
activityTimestamp: Double?,
activityRank: Int?,
muteState: MuteState?,
volume: Int32?
) {
@ -609,6 +614,7 @@ public final class GroupCallParticipantsContext {
self.jsonParams = jsonParams
self.joinTimestamp = joinTimestamp
self.activityTimestamp = activityTimestamp
self.activityRank = activityRank
self.muteState = muteState
self.volume = volume
}
@ -626,6 +632,9 @@ public final class GroupCallParticipantsContext {
if lhs.activityTimestamp != rhs.activityTimestamp {
return false
}
if lhs.activityRank != rhs.activityRank {
return false
}
if lhs.muteState != rhs.muteState {
return false
}
@ -636,6 +645,16 @@ public final class GroupCallParticipantsContext {
}
public static func <(lhs: Participant, rhs: Participant) -> Bool {
if let lhsActivityRank = lhs.activityRank, let rhsActivityRank = rhs.activityRank {
if lhsActivityRank != rhsActivityRank {
return lhsActivityRank < rhsActivityRank
}
} else if lhs.activityRank != nil {
return true
} else if rhs.activityRank != nil {
return false
}
if let lhsActivityTimestamp = lhs.activityTimestamp, let rhsActivityTimestamp = rhs.activityTimestamp {
if lhsActivityTimestamp != rhsActivityTimestamp {
return lhsActivityTimestamp > rhsActivityTimestamp
@ -823,6 +842,9 @@ public final class GroupCallParticipantsContext {
private var shouldResetStateFromServer: Bool = false
private var missingSsrcs = Set<UInt32>()
private var nextActivityRank: Int = 0
private var activityRankResetTimer: SwiftSignalKit.Timer?
private let updateDefaultMuteDisposable = MetaDisposable()
public init(account: Account, peerId: PeerId, id: Int64, accessHash: Int64, state: State) {
@ -912,6 +934,36 @@ public final class GroupCallParticipantsContext {
}
}
})
self.activityRankResetTimer = SwiftSignalKit.Timer(timeout: 10.0, repeat: true, completion: { [weak self] in
guard let strongSelf = self else {
return
}
var updated = false
let timestamp = CFAbsoluteTimeGetCurrent() + NSTimeIntervalSince1970
for i in 0 ..< strongSelf.stateValue.state.participants.count {
if strongSelf.stateValue.state.participants[i].activityRank != nil {
var clearRank = false
if let activityTimestamp = strongSelf.stateValue.state.participants[i].activityTimestamp {
if activityTimestamp < timestamp - 60.0 {
clearRank = true
}
} else {
clearRank = true
}
if clearRank {
updated = true
strongSelf.stateValue.state.participants[i].activityRank = nil
}
}
}
if updated {
strongSelf.stateValue.state.participants.sort()
}
}, queue: .mainQueue())
self.activityRankResetTimer?.start()
}
deinit {
@ -919,6 +971,7 @@ public final class GroupCallParticipantsContext {
self.updatesDisposable.dispose()
self.activitiesDisposable?.dispose()
self.updateDefaultMuteDisposable.dispose()
self.activityRankResetTimer?.invalidate()
}
public func addUpdates(updates: [Update]) {
@ -937,6 +990,12 @@ public final class GroupCallParticipantsContext {
}
}
private func takeNextActivityRank() -> Int {
let value = self.nextActivityRank
self.nextActivityRank += 1
return value
}
public func reportSpeakingParticipants(ids: [PeerId: UInt32]) {
if !ids.isEmpty {
self.hasReceivedSpeakingParticipantsReport = true
@ -965,6 +1024,9 @@ public final class GroupCallParticipantsContext {
}
if updateTimestamp {
updatedParticipants[index].activityTimestamp = timestamp
if updatedParticipants[index].activityRank == nil {
updatedParticipants[index].activityRank = self.takeNextActivityRank()
}
updated = true
}
}
@ -1040,19 +1102,7 @@ public final class GroupCallParticipantsContext {
var updatedState = strongSelf.stateValue.state
var existingParticipantIds = Set<PeerId>()
for participant in updatedState.participants {
existingParticipantIds.insert(participant.peer.id)
}
for participant in state.participants {
if existingParticipantIds.contains(participant.peer.id) {
continue
}
existingParticipantIds.insert(participant.peer.id)
updatedState.participants.append(participant)
}
updatedState.participants.sort()
updatedState.participants = mergeAndSortParticipants(current: updatedState.participants, with: state.participants)
updatedState.totalCount = max(updatedState.totalCount, state.totalCount)
updatedState.version = max(updatedState.version, updatedState.version)
@ -1138,8 +1188,10 @@ public final class GroupCallParticipantsContext {
continue
}
var previousActivityTimestamp: Double?
var previousActivityRank: Int?
if let index = updatedParticipants.firstIndex(where: { $0.peer.id == participantUpdate.peerId }) {
previousActivityTimestamp = updatedParticipants[index].activityTimestamp
previousActivityRank = updatedParticipants[index].activityRank
updatedParticipants.remove(at: index)
} else if case .joined = participantUpdate.participationStatusChange {
updatedTotalCount += 1
@ -1159,6 +1211,7 @@ public final class GroupCallParticipantsContext {
jsonParams: participantUpdate.jsonParams,
joinTimestamp: participantUpdate.joinTimestamp,
activityTimestamp: activityTimestamp,
activityRank: previousActivityRank,
muteState: participantUpdate.muteState,
volume: participantUpdate.volume
)
@ -1179,14 +1232,6 @@ public final class GroupCallParticipantsContext {
let defaultParticipantsAreMuted = strongSelf.stateValue.state.defaultParticipantsAreMuted
updatedParticipants.sort()
/*for i in 0 ..< updatedParticipants.count {
if updatedParticipants[i].peer.id == strongSelf.account.peerId {
let member = updatedParticipants[i]
updatedParticipants.remove(at: i)
updatedParticipants.insert(member, at: 0)
break
}
}*/
strongSelf.stateValue = InternalState(
state: State(
@ -1344,17 +1389,7 @@ public final class GroupCallParticipantsContext {
var updatedState = strongSelf.stateValue.state
var existingParticipantIds = Set<PeerId>()
for participant in updatedState.participants {
existingParticipantIds.insert(participant.peer.id)
}
for participant in state.participants {
if existingParticipantIds.contains(participant.peer.id) {
continue
}
existingParticipantIds.insert(participant.peer.id)
updatedState.participants.append(participant)
}
updatedState.participants = mergeAndSortParticipants(current: updatedState.participants, with: state.participants)
updatedState.nextParticipantsFetchOffset = state.nextParticipantsFetchOffset
updatedState.totalCount = max(updatedState.totalCount, state.totalCount)
@ -1513,3 +1548,23 @@ public func updatedCurrentPeerGroupCall(account: Account, peerId: PeerId) -> Sig
}
}
}
private func mergeAndSortParticipants(current currentParticipants: [GroupCallParticipantsContext.Participant], with updatedParticipants: [GroupCallParticipantsContext.Participant]) -> [GroupCallParticipantsContext.Participant] {
var mergedParticipants = currentParticipants
var existingParticipantIndices: [PeerId: Int] = [:]
for i in 0 ..< mergedParticipants.count {
existingParticipantIndices[mergedParticipants[i].peer.id] = i
}
for participant in updatedParticipants {
if let _ = existingParticipantIndices[participant.peer.id] {
} else {
existingParticipantIndices[participant.peer.id] = mergedParticipants.count
mergedParticipants.append(participant)
}
}
mergedParticipants.sort()
return mergedParticipants
}

@ -1 +1 @@
Subproject commit 69e6c2b84b8d178ad2d8a5a6ca3fa09cc2be6b62
Subproject commit 0a1590245ac4c27021f6998c99ec1167ee0ef3f1