2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-31 06:35:14 +00:00

Update scheme for special export methods.

Export all saved contacts.
This commit is contained in:
John Preston
2018-06-15 00:47:09 +03:00
parent 241fee80a7
commit 9d02e539c8
11 changed files with 159 additions and 50 deletions

View File

@@ -826,6 +826,23 @@ ContactsList ParseContactsList(const MTPcontacts_Contacts &data) {
return result;
}
ContactsList ParseContactsList(const MTPVector<MTPSavedContact> &data) {
auto result = ContactsList();
result.list.reserve(data.v.size());
for (const auto &contact : data.v) {
auto info = contact.match([](const MTPDsavedPhoneContact &data) {
auto info = ContactInfo();
info.firstName = ParseString(data.vfirst_name);
info.lastName = ParseString(data.vlast_name);
info.phoneNumber = ParseString(data.vphone);
info.date = data.vdate.v;
return info;
});
result.list.push_back(std::move(info));
}
return result;
}
std::vector<int> SortedContactsIndices(const ContactsList &data) {
const auto names = ranges::view::all(
data.list
@@ -890,14 +907,19 @@ DialogsInfo ParseDialogsInfo(const MTPmessages_Dialogs &data) {
const auto peerId = ParsePeerId(fields.vpeer);
const auto peerIt = peers.find(peerId);
if (peerIt != end(peers)) {
using Type = DialogInfo::Type;
const auto &peer = peerIt->second;
info.type = peer.user()
? DialogInfo::Type::Personal
: peer.chat()->broadcast
? DialogInfo::Type::Channel
: peer.chat()->username.isEmpty()
? DialogInfo::Type::PrivateGroup
: DialogInfo::Type::PublicGroup;
? (peer.user()->isBot
? Type::Bot
: Type::Personal)
: (peer.chat()->broadcast
? (peer.chat()->username.isEmpty()
? Type::PrivateChannel
: Type::PublicChannel)
: (peer.chat()->username.isEmpty()
? Type::PrivateGroup
: Type::PublicGroup));
info.name = peer.name();
info.input = peer.input();
}
@@ -940,6 +962,9 @@ Utf8String FormatDateTime(
QChar dateSeparator,
QChar timeSeparator,
QChar separator) {
if (!date) {
return Utf8String();
}
const auto value = QDateTime::fromTime_t(date);
return (QString("%1") + dateSeparator + "%2" + dateSeparator + "%3"
+ separator + "%4" + timeSeparator + "%5" + timeSeparator + "%6"

View File

@@ -136,6 +136,7 @@ struct ContactInfo {
Utf8String firstName;
Utf8String lastName;
Utf8String phoneNumber;
TimeId date = 0;
Utf8String name() const;
};
@@ -196,6 +197,7 @@ struct ContactsList {
};
ContactsList ParseContactsList(const MTPcontacts_Contacts &data);
ContactsList ParseContactsList(const MTPVector<MTPSavedContact> &data);
std::vector<int> SortedContactsIndices(const ContactsList &data);
struct Session {
@@ -394,9 +396,11 @@ struct DialogInfo {
enum class Type {
Unknown,
Personal,
Bot,
PrivateGroup,
PublicGroup,
Channel,
PrivateChannel,
PublicChannel,
};
Type type = Type::Unknown;
Utf8String name;

View File

@@ -11,6 +11,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "export/data/export_data_types.h"
#include "export/output/export_output_file.h"
#include "mtproto/rpc_sender.h"
#include "base/bytes.h"
#include <deque>
@@ -23,6 +24,7 @@ constexpr auto kFileRequestsCount = 2;
constexpr auto kFileNextRequestDelay = TimeMs(20);
constexpr auto kChatsSliceLimit = 100;
constexpr auto kMessagesSliceLimit = 100;
constexpr auto kFileMaxSize = 1500 * 1024 * 1024;
bool WillLoadFile(const Data::File &file) {
return file.relativePath.isEmpty()
@@ -102,23 +104,29 @@ ApiWrap::DialogsProcess::Single::Single(const Data::DialogInfo &info)
template <typename Request>
auto ApiWrap::mainRequest(Request &&request) {
return std::move(_mtp.request(
std::move(request)
).fail([=](RPCError &&result) {
Expects(_takeoutId.has_value());
return std::move(_mtp.request(MTPInvokeWithTakeout<Request>(
MTP_long(*_takeoutId),
request
)).fail([=](RPCError &&result) {
error(std::move(result));
}).toDC(MTP::ShiftDcId(0, MTP::kExportDcShift)));
}
auto ApiWrap::fileRequest(const Data::FileLocation &location, int offset) {
Expects(location.dcId != 0);
Expects(_takeoutId.has_value());
return std::move(_mtp.request(MTPupload_GetFile(
location.data,
MTP_int(offset),
MTP_int(kFileChunkSize)
return std::move(_mtp.request(MTPInvokeWithTakeout<MTPupload_GetFile>(
MTP_long(*_takeoutId),
MTPupload_GetFile(
location.data,
MTP_int(offset),
MTP_int(kFileChunkSize))
)).fail([=](RPCError &&result) {
error(std::move(result));
}).toDC(MTP::ShiftDcId(location.dcId, MTP::kExportDcShift)));
}).toDC(MTP::ShiftDcId(location.dcId, MTP::kExportMediaDcShift)));
}
ApiWrap::ApiWrap(Fn<void(FnMut<void()>)> runner)
@@ -129,10 +137,57 @@ rpl::producer<RPCError> ApiWrap::errors() const {
return _errors.events();
}
void ApiWrap::startExport(const Settings &settings) {
void ApiWrap::startExport(
const Settings &settings,
FnMut<void()> done) {
Expects(_settings == nullptr);
_settings = std::make_unique<Settings>(settings);
startMainSession(std::move(done));
}
void ApiWrap::startMainSession(FnMut<void()> done) {
auto sizeLimit = _settings->defaultMedia.sizeLimit;
auto hasFiles = _settings->defaultMedia.types != 0;
for (const auto &item : _settings->customMedia) {
sizeLimit = std::max(sizeLimit, item.second.sizeLimit);
hasFiles = hasFiles || (item.second.types != 0);
}
if (!sizeLimit) {
hasFiles = false;
}
using Type = Settings::Type;
using Flag = MTPaccount_InitTakeoutSession::Flag;
const auto flags = Flag(0)
| (_settings->types & Type::Contacts ? Flag::f_contacts : Flag(0))
| (hasFiles ? Flag::f_files : Flag(0))
| (sizeLimit < kFileMaxSize ? Flag::f_file_max_size : Flag(0))
| (_settings->types & (Type::PersonalChats | Type::BotChats)
? Flag::f_message_users
: Flag(0))
| (_settings->types & Type::PrivateGroups
? (Flag::f_message_chats | Flag::f_message_megagroups)
: Flag(0))
| (_settings->types & Type::PublicGroups
? Flag::f_message_megagroups
: Flag(0))
| (_settings->types & (Type::PrivateChannels | Type::PublicChannels)
? Flag::f_message_channels
: Flag(0));
_mtp.request(MTPaccount_InitTakeoutSession(
MTP_flags(flags),
MTP_int(sizeLimit)
)).done([=, done = std::move(done)](
const MTPaccount_Takeout &result) mutable {
_takeoutId = result.match([](const MTPDaccount_takeout &data) {
return data.vid.v;
});
done();
}).fail([=](RPCError &&result) {
error(std::move(result));
}).toDC(MTP::ShiftDcId(0, MTP::kExportDcShift)).send();
}
void ApiWrap::requestPersonalInfo(FnMut<void(Data::PersonalInfo&&)> done) {
@@ -266,16 +321,10 @@ void ApiWrap::finishUserpics() {
}
void ApiWrap::requestContacts(FnMut<void(Data::ContactsList&&)> done) {
const auto hash = 0;
mainRequest(MTPcontacts_GetContacts(
MTP_int(hash)
mainRequest(MTPcontacts_GetSaved(
)).done([=, done = std::move(done)](
const MTPcontacts_Contacts &result) mutable {
if (result.type() == mtpc_contacts_contacts) {
done(Data::ParseContactsList(result));
} else {
error("Bad contacts type.");
}
const MTPVector<MTPSavedContact> &result) mutable {
done(Data::ParseContactsList(result));
}).send();
}
@@ -354,12 +403,16 @@ void ApiWrap::appendDialogsSlice(Data::DialogsInfo &&info) {
switch (info.type) {
case DialogType::Personal:
return Settings::Type::PersonalChats;
case DialogType::Bot:
return Settings::Type::BotChats;
case DialogType::PrivateGroup:
return Settings::Type::PrivateGroups;
case DialogType::PublicGroup:
return Settings::Type::PublicGroups;
case DialogType::Channel:
return Settings::Type::MyChannels;
case DialogType::PrivateChannel:
return Settings::Type::PrivateChannels;
case DialogType::PublicChannel:
return Settings::Type::PublicChannels;
}
return Settings::Type(0);
}();
@@ -536,6 +589,7 @@ void ApiWrap::loadFile(const Data::File &file, FnMut<void(QString)> done) {
_settings->path + relativePath);
_fileProcess->relativePath = relativePath;
_fileProcess->location = file.location;
_fileProcess->size = file.size;
_fileProcess->done = std::move(done);
if (!file.content.isEmpty()) {

View File

@@ -31,7 +31,9 @@ public:
rpl::producer<RPCError> errors() const;
void startExport(const Settings &settings);
void startExport(
const Settings &settings,
FnMut<void()> done);
void requestPersonalInfo(FnMut<void(Data::PersonalInfo&&)> done);
@@ -54,12 +56,16 @@ public:
~ApiWrap();
private:
void startMainSession(FnMut<void()> done);
void handleUserpicsSlice(const MTPphotos_Photos &result);
void loadUserpicsFiles(Data::UserpicsSlice &&slice);
void loadNextUserpic();
void loadUserpicDone(const QString &relativePath);
void finishUserpics();
void requestSavedContacts();
void requestDialogsSlice();
void appendDialogsSlice(Data::DialogsInfo &&info);
void finishDialogsList();
@@ -88,6 +94,7 @@ private:
void error(const QString &text);
MTP::ConcurrentSender _mtp;
base::optional<uint64> _takeoutId;
std::unique_ptr<Settings> _settings;
MTPInputUser _user = MTP_inputUserSelf();

View File

@@ -157,9 +157,10 @@ void Controller::startExport(const Settings &settings) {
return;
}
_writer = Output::CreateWriter(_settings.format);
_api.startExport(_settings);
fillExportSteps();
exportNext();
_api.startExport(_settings, [=] {
exportNext();
});
}
bool Controller::normalizePath() {
@@ -207,9 +208,11 @@ void Controller::fillExportSteps() {
_steps.push_back(Step::Sessions);
}
const auto dialogTypes = Type::PersonalChats
| Type::BotChats
| Type::PrivateGroups
| Type::PublicGroups
| Type::MyChannels;
| Type::PrivateChannels
| Type::PublicChannels;
if (_settings.types & dialogTypes) {
_steps.push_back(Step::Dialogs);
}

View File

@@ -28,5 +28,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "platform/win/windows_range_v3_helpers.h"
#endif // Q_OS_WIN
#include "base/flat_map.h"
#include "base/flat_set.h"
#include "scheme.h"
#include "logs.h"

View File

@@ -37,14 +37,16 @@ struct MediaSettings {
struct Settings {
enum class Type {
PersonalInfo = 0x01,
Userpics = 0x02,
Contacts = 0x04,
Sessions = 0x08,
PersonalChats = 0x10,
PrivateGroups = 0x20,
PublicGroups = 0x40,
MyChannels = 0x80,
PersonalInfo = 0x001,
Userpics = 0x002,
Contacts = 0x004,
Sessions = 0x008,
PersonalChats = 0x010,
BotChats = 0x020,
PrivateGroups = 0x040,
PublicGroups = 0x080,
PrivateChannels = 0x100,
PublicChannels = 0x200,
};
using Types = base::flags<Type>;
friend inline constexpr auto is_flag_type(Type) { return true; };

View File

@@ -460,9 +460,7 @@ bool TextWriter::writeContactsList(const Data::ContactsList &data) {
list.reserve(data.list.size());
for (const auto &index : Data::SortedContactsIndices(data)) {
const auto &contact = data.list[index];
if (!contact.userId) {
list.push_back("(user unavailable)" + kLineBreak);
} else if (contact.firstName.isEmpty()
if (contact.firstName.isEmpty()
&& contact.lastName.isEmpty()
&& contact.phoneNumber.isEmpty()) {
list.push_back("(deleted user)" + kLineBreak);
@@ -474,6 +472,7 @@ bool TextWriter::writeContactsList(const Data::ContactsList &data) {
"Phone number",
Data::FormatPhoneNumber(contact.phoneNumber)
},
{ "Date", Data::FormatDateTime(contact.date) }
}));
}
}
@@ -539,10 +538,12 @@ bool TextWriter::writeDialogsStart(const Data::DialogsInfo &data) {
const auto TypeString = [](Type type) {
switch (type) {
case Type::Unknown: return "(unknown)";
case Type::Personal: return "Personal Chat";
case Type::PrivateGroup: return "Private Group";
case Type::PublicGroup: return "Public Group";
case Type::Channel: return "Channel";
case Type::Personal: return "Personal chat";
case Type::Bot: return "Bot chat";
case Type::PrivateGroup: return "Private group";
case Type::PublicGroup: return "Public group";
case Type::PrivateChannel: return "Private channel";
case Type::PublicChannel: return "Private channel";
}
Unexpected("Dialog type in TypeString.");
};
@@ -555,9 +556,11 @@ bool TextWriter::writeDialogsStart(const Data::DialogsInfo &data) {
switch (type) {
case Type::Unknown: return "(unknown)";
case Type::Personal: return "(deleted user)";
case Type::Bot: return "(deleted bot)";
case Type::PrivateGroup:
case Type::PublicGroup: return "(deleted group)";
case Type::Channel: return "(deleted channel)";
case Type::PrivateChannel:
case Type::PublicChannel: return "(deleted channel)";
}
Unexpected("Dialog type in TypeString.");
};