- fixes for slowmode

This commit is contained in:
overtake 2019-07-17 14:00:43 +02:00
parent 1aada1ce89
commit 5d871af858
3 changed files with 39 additions and 5 deletions

View File

@ -2075,6 +2075,7 @@ func replayFinalState(accountManager: AccountManager, postbox: Postbox, accountP
var isContactUpdates: [(PeerId, Bool)] = []
var stickerPackOperations: [AccountStateUpdateStickerPacksOperation] = []
var recentlyUsedStickers: [MediaId: (MessageIndex, TelegramMediaFile)] = [:]
var slowModeLastMessageTimeouts:[PeerId : Int32] = [:]
var recentlyUsedGifs: [MediaId: (MessageIndex, TelegramMediaFile)] = [:]
var syncRecentGifs = false
var langPackDifferences: [String: [Api.LangPackDifference]] = [:]
@ -2181,6 +2182,11 @@ func replayFinalState(accountManager: AccountManager, postbox: Postbox, accountP
}
}
}
if !message.flags.contains(.Incoming) && !message.flags.contains(.Unsent) {
if message.id.peerId.namespace == Namespaces.Peer.CloudChannel {
slowModeLastMessageTimeouts[message.id.peerId] = max(slowModeLastMessageTimeouts[message.id.peerId] ?? 0, message.timestamp)
}
}
if !message.flags.contains(.Incoming), message.forwardInfo == nil {
inner: for media in message.media {
@ -2713,6 +2719,22 @@ func replayFinalState(accountManager: AccountManager, postbox: Postbox, accountP
}
}
if !slowModeLastMessageTimeouts.isEmpty {
var peerIds:Set<PeerId> = Set()
var cachedDatas:[PeerId : CachedChannelData] = [:]
for (peerId, timeout) in slowModeLastMessageTimeouts {
var cachedData = transaction.getPeerCachedData(peerId: peerId) as? CachedChannelData ?? CachedChannelData()
if let slowModeTimeout = cachedData.slowModeTimeout {
cachedData = cachedData.withUpdatedSlowModeValidUntilTimestamp(slowModeValidUntilTimestamp: timeout + slowModeTimeout)
peerIds.insert(peerId)
cachedDatas[peerId] = cachedData
}
}
transaction.updatePeerCachedData(peerIds: peerIds, update: { peerId, current in
return cachedDatas[peerId] ?? current
})
}
if syncRecentGifs {
addSynchronizeSavedGifsOperation(transaction: transaction, operation: .sync)
} else {

View File

@ -165,6 +165,20 @@ func applyUpdateMessage(postbox: Postbox, stateManager: AccountStateManager, mes
let (tags, globalTags) = tagsForStoreMessage(incoming: currentMessage.flags.contains(.Incoming), attributes: attributes, media: media, textEntities: entitiesAttribute?.entities)
if currentMessage.id.peerId.namespace == Namespaces.Peer.CloudChannel, !currentMessage.flags.contains(.Incoming) {
let peerId = currentMessage.id.peerId
transaction.updatePeerCachedData(peerIds: [peerId], update: { peerId, current in
var cachedData = current as? CachedChannelData ?? CachedChannelData()
if let slowModeTimeout = cachedData.slowModeTimeout {
cachedData = cachedData.withUpdatedSlowModeValidUntilTimestamp(slowModeValidUntilTimestamp: currentMessage.timestamp + slowModeTimeout)
return cachedData
} else {
return current
}
})
}
return .update(StoreMessage(id: updatedId, globallyUniqueId: nil, groupingKey: currentMessage.groupingKey, timestamp: updatedTimestamp ?? currentMessage.timestamp, flags: [], tags: tags, globalTags: globalTags, localTags: currentMessage.localTags, forwardInfo: forwardInfo, authorId: currentMessage.author?.id, text: text, attributes: attributes, media: media))
})
for file in sentStickers {
@ -173,6 +187,7 @@ func applyUpdateMessage(postbox: Postbox, stateManager: AccountStateManager, mes
for file in sentGifs {
transaction.addOrMoveToFirstPositionOrderedItemListItem(collectionId: Namespaces.OrderedItemList.CloudRecentGifs, item: OrderedItemListEntry(id: RecentMediaItemId(file.fileId).rawValue, contents: RecentMediaItem(file)), removeTailIfCountExceeds: 200)
}
stateManager.addUpdates(result)
}
}

View File

@ -30,11 +30,8 @@ public func updateChannelSlowModeInteractively(postbox: Postbox, network: Networ
accountStateManager.addUpdates(updates)
return postbox.transaction { transaction -> Void in
transaction.updatePeerCachedData(peerIds: [peerId], update: { peerId, currentData in
if let currentData = currentData as? CachedChannelData {
return currentData.withUpdatedSlowModeTimeout(slowModeTimeout: timeout)
} else {
return currentData
}
let currentData = currentData as? CachedChannelData ?? CachedChannelData()
return currentData.withUpdatedSlowModeTimeout(slowModeTimeout: timeout)
})
}
|> introduceError(UpdateChannelSlowModeError.self)