* 'master' of https://github.com/peter-iakovlev/TelegramCore:
  no message
This commit is contained in:
overtake 2017-09-27 16:26:38 +03:00
commit 33c6d65e5c
10 changed files with 134 additions and 63 deletions

View File

@ -245,6 +245,7 @@ private var declaredEncodables: Void = {
declareEncodable(SavedStickerItem.self, f: { SavedStickerItem(decoder: $0) }) declareEncodable(SavedStickerItem.self, f: { SavedStickerItem(decoder: $0) })
declareEncodable(ConsumablePersonalMentionMessageAttribute.self, f: { ConsumablePersonalMentionMessageAttribute(decoder: $0) }) declareEncodable(ConsumablePersonalMentionMessageAttribute.self, f: { ConsumablePersonalMentionMessageAttribute(decoder: $0) })
declareEncodable(ConsumePersonalMessageAction.self, f: { ConsumePersonalMessageAction(decoder: $0) }) declareEncodable(ConsumePersonalMessageAction.self, f: { ConsumePersonalMessageAction(decoder: $0) })
declareEncodable(CachedStickerPack.self, f: { CachedStickerPack(decoder: $0) })
return return
}() }()

View File

@ -681,6 +681,11 @@ public final class AccountStateManager {
} }
public func messageForNotification(modifier: Modifier, id: MessageId, alwaysReturnMessage: Bool) -> (message: Message?, notify: Bool, sound: PeerMessageSound, displayContents: Bool) { public func messageForNotification(modifier: Modifier, id: MessageId, alwaysReturnMessage: Bool) -> (message: Message?, notify: Bool, sound: PeerMessageSound, displayContents: Bool) {
guard let message = modifier.getMessage(id) else {
Logger.shared.log("AccountStateManager", "notification message doesn't exist")
return (nil, false, .bundledModern(id: 0), false)
}
var notify = true var notify = true
var sound: PeerMessageSound = .bundledModern(id: 0) var sound: PeerMessageSound = .bundledModern(id: 0)
var displayContents = true var displayContents = true
@ -691,6 +696,9 @@ public func messageForNotification(modifier: Modifier, id: MessageId, alwaysRetu
if let peer = modifier.getPeer(id.peerId), let associatedPeerId = peer.associatedPeerId { if let peer = modifier.getPeer(id.peerId), let associatedPeerId = peer.associatedPeerId {
notificationPeerId = associatedPeerId notificationPeerId = associatedPeerId
} }
if message.personal, let author = message.author {
notificationPeerId = author.id
}
if let notificationSettings = modifier.getPeerNotificationSettings(notificationPeerId) as? TelegramPeerNotificationSettings { if let notificationSettings = modifier.getPeerNotificationSettings(notificationPeerId) as? TelegramPeerNotificationSettings {
switch notificationSettings.muteState { switch notificationSettings.muteState {
@ -702,14 +710,18 @@ public func messageForNotification(modifier: Modifier, id: MessageId, alwaysRetu
break break
} }
var defaultSound: PeerMessageSound = .bundledModern(id: 0) var defaultSound: PeerMessageSound = .bundledModern(id: 0)
var defaultNotify: Bool = true
if let globalNotificationSettings = modifier.getPreferencesEntry(key: PreferencesKeys.globalNotifications) as? GlobalNotificationSettings { if let globalNotificationSettings = modifier.getPreferencesEntry(key: PreferencesKeys.globalNotifications) as? GlobalNotificationSettings {
if id.peerId.namespace == Namespaces.Peer.CloudUser { if id.peerId.namespace == Namespaces.Peer.CloudUser {
defaultNotify = globalNotificationSettings.effective.privateChats.enabled
defaultSound = globalNotificationSettings.effective.privateChats.sound defaultSound = globalNotificationSettings.effective.privateChats.sound
displayContents = globalNotificationSettings.effective.privateChats.displayPreviews displayContents = globalNotificationSettings.effective.privateChats.displayPreviews
} else if id.peerId.namespace == Namespaces.Peer.SecretChat { } else if id.peerId.namespace == Namespaces.Peer.SecretChat {
defaultNotify = globalNotificationSettings.effective.privateChats.enabled
defaultSound = globalNotificationSettings.effective.privateChats.sound defaultSound = globalNotificationSettings.effective.privateChats.sound
displayContents = false displayContents = false
} else { } else {
defaultNotify = globalNotificationSettings.effective.groupChats.enabled
defaultSound = globalNotificationSettings.effective.groupChats.sound defaultSound = globalNotificationSettings.effective.groupChats.sound
displayContents = globalNotificationSettings.effective.groupChats.displayPreviews displayContents = globalNotificationSettings.effective.groupChats.displayPreviews
} }
@ -719,12 +731,13 @@ public func messageForNotification(modifier: Modifier, id: MessageId, alwaysRetu
} else { } else {
sound = notificationSettings.messageSound sound = notificationSettings.messageSound
} }
if !defaultNotify {
notify = false
}
} else { } else {
Logger.shared.log("AccountStateManager", "notification settings for \(notificationPeerId) are undefined") Logger.shared.log("AccountStateManager", "notification settings for \(notificationPeerId) are undefined")
} }
let message = modifier.getMessage(id)
if let message = message {
if let channel = message.peers[message.id.peerId] as? TelegramChannel { if let channel = message.peers[message.id.peerId] as? TelegramChannel {
switch channel.participationStatus { switch channel.participationStatus {
case .kicked, .left: case .kicked, .left:
@ -747,13 +760,9 @@ public func messageForNotification(modifier: Modifier, id: MessageId, alwaysRetu
Logger.shared.log("AccountStateManager", "read state for \(id.peerId) is undefined") Logger.shared.log("AccountStateManager", "read state for \(id.peerId) is undefined")
} }
if notify || message.personal { if notify {
return (message, isUnread, sound, displayContents) return (message, isUnread, sound, displayContents)
} else { } else {
return (alwaysReturnMessage ? message : nil, false, sound, displayContents) return (alwaysReturnMessage ? message : nil, false, sound, displayContents)
} }
} else {
Logger.shared.log("AccountStateManager", "notification message doesn't exist")
return (nil, false, .bundledModern(id: 0), false)
}
} }

View File

@ -113,6 +113,7 @@ public final class CachedChannelData: CachedPeerData {
public let stickerPack: StickerPackCollectionInfo? public let stickerPack: StickerPackCollectionInfo?
public let peerIds: Set<PeerId> public let peerIds: Set<PeerId>
public let messageIds: Set<MessageId>
init() { init() {
self.flags = [] self.flags = []
@ -124,6 +125,7 @@ public final class CachedChannelData: CachedPeerData {
self.reportStatus = .unknown self.reportStatus = .unknown
self.pinnedMessageId = nil self.pinnedMessageId = nil
self.peerIds = Set() self.peerIds = Set()
self.messageIds = Set()
self.stickerPack = nil self.stickerPack = nil
} }
@ -148,6 +150,12 @@ public final class CachedChannelData: CachedPeerData {
peerIds.insert(botInfo.peerId) peerIds.insert(botInfo.peerId)
} }
self.peerIds = peerIds self.peerIds = peerIds
var messageIds = Set<MessageId>()
if let pinnedMessageId = self.pinnedMessageId {
messageIds.insert(pinnedMessageId)
}
self.messageIds = messageIds
} }
func withUpdatedFlags(_ flags: CachedChannelFlags) -> CachedChannelData { func withUpdatedFlags(_ flags: CachedChannelFlags) -> CachedChannelData {
@ -217,6 +225,12 @@ public final class CachedChannelData: CachedPeerData {
} }
self.peerIds = peerIds self.peerIds = peerIds
var messageIds = Set<MessageId>()
if let pinnedMessageId = self.pinnedMessageId {
messageIds.insert(pinnedMessageId)
}
self.messageIds = messageIds
} }
public func encode(_ encoder: PostboxEncoder) { public func encode(_ encoder: PostboxEncoder) {

View File

@ -36,6 +36,7 @@ public final class CachedGroupData: CachedPeerData {
public let reportStatus: PeerReportStatus public let reportStatus: PeerReportStatus
public let peerIds: Set<PeerId> public let peerIds: Set<PeerId>
public let messageIds = Set<MessageId>()
init() { init() {
self.participants = nil self.participants = nil

View File

@ -7,64 +7,77 @@ import Foundation
import SwiftSignalKit import SwiftSignalKit
#endif #endif
private final class CachedStickerPack: PostboxCoding { final class CachedStickerPack: PostboxCoding {
let info: StickerPackCollectionInfo?
let items: [StickerPackItem] let items: [StickerPackItem]
let hash: Int32 let hash: Int32
init(items: [StickerPackItem], hash: Int32) { init(info: StickerPackCollectionInfo?, items: [StickerPackItem], hash: Int32) {
self.info = info
self.items = items self.items = items
self.hash = hash self.hash = hash
} }
init(decoder: PostboxDecoder) { init(decoder: PostboxDecoder) {
self.info = decoder.decodeObjectForKey("in", decoder: { StickerPackCollectionInfo(decoder: $0) }) as? StickerPackCollectionInfo
self.items = decoder.decodeObjectArrayForKey("it").map { $0 as! StickerPackItem } self.items = decoder.decodeObjectArrayForKey("it").map { $0 as! StickerPackItem }
self.hash = decoder.decodeInt32ForKey("h", orElse: 0) self.hash = decoder.decodeInt32ForKey("h", orElse: 0)
} }
func encode(_ encoder: PostboxEncoder) { func encode(_ encoder: PostboxEncoder) {
if let info = self.info {
encoder.encodeObject(info, forKey: "in")
} else {
encoder.encodeNil(forKey: "in")
}
encoder.encodeObjectArray(self.items, forKey: "it") encoder.encodeObjectArray(self.items, forKey: "it")
encoder.encodeInt32(self.hash, forKey: "h") encoder.encodeInt32(self.hash, forKey: "h")
} }
static func cacheKey(_ info: StickerPackCollectionInfo) -> ValueBoxKey { static func cacheKey(_ id: ItemCollectionId) -> ValueBoxKey {
let key = ValueBoxKey(length: 4 + 8) let key = ValueBoxKey(length: 4 + 8)
key.setInt32(0, value: info.id.namespace) key.setInt32(0, value: id.namespace)
key.setInt64(4, value: info.id.id) key.setInt64(4, value: id.id)
return key return key
} }
} }
private let collectionSpec = ItemCacheCollectionSpec(lowWaterItemCount: 100, highWaterItemCount: 200) private let collectionSpec = ItemCacheCollectionSpec(lowWaterItemCount: 100, highWaterItemCount: 200)
public func cachedStickerPack(postbox: Postbox, network: Network, info: StickerPackCollectionInfo) -> Signal<(StickerPackCollectionInfo, [ItemCollectionItem])?, NoError> { public func cachedStickerPack(postbox: Postbox, network: Network, reference: StickerPackReference) -> Signal<(StickerPackCollectionInfo, [ItemCollectionItem], Bool)?, NoError> {
return postbox.modify { modifier -> Signal<(StickerPackCollectionInfo, [ItemCollectionItem])?, NoError> in return postbox.modify { modifier -> Signal<(StickerPackCollectionInfo, [ItemCollectionItem], Bool)?, NoError> in
if let currentInfo = modifier.getItemCollectionInfo(collectionId: info.id) as? StickerPackCollectionInfo { let namespace = Namespaces.ItemCollection.CloudStickerPacks
let items = modifier.getItemCollectionItems(collectionId: info.id) if case let .id(id, _) = reference, let currentInfo = modifier.getItemCollectionInfo(collectionId: ItemCollectionId(namespace: namespace, id: id)) as? StickerPackCollectionInfo {
return .single((currentInfo, items)) let items = modifier.getItemCollectionItems(collectionId: ItemCollectionId(namespace: namespace, id: id))
return .single((currentInfo, items, true))
} else { } else {
let current: Signal<(StickerPackCollectionInfo, [ItemCollectionItem])?, NoError> let current: Signal<(StickerPackCollectionInfo, [ItemCollectionItem], Bool)?, NoError>
var loadRemote = false var loadRemote = false
if let cached = modifier.retrieveItemCacheEntry(id: ItemCacheEntryId(collectionId: Namespaces.CachedItemCollection.cachedStickerPacks, key: CachedStickerPack.cacheKey(info))) as? CachedStickerPack { if case let .id(id, _) = reference, let cached = modifier.retrieveItemCacheEntry(id: ItemCacheEntryId(collectionId: Namespaces.CachedItemCollection.cachedStickerPacks, key: CachedStickerPack.cacheKey(ItemCollectionId(namespace: namespace, id: id)))) as? CachedStickerPack, let info = cached.info {
current = .single((info, cached.items)) current = .single((info, cached.items, false))
if cached.hash != info.hash { if cached.hash != info.hash {
loadRemote = true loadRemote = true
} }
} else { } else {
current = .complete() current = .single(nil)
loadRemote = true loadRemote = true
} }
var signal = current var signal = current
if loadRemote { if loadRemote {
let appliedRemote = remoteStickerPack(network: network, reference: .id(id: info.id.id, accessHash: info.accessHash)) let appliedRemote = remoteStickerPack(network: network, reference: reference)
|> mapToSignal { result -> Signal<(StickerPackCollectionInfo, [ItemCollectionItem])?, NoError> in |> mapToSignal { result -> Signal<(StickerPackCollectionInfo, [ItemCollectionItem], Bool)?, NoError> in
return postbox.modify { modifier -> (StickerPackCollectionInfo, [ItemCollectionItem])? in return postbox.modify { modifier -> (StickerPackCollectionInfo, [ItemCollectionItem], Bool)? in
if let result = result { if let result = result {
modifier.putItemCacheEntry(id: ItemCacheEntryId(collectionId: Namespaces.CachedItemCollection.cachedStickerPacks, key: CachedStickerPack.cacheKey(result.0)), entry: CachedStickerPack(items: result.1.map { $0 as! StickerPackItem }, hash: result.0.hash), collectionSpec: collectionSpec) modifier.putItemCacheEntry(id: ItemCacheEntryId(collectionId: Namespaces.CachedItemCollection.cachedStickerPacks, key: CachedStickerPack.cacheKey(result.0.id)), entry: CachedStickerPack(info: result.0, items: result.1.map { $0 as! StickerPackItem }, hash: result.0.hash), collectionSpec: collectionSpec)
}
return result let currentInfo = modifier.getItemCollectionInfo(collectionId: result.0.id) as? StickerPackCollectionInfo
return (result.0, result.1, currentInfo != nil)
} else {
return nil
}
} }
} }

View File

@ -13,6 +13,7 @@ public final class CachedUserData: CachedPeerData {
public let commonGroupCount: Int32 public let commonGroupCount: Int32
public let peerIds = Set<PeerId>() public let peerIds = Set<PeerId>()
public let messageIds = Set<MessageId>()
init() { init() {
self.about = nil self.about = nil

View File

@ -86,7 +86,18 @@ func remoteStickerPack(network: Network, reference: StickerPackReference) -> Sig
} }
} }
public func loadedStickerPack(account: Account, reference: StickerPackReference) -> Signal<LoadedStickerPack, NoError> { public func loadedStickerPack(postbox: Postbox, network: Network, reference: StickerPackReference) -> Signal<LoadedStickerPack, NoError> {
return cachedStickerPack(postbox: postbox, network: network, reference: reference)
|> map { result -> LoadedStickerPack in
if let result = result {
return .result(info: result.0, items: result.1, installed: result.2)
} else {
return .fetching
}
}
}
private func loadedStickerPack1(account: Account, reference: StickerPackReference) -> Signal<LoadedStickerPack, NoError> {
return account.postbox.modify { modifier -> Signal<LoadedStickerPack, NoError> in return account.postbox.modify { modifier -> Signal<LoadedStickerPack, NoError> in
switch reference { switch reference {
case let .id(id, _): case let .id(id, _):

View File

@ -25,7 +25,14 @@ public func peerSpecificStickerPack(postbox: Postbox, network: Network, peerId:
|> distinctUntilChanged |> distinctUntilChanged
|> mapToSignal { info -> Signal<(StickerPackCollectionInfo, [ItemCollectionItem])?, NoError> in |> mapToSignal { info -> Signal<(StickerPackCollectionInfo, [ItemCollectionItem])?, NoError> in
if let info = info.info { if let info = info.info {
return cachedStickerPack(postbox: postbox, network: network, info: info) return cachedStickerPack(postbox: postbox, network: network, reference: .id(id: info.id.id, accessHash: info.accessHash))
|> map { result -> (StickerPackCollectionInfo, [ItemCollectionItem])? in
if let result = result {
return (result.0, result.1)
} else {
return nil
}
}
} else { } else {
return .single(nil) return .single(nil)
} }

View File

@ -8,10 +8,11 @@ import Foundation
#endif #endif
public struct PendingMessageStatus: Equatable { public struct PendingMessageStatus: Equatable {
public let isRunning: Bool
public let progress: Float public let progress: Float
public static func ==(lhs: PendingMessageStatus, rhs: PendingMessageStatus) -> Bool { public static func ==(lhs: PendingMessageStatus, rhs: PendingMessageStatus) -> Bool {
return lhs.progress.isEqual(to: rhs.progress) return lhs.isRunning == rhs.isRunning && lhs.progress.isEqual(to: rhs.progress)
} }
} }
@ -173,7 +174,7 @@ public final class PendingMessageManager {
self.messageContexts[id] = messageContext self.messageContexts[id] = messageContext
} }
let status = PendingMessageStatus(progress: 0.0) let status = PendingMessageStatus(isRunning: false, progress: 0.0)
if status != messageContext.status { if status != messageContext.status {
messageContext.status = status messageContext.status = status
for subscriber in messageContext.statusSubscribers.copyItems() { for subscriber in messageContext.statusSubscribers.copyItems() {
@ -247,7 +248,7 @@ public final class PendingMessageManager {
switch next { switch next {
case let .progress(progress): case let .progress(progress):
if let current = strongSelf.messageContexts[messageId] { if let current = strongSelf.messageContexts[messageId] {
let status = PendingMessageStatus(progress: progress) let status = PendingMessageStatus(isRunning: true, progress: progress)
current.status = status current.status = status
for subscriber in current.statusSubscribers.copyItems() { for subscriber in current.statusSubscribers.copyItems() {
subscriber(status) subscriber(status)
@ -261,6 +262,12 @@ public final class PendingMessageManager {
private func beginUploadingMessage(messageContext: PendingMessageContext, id: MessageId, uploadSignal: Signal<PendingMessageUploadedContentResult, PendingMessageUploadError>) { private func beginUploadingMessage(messageContext: PendingMessageContext, id: MessageId, uploadSignal: Signal<PendingMessageUploadedContentResult, PendingMessageUploadError>) {
messageContext.state = .uploading messageContext.state = .uploading
let status = PendingMessageStatus(isRunning: true, progress: 0.0)
messageContext.status = status
for subscriber in messageContext.statusSubscribers.copyItems() {
subscriber(status)
}
messageContext.disposable.set((uploadSignal |> deliverOn(self.queue)).start(next: { [weak self] next in messageContext.disposable.set((uploadSignal |> deliverOn(self.queue)).start(next: { [weak self] next in
if let strongSelf = self { if let strongSelf = self {
assert(strongSelf.queue.isCurrent()) assert(strongSelf.queue.isCurrent())
@ -268,7 +275,7 @@ public final class PendingMessageManager {
switch next { switch next {
case let .progress(progress): case let .progress(progress):
if let current = strongSelf.messageContexts[id] { if let current = strongSelf.messageContexts[id] {
let status = PendingMessageStatus(progress: progress) let status = PendingMessageStatus(isRunning: true, progress: progress)
current.status = status current.status = status
for subscriber in current.statusSubscribers.copyItems() { for subscriber in current.statusSubscribers.copyItems() {
subscriber(status) subscriber(status)
@ -293,6 +300,12 @@ public final class PendingMessageManager {
if case let .waitingForUploadToStart(uploadSignal) = context.state { if case let .waitingForUploadToStart(uploadSignal) = context.state {
if self.canBeginUploadingMessage(id: contextId) { if self.canBeginUploadingMessage(id: contextId) {
context.state = .uploading context.state = .uploading
let status = PendingMessageStatus(isRunning: true, progress: 0.0)
context.status = status
for subscriber in context.statusSubscribers.copyItems() {
subscriber(status)
}
context.disposable.set((uploadSignal |> deliverOn(self.queue)).start(next: { [weak self] next in context.disposable.set((uploadSignal |> deliverOn(self.queue)).start(next: { [weak self] next in
if let strongSelf = self { if let strongSelf = self {
assert(strongSelf.queue.isCurrent()) assert(strongSelf.queue.isCurrent())
@ -300,7 +313,7 @@ public final class PendingMessageManager {
switch next { switch next {
case let .progress(progress): case let .progress(progress):
if let current = strongSelf.messageContexts[contextId] { if let current = strongSelf.messageContexts[contextId] {
let status = PendingMessageStatus(progress: progress) let status = PendingMessageStatus(isRunning: true, progress: progress)
current.status = status current.status = status
for subscriber in current.statusSubscribers.copyItems() { for subscriber in current.statusSubscribers.copyItems() {
subscriber(status) subscriber(status)
@ -320,7 +333,7 @@ public final class PendingMessageManager {
switch next { switch next {
case let .progress(progress): case let .progress(progress):
if let current = strongSelf.messageContexts[contextId] { if let current = strongSelf.messageContexts[contextId] {
let status = PendingMessageStatus(progress: progress) let status = PendingMessageStatus(isRunning: true, progress: progress)
current.status = status current.status = status
for subscriber in current.statusSubscribers.copyItems() { for subscriber in current.statusSubscribers.copyItems() {
subscriber(status) subscriber(status)

View File

@ -78,6 +78,7 @@ public final class TelegramSecretChat: Peer {
public final class CachedSecretChatData: CachedPeerData { public final class CachedSecretChatData: CachedPeerData {
public let peerIds: Set<PeerId> = Set() public let peerIds: Set<PeerId> = Set()
public let messageIds: Set<MessageId> = Set()
public let reportStatus: PeerReportStatus public let reportStatus: PeerReportStatus