Various fixes

This commit is contained in:
Ilya Laktyushin
2023-08-10 17:34:56 +02:00
parent e7c717867c
commit 5bf4a36d46
6 changed files with 155 additions and 45 deletions

View File

@@ -6,24 +6,79 @@ import TelegramUIPreferences
import MediaEditor
import AccountContext
public func saveStorySource(engine: TelegramEngine, item: MediaEditorDraft, peerId: EnginePeer.Id, id: Int64) {
public func updateStorySources(engine: TelegramEngine) {
let currentTimestamp = Int32(CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970)
let _ = engine.data.get(
TelegramEngine.EngineData.Item.OrderedLists.ListItems(collectionId: ApplicationSpecificOrderedItemListCollectionId.storySources)
).start(next: { items in
for item in items {
let key = EngineDataBuffer(item.id)
let _ = getStorySource(engine: engine, key: key).start(next: { source in
if let source {
if let expiresOn = source.expiresOn, expiresOn < currentTimestamp {
let _ = removeStorySource(engine: engine, key: key, delete: true).start()
}
}
})
}
})
}
private func key(peerId: EnginePeer.Id, id: Int64) -> EngineDataBuffer {
let key = EngineDataBuffer(length: 16)
key.setInt64(0, value: peerId.toInt64())
key.setInt64(8, value: id)
return key
}
private class StorySourceItem: Codable {
}
private func addStorySource(engine: TelegramEngine, key: EngineDataBuffer) {
let _ = engine.orderedLists.addOrMoveToFirstPosition(
collectionId: ApplicationSpecificOrderedItemListCollectionId.storySources,
id: key.toMemoryBuffer(),
item: StorySourceItem(),
removeTailIfCountExceeds: nil
).start()
}
private func removeStorySource(engine: TelegramEngine, peerId: EnginePeer.Id, id: Int64, delete: Bool) -> Signal<Never, NoError> {
let key = key(peerId: peerId, id: id)
return getStorySource(engine: engine, peerId: peerId, id: id)
|> mapToSignal { source in
if let source {
let _ = engine.itemCache.remove(collectionId: ApplicationSpecificItemCacheCollectionId.storySource, id: key).start()
removeStoryDraft(engine: engine, path: source.path, delete: delete)
}
return engine.orderedLists.removeItem(collectionId: ApplicationSpecificOrderedItemListCollectionId.storySources, id: key.toMemoryBuffer())
}
}
private func removeStorySource(engine: TelegramEngine, key: EngineDataBuffer, delete: Bool) -> Signal<Never, NoError> {
return getStorySource(engine: engine, key: key)
|> mapToSignal { source in
if let source {
let _ = engine.itemCache.remove(collectionId: ApplicationSpecificItemCacheCollectionId.storySource, id: key).start()
removeStoryDraft(engine: engine, path: source.path, delete: delete)
}
return engine.orderedLists.removeItem(collectionId: ApplicationSpecificOrderedItemListCollectionId.storySources, id: key.toMemoryBuffer())
}
}
public func saveStorySource(engine: TelegramEngine, item: MediaEditorDraft, peerId: EnginePeer.Id, id: Int64) {
let key = key(peerId: peerId, id: id)
addStorySource(engine: engine, key: key)
let _ = engine.itemCache.put(collectionId: ApplicationSpecificItemCacheCollectionId.storySource, id: key, item: item).start()
}
public func removeStorySource(engine: TelegramEngine, peerId: EnginePeer.Id, id: Int64) {
let key = EngineDataBuffer(length: 16)
key.setInt64(0, value: peerId.toInt64())
key.setInt64(8, value: id)
let _ = engine.itemCache.remove(collectionId: ApplicationSpecificItemCacheCollectionId.storySource, id: key).start()
public func getStorySource(engine: TelegramEngine, peerId: EnginePeer.Id, id: Int64) -> Signal<MediaEditorDraft?, NoError> {
let key = key(peerId: peerId, id: id)
return getStorySource(engine: engine, key: key)
}
public func getStorySource(engine: TelegramEngine, peerId: EnginePeer.Id, id: Int64) -> Signal<MediaEditorDraft?, NoError> {
let key = EngineDataBuffer(length: 16)
key.setInt64(0, value: peerId.toInt64())
key.setInt64(8, value: id)
private func getStorySource(engine: TelegramEngine, key: EngineDataBuffer) -> Signal<MediaEditorDraft?, NoError> {
return engine.data.get(TelegramEngine.EngineData.Item.ItemCache.Item(collectionId: ApplicationSpecificItemCacheCollectionId.storySource, id: key))
|> map { result -> MediaEditorDraft? in
return result?.get(MediaEditorDraft.self)
@@ -31,20 +86,16 @@ public func getStorySource(engine: TelegramEngine, peerId: EnginePeer.Id, id: In
}
public func moveStorySource(engine: TelegramEngine, peerId: EnginePeer.Id, from fromId: Int64, to toId: Int64) {
let fromKey = EngineDataBuffer(length: 16)
fromKey.setInt64(0, value: peerId.toInt64())
fromKey.setInt64(8, value: fromId)
let toKey = EngineDataBuffer(length: 16)
toKey.setInt64(0, value: peerId.toInt64())
toKey.setInt64(8, value: toId)
let fromKey = key(peerId: peerId, id: fromId)
let toKey = key(peerId: peerId, id: toId)
let _ = (engine.data.get(TelegramEngine.EngineData.Item.ItemCache.Item(collectionId: ApplicationSpecificItemCacheCollectionId.storySource, id: fromKey))
|> mapToSignal { item -> Signal<Never, NoError> in
if let item = item?.get(MediaEditorDraft.self) {
addStorySource(engine: engine, key: toKey)
return engine.itemCache.put(collectionId: ApplicationSpecificItemCacheCollectionId.storySource, id: toKey, item: item)
|> then(
engine.itemCache.remove(collectionId: ApplicationSpecificItemCacheCollectionId.storySource, id: fromKey)
removeStorySource(engine: engine, key: fromKey, delete: false)
)
} else {
return .complete()