no message

This commit is contained in:
Peter
2017-06-05 13:53:22 +03:00
parent 1ed46b9c28
commit 3c92d2bb15
6 changed files with 63 additions and 27 deletions

View File

@@ -9,7 +9,7 @@ import Foundation
public func togglePeerMuted(account: Account, peerId: PeerId) -> Signal<Void, NoError> {
return account.postbox.modify { modifier -> Signal<Void, NoError> 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<Void, NoError> 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<Void, NoError> 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<Void, NoError> in
return account.postbox.modify { modifier -> Void in
modifier.updatePeerNotificationSettings([peerId: settings])
}
}
}
} else {
return .complete()

View File

@@ -148,3 +148,33 @@ public func downloadMessage(account: Account, message: MessageId) -> Signal<Mess
}
}
}
public func searchMessageIdByTimestamp(account: Account, peerId: PeerId, timestamp: Int32) -> Signal<MessageId?, NoError> {
return account.postbox.modify { modifier -> Signal<MessageId?, NoError> 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<MessageId?, NoError> in
return .single(nil)
}
} else {
return .single(nil)
}
} |> switchToLatest
}

View File

@@ -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?) {

View File

@@ -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) {

View File

@@ -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")
}

View File

@@ -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) {