Don't apply count from getParticipants(Recent).

This commit is contained in:
John Preston 2017-11-28 22:30:01 +04:00
parent ff16897dde
commit 196ff7f4c1
8 changed files with 86 additions and 51 deletions

View File

@ -572,28 +572,21 @@ void ApiWrap::requestPeers(const QList<PeerData*> &peers) {
}
}
void ApiWrap::requestLastParticipants(ChannelData *channel, bool fromStart) {
void ApiWrap::requestLastParticipants(ChannelData *channel) {
if (!channel || !channel->isMegagroup()) {
return;
}
auto needAdmins = channel->canViewAdmins();
auto adminsOutdated = (channel->mgInfo->lastParticipantsStatus & MegagroupInfo::LastParticipantsAdminsOutdated) != 0;
if ((needAdmins && adminsOutdated) || channel->lastParticipantsCountOutdated()) {
fromStart = true;
}
auto i = _participantsRequests.find(channel);
if (i != _participantsRequests.cend()) {
if (fromStart && i.value() < 0) { // was not loading from start
_participantsRequests.erase(i);
} else {
return;
}
return;
}
auto offset = fromStart ? 0 : channel->mgInfo->lastParticipants.size();
auto participantsHash = 0;
auto requestId = request(MTPchannels_GetParticipants(
const auto offset = 0;
const auto participantsHash = 0;
const auto requestId = request(MTPchannels_GetParticipants(
channel->inputChannel,
MTP_channelParticipantsRecent(),
MTP_int(offset),
@ -608,7 +601,7 @@ void ApiWrap::requestLastParticipants(ChannelData *channel, bool fromStart) {
}
}).send();
_participantsRequests.insert(channel, fromStart ? requestId : -requestId);
_participantsRequests.insert(channel, requestId);
}
void ApiWrap::requestBots(ChannelData *channel) {
@ -656,11 +649,11 @@ void ApiWrap::lastParticipantsDone(
if (!peer->mgInfo) return;
parseChannelParticipants(result, [&](
int fullCount,
int availableCount,
const QVector<MTPChannelParticipant> &list) {
applyLastParticipantsList(
peer,
fullCount,
availableCount,
list,
bots,
fromStart);
@ -669,7 +662,7 @@ void ApiWrap::lastParticipantsDone(
void ApiWrap::applyLastParticipantsList(
ChannelData *peer,
int fullCount,
int availableCount,
const QVector<MTPChannelParticipant> &list,
bool bots,
bool fromStart) {
@ -753,11 +746,18 @@ void ApiWrap::applyLastParticipantsList(
h->clearLastKeyboard();
}
if (!bots) {
if (list.isEmpty()) {
peer->setMembersCount(peer->mgInfo->lastParticipants.size());
} else {
peer->setMembersCount(fullCount);
}
//
// getParticipants(Recent) sometimes can't return all members,
// only some last subset, size of this subset is availableCount.
//
// So both list size and availableCount have nothing to do with
// the full supergroup members count.
//
//if (list.isEmpty()) {
// peer->setMembersCount(peer->mgInfo->lastParticipants.size());
//} else {
// peer->setMembersCount(availableCount);
//}
Notify::PeerUpdate update(peer);
update.flags |= Notify::PeerUpdate::Flag::MembersChanged | Notify::PeerUpdate::Flag::AdminsChanged;
Notify::peerUpdatedDelayed(update);
@ -1775,7 +1775,9 @@ void ApiWrap::readFeaturedSets() {
void ApiWrap::parseChannelParticipants(
const MTPchannels_ChannelParticipants &result,
base::lambda<void(int fullCount, const QVector<MTPChannelParticipant> &list)> callbackList,
base::lambda<void(
int availableCount,
const QVector<MTPChannelParticipant> &list)> callbackList,
base::lambda<void()> callbackNotModified) {
TLHelp::VisitChannelParticipants(result, base::overload([&](
const MTPDchannels_channelParticipants &data) {
@ -1795,22 +1797,24 @@ void ApiWrap::parseChannelParticipants(
void ApiWrap::parseRecentChannelParticipants(
not_null<ChannelData*> channel,
const MTPchannels_ChannelParticipants &result,
base::lambda<void(int fullCount, const QVector<MTPChannelParticipant> &list)> callbackList,
base::lambda<void(
int availableCount,
const QVector<MTPChannelParticipant> &list)> callbackList,
base::lambda<void()> callbackNotModified) {
parseChannelParticipants(result, [&](
int fullCount,
int availableCount,
const QVector<MTPChannelParticipant> &list) {
auto applyLast = channel->isMegagroup()
&& (channel->mgInfo->lastParticipants.size() <= list.size());
if (applyLast) {
applyLastParticipantsList(
channel,
fullCount,
availableCount,
list,
false,
true);
}
callbackList(fullCount, list);
callbackList(availableCount, list);
}, std::move(callbackNotModified));
}
@ -1819,13 +1823,43 @@ void ApiWrap::applyUpdatesNoPtsCheck(const MTPUpdates &updates) {
case mtpc_updateShortMessage: {
auto &d = updates.c_updateShortMessage();
auto flags = mtpCastFlags(d.vflags.v) | MTPDmessage::Flag::f_from_id;
App::histories().addNewMessage(MTP_message(MTP_flags(flags), d.vid, d.is_out() ? MTP_int(Auth().userId()) : d.vuser_id, MTP_peerUser(d.is_out() ? d.vuser_id : MTP_int(Auth().userId())), d.vfwd_from, d.vvia_bot_id, d.vreply_to_msg_id, d.vdate, d.vmessage, MTP_messageMediaEmpty(), MTPnullMarkup, d.has_entities() ? d.ventities : MTPnullEntities, MTPint(), MTPint(), MTPstring()), NewMessageUnread);
App::histories().addNewMessage(MTP_message(
MTP_flags(flags),
d.vid,
d.is_out() ? MTP_int(Auth().userId()) : d.vuser_id,
MTP_peerUser(d.is_out() ? d.vuser_id : MTP_int(Auth().userId())),
d.vfwd_from,
d.vvia_bot_id,
d.vreply_to_msg_id,
d.vdate,
d.vmessage,
MTP_messageMediaEmpty(),
MTPnullMarkup,
d.has_entities() ? d.ventities : MTPnullEntities,
MTPint(),
MTPint(),
MTPstring()), NewMessageUnread);
} break;
case mtpc_updateShortChatMessage: {
auto &d = updates.c_updateShortChatMessage();
auto flags = mtpCastFlags(d.vflags.v) | MTPDmessage::Flag::f_from_id;
App::histories().addNewMessage(MTP_message(MTP_flags(flags), d.vid, d.vfrom_id, MTP_peerChat(d.vchat_id), d.vfwd_from, d.vvia_bot_id, d.vreply_to_msg_id, d.vdate, d.vmessage, MTP_messageMediaEmpty(), MTPnullMarkup, d.has_entities() ? d.ventities : MTPnullEntities, MTPint(), MTPint(), MTPstring()), NewMessageUnread);
App::histories().addNewMessage(MTP_message(
MTP_flags(flags),
d.vid,
d.vfrom_id,
MTP_peerChat(d.vchat_id),
d.vfwd_from,
d.vvia_bot_id,
d.vreply_to_msg_id,
d.vdate,
d.vmessage,
MTP_messageMediaEmpty(),
MTPnullMarkup,
d.has_entities() ? d.ventities : MTPnullEntities,
MTPint(),
MTPint(),
MTPstring()), NewMessageUnread);
} break;
case mtpc_updateShortSentMessage: {

View File

@ -61,7 +61,7 @@ public:
void requestFullPeer(PeerData *peer);
void requestPeer(PeerData *peer);
void requestPeers(const QList<PeerData*> &peers);
void requestLastParticipants(ChannelData *channel, bool fromStart = true);
void requestLastParticipants(ChannelData *channel);
void requestBots(ChannelData *channel);
void requestParticipantsCountDelayed(ChannelData *channel);
@ -145,12 +145,16 @@ public:
void parseChannelParticipants(
const MTPchannels_ChannelParticipants &result,
base::lambda<void(int fullCount, const QVector<MTPChannelParticipant> &list)> callbackList,
base::lambda<void(
int availableCount,
const QVector<MTPChannelParticipant> &list)> callbackList,
base::lambda<void()> callbackNotModified = nullptr);
void parseRecentChannelParticipants(
not_null<ChannelData*> channel,
const MTPchannels_ChannelParticipants &result,
base::lambda<void(int fullCount, const QVector<MTPChannelParticipant> &list)> callbackList,
base::lambda<void(
int availableCount,
const QVector<MTPChannelParticipant> &list)> callbackList,
base::lambda<void()> callbackNotModified = nullptr);
~ApiWrap();
@ -188,7 +192,7 @@ private:
mtpRequestId req);
void applyLastParticipantsList(
ChannelData *peer,
int fullCount,
int availableCount,
const QVector<MTPChannelParticipant> &list,
bool bots,
bool fromStart);

View File

@ -358,7 +358,7 @@ void InnerWidget::requestAdmins() {
return false;
});
Auth().api().parseChannelParticipants(result, [&](
int fullCount,
int availableCount,
const QVector<MTPChannelParticipant> &list) {
auto filtered = (
list

View File

@ -668,7 +668,7 @@ void HistoryTopBarWidget::updateOnlineDisplay() {
}
} else if (auto channel = _historyPeer->asChannel()) {
if (channel->isMegagroup() && channel->membersCount() > 0 && channel->membersCount() <= Global::ChatSizeMax()) {
if (channel->mgInfo->lastParticipants.size() < channel->membersCount() || channel->lastParticipantsCountOutdated()) {
if (channel->mgInfo->lastParticipants.isEmpty() || channel->lastParticipantsCountOutdated()) {
Auth().api().requestLastParticipants(channel);
}
auto online = 0;

View File

@ -175,12 +175,15 @@ void GroupMembersWidget::refreshUserOnline(UserData *user) {
}
void GroupMembersWidget::preloadMore() {
if (auto megagroup = peer()->asMegagroup()) {
auto &megagroupInfo = megagroup->mgInfo;
if (!megagroupInfo->lastParticipants.isEmpty() && megagroupInfo->lastParticipants.size() < megagroup->membersCount()) {
Auth().api().requestLastParticipants(megagroup, false);
}
}
//
// This can cause a ddos, because lastParticipants may never reach members count.
//
//if (auto megagroup = peer()->asMegagroup()) {
// auto &megagroupInfo = megagroup->mgInfo;
// if (!megagroupInfo->lastParticipants.isEmpty() && megagroupInfo->lastParticipants.size() < megagroup->membersCount()) {
// Auth().api().requestLastParticipants(megagroup, false);
// }
//}
}
int GroupMembersWidget::resizeGetHeight(int newWidth) {

View File

@ -480,7 +480,7 @@ void ParticipantsBoxController::loadMoreRows() {
}
};
parseParticipants(result, [&](
int fullCount,
int availableCount,
const QVector<MTPChannelParticipant> &list) {
for (auto &participant : list) {
HandleParticipant(
@ -541,13 +541,6 @@ bool ParticipantsBoxController::feedMegagroupLastParticipants() {
if (info->lastParticipants.isEmpty()) {
return false;
}
if ((info->lastParticipants.size() < Global::ChatSizeMax() / 2)
&& (_channel->membersCount() > Global::ChatSizeMax())) {
// If we have really small non-empty count of last participants.
// Request them from the beginning so that we won't do that each
// time we open megagroup profile.
Auth().api().requestLastParticipants(_channel);
}
if (info->creator) {
_additional.creator = info->creator;
@ -1192,7 +1185,7 @@ void AddParticipantBoxController::loadMoreRows() {
_loadRequestId = 0;
Auth().api().parseChannelParticipants(result, [&](
int fullCount,
int availableCount,
const QVector<MTPChannelParticipant> &list) {
for (auto &participant : list) {
HandleParticipant(

View File

@ -712,6 +712,8 @@ void UserpicButton::grabOldUserpic() {
void UserpicButton::startNewPhotoShowing() {
auto oldUniqueKey = _userpicUniqueKey;
prepareUserpicPixmap();
update();
if (_notShownYet) {
return;
}
@ -719,7 +721,6 @@ void UserpicButton::startNewPhotoShowing() {
|| _a_appearance.animating()) {
startAnimation();
}
update();
}
void UserpicButton::startAnimation() {

View File

@ -508,7 +508,7 @@ void PeerMenuAddChannelMembers(not_null<ChannelData*> channel) {
}
auto callback = [channel](const MTPchannels_ChannelParticipants &result) {
Auth().api().parseChannelParticipants(result, [&](
int fullCount,
int availableCount,
const QVector<MTPChannelParticipant> &list) {
auto already = (
list