mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-07 15:43:55 +02:00
Allow pinning chats in folders when 100 added.
This commit is contained in:
parent
d1a913450a
commit
3b9ac19482
3 changed files with 43 additions and 55 deletions
|
@ -1823,30 +1823,25 @@ void Session::applyDialog(
|
||||||
setPinnedFromDialog(folder, data.is_pinned());
|
setPinnedFromDialog(folder, data.is_pinned());
|
||||||
}
|
}
|
||||||
|
|
||||||
int Session::pinnedChatsCount(
|
int Session::pinnedCanPin(
|
||||||
Data::Folder *folder,
|
Data::Folder *folder,
|
||||||
FilterId filterId) const {
|
FilterId filterId,
|
||||||
|
not_null<History*> history) const {
|
||||||
if (!filterId) {
|
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 &list = chatsFilters().list();
|
||||||
const auto i = ranges::find(list, filterId, &Data::ChatFilter::id);
|
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(
|
int Session::pinnedChatsLimit(Data::Folder *folder) const {
|
||||||
Data::Folder *folder,
|
return folder
|
||||||
FilterId filterId) const {
|
? session().serverConfig().pinnedDialogsInFolderMax.current()
|
||||||
if (!filterId) {
|
: session().serverConfig().pinnedDialogsCountMax.current();
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::vector<Dialogs::Key> &Session::pinnedChatsOrder(
|
const std::vector<Dialogs::Key> &Session::pinnedChatsOrder(
|
||||||
|
|
|
@ -322,8 +322,11 @@ public:
|
||||||
const QVector<MTPDialog> &dialogs,
|
const QVector<MTPDialog> &dialogs,
|
||||||
std::optional<int> count = std::nullopt);
|
std::optional<int> count = std::nullopt);
|
||||||
|
|
||||||
int pinnedChatsCount(Data::Folder *folder, FilterId filterId) const;
|
int pinnedCanPin(
|
||||||
int pinnedChatsLimit(Data::Folder *folder, FilterId filterId) const;
|
Data::Folder *folder,
|
||||||
|
FilterId filterId,
|
||||||
|
not_null<History*> history) const;
|
||||||
|
int pinnedChatsLimit(Data::Folder *folder) const;
|
||||||
const std::vector<Dialogs::Key> &pinnedChatsOrder(
|
const std::vector<Dialogs::Key> &pinnedChatsOrder(
|
||||||
Data::Folder *folder,
|
Data::Folder *folder,
|
||||||
FilterId filterId) const;
|
FilterId filterId) const;
|
||||||
|
|
|
@ -197,31 +197,28 @@ void AddChatMembers(
|
||||||
|
|
||||||
bool PinnedLimitReached(
|
bool PinnedLimitReached(
|
||||||
not_null<Window::SessionController*> controller,
|
not_null<Window::SessionController*> controller,
|
||||||
Dialogs::Key key,
|
not_null<History*> history,
|
||||||
FilterId filterId) {
|
FilterId filterId) {
|
||||||
Expects(filterId != 0 || key.entry()->folderKnown());
|
Expects(filterId != 0 || history->folderKnown());
|
||||||
|
|
||||||
const auto entry = key.entry();
|
const auto owner = &history->owner();
|
||||||
const auto owner = &entry->owner();
|
const auto folder = history->folder();
|
||||||
const auto folder = entry->folder();
|
if (owner->pinnedCanPin(folder, filterId, history)) {
|
||||||
const auto pinnedCount = owner->pinnedChatsCount(folder, filterId);
|
|
||||||
const auto pinnedMax = owner->pinnedChatsLimit(folder, filterId);
|
|
||||||
if (pinnedCount < pinnedMax) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// Some old chat, that was converted, maybe is still pinned.
|
// Some old chat, that was converted, maybe is still pinned.
|
||||||
const auto wasted = filterId ? nullptr : FindWastedPin(owner, folder);
|
const auto wasted = filterId ? nullptr : FindWastedPin(owner, folder);
|
||||||
if (wasted) {
|
if (wasted) {
|
||||||
owner->setChatPinned(wasted, FilterId(), false);
|
owner->setChatPinned(wasted, FilterId(), false);
|
||||||
owner->setChatPinned(key, FilterId(), true);
|
owner->setChatPinned(history, FilterId(), true);
|
||||||
entry->session().api().savePinnedOrder(folder);
|
history->session().api().savePinnedOrder(folder);
|
||||||
} else {
|
} else {
|
||||||
const auto errorText = filterId
|
const auto errorText = filterId
|
||||||
? tr::lng_filters_error_pinned_max(tr::now)
|
? tr::lng_filters_error_pinned_max(tr::now)
|
||||||
: tr::lng_error_pinned_max(
|
: tr::lng_error_pinned_max(
|
||||||
tr::now,
|
tr::now,
|
||||||
lt_count,
|
lt_count,
|
||||||
pinnedMax);
|
owner->pinnedChatsLimit(folder));
|
||||||
controller->show(
|
controller->show(
|
||||||
Ui::MakeInformBox(errorText),
|
Ui::MakeInformBox(errorText),
|
||||||
Ui::LayerOption::CloseOther);
|
Ui::LayerOption::CloseOther);
|
||||||
|
@ -231,33 +228,26 @@ bool PinnedLimitReached(
|
||||||
|
|
||||||
void TogglePinnedDialog(
|
void TogglePinnedDialog(
|
||||||
not_null<Window::SessionController*> controller,
|
not_null<Window::SessionController*> controller,
|
||||||
Dialogs::Key key) {
|
not_null<History*> history) {
|
||||||
if (!key.entry()->folderKnown()) {
|
if (!history->folderKnown()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const auto owner = &key.entry()->owner();
|
const auto owner = &history->owner();
|
||||||
const auto isPinned = !key.entry()->isPinnedDialog(0);
|
const auto isPinned = !history->isPinnedDialog(0);
|
||||||
if (isPinned && PinnedLimitReached(controller, key, 0)) {
|
if (isPinned && PinnedLimitReached(controller, history, 0)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
owner->setChatPinned(key, FilterId(), isPinned);
|
owner->setChatPinned(history, FilterId(), isPinned);
|
||||||
const auto flags = isPinned
|
const auto flags = isPinned
|
||||||
? MTPmessages_ToggleDialogPin::Flag::f_pinned
|
? MTPmessages_ToggleDialogPin::Flag::f_pinned
|
||||||
: MTPmessages_ToggleDialogPin::Flag(0);
|
: MTPmessages_ToggleDialogPin::Flag(0);
|
||||||
if (const auto history = key.history()) {
|
history->session().api().request(MTPmessages_ToggleDialogPin(
|
||||||
history->session().api().request(MTPmessages_ToggleDialogPin(
|
MTP_flags(flags),
|
||||||
MTP_flags(flags),
|
MTP_inputDialogPeer(history->peer->input)
|
||||||
MTP_inputDialogPeer(key.history()->peer->input)
|
)).done([=] {
|
||||||
)).done([=] {
|
owner->notifyPinnedDialogsOrderUpdated();
|
||||||
owner->notifyPinnedDialogsOrderUpdated();
|
}).send();
|
||||||
}).send();
|
|
||||||
} else if (const auto folder = key.folder()) {
|
|
||||||
folder->session().api().request(MTPmessages_ToggleDialogPin(
|
|
||||||
MTP_flags(flags),
|
|
||||||
MTP_inputDialogPeerFolder(MTP_int(folder->id()))
|
|
||||||
)).send();
|
|
||||||
}
|
|
||||||
if (isPinned) {
|
if (isPinned) {
|
||||||
controller->content()->dialogsToUp();
|
controller->content()->dialogsToUp();
|
||||||
}
|
}
|
||||||
|
@ -265,12 +255,12 @@ void TogglePinnedDialog(
|
||||||
|
|
||||||
void TogglePinnedDialog(
|
void TogglePinnedDialog(
|
||||||
not_null<Window::SessionController*> controller,
|
not_null<Window::SessionController*> controller,
|
||||||
Dialogs::Key key,
|
not_null<History*> history,
|
||||||
FilterId filterId) {
|
FilterId filterId) {
|
||||||
if (!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.
|
// This can happen when you remove this filter from another client.
|
||||||
if (!ranges::contains(
|
if (!ranges::contains(
|
||||||
|
@ -281,12 +271,12 @@ void TogglePinnedDialog(
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto isPinned = !key.entry()->isPinnedDialog(filterId);
|
const auto isPinned = !history->isPinnedDialog(filterId);
|
||||||
if (isPinned && PinnedLimitReached(controller, key, filterId)) {
|
if (isPinned && PinnedLimitReached(controller, history, filterId)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
owner->setChatPinned(key, filterId, isPinned);
|
owner->setChatPinned(history, filterId, isPinned);
|
||||||
Api::SaveNewFilterPinned(&owner->session(), filterId);
|
Api::SaveNewFilterPinned(&owner->session(), filterId);
|
||||||
if (isPinned) {
|
if (isPinned) {
|
||||||
controller->content()->dialogsToUp();
|
controller->content()->dialogsToUp();
|
||||||
|
|
Loading…
Add table
Reference in a new issue