From 249f3ef8d7f8a2d21f0840761de29c082a98be18 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Tue, 6 Apr 2021 14:06:40 +0300 Subject: [PATCH] Update API [skip ci] --- .../SyncCore/Sources/CachedChannelData.swift | 16 +- submodules/TelegramApi/Sources/Api4.swift | 15 ++ .../TelegramCore/Sources/GroupCalls.swift | 152 ++++++++++++++++-- .../State/AccountStateManagementUtils.swift | 4 +- .../Sources/UpdateCachedPeerData.swift | 4 +- 5 files changed, 177 insertions(+), 14 deletions(-) diff --git a/submodules/SyncCore/Sources/CachedChannelData.swift b/submodules/SyncCore/Sources/CachedChannelData.swift index 24adca7534..dff76b59aa 100644 --- a/submodules/SyncCore/Sources/CachedChannelData.swift +++ b/submodules/SyncCore/Sources/CachedChannelData.swift @@ -159,21 +159,29 @@ public final class CachedChannelData: CachedPeerData { public var id: Int64 public var accessHash: Int64 public var title: String? + public var scheduleTimestamp: Int32? + public var subscribed: Bool public init( id: Int64, accessHash: Int64, - title: String? + title: String?, + scheduleTimestamp: Int32?, + subscribed: Bool ) { self.id = id self.accessHash = accessHash self.title = title + self.scheduleTimestamp = scheduleTimestamp + self.subscribed = subscribed } public init(decoder: PostboxDecoder) { self.id = decoder.decodeInt64ForKey("id", orElse: 0) self.accessHash = decoder.decodeInt64ForKey("accessHash", orElse: 0) self.title = decoder.decodeOptionalStringForKey("title") + self.scheduleTimestamp = decoder.decodeOptionalInt32ForKey("scheduleTimestamp") + self.subscribed = decoder.decodeBoolForKey("subscribed", orElse: false) } public func encode(_ encoder: PostboxEncoder) { @@ -184,6 +192,12 @@ public final class CachedChannelData: CachedPeerData { } else { encoder.encodeNil(forKey: "title") } + if let scheduleTimestamp = self.scheduleTimestamp { + encoder.encodeInt32(scheduleTimestamp, forKey: "scheduleTimestamp") + } else { + encoder.encodeNil(forKey: "scheduleTimestamp") + } + encoder.encodeBool(self.subscribed, forKey: "subscribed") } } diff --git a/submodules/TelegramApi/Sources/Api4.swift b/submodules/TelegramApi/Sources/Api4.swift index 8728232aab..774fb67497 100644 --- a/submodules/TelegramApi/Sources/Api4.swift +++ b/submodules/TelegramApi/Sources/Api4.swift @@ -7958,6 +7958,21 @@ public extension Api { return result }) } + + public static func saveDefaultGroupCallJoinAs(peer: Api.InputPeer, joinAs: Api.InputPeer) -> (FunctionDescription, Buffer, DeserializeFunctionResponse) { + let buffer = Buffer() + buffer.appendInt32(1465786252) + peer.serialize(buffer, true) + joinAs.serialize(buffer, true) + return (FunctionDescription(name: "phone.saveDefaultGroupCallJoinAs", parameters: [("peer", peer), ("joinAs", joinAs)]), buffer, DeserializeFunctionResponse { (buffer: Buffer) -> Api.Bool? in + let reader = BufferReader(buffer) + var result: Api.Bool? + if let signature = reader.readInt32() { + result = Api.parse(reader, signature: signature) as? Api.Bool + } + return result + }) + } } } } diff --git a/submodules/TelegramCore/Sources/GroupCalls.swift b/submodules/TelegramCore/Sources/GroupCalls.swift index 93374cf163..7e8c864861 100644 --- a/submodules/TelegramCore/Sources/GroupCalls.swift +++ b/submodules/TelegramCore/Sources/GroupCalls.swift @@ -11,6 +11,7 @@ public struct GroupCallInfo: Equatable { public var clientParams: String? public var streamDcId: Int32? public var title: String? + public var scheduleTimestamp: Int32? public var recordingStartTimestamp: Int32? public var sortAscending: Bool @@ -21,6 +22,7 @@ public struct GroupCallInfo: Equatable { clientParams: String?, streamDcId: Int32?, title: String?, + scheduleTimestamp: Int32?, recordingStartTimestamp: Int32?, sortAscending: Bool ) { @@ -30,6 +32,7 @@ public struct GroupCallInfo: Equatable { self.clientParams = clientParams self.streamDcId = streamDcId self.title = title + self.scheduleTimestamp = scheduleTimestamp self.recordingStartTimestamp = recordingStartTimestamp self.sortAscending = sortAscending } @@ -58,6 +61,7 @@ extension GroupCallInfo { clientParams: clientParams, streamDcId: streamDcId, title: title, + scheduleTimestamp: scheduleDate, recordingStartTimestamp: recordStartDate, sortAscending: (flags & (1 << 6)) != 0 ) @@ -166,7 +170,7 @@ public enum CreateGroupCallError { case anonymousNotAllowed } -public func createGroupCall(account: Account, peerId: PeerId) -> Signal { +public func createGroupCall(account: Account, peerId: PeerId, title: String?, scheduleDate: Int32?) -> Signal { return account.postbox.transaction { transaction -> Api.InputPeer? in let callPeer = transaction.getPeer(peerId).flatMap(apiInputPeer) return callPeer @@ -177,7 +181,14 @@ public func createGroupCall(account: Account, peerId: PeerId) -> Signal mapError { error -> CreateGroupCallError in if error.errorDescription == "ANONYMOUS_CALLS_DISABLED" { return .anonymousNotAllowed @@ -203,9 +214,9 @@ public func createGroupCall(account: Account, peerId: PeerId) -> Signal GroupCallInfo in transaction.updatePeerCachedData(peerIds: Set([peerId]), update: { _, cachedData -> CachedPeerData? in if let cachedData = cachedData as? CachedChannelData { - return cachedData.withUpdatedActiveCall(CachedChannelData.ActiveCall(id: callInfo.id, accessHash: callInfo.accessHash, title: callInfo.title)) + return cachedData.withUpdatedActiveCall(CachedChannelData.ActiveCall(id: callInfo.id, accessHash: callInfo.accessHash, title: callInfo.title, scheduleTimestamp: callInfo.scheduleTimestamp, subscribed: false)) } else if let cachedData = cachedData as? CachedGroupData { - return cachedData.withUpdatedActiveCall(CachedChannelData.ActiveCall(id: callInfo.id, accessHash: callInfo.accessHash, title: callInfo.title)) + return cachedData.withUpdatedActiveCall(CachedChannelData.ActiveCall(id: callInfo.id, accessHash: callInfo.accessHash, title: callInfo.title, scheduleTimestamp: callInfo.scheduleTimestamp, subscribed: false)) } else { return cachedData } @@ -220,24 +231,137 @@ public func createGroupCall(account: Account, peerId: PeerId) -> Signal Signal { + return account.network.request(Api.functions.phone.startScheduledGroupCall(call: .inputGroupCall(id: callId, accessHash: accessHash))) + |> mapError { error -> StartScheduledGroupCallError in + return .generic + } + |> mapToSignal { result -> Signal in + var parsedCall: GroupCallInfo? + loop: for update in result.allUpdates { + switch update { + case let .updateGroupCall(_, call): + parsedCall = GroupCallInfo(call) + break loop + default: + break + } + } + + guard let callInfo = parsedCall else { + return .fail(.generic) + } + + return account.postbox.transaction { transaction -> GroupCallInfo in + transaction.updatePeerCachedData(peerIds: Set([peerId]), update: { _, cachedData -> CachedPeerData? in + if let cachedData = cachedData as? CachedChannelData { + return cachedData.withUpdatedActiveCall(CachedChannelData.ActiveCall(id: callInfo.id, accessHash: callInfo.accessHash, title: callInfo.title, scheduleTimestamp: callInfo.scheduleTimestamp, subscribed: false)) + } else if let cachedData = cachedData as? CachedGroupData { + return cachedData.withUpdatedActiveCall(CachedChannelData.ActiveCall(id: callInfo.id, accessHash: callInfo.accessHash, title: callInfo.title, scheduleTimestamp: callInfo.scheduleTimestamp, subscribed: false)) + } else { + return cachedData + } + }) + + account.stateManager.addUpdates(result) + + return callInfo + } + |> castError(StartScheduledGroupCallError.self) + } +} + +public enum ToggleScheduledGroupCallSubscriptionError { + case generic +} + +public func toggleScheduledGroupCallSubscription(account: Account, peerId: PeerId, callId: Int64, accessHash: Int64, subscribe: Bool) -> Signal { + return account.network.request(Api.functions.phone.toggleGroupCallStartSubscription(call: .inputGroupCall(id: callId, accessHash: accessHash), subscribed: subscribe ? .boolTrue : .boolFalse)) + |> mapError { error -> ToggleScheduledGroupCallSubscriptionError in + return .generic + } + |> mapToSignal { result -> Signal in + var parsedCall: GroupCallInfo? + loop: for update in result.allUpdates { + switch update { + case let .updateGroupCall(_, call): + parsedCall = GroupCallInfo(call) + break loop + default: + break + } + } + + guard let callInfo = parsedCall else { + return .fail(.generic) + } + + return account.postbox.transaction { transaction in + transaction.updatePeerCachedData(peerIds: Set([peerId]), update: { _, cachedData -> CachedPeerData? in + if let cachedData = cachedData as? CachedChannelData { + return cachedData.withUpdatedActiveCall(CachedChannelData.ActiveCall(id: callInfo.id, accessHash: callInfo.accessHash, title: callInfo.title, scheduleTimestamp: callInfo.scheduleTimestamp, subscribed: subscribe)) + } else if let cachedData = cachedData as? CachedGroupData { + return cachedData.withUpdatedActiveCall(CachedChannelData.ActiveCall(id: callInfo.id, accessHash: callInfo.accessHash, title: callInfo.title, scheduleTimestamp: callInfo.scheduleTimestamp, subscribed: subscribe)) + } else { + return cachedData + } + }) + + account.stateManager.addUpdates(result) + } + |> castError(ToggleScheduledGroupCallSubscriptionError.self) + } +} + +public enum UpdateGroupCallJoinAsPeerError { + case generic +} + +public func updateGroupCallJoinAsPeer(account: Account, peerId: PeerId, joinAs: PeerId) -> Signal { + return account.postbox.transaction { transaction -> (Api.InputPeer, Api.InputPeer)? in + if let peer = transaction.getPeer(peerId), let joinAsPeer = transaction.getPeer(joinAs), let inputPeer = apiInputPeer(peer), let joinInputPeer = apiInputPeer(joinAsPeer) { + return (inputPeer, joinInputPeer) + } else { + return nil + } + } + |> castError(UpdateGroupCallJoinAsPeerError.self) + |> mapToSignal { result in + guard let (peer, joinAs) = result else { + return .fail(.generic) + } + return account.network.request(Api.functions.phone.saveDefaultGroupCallJoinAs(peer: peer, joinAs: joinAs)) + |> mapError { _ -> UpdateGroupCallJoinAsPeerError in + return .generic + } + |> mapToSignal { result -> Signal in + return .complete() + } + } +} + public enum GetGroupCallParticipantsError { case generic } public func getGroupCallParticipants(account: Account, callId: Int64, accessHash: Int64, offset: String, ssrcs: [UInt32], limit: Int32, sortAscending: Bool?) -> Signal { - let sortAscendingValue: Signal + let sortAscendingValue: Signal<(Bool, Int32?), GetGroupCallParticipantsError> if let sortAscending = sortAscending { - sortAscendingValue = .single(sortAscending) + sortAscendingValue = .single((sortAscending, nil)) } else { sortAscendingValue = getCurrentGroupCall(account: account, callId: callId, accessHash: accessHash) |> mapError { _ -> GetGroupCallParticipantsError in return .generic } - |> mapToSignal { result -> Signal in + |> mapToSignal { result -> Signal<(Bool, Int32?), GetGroupCallParticipantsError> in guard let result = result else { return .fail(.generic) } - return .single(result.info.sortAscending) + return .single((result.info.sortAscending, result.info.scheduleTimestamp)) } } @@ -248,13 +372,15 @@ public func getGroupCallParticipants(account: Account, callId: Int64, accessHash }, sortAscendingValue ) - |> mapToSignal { result, sortAscendingValue -> Signal in + |> mapToSignal { result, sortAscendingAndScheduleTimestamp -> Signal in return account.postbox.transaction { transaction -> GroupCallParticipantsContext.State in var parsedParticipants: [GroupCallParticipantsContext.Participant] = [] let totalCount: Int let version: Int32 let nextParticipantsFetchOffset: String? + let (sortAscendingValue, scheduleTimestamp) = sortAscendingAndScheduleTimestamp + switch result { case let .groupParticipants(count, participants, nextOffset, chats, users, apiVersion): totalCount = Int(count) @@ -341,6 +467,7 @@ public func getGroupCallParticipants(account: Account, callId: Int64, accessHash sortAscending: sortAscendingValue, recordingStartTimestamp: nil, title: nil, + scheduleTimestamp: scheduleTimestamp, totalCount: totalCount, version: version ) @@ -471,6 +598,7 @@ public func joinGroupCall(account: Account, peerId: PeerId, joinAs: PeerId?, cal state.defaultParticipantsAreMuted = GroupCallParticipantsContext.State.DefaultParticipantsAreMuted(isMuted: isMuted, canChange: canChange) state.title = title state.recordingStartTimestamp = recordStartDate + state.scheduleTimestamp = scheduleDate default: break } @@ -858,6 +986,7 @@ public final class GroupCallParticipantsContext { public var sortAscending: Bool public var recordingStartTimestamp: Int32? public var title: String? + public var scheduleTimestamp: Int32? public var totalCount: Int public var version: Int32 @@ -1146,6 +1275,7 @@ public final class GroupCallParticipantsContext { sortAscending: strongSelf.stateValue.state.sortAscending, recordingStartTimestamp: strongSelf.stateValue.state.recordingStartTimestamp, title: strongSelf.stateValue.state.title, + scheduleTimestamp: strongSelf.stateValue.state.scheduleTimestamp, totalCount: strongSelf.stateValue.state.totalCount, version: strongSelf.stateValue.state.version ), @@ -1278,6 +1408,7 @@ public final class GroupCallParticipantsContext { sortAscending: strongSelf.stateValue.state.sortAscending, recordingStartTimestamp: strongSelf.stateValue.state.recordingStartTimestamp, title: strongSelf.stateValue.state.title, + scheduleTimestamp: strongSelf.stateValue.state.scheduleTimestamp, totalCount: strongSelf.stateValue.state.totalCount, version: strongSelf.stateValue.state.version ), @@ -1493,6 +1624,7 @@ public final class GroupCallParticipantsContext { let defaultParticipantsAreMuted = strongSelf.stateValue.state.defaultParticipantsAreMuted let recordingStartTimestamp = strongSelf.stateValue.state.recordingStartTimestamp let title = strongSelf.stateValue.state.title + let scheduleTimestamp = strongSelf.stateValue.state.scheduleTimestamp updatedParticipants.sort(by: { GroupCallParticipantsContext.Participant.compare(lhs: $0, rhs: $1, sortAscending: strongSelf.stateValue.state.sortAscending) }) @@ -1506,6 +1638,7 @@ public final class GroupCallParticipantsContext { sortAscending: strongSelf.stateValue.state.sortAscending, recordingStartTimestamp: recordingStartTimestamp, title: title, + scheduleTimestamp: scheduleTimestamp, totalCount: updatedTotalCount, version: update.version ), @@ -1539,6 +1672,7 @@ public final class GroupCallParticipantsContext { state.defaultParticipantsAreMuted = strongSelf.stateValue.state.defaultParticipantsAreMuted state.title = strongSelf.stateValue.state.title state.recordingStartTimestamp = strongSelf.stateValue.state.recordingStartTimestamp + state.scheduleTimestamp = strongSelf.stateValue.state.scheduleTimestamp state.mergeActivity(from: strongSelf.stateValue.state, myPeerId: nil, previousMyPeerId: nil, mergeActivityTimestamps: false) strongSelf.stateValue.state = state strongSelf.endedProcessingUpdate() diff --git a/submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift b/submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift index d826b2ce17..5cac66545f 100644 --- a/submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift +++ b/submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift @@ -2982,9 +2982,9 @@ func replayFinalState(accountManager: AccountManager, postbox: Postbox, accountP if let info = GroupCallInfo(call) { transaction.updatePeerCachedData(peerIds: Set([peerId]), update: { _, current in if let current = current as? CachedChannelData { - return current.withUpdatedActiveCall(CachedChannelData.ActiveCall(id: info.id, accessHash: info.accessHash, title: info.title)) + return current.withUpdatedActiveCall(CachedChannelData.ActiveCall(id: info.id, accessHash: info.accessHash, title: info.title, scheduleTimestamp: info.scheduleTimestamp, subscribed: false)) } else if let current = current as? CachedGroupData { - return current.withUpdatedActiveCall(CachedChannelData.ActiveCall(id: info.id, accessHash: info.accessHash, title: info.title)) + return current.withUpdatedActiveCall(CachedChannelData.ActiveCall(id: info.id, accessHash: info.accessHash, title: info.title, scheduleTimestamp: info.scheduleTimestamp, subscribed: false)) } else { return current } diff --git a/submodules/TelegramCore/Sources/UpdateCachedPeerData.swift b/submodules/TelegramCore/Sources/UpdateCachedPeerData.swift index 9e74ccb9ff..7ebfa241a6 100644 --- a/submodules/TelegramCore/Sources/UpdateCachedPeerData.swift +++ b/submodules/TelegramCore/Sources/UpdateCachedPeerData.swift @@ -306,7 +306,7 @@ public func fetchAndUpdateCachedPeerData(accountPeerId: PeerId, peerId rawPeerId if let inputCall = chatFull.call { switch inputCall { case let .inputGroupCall(id, accessHash): - updatedActiveCall = CachedChannelData.ActiveCall(id: id, accessHash: accessHash, title: previous.activeCall?.title) + updatedActiveCall = CachedChannelData.ActiveCall(id: id, accessHash: accessHash, title: previous.activeCall?.title, scheduleTimestamp: previous.activeCall?.scheduleTimestamp, subscribed: previous.activeCall?.subscribed ?? false) } } @@ -516,7 +516,7 @@ public func fetchAndUpdateCachedPeerData(accountPeerId: PeerId, peerId rawPeerId if let inputCall = inputCall { switch inputCall { case let .inputGroupCall(id, accessHash): - updatedActiveCall = CachedChannelData.ActiveCall(id: id, accessHash: accessHash, title: previous.activeCall?.title) + updatedActiveCall = CachedChannelData.ActiveCall(id: id, accessHash: accessHash, title: previous.activeCall?.title, scheduleTimestamp: previous.activeCall?.scheduleTimestamp, subscribed: previous.activeCall?.subscribed ?? false) } }