From 01a434f3d9d17f1a38001ae0f2f2402816b2f3d8 Mon Sep 17 00:00:00 2001 From: Peter <> Date: Mon, 29 Apr 2019 18:05:52 +0400 Subject: [PATCH] Support latest API --- TelegramCore.xcodeproj/project.pbxproj | 6 +++ TelegramCore/Account.swift | 2 + .../AccountStateManagementUtils.swift | 8 ++++ ...ManagedNotificationSettingsBehaviors.swift | 39 +++++++++++++++++++ .../TelegramPeerNotificationSettings.swift | 8 ++++ 5 files changed, 63 insertions(+) create mode 100644 TelegramCore/ManagedNotificationSettingsBehaviors.swift diff --git a/TelegramCore.xcodeproj/project.pbxproj b/TelegramCore.xcodeproj/project.pbxproj index 35547ac657..3014ffd766 100644 --- a/TelegramCore.xcodeproj/project.pbxproj +++ b/TelegramCore.xcodeproj/project.pbxproj @@ -422,6 +422,8 @@ D067066D1D512ADB00DED3E3 /* SwiftSignalKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D06706681D512ADB00DED3E3 /* SwiftSignalKit.framework */; }; D06AFE8920BF66BF00EA5124 /* DeserializeFunctionResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = D06AFE8820BF66BF00EA5124 /* DeserializeFunctionResponse.swift */; }; D06AFE8A20BF66BF00EA5124 /* DeserializeFunctionResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = D06AFE8820BF66BF00EA5124 /* DeserializeFunctionResponse.swift */; }; + D06CA13522772EB20094E707 /* ManagedNotificationSettingsBehaviors.swift in Sources */ = {isa = PBXBuildFile; fileRef = D06CA13422772EB20094E707 /* ManagedNotificationSettingsBehaviors.swift */; }; + D06CA13622772EB20094E707 /* ManagedNotificationSettingsBehaviors.swift in Sources */ = {isa = PBXBuildFile; fileRef = D06CA13422772EB20094E707 /* ManagedNotificationSettingsBehaviors.swift */; }; D06ECFC820B810D300C576C2 /* TermsOfService.swift in Sources */ = {isa = PBXBuildFile; fileRef = D06ECFC720B810D300C576C2 /* TermsOfService.swift */; }; D06ECFC920B810D300C576C2 /* TermsOfService.swift in Sources */ = {isa = PBXBuildFile; fileRef = D06ECFC720B810D300C576C2 /* TermsOfService.swift */; }; D07047B41F3DF1FE00F6A8D4 /* ConsumablePersonalMentionMessageAttribute.swift in Sources */ = {isa = PBXBuildFile; fileRef = D07047B31F3DF1FE00F6A8D4 /* ConsumablePersonalMentionMessageAttribute.swift */; }; @@ -1043,6 +1045,7 @@ D06706681D512ADB00DED3E3 /* SwiftSignalKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = SwiftSignalKit.framework; sourceTree = BUILT_PRODUCTS_DIR; }; D067066E1D512AEB00DED3E3 /* MtProtoKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = MtProtoKit.framework; sourceTree = BUILT_PRODUCTS_DIR; }; D06AFE8820BF66BF00EA5124 /* DeserializeFunctionResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeserializeFunctionResponse.swift; sourceTree = ""; }; + D06CA13422772EB20094E707 /* ManagedNotificationSettingsBehaviors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ManagedNotificationSettingsBehaviors.swift; sourceTree = ""; }; D06ECFC720B810D300C576C2 /* TermsOfService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TermsOfService.swift; sourceTree = ""; }; D07047B31F3DF1FE00F6A8D4 /* ConsumablePersonalMentionMessageAttribute.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConsumablePersonalMentionMessageAttribute.swift; sourceTree = ""; }; D07047B61F3DF2CD00F6A8D4 /* ManagedConsumePersonalMessagesActions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ManagedConsumePersonalMessagesActions.swift; sourceTree = ""; }; @@ -1578,6 +1581,7 @@ 093857A72243D87900EB6A54 /* SynchronizeEmojiKeywordsOperation.swift */, 093857A62243D87800EB6A54 /* ManagedSynchronizeEmojiKeywordsOperations.swift */, D0CA8E4A227209C4008A74C3 /* ManagedSynchronizeGroupMessageStats.swift */, + D06CA13422772EB20094E707 /* ManagedNotificationSettingsBehaviors.swift */, ); name = State; sourceTree = ""; @@ -2502,6 +2506,7 @@ D0C27B421F4B58C000A4E170 /* PeerSpecificStickerPack.swift in Sources */, D054648B2073854A002ECC1E /* SecureIdPersonalDetailsValue.swift in Sources */, D01749591E1092BC0057C89A /* RequestStartBot.swift in Sources */, + D06CA13522772EB20094E707 /* ManagedNotificationSettingsBehaviors.swift in Sources */, D03DC9101F82E344001D584C /* AccountStateReset.swift in Sources */, D01B27A21E394D8B0022A4C0 /* PrivacySettings.swift in Sources */, D0223A9B1EA5654D00211D94 /* TelegramMediaResource.swift in Sources */, @@ -2738,6 +2743,7 @@ D0B8442B1DAB91E0005F29E1 /* NBMetadataCore.m in Sources */, D093D7EF206413F600BC3599 /* SecureIdDataTypes.swift in Sources */, D03DC9141F82F89D001D584C /* RegularChatState.swift in Sources */, + D06CA13622772EB20094E707 /* ManagedNotificationSettingsBehaviors.swift in Sources */, D0C44B621FC616E200227BE0 /* SearchGroupMembers.swift in Sources */, D0F3A8A61E82C94C00B4C64C /* SynchronizeableChatInputState.swift in Sources */, D00BDA1A1EE593D600C64C5E /* TelegramChannelAdminRights.swift in Sources */, diff --git a/TelegramCore/Account.swift b/TelegramCore/Account.swift index e10c6c4dc1..2b2a0abbc1 100644 --- a/TelegramCore/Account.swift +++ b/TelegramCore/Account.swift @@ -1209,6 +1209,8 @@ public class Account { self.managedOperationsDisposable.add(managedPendingPeerNotificationSettings(postbox: self.postbox, network: self.network).start()) self.managedOperationsDisposable.add(managedSynchronizeAppLogEventsOperations(postbox: self.postbox, network: self.network).start()) + self.managedOperationsDisposable.add(managedNotificationSettingsBehaviors(postbox: self.postbox).start()) + let mediaBox = postbox.mediaBox self.storageSettingsDisposable = accountManager.sharedData(keys: [SharedDataKeys.cacheStorageSettings]).start(next: { [weak mediaBox] sharedData in guard let mediaBox = mediaBox else { diff --git a/TelegramCore/AccountStateManagementUtils.swift b/TelegramCore/AccountStateManagementUtils.swift index 47ef6f5e14..23fb21bc85 100644 --- a/TelegramCore/AccountStateManagementUtils.swift +++ b/TelegramCore/AccountStateManagementUtils.swift @@ -2680,6 +2680,14 @@ func replayFinalState(accountManager: AccountManager, postbox: Postbox, accountP if processSecretChatIncomingEncryptedOperations(transaction: transaction, peerId: peerId) { let processResult = processSecretChatIncomingDecryptedOperations(mediaBox: mediaBox, transaction: transaction, peerId: peerId) if !processResult.addedMessages.isEmpty { + let currentInclusion = transaction.getPeerChatListInclusion(peerId) + if let groupId = currentInclusion.groupId, groupId == Namespaces.PeerGroup.archive { + if let peer = transaction.getPeer(peerId) as? TelegramSecretChat { + if let notificationSettings = transaction.getPeerNotificationSettings(peer.regularPeerId) as? TelegramPeerNotificationSettings, !notificationSettings.isRemovedFromTotalUnreadCount { + transaction.updatePeerChatListInclusion(peerId, inclusion: currentInclusion.withGroupId(groupId: .root)) + } + } + } for message in processResult.addedMessages { if case let .Id(id) = message.id { addedSecretMessageIds.append(id) diff --git a/TelegramCore/ManagedNotificationSettingsBehaviors.swift b/TelegramCore/ManagedNotificationSettingsBehaviors.swift new file mode 100644 index 0000000000..828a4d46c4 --- /dev/null +++ b/TelegramCore/ManagedNotificationSettingsBehaviors.swift @@ -0,0 +1,39 @@ +import Foundation +#if os(macOS) +import PostboxMac +import SwiftSignalKitMac +#else +import Postbox +import SwiftSignalKit +#endif + +func managedNotificationSettingsBehaviors(postbox: Postbox) -> Signal { + return postbox.combinedView(keys: [.peerNotificationSettingsBehaviorTimestampView]) + |> mapToSignal { views -> Signal in + guard let view = views.views[.peerNotificationSettingsBehaviorTimestampView] as? PeerNotificationSettingsBehaviorTimestampView else { + return .complete() + } + guard let earliestTimestamp = view.earliestTimestamp else { + return .complete() + } + + let checkSignal = postbox.transaction { transaction -> Void in + let timestamp = Int32(CFAbsoluteTimeGetCurrent() + NSTimeIntervalSince1970) + for (peerId, notificationSettings) in transaction.getPeerIdsAndNotificationSettingsWithBehaviorTimestampLessThanOrEqualTo(timestamp) { + if let notificationSettings = notificationSettings as? TelegramPeerNotificationSettings { + if case let .muted(untilTimestamp) = notificationSettings.muteState, untilTimestamp <= timestamp { + transaction.updateCurrentPeerNotificationSettings([peerId: notificationSettings.withUpdatedMuteState(.unmuted)]) + } + } + } + } + |> ignoreValues + + let timeout = earliestTimestamp - Int32(CFAbsoluteTimeGetCurrent() + NSTimeIntervalSince1970) + if timeout <= 0 { + return checkSignal + } else { + return checkSignal |> delay(Double(timeout), queue: .mainQueue()) + } + } +} diff --git a/TelegramCore/TelegramPeerNotificationSettings.swift b/TelegramCore/TelegramPeerNotificationSettings.swift index fa51bbfed0..fe3aab1734 100644 --- a/TelegramCore/TelegramPeerNotificationSettings.swift +++ b/TelegramCore/TelegramPeerNotificationSettings.swift @@ -129,6 +129,14 @@ public final class TelegramPeerNotificationSettings: PeerNotificationSettings, E } } + public var behavior: PeerNotificationSettingsBehavior { + if case let .muted(untilTimestamp) = self.muteState, untilTimestamp < Int32.max { + return .reset(atTimestamp: untilTimestamp, toValue: self.withUpdatedMuteState(.unmuted)) + } else { + return .none + } + } + public init(muteState: PeerMuteState, messageSound: PeerMessageSound) { self.muteState = muteState self.messageSound = messageSound