From 0a80ef0006b35108883b086980b4b66693a15ea9 Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 9 Sep 2015 11:19:25 +0300 Subject: [PATCH] writing report spam statuses to local storage --- Telegram/SourceFiles/app.cpp | 1 + Telegram/SourceFiles/localstorage.cpp | 109 +++++++++++++++++++++----- 2 files changed, 92 insertions(+), 18 deletions(-) diff --git a/Telegram/SourceFiles/app.cpp b/Telegram/SourceFiles/app.cpp index b598b067f..3e142c73f 100644 --- a/Telegram/SourceFiles/app.cpp +++ b/Telegram/SourceFiles/app.cpp @@ -1743,6 +1743,7 @@ namespace App { cSetStickerSets(StickerSets()); cSetStickerSetsOrder(StickerSetsOrder()); cSetLastStickersUpdate(0); + cSetReportSpamStatuses(ReportSpamStatuses()); ::videoItems.clear(); ::audioItems.clear(); ::documentItems.clear(); diff --git a/Telegram/SourceFiles/localstorage.cpp b/Telegram/SourceFiles/localstorage.cpp index 56fab4908..640b5c27d 100644 --- a/Telegram/SourceFiles/localstorage.cpp +++ b/Telegram/SourceFiles/localstorage.cpp @@ -495,19 +495,20 @@ namespace { FileKey _dataNameKey = 0; enum { // Local Storage Keys - lskUserMap = 0x00, - lskDraft = 0x01, // data: PeerId peer - lskDraftPosition = 0x02, // data: PeerId peer - lskImages = 0x03, // data: StorageKey location - lskLocations = 0x04, // no data - lskStickerImages = 0x05, // data: StorageKey location - lskAudios = 0x06, // data: StorageKey location - lskRecentStickersOld = 0x07, // no data - lskBackground = 0x08, // no data - lskUserSettings = 0x09, // no data - lskRecentHashtags = 0x0a, // no data - lskStickers = 0x0b, // no data - lskSavedPeers = 0x0c, // no data + lskUserMap = 0x00, + lskDraft = 0x01, // data: PeerId peer + lskDraftPosition = 0x02, // data: PeerId peer + lskImages = 0x03, // data: StorageKey location + lskLocations = 0x04, // no data + lskStickerImages = 0x05, // data: StorageKey location + lskAudios = 0x06, // data: StorageKey location + lskRecentStickersOld = 0x07, // no data + lskBackground = 0x08, // no data + lskUserSettings = 0x09, // no data + lskRecentHashtags = 0x0a, // no data + lskStickers = 0x0b, // no data + lskSavedPeers = 0x0c, // no data + lskReportSpamStatuses = 0x0d, // no data }; typedef QMap DraftsMap; @@ -522,7 +523,7 @@ namespace { FileLocationPairs _fileLocationPairs; typedef QMap FileLocationAliases; FileLocationAliases _fileLocationAliases; - FileKey _locationsKey = 0; + FileKey _locationsKey = 0, _reportSpamStatusesKey = 0; FileKey _recentStickersKeyOld = 0, _stickersKey = 0; @@ -550,7 +551,7 @@ namespace { }; void _writeMap(WriteMapWhen when = WriteMapSoon); - + void _writeLocations(WriteMapWhen when = WriteMapSoon) { if (when != WriteMapNow) { _manager->writeLocations(when == WriteMapFast); @@ -643,6 +644,63 @@ namespace { } } + void _writeReportSpamStatuses() { + if (!_working()) return; + + if (cReportSpamStatuses().isEmpty()) { + if (_reportSpamStatusesKey) { + clearKey(_reportSpamStatusesKey); + _reportSpamStatusesKey = 0; + _mapChanged = true; + _writeMap(); + } + } else { + if (!_reportSpamStatusesKey) { + _reportSpamStatusesKey = genKey(); + _mapChanged = true; + _writeMap(WriteMapFast); + } + const ReportSpamStatuses &statuses(cReportSpamStatuses()); + + quint32 size = sizeof(qint32); + for (ReportSpamStatuses::const_iterator i = statuses.cbegin(), e = statuses.cend(); i != e; ++i) { + // peer + status + size += sizeof(quint64) + sizeof(qint32); + } + + EncryptedDescriptor data(size); + data.stream << qint32(statuses.size()); + for (ReportSpamStatuses::const_iterator i = statuses.cbegin(), e = statuses.cend(); i != e; ++i) { + data.stream << quint64(i.key()) << qint32(i.value()); + } + + FileWriteDescriptor file(_reportSpamStatusesKey); + file.writeEncrypted(data); + } + } + + void _readReportSpamStatuses() { + FileReadDescriptor statuses; + if (!readEncryptedFile(statuses, _reportSpamStatusesKey)) { + clearKey(_reportSpamStatusesKey); + _reportSpamStatusesKey = 0; + _writeMap(); + return; + } + + ReportSpamStatuses &map(cRefReportSpamStatuses()); + map.clear(); + + qint32 size = 0; + statuses.stream >> size; + for (int32 i = 0; i < size; ++i) { + quint64 peer = 0; + qint32 status = 0; + statuses.stream >> peer >> status; + map.insert(peer, DBIPeerReportSpamStatus(status)); + } + } + mtpDcOptions *_dcOpts = 0; bool _readSetting(quint32 blockId, QDataStream &stream, int version) { switch (blockId) { @@ -1458,7 +1516,7 @@ namespace { DraftsNotReadMap draftsNotReadMap; StorageMap imagesMap, stickerImagesMap, audiosMap; qint64 storageImagesSize = 0, storageStickersSize = 0, storageAudiosSize = 0; - quint64 locationsKey = 0, recentStickersKeyOld = 0, stickersKey = 0, backgroundKey = 0, userSettingsKey = 0, recentHashtagsKey = 0, savedPeersKey = 0; + quint64 locationsKey = 0, reportSpamStatusesKey = 0, recentStickersKeyOld = 0, stickersKey = 0, backgroundKey = 0, userSettingsKey = 0, recentHashtagsKey = 0, savedPeersKey = 0; while (!map.stream.atEnd()) { quint32 keyType; map.stream >> keyType; @@ -1523,6 +1581,9 @@ namespace { case lskLocations: { map.stream >> locationsKey; } break; + case lskReportSpamStatuses: { + map.stream >> reportSpamStatusesKey; + } break; case lskRecentStickersOld: { map.stream >> recentStickersKeyOld; } break; @@ -1562,6 +1623,7 @@ namespace { _storageAudiosSize = storageAudiosSize; _locationsKey = locationsKey; + _reportSpamStatusesKey = reportSpamStatusesKey; _recentStickersKeyOld = recentStickersKeyOld; _stickersKey = stickersKey; _savedPeersKey = savedPeersKey; @@ -1579,6 +1641,9 @@ namespace { if (_locationsKey) { _readLocations(); } + if (_reportSpamStatusesKey) { + _readReportSpamStatuses(); + } _readUserSettings(); _readMtpData(); @@ -1630,6 +1695,7 @@ namespace { if (!_stickerImagesMap.isEmpty()) mapSize += sizeof(quint32) * 2 + _stickerImagesMap.size() * (sizeof(quint64) * 3 + sizeof(qint32)); if (!_audiosMap.isEmpty()) mapSize += sizeof(quint32) * 2 + _audiosMap.size() * (sizeof(quint64) * 3 + sizeof(qint32)); if (_locationsKey) mapSize += sizeof(quint32) + sizeof(quint64); + if (_reportSpamStatusesKey) mapSize += sizeof(quint32) + sizeof(quint64); if (_recentStickersKeyOld) mapSize += sizeof(quint32) + sizeof(quint64); if (_stickersKey) mapSize += sizeof(quint32) + sizeof(quint64); if (_savedPeersKey) mapSize += sizeof(quint32) + sizeof(quint64); @@ -1670,6 +1736,9 @@ namespace { if (_locationsKey) { mapData.stream << quint32(lskLocations) << quint64(_locationsKey); } + if (_reportSpamStatusesKey) { + mapData.stream << quint32(lskReportSpamStatuses) << quint64(_reportSpamStatusesKey); + } if (_recentStickersKeyOld) { mapData.stream << quint32(lskRecentStickersOld) << quint64(_recentStickersKeyOld); } @@ -1938,7 +2007,7 @@ namespace Local { _draftsNotReadMap.clear(); _stickerImagesMap.clear(); _audiosMap.clear(); - _locationsKey = _recentStickersKeyOld = _stickersKey = _backgroundKey = _userSettingsKey = _recentHashtagsKey = _savedPeersKey = 0; + _locationsKey = _reportSpamStatusesKey = _recentStickersKeyOld = _stickersKey = _backgroundKey = _userSettingsKey = _recentHashtagsKey = _savedPeersKey = 0; _mapChanged = true; _writeMap(WriteMapNow); @@ -2945,7 +3014,7 @@ namespace Local { } void writeReportSpamStatuses() { - + _writeReportSpamStatuses(); } struct ClearManagerData { @@ -2995,6 +3064,10 @@ namespace Local { _locationsKey = 0; _mapChanged = true; } + if (_reportSpamStatusesKey) { + _reportSpamStatusesKey = 0; + _mapChanged = true; + } if (_recentStickersKeyOld) { _recentStickersKeyOld = 0; _mapChanged = true;