tdesktop/Telegram/SourceFiles/data/data_chat_filters.h

161 lines
4.2 KiB
C
Raw Normal View History

2020-02-07 09:43:12 +00:00
/*
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
#include "base/flags.h"
class History;
namespace Dialogs {
class MainList;
class Key;
} // namespace Dialogs
2020-02-07 09:43:12 +00:00
namespace Data {
2020-02-07 11:34:05 +00:00
class Session;
2020-02-07 09:43:12 +00:00
class ChatFilter final {
public:
enum class Flag : uchar {
2020-02-07 16:07:21 +00:00
Contacts = 0x01,
NonContacts = 0x02,
Groups = 0x04,
Channels = 0x08,
2020-02-07 16:07:21 +00:00
Bots = 0x10,
NoMuted = 0x20,
NoRead = 0x40,
NoArchived = 0x80,
2020-02-07 09:43:12 +00:00
};
friend constexpr inline bool is_flag_type(Flag) { return true; };
using Flags = base::flags<Flag>;
ChatFilter() = default;
ChatFilter(
2020-02-07 16:07:21 +00:00
FilterId id,
2020-02-07 09:43:12 +00:00
const QString &title,
const QString &iconEmoji,
2020-02-07 09:43:12 +00:00
Flags flags,
2020-02-07 16:07:21 +00:00
base::flat_set<not_null<History*>> always,
2020-03-17 13:04:30 +00:00
std::vector<not_null<History*>> pinned,
2020-02-07 16:07:21 +00:00
base::flat_set<not_null<History*>> never);
2020-02-07 09:43:12 +00:00
2020-02-07 16:07:21 +00:00
[[nodiscard]] static ChatFilter FromTL(
const MTPDialogFilter &data,
not_null<Session*> owner);
[[nodiscard]] MTPDialogFilter tl(FilterId replaceId = 0) const;
2020-02-07 16:07:21 +00:00
[[nodiscard]] FilterId id() const;
2020-02-07 11:34:05 +00:00
[[nodiscard]] QString title() const;
[[nodiscard]] QString iconEmoji() const;
2020-02-07 16:07:21 +00:00
[[nodiscard]] Flags flags() const;
[[nodiscard]] const base::flat_set<not_null<History*>> &always() const;
2020-03-17 13:04:30 +00:00
[[nodiscard]] const std::vector<not_null<History*>> &pinned() const;
2020-02-07 16:07:21 +00:00
[[nodiscard]] const base::flat_set<not_null<History*>> &never() const;
2020-02-07 11:34:05 +00:00
2020-02-07 09:43:12 +00:00
[[nodiscard]] bool contains(not_null<History*> history) const;
private:
2020-02-07 16:07:21 +00:00
FilterId _id = 0;
2020-02-07 09:43:12 +00:00
QString _title;
QString _iconEmoji;
2020-02-07 09:43:12 +00:00
base::flat_set<not_null<History*>> _always;
2020-03-17 13:04:30 +00:00
std::vector<not_null<History*>> _pinned;
2020-02-07 16:07:21 +00:00
base::flat_set<not_null<History*>> _never;
2020-02-07 09:43:12 +00:00
Flags _flags;
};
inline bool operator==(const ChatFilter &a, const ChatFilter &b) {
return (a.title() == b.title())
2020-03-19 14:55:17 +00:00
&& (a.iconEmoji() == b.iconEmoji())
&& (a.flags() == b.flags())
&& (a.always() == b.always())
&& (a.never() == b.never());
}
inline bool operator!=(const ChatFilter &a, const ChatFilter &b) {
return !(a == b);
}
struct SuggestedFilter {
ChatFilter filter;
QString description;
};
2020-02-07 11:34:05 +00:00
class ChatFilters final {
public:
explicit ChatFilters(not_null<Session*> owner);
~ChatFilters();
2020-02-07 11:34:05 +00:00
void setPreloaded(const QVector<MTPDialogFilter> &result);
2020-02-07 16:07:21 +00:00
void load();
void apply(const MTPUpdate &update);
void set(ChatFilter filter);
void remove(FilterId id);
void moveAllToFront();
2020-02-07 16:07:21 +00:00
[[nodiscard]] const std::vector<ChatFilter> &list() const;
[[nodiscard]] rpl::producer<> changed() const;
[[nodiscard]] bool loaded() const;
[[nodiscard]] bool has() const;
2020-02-07 11:34:05 +00:00
[[nodiscard]] FilterId defaultId() const;
[[nodiscard]] FilterId lookupId(int index) const;
bool loadNextExceptions(bool chatsListLoaded);
2020-02-07 11:34:05 +00:00
void refreshHistory(not_null<History*> history);
[[nodiscard]] not_null<Dialogs::MainList*> chatsList(FilterId filterId);
const ChatFilter &applyUpdatedPinned(
FilterId id,
const std::vector<Dialogs::Key> &dialogs);
2020-03-20 12:48:52 +00:00
void saveOrder(
const std::vector<FilterId> &order,
mtpRequestId after = 0);
[[nodiscard]] bool archiveNeeded() const;
void requestSuggested();
[[nodiscard]] bool suggestedLoaded() const;
[[nodiscard]] auto suggestedFilters() const
-> const std::vector<SuggestedFilter> &;
[[nodiscard]] rpl::producer<> suggestedUpdated() const;
2020-02-07 11:34:05 +00:00
private:
2020-02-07 16:07:21 +00:00
void load(bool force);
void received(const QVector<MTPDialogFilter> &list);
2020-02-07 16:07:21 +00:00
bool applyOrder(const QVector<MTPint> &order);
bool applyChange(ChatFilter &filter, ChatFilter &&updated);
void applyInsert(ChatFilter filter, int position);
void applyRemove(int position);
2020-02-07 11:34:05 +00:00
const not_null<Session*> _owner;
2020-02-07 16:07:21 +00:00
std::vector<ChatFilter> _list;
base::flat_map<FilterId, std::unique_ptr<Dialogs::MainList>> _chatsLists;
2020-02-07 16:07:21 +00:00
rpl::event_stream<> _listChanged;
mtpRequestId _loadRequestId = 0;
2020-03-20 12:48:52 +00:00
mtpRequestId _saveOrderRequestId = 0;
mtpRequestId _saveOrderAfterId = 0;
bool _loaded = false;
2020-02-07 11:34:05 +00:00
mtpRequestId _suggestedRequestId = 0;
std::vector<SuggestedFilter> _suggested;
rpl::event_stream<> _suggestedUpdated;
crl::time _suggestedLastReceived = 0;
std::deque<FilterId> _exceptionsToLoad;
mtpRequestId _exceptionsLoadRequestId = 0;
2020-02-07 11:34:05 +00:00
};
2020-02-07 09:43:12 +00:00
} // namespace Data