Allow pinning chats in folders when 100 added.

This commit is contained in:
John Preston 2022-03-14 11:11:14 +04:00
parent d1a913450a
commit 3b9ac19482
3 changed files with 43 additions and 55 deletions

View File

@ -1823,30 +1823,25 @@ void Session::applyDialog(
setPinnedFromDialog(folder, data.is_pinned());
}
int Session::pinnedChatsCount(
int Session::pinnedCanPin(
Data::Folder *folder,
FilterId filterId) const {
FilterId filterId,
not_null<History*> history) const {
if (!filterId) {
return pinnedChatsOrder(folder, filterId).size();
const auto limit = pinnedChatsLimit(folder);
return pinnedChatsOrder(folder, FilterId()).size() < limit;
}
const auto &list = chatsFilters().list();
const auto i = ranges::find(list, filterId, &Data::ChatFilter::id);
return (i != end(list)) ? i->pinned().size() : 0;
return (i == end(list))
|| (i->always().contains(history))
|| (i->always().size() < Data::ChatFilter::kPinnedLimit);
}
int Session::pinnedChatsLimit(
Data::Folder *folder,
FilterId filterId) const {
if (!filterId) {
return folder
? session().serverConfig().pinnedDialogsInFolderMax.current()
: session().serverConfig().pinnedDialogsCountMax.current();
}
const auto &list = chatsFilters().list();
const auto i = ranges::find(list, filterId, &Data::ChatFilter::id);
const auto pinned = (i != end(list)) ? i->pinned().size() : 0;
const auto already = (i != end(list)) ? i->always().size() : 0;
return Data::ChatFilter::kPinnedLimit + pinned - already;
int Session::pinnedChatsLimit(Data::Folder *folder) const {
return folder
? session().serverConfig().pinnedDialogsInFolderMax.current()
: session().serverConfig().pinnedDialogsCountMax.current();
}
const std::vector<Dialogs::Key> &Session::pinnedChatsOrder(

View File

@ -322,8 +322,11 @@ public:
const QVector<MTPDialog> &dialogs,
std::optional<int> count = std::nullopt);
int pinnedChatsCount(Data::Folder *folder, FilterId filterId) const;
int pinnedChatsLimit(Data::Folder *folder, FilterId filterId) const;
int pinnedCanPin(
Data::Folder *folder,
FilterId filterId,
not_null<History*> history) const;
int pinnedChatsLimit(Data::Folder *folder) const;
const std::vector<Dialogs::Key> &pinnedChatsOrder(
Data::Folder *folder,
FilterId filterId) const;

View File

@ -197,31 +197,28 @@ void AddChatMembers(
bool PinnedLimitReached(
not_null<Window::SessionController*> controller,
Dialogs::Key key,
not_null<History*> history,
FilterId filterId) {
Expects(filterId != 0 || key.entry()->folderKnown());
Expects(filterId != 0 || history->folderKnown());
const auto entry = key.entry();
const auto owner = &entry->owner();
const auto folder = entry->folder();
const auto pinnedCount = owner->pinnedChatsCount(folder, filterId);
const auto pinnedMax = owner->pinnedChatsLimit(folder, filterId);
if (pinnedCount < pinnedMax) {
const auto owner = &history->owner();
const auto folder = history->folder();
if (owner->pinnedCanPin(folder, filterId, history)) {
return false;
}
// Some old chat, that was converted, maybe is still pinned.
const auto wasted = filterId ? nullptr : FindWastedPin(owner, folder);
if (wasted) {
owner->setChatPinned(wasted, FilterId(), false);
owner->setChatPinned(key, FilterId(), true);
entry->session().api().savePinnedOrder(folder);
owner->setChatPinned(history, FilterId(), true);
history->session().api().savePinnedOrder(folder);
} else {
const auto errorText = filterId
? tr::lng_filters_error_pinned_max(tr::now)
: tr::lng_error_pinned_max(
tr::now,
lt_count,
pinnedMax);
owner->pinnedChatsLimit(folder));
controller->show(
Ui::MakeInformBox(errorText),
Ui::LayerOption::CloseOther);
@ -231,33 +228,26 @@ bool PinnedLimitReached(
void TogglePinnedDialog(
not_null<Window::SessionController*> controller,
Dialogs::Key key) {
if (!key.entry()->folderKnown()) {
not_null<History*> history) {
if (!history->folderKnown()) {
return;
}
const auto owner = &key.entry()->owner();
const auto isPinned = !key.entry()->isPinnedDialog(0);
if (isPinned && PinnedLimitReached(controller, key, 0)) {
const auto owner = &history->owner();
const auto isPinned = !history->isPinnedDialog(0);
if (isPinned && PinnedLimitReached(controller, history, 0)) {
return;
}
owner->setChatPinned(key, FilterId(), isPinned);
owner->setChatPinned(history, FilterId(), isPinned);
const auto flags = isPinned
? MTPmessages_ToggleDialogPin::Flag::f_pinned
: MTPmessages_ToggleDialogPin::Flag(0);
if (const auto history = key.history()) {
history->session().api().request(MTPmessages_ToggleDialogPin(
MTP_flags(flags),
MTP_inputDialogPeer(key.history()->peer->input)
)).done([=] {
owner->notifyPinnedDialogsOrderUpdated();
}).send();
} else if (const auto folder = key.folder()) {
folder->session().api().request(MTPmessages_ToggleDialogPin(
MTP_flags(flags),
MTP_inputDialogPeerFolder(MTP_int(folder->id()))
)).send();
}
history->session().api().request(MTPmessages_ToggleDialogPin(
MTP_flags(flags),
MTP_inputDialogPeer(history->peer->input)
)).done([=] {
owner->notifyPinnedDialogsOrderUpdated();
}).send();
if (isPinned) {
controller->content()->dialogsToUp();
}
@ -265,12 +255,12 @@ void TogglePinnedDialog(
void TogglePinnedDialog(
not_null<Window::SessionController*> controller,
Dialogs::Key key,
not_null<History*> history,
FilterId filterId) {
if (!filterId) {
return TogglePinnedDialog(controller, key);
return TogglePinnedDialog(controller, history);
}
const auto owner = &key.entry()->owner();
const auto owner = &history->owner();
// This can happen when you remove this filter from another client.
if (!ranges::contains(
@ -281,12 +271,12 @@ void TogglePinnedDialog(
return;
}
const auto isPinned = !key.entry()->isPinnedDialog(filterId);
if (isPinned && PinnedLimitReached(controller, key, filterId)) {
const auto isPinned = !history->isPinnedDialog(filterId);
if (isPinned && PinnedLimitReached(controller, history, filterId)) {
return;
}
owner->setChatPinned(key, filterId, isPinned);
owner->setChatPinned(history, filterId, isPinned);
Api::SaveNewFilterPinned(&owner->session(), filterId);
if (isPinned) {
controller->content()->dialogsToUp();