2016-05-24 16:13:07 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2017-01-11 18:31:31 +00:00
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
2016-05-24 16:13:07 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2017-10-24 17:11:35 +00:00
|
|
|
#include <rpl/producer.h>
|
|
|
|
#include <rpl/filter.h>
|
|
|
|
#include <rpl/then.h>
|
|
|
|
#include <rpl/range.h>
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "base/observer.h"
|
2017-08-31 16:28:58 +00:00
|
|
|
#include "base/flags.h"
|
2016-05-24 16:13:07 +00:00
|
|
|
|
|
|
|
namespace Notify {
|
|
|
|
|
|
|
|
// Generic notifications about updates of some PeerData.
|
|
|
|
// You can subscribe to them by Notify::registerPeerObserver().
|
2016-05-25 12:09:05 +00:00
|
|
|
// 0x0000FFFFU for general peer updates (valid for any peer).
|
|
|
|
// 0xFFFF0000U for specific peer updates (valid for user / chat / channel).
|
2016-05-24 16:13:07 +00:00
|
|
|
struct PeerUpdate {
|
2016-05-25 12:09:05 +00:00
|
|
|
PeerUpdate(PeerData *updated = nullptr) : peer(updated) {
|
|
|
|
}
|
|
|
|
PeerData *peer;
|
2016-06-01 20:05:37 +00:00
|
|
|
|
2017-08-03 12:52:56 +00:00
|
|
|
enum class Flag : uint32 {
|
2017-09-03 18:36:06 +00:00
|
|
|
None = 0,
|
2017-06-09 16:12:02 +00:00
|
|
|
|
2016-06-20 15:40:36 +00:00
|
|
|
// Common flags
|
2017-09-03 18:36:06 +00:00
|
|
|
NameChanged = (1 << 0),
|
|
|
|
UsernameChanged = (1 << 1),
|
|
|
|
PhotoChanged = (1 << 2),
|
|
|
|
AboutChanged = (1 << 3),
|
|
|
|
NotificationsEnabled = (1 << 4),
|
|
|
|
SharedMediaChanged = (1 << 5),
|
|
|
|
MigrationChanged = (1 << 6),
|
|
|
|
PinnedChanged = (1 << 7),
|
|
|
|
RestrictionReasonChanged = (1 << 8),
|
2016-06-02 13:02:55 +00:00
|
|
|
|
|
|
|
// For chats and channels
|
2017-09-03 18:36:06 +00:00
|
|
|
InviteLinkChanged = (1 << 9),
|
|
|
|
MembersChanged = (1 << 10),
|
|
|
|
AdminsChanged = (1 << 11),
|
|
|
|
BannedUsersChanged = (1 << 12),
|
|
|
|
UnreadMentionsChanged = (1 << 13),
|
2016-06-02 13:02:55 +00:00
|
|
|
|
2016-06-20 15:40:36 +00:00
|
|
|
// For users
|
2017-09-03 18:36:06 +00:00
|
|
|
UserCanShareContact = (1 << 16),
|
|
|
|
UserIsContact = (1 << 17),
|
|
|
|
UserPhoneChanged = (1 << 18),
|
|
|
|
UserIsBlocked = (1 << 19),
|
|
|
|
BotCommandsChanged = (1 << 20),
|
|
|
|
UserOnlineChanged = (1 << 21),
|
|
|
|
BotCanAddToGroups = (1 << 22),
|
|
|
|
UserCommonChatsChanged = (1 << 23),
|
|
|
|
UserHasCalls = (1 << 24),
|
2016-06-02 13:02:55 +00:00
|
|
|
|
2016-06-20 15:40:36 +00:00
|
|
|
// For chats
|
2017-09-03 18:36:06 +00:00
|
|
|
ChatCanEdit = (1 << 16),
|
2016-06-02 13:02:55 +00:00
|
|
|
|
2016-06-20 15:40:36 +00:00
|
|
|
// For channels
|
2017-09-03 18:36:06 +00:00
|
|
|
ChannelAmIn = (1 << 16),
|
|
|
|
ChannelRightsChanged = (1 << 17),
|
|
|
|
ChannelStickersChanged = (1 << 18),
|
|
|
|
ChannelPinnedChanged = (1 << 19),
|
2016-06-01 20:05:37 +00:00
|
|
|
};
|
2017-08-31 16:28:58 +00:00
|
|
|
using Flags = base::flags<Flag>;
|
|
|
|
friend inline constexpr auto is_flag_type(Flag) { return true; }
|
|
|
|
|
2016-06-01 20:05:37 +00:00
|
|
|
Flags flags = 0;
|
2016-05-25 12:09:05 +00:00
|
|
|
|
|
|
|
// NameChanged data
|
|
|
|
PeerData::NameFirstChars oldNameFirstChars;
|
2016-06-02 13:02:55 +00:00
|
|
|
|
|
|
|
// SharedMediaChanged data
|
|
|
|
int32 mediaTypesMask = 0;
|
|
|
|
|
2016-05-24 16:13:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void peerUpdatedDelayed(const PeerUpdate &update);
|
2016-06-01 20:05:37 +00:00
|
|
|
inline void peerUpdatedDelayed(PeerData *peer, PeerUpdate::Flags events) {
|
|
|
|
PeerUpdate update(peer);
|
|
|
|
update.flags = events;
|
|
|
|
peerUpdatedDelayed(update);
|
|
|
|
}
|
2016-05-24 16:13:07 +00:00
|
|
|
void peerUpdatedSendDelayed();
|
|
|
|
|
2017-01-01 16:45:20 +00:00
|
|
|
inline void mediaOverviewUpdated(PeerData *peer, MediaOverviewType type) {
|
|
|
|
PeerUpdate update(peer);
|
|
|
|
update.flags |= PeerUpdate::Flag::SharedMediaChanged;
|
|
|
|
update.mediaTypesMask |= (1 << type);
|
|
|
|
peerUpdatedDelayed(update);
|
|
|
|
}
|
|
|
|
|
2016-09-26 14:50:24 +00:00
|
|
|
class PeerUpdatedHandler {
|
|
|
|
public:
|
|
|
|
template <typename Lambda>
|
2017-02-21 13:45:56 +00:00
|
|
|
PeerUpdatedHandler(PeerUpdate::Flags events, Lambda &&handler) : _events(events), _handler(std::move(handler)) {
|
2016-09-26 14:50:24 +00:00
|
|
|
}
|
|
|
|
void operator()(const PeerUpdate &update) const {
|
|
|
|
if (update.flags & _events) {
|
|
|
|
_handler(update);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
PeerUpdate::Flags _events;
|
2016-11-20 12:54:07 +00:00
|
|
|
base::lambda<void(const PeerUpdate&)> _handler;
|
2016-09-26 14:50:24 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
base::Observable<PeerUpdate, PeerUpdatedHandler> &PeerUpdated();
|
|
|
|
|
2017-10-24 17:11:35 +00:00
|
|
|
inline auto PeerUpdateViewer(
|
|
|
|
PeerUpdate::Flags flags) {
|
|
|
|
return rpl::make_producer<PeerUpdate>([=](
|
|
|
|
const auto &consumer) {
|
|
|
|
auto lifetime = rpl::lifetime();
|
|
|
|
lifetime.make_state<base::Subscription>(
|
|
|
|
PeerUpdated().add_subscription({ flags, [=](
|
|
|
|
const PeerUpdate &update) {
|
|
|
|
consumer.put_next_copy(update);
|
|
|
|
}}));
|
|
|
|
return lifetime;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto PeerUpdateViewer(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
PeerUpdate::Flags flags) {
|
|
|
|
return PeerUpdateViewer(flags)
|
|
|
|
| rpl::filter([=](const PeerUpdate &update) {
|
|
|
|
return (update.peer == peer);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
inline auto PeerUpdateValue(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
PeerUpdate::Flags flags) {
|
|
|
|
auto initial = PeerUpdate(peer);
|
|
|
|
initial.flags = flags;
|
|
|
|
return rpl::single(initial)
|
|
|
|
| rpl::then(PeerUpdateViewer(peer, flags));
|
|
|
|
}
|
|
|
|
|
2016-05-24 16:13:07 +00:00
|
|
|
} // namespace Notify
|