diff --git a/TelegramCore/ChangePeerNotificationSettings.swift b/TelegramCore/ChangePeerNotificationSettings.swift index dc2445316e..bf07cb0e4d 100644 --- a/TelegramCore/ChangePeerNotificationSettings.swift +++ b/TelegramCore/ChangePeerNotificationSettings.swift @@ -9,7 +9,7 @@ import Foundation public func togglePeerMuted(account: Account, peerId: PeerId) -> Signal { return account.postbox.modify { modifier -> Signal in - if let peer = modifier.getPeer(peerId), let inputPeer = apiInputPeer(peer) { + if let peer = modifier.getPeer(peerId) { let currentSettings = modifier.getPeerNotificationSettings(peerId) as? TelegramPeerNotificationSettings let previousSettings: TelegramPeerNotificationSettings if let currentSettings = currentSettings { @@ -36,29 +36,35 @@ public func changePeerNotificationSettings(account: Account, peerId: PeerId, set return account.postbox.loadedPeerWithId(peerId) |> mapToSignal { peer -> Signal in if let inputPeer = apiInputPeer(peer) { - let muteUntil: Int32 - switch settings.muteState { - case let .muted(until): - muteUntil = until - case .unmuted: - muteUntil = 0 - } - let sound: String - switch settings.messageSound { - case .none: - sound = "" - case let .bundledModern(id): - sound = "\(id)" - case let .bundledClassic(id): - sound = "\(id + 12)" - } - let inputSettings = Api.InputPeerNotifySettings.inputPeerNotifySettings(flags: Int32(1 << 0), muteUntil: muteUntil, sound: sound) - return account.network.request(Api.functions.account.updateNotifySettings(peer: .inputNotifyPeer(peer: inputPeer), settings: inputSettings)) + return account.network.request(Api.functions.account.getNotifySettings(peer: .inputNotifyPeer(peer: inputPeer))) |> retryRequest |> mapToSignal { result -> Signal in - return account.postbox.modify { modifier -> Void in - modifier.updatePeerNotificationSettings([peerId: settings]) + let current = TelegramPeerNotificationSettings(apiSettings: result) + + let muteUntil: Int32 + switch settings.muteState { + case let .muted(until): + muteUntil = until + case .unmuted: + muteUntil = 0 } + let sound: String + switch current.messageSound { + case .none: + sound = "" + case let .bundledModern(id): + sound = "\(id)" + case let .bundledClassic(id): + sound = "\(id + 12)" + } + let inputSettings = Api.InputPeerNotifySettings.inputPeerNotifySettings(flags: Int32(1 << 0), muteUntil: muteUntil, sound: sound) + return account.network.request(Api.functions.account.updateNotifySettings(peer: .inputNotifyPeer(peer: inputPeer), settings: inputSettings)) + |> retryRequest + |> mapToSignal { result -> Signal in + return account.postbox.modify { modifier -> Void in + modifier.updatePeerNotificationSettings([peerId: settings]) + } + } } } else { return .complete() diff --git a/TelegramCore/SearchMessages.swift b/TelegramCore/SearchMessages.swift index de0e461bcb..8b8bac2b7d 100644 --- a/TelegramCore/SearchMessages.swift +++ b/TelegramCore/SearchMessages.swift @@ -148,3 +148,33 @@ public func downloadMessage(account: Account, message: MessageId) -> Signal Signal { + return account.postbox.modify { modifier -> Signal in + if let peer = modifier.getPeer(peerId), let inputPeer = apiInputPeer(peer) { + return account.network.request(Api.functions.messages.getHistory(peer: inputPeer, offsetId: 0, offsetDate: timestamp, addOffset: -1, limit: 1, maxId: 0, minId: 0)) + |> map { result -> MessageId? in + let messages: [Api.Message] + switch result { + case let .messages(apiMessages, _, _): + messages = apiMessages + case let .channelMessages(_, _, _, apiMessages, _, _): + messages = apiMessages + case let.messagesSlice(_, apiMessages, _, _): + messages = apiMessages + } + for message in messages { + if let message = StoreMessage(apiMessage: message), case let .Id(id) = message.id { + return id + } + } + return nil + } + |> `catch` { _ -> Signal in + return .single(nil) + } + } else { + return .single(nil) + } + } |> switchToLatest +} diff --git a/TelegramCore/TelegramChannel.swift b/TelegramCore/TelegramChannel.swift index b2e9fc34d5..32046da4cb 100644 --- a/TelegramCore/TelegramChannel.swift +++ b/TelegramCore/TelegramChannel.swift @@ -190,7 +190,7 @@ public final class TelegramChannel: Peer { return .title(title: self.title, addressName: self.username) } - public let associatedPeerIds: [PeerId]? = nil + public let associatedPeerId: PeerId? = nil public let notificationSettingsPeerId: PeerId? = nil public init(id: PeerId, accessHash: Int64?, title: String, username: String?, photo: [TelegramMediaImageRepresentation], creationDate: Int32, version: Int32, participationStatus: TelegramChannelParticipationStatus, role: TelegramChannelRole, info: TelegramChannelInfo, flags: TelegramChannelFlags, restrictionInfo: PeerAccessRestrictionInfo?) { diff --git a/TelegramCore/TelegramGroup.swift b/TelegramCore/TelegramGroup.swift index f102201ef8..6db83f8d56 100644 --- a/TelegramCore/TelegramGroup.swift +++ b/TelegramCore/TelegramGroup.swift @@ -57,7 +57,7 @@ public final class TelegramGroup: Peer { return .title(title: self.title, addressName: nil) } - public let associatedPeerIds: [PeerId]? = nil + public let associatedPeerId: PeerId? = nil public let notificationSettingsPeerId: PeerId? = nil public init(id: PeerId, title: String, photo: [TelegramMediaImageRepresentation], participantCount: Int, role: TelegramGroupRole, membership: TelegramGroupMembership, flags: TelegramGroupFlags, migrationReference: TelegramGroupToChannelMigrationReference?, creationDate: Int32, version: Int) { diff --git a/TelegramCore/TelegramSecretChat.swift b/TelegramCore/TelegramSecretChat.swift index 2abed5fd3d..af1c83efee 100644 --- a/TelegramCore/TelegramSecretChat.swift +++ b/TelegramCore/TelegramSecretChat.swift @@ -18,7 +18,7 @@ public final class TelegramSecretChat: Peer { return .title(title: "", addressName: nil) } - public let associatedPeerIds: [PeerId]? + public let associatedPeerId: PeerId? public let notificationSettingsPeerId: PeerId? public init(id: PeerId, creationDate: Int32, regularPeerId: PeerId, accessHash: Int64, role: SecretChatRole, embeddedState: SecretChatEmbeddedPeerState, messageAutoremoveTimeout: Int32?) { @@ -28,7 +28,7 @@ public final class TelegramSecretChat: Peer { self.creationDate = creationDate self.role = role self.embeddedState = embeddedState - self.associatedPeerIds = [regularPeerId] + self.associatedPeerId = regularPeerId self.notificationSettingsPeerId = regularPeerId self.messageAutoremoveTimeout = messageAutoremoveTimeout } @@ -41,7 +41,7 @@ public final class TelegramSecretChat: Peer { self.creationDate = decoder.decodeInt32ForKey("d", orElse: 0) self.role = SecretChatRole(rawValue: decoder.decodeInt32ForKey("o", orElse: 0))! self.embeddedState = SecretChatEmbeddedPeerState(rawValue: decoder.decodeInt32ForKey("s", orElse: 0))! - self.associatedPeerIds = [self.regularPeerId] + self.associatedPeerId = self.regularPeerId self.messageAutoremoveTimeout = decoder.decodeOptionalInt32ForKey("at") } diff --git a/TelegramCore/TelegramUser.swift b/TelegramCore/TelegramUser.swift index d97ab17f51..8f1a3b4853 100644 --- a/TelegramCore/TelegramUser.swift +++ b/TelegramCore/TelegramUser.swift @@ -92,7 +92,7 @@ public final class TelegramUser: Peer { return .personName(first: self.firstName ?? "", last: self.lastName ?? "", addressName: self.username) } - public let associatedPeerIds: [PeerId]? = nil + public let associatedPeerId: PeerId? = nil public let notificationSettingsPeerId: PeerId? = nil public init(id: PeerId, accessHash: Int64?, firstName: String?, lastName: String?, username: String?, phone: String?, photo: [TelegramMediaImageRepresentation], botInfo: BotUserInfo?, flags: UserInfoFlags) {