Replace TLHelp helpers with .match()

This commit is contained in:
John Preston 2019-01-10 15:15:13 +04:00
parent 9a60e744d3
commit dba9ca2084
9 changed files with 42 additions and 102 deletions

View File

@ -21,10 +21,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/data_chat.h"
#include "data/data_user.h"
#include "dialogs/dialogs_key.h"
#include "core/tl_help.h"
#include "core/core_cloud_password.h"
#include "base/openssl_help.h"
#include "base/overload.h"
#include "observer_peer.h"
#include "lang/lang_keys.h"
#include "application.h"
@ -1377,16 +1375,15 @@ void ApiWrap::requestAdmins(not_null<ChannelData*> channel) {
MTP_int(participantsHash)
)).done([this, channel](const MTPchannels_ChannelParticipants &result) {
_adminsRequests.remove(channel);
TLHelp::VisitChannelParticipants(result, base::overload([&](
const MTPDchannels_channelParticipants &data) {
result.match([&](const MTPDchannels_channelParticipants &data) {
App::feedUsers(data.vusers);
applyAdminsList(
channel,
data.vcount.v,
data.vparticipants.v);
}, [&](mtpTypeId) {
}, [&](const MTPDchannels_channelParticipantsNotModified &) {
LOG(("API Error: channels.channelParticipantsNotModified received!"));
}));
});
}).fail([this, channel](const RPCError &error) {
_adminsRequests.remove(channel);
}).send();
@ -1409,7 +1406,9 @@ void ApiWrap::applyLastParticipantsList(
MTP_flags(0),
MTP_int(0));
for (const auto &p : list) {
const auto userId = TLHelp::ReadChannelParticipantUserId(p);
const auto userId = p.match([](const auto &data) {
return data.vuser_id.v;
});
const auto adminCanEdit = (p.type() == mtpc_channelParticipantAdmin)
? p.c_channelParticipantAdmin().is_can_edit()
: false;
@ -1483,7 +1482,9 @@ void ApiWrap::applyBotsList(
auto botStatus = channel->mgInfo->botStatus;
auto keyboardBotFound = !history || !history->lastKeyboardFrom;
for (const auto &p : list) {
const auto userId = TLHelp::ReadChannelParticipantUserId(p);
const auto userId = p.match([](const auto &data) {
return data.vuser_id.v;
});
if (!userId) {
continue;
}
@ -1518,7 +1519,7 @@ void ApiWrap::applyAdminsList(
auto admins = ranges::make_iterator_range(
list.begin(), list.end()
) | ranges::view::transform([](const MTPChannelParticipant &p) {
return TLHelp::ReadChannelParticipantUserId(p);
return p.match([](const auto &data) { return data.vuser_id.v; });
});
auto adding = base::flat_set<UserId>{ admins.begin(), admins.end() };
if (channel->mgInfo->creator) {
@ -3224,8 +3225,7 @@ void ApiWrap::parseChannelParticipants(
int availableCount,
const QVector<MTPChannelParticipant> &list)> callbackList,
Fn<void()> callbackNotModified) {
TLHelp::VisitChannelParticipants(result, base::overload([&](
const MTPDchannels_channelParticipants &data) {
result.match([&](const MTPDchannels_channelParticipants &data) {
App::feedUsers(data.vusers);
if (channel->mgInfo) {
refreshChannelAdmins(channel, data.vparticipants.v);
@ -3233,21 +3233,24 @@ void ApiWrap::parseChannelParticipants(
if (callbackList) {
callbackList(data.vcount.v, data.vparticipants.v);
}
}, [&](mtpTypeId) {
}, [&](const MTPDchannels_channelParticipantsNotModified &) {
if (callbackNotModified) {
callbackNotModified();
} else {
LOG(("API Error: channels.channelParticipantsNotModified received!"));
LOG(("API Error: "
"channels.channelParticipantsNotModified received!"));
}
}));
});
}
void ApiWrap::refreshChannelAdmins(
not_null<ChannelData*> channel,
const QVector<MTPChannelParticipant> &participants) {
Data::ChannelAdminChanges changes(channel);
for (auto &p : participants) {
const auto userId = TLHelp::ReadChannelParticipantUserId(p);
for (const auto &p : participants) {
const auto userId = p.match([](const auto &data) {
return data.vuser_id.v;
});
const auto isAdmin = (p.type() == mtpc_channelParticipantAdmin)
|| (p.type() == mtpc_channelParticipantCreator);
changes.feed(userId, isAdmin);

View File

@ -15,8 +15,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/data_user.h"
#include "history/history.h"
#include "dialogs/dialogs_indexed_list.h"
#include "core/tl_help.h"
#include "base/overload.h"
#include "auth_session.h"
#include "mainwidget.h"
#include "apiwrap.h"
@ -874,8 +872,7 @@ void AddSpecialBoxSearchController::searchParticipantsDone(
return;
}
_requestId = 0;
TLHelp::VisitChannelParticipants(result, base::overload([&](
const MTPDchannels_channelParticipants &data) {
result.match([&](const MTPDchannels_channelParticipants &data) {
auto &list = data.vparticipants.v;
if (list.size() < requestedCount) {
// We want cache to have full information about a query with small
@ -893,9 +890,9 @@ void AddSpecialBoxSearchController::searchParticipantsDone(
}
}
_offset += list.size();
}, [&](mtpTypeId type) {
}, [&](const MTPDchannels_channelParticipantsNotModified &) {
_participantsLoaded = true;
}));
});
delegate()->peerListSearchRefreshRows();
}

View File

@ -12,8 +12,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "boxes/peers/add_participants_box.h"
#include "boxes/confirm_box.h"
#include "boxes/add_contact_box.h"
#include "core/tl_help.h"
#include "base/overload.h"
#include "auth_session.h"
#include "apiwrap.h"
#include "lang/lang_keys.h"
@ -1705,8 +1703,7 @@ void ParticipantsBoxSearchController::searchDone(
}
_requestId = 0;
TLHelp::VisitChannelParticipants(result, base::overload([&](
const MTPDchannels_channelParticipants &data) {
result.match([&](const MTPDchannels_channelParticipants &data) {
auto &list = data.vparticipants.v;
if (list.size() < requestedCount) {
// We want cache to have full information about a query with small
@ -1726,9 +1723,9 @@ void ParticipantsBoxSearchController::searchDone(
}
}
_offset += list.size();
}, [&](mtpTypeId type) {
}, [&](const MTPDchannels_channelParticipantsNotModified &) {
_allLoaded = true;
}));
});
delegate()->peerListSearchRefreshRows();
}

View File

@ -1,50 +0,0 @@
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
namespace TLHelp {
template <typename Callback>
inline auto VisitChannelParticipant(
const MTPChannelParticipant &p,
Callback &&callback) {
switch (p.type()) {
case mtpc_channelParticipant:
return callback(p.c_channelParticipant());
case mtpc_channelParticipantSelf:
return callback(p.c_channelParticipantSelf());
case mtpc_channelParticipantAdmin:
return callback(p.c_channelParticipantAdmin());
case mtpc_channelParticipantCreator:
return callback(p.c_channelParticipantCreator());
case mtpc_channelParticipantBanned:
return callback(p.c_channelParticipantBanned());
default: Unexpected("Type in VisitChannelParticipant()");
}
}
inline UserId ReadChannelParticipantUserId(const MTPChannelParticipant &p) {
return VisitChannelParticipant(p, [](auto &&data) {
return data.vuser_id.v;
});
}
template <typename Callback>
inline auto VisitChannelParticipants(
const MTPchannels_ChannelParticipants &p,
Callback &&callback) {
switch (p.type()) {
case mtpc_channels_channelParticipants:
return callback(p.c_channels_channelParticipants());
case mtpc_channels_channelParticipantsNotModified:
return callback(p.type());
default: Unexpected("Type in VisitChannelParticipants()");
}
}
} // namespace TLHelp

View File

@ -31,8 +31,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/widgets/popup_menu.h"
#include "ui/image/image.h"
#include "core/file_utilities.h"
#include "core/tl_help.h"
#include "base/overload.h"
#include "lang/lang_keys.h"
#include "boxes/peers/edit_participant_box.h"
#include "data/data_session.h"
@ -388,20 +386,22 @@ void InnerWidget::requestAdmins() {
MTP_int(kMaxChannelAdmins),
MTP_int(participantsHash)
)).done([this](const MTPchannels_ChannelParticipants &result) {
auto readCanEdit = base::overload([](const MTPDchannelParticipantAdmin &v) {
return v.is_can_edit();
}, [](auto &&) {
return false;
});
Auth().api().parseChannelParticipants(_channel, result, [&](
int availableCount,
const QVector<MTPChannelParticipant> &list) {
auto filtered = (
list
) | ranges::view::transform([&](const MTPChannelParticipant &p) {
return std::make_pair(
TLHelp::ReadChannelParticipantUserId(p),
TLHelp::VisitChannelParticipant(p, readCanEdit));
const auto userId = p.match([](const auto &data) {
return data.vuser_id.v;
});
const auto canEdit = p.match([](
const MTPDchannelParticipantAdmin &data) {
return data.is_can_edit();
}, [](const auto &) {
return false;
});
return std::make_pair(userId, canEdit);
}) | ranges::view::transform([&](auto &&pair) {
return std::make_pair(
App::userLoaded(pair.first),

View File

@ -17,8 +17,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/data_session.h"
#include "lang/lang_keys.h"
#include "boxes/sticker_set_box.h"
#include "core/tl_help.h"
#include "base/overload.h"
#include "messenger.h"
#include "auth_session.h"
@ -278,9 +276,8 @@ auto GenerateParticipantChangeTextInner(
not_null<ChannelData*> channel,
const MTPChannelParticipant &participant,
const MTPChannelParticipant *oldParticipant) {
auto oldType = oldParticipant ? oldParticipant->type() : 0;
auto readResult = base::overload([&](const MTPDchannelParticipantCreator &data) {
const auto oldType = oldParticipant ? oldParticipant->type() : 0;
return participant.match([&](const MTPDchannelParticipantCreator &data) {
// No valid string here :(
return lng_admin_log_invited__generic(
lt_user,
@ -318,8 +315,6 @@ auto GenerateParticipantChangeTextInner(
}
return lng_admin_log_invited__generic(lt_user, user);
});
return TLHelp::VisitChannelParticipant(participant, readResult);
}
TextWithEntities GenerateParticipantChangeText(not_null<ChannelData*> channel, const MTPChannelParticipant &participant, const MTPChannelParticipant *oldParticipant = nullptr) {

View File

@ -27,8 +27,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "window/window_peer_menu.h"
#include "auth_session.h"
#include "ui/widgets/popup_menu.h"
#include "core/tl_help.h"
#include "base/overload.h"
#include "lang/lang_keys.h"
#include "boxes/peers/edit_participant_box.h"
#include "data/data_session.h"

View File

@ -17,7 +17,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "boxes/peers/manage_peer_box.h"
#include "boxes/peers/edit_peer_info_box.h"
#include "ui/toast/toast.h"
#include "core/tl_help.h"
#include "auth_session.h"
#include "apiwrap.h"
#include "mainwidget.h"
@ -698,14 +697,16 @@ void PeerMenuAddChannelMembers(not_null<ChannelData*> channel) {
LayerOption::KeepOther);
return;
}
auto callback = [channel](const MTPchannels_ChannelParticipants &result) {
auto callback = [=](const MTPchannels_ChannelParticipants &result) {
Auth().api().parseChannelParticipants(channel, result, [&](
int availableCount,
const QVector<MTPChannelParticipant> &list) {
auto already = (
list
) | ranges::view::transform([&](auto &&p) {
return TLHelp::ReadChannelParticipantUserId(p);
) | ranges::view::transform([](const MTPChannelParticipant &p) {
return p.match([](const auto &data) {
return data.vuser_id.v;
});
}) | ranges::view::transform([](UserId userId) {
return App::userLoaded(userId);
}) | ranges::view::filter([](UserData *user) {

View File

@ -142,7 +142,6 @@
<(src_loc)/core/shortcuts.h
<(src_loc)/core/single_timer.cpp
<(src_loc)/core/single_timer.h
<(src_loc)/core/tl_help.h
<(src_loc)/core/update_checker.cpp
<(src_loc)/core/update_checker.h
<(src_loc)/core/utils.cpp