2016-05-24 16:13:07 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-05-24 16:13:07 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-05-24 16:13:07 +00:00
|
|
|
*/
|
|
|
|
#include "observer_peer.h"
|
|
|
|
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "base/observer.h"
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "facades.h"
|
2016-05-24 16:13:07 +00:00
|
|
|
|
|
|
|
namespace Notify {
|
|
|
|
namespace {
|
|
|
|
|
2016-05-25 12:09:05 +00:00
|
|
|
using SmallUpdatesList = QVector<PeerUpdate>;
|
|
|
|
NeverFreedPointer<SmallUpdatesList> SmallUpdates;
|
|
|
|
using AllUpdatesList = QMap<PeerData*, PeerUpdate>;
|
|
|
|
NeverFreedPointer<AllUpdatesList> AllUpdates;
|
|
|
|
|
|
|
|
void StartCallback() {
|
2016-10-12 19:34:25 +00:00
|
|
|
SmallUpdates.createIfNull();
|
|
|
|
AllUpdates.createIfNull();
|
2016-05-25 12:09:05 +00:00
|
|
|
}
|
|
|
|
void FinishCallback() {
|
|
|
|
SmallUpdates.clear();
|
|
|
|
AllUpdates.clear();
|
|
|
|
}
|
2016-05-24 16:13:07 +00:00
|
|
|
|
2016-09-26 14:50:24 +00:00
|
|
|
base::Observable<PeerUpdate, PeerUpdatedHandler> PeerUpdatedObservable;
|
|
|
|
|
2016-05-24 16:13:07 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void mergePeerUpdate(PeerUpdate &mergeTo, const PeerUpdate &mergeFrom) {
|
2016-06-01 20:05:37 +00:00
|
|
|
if (!(mergeTo.flags & PeerUpdate::Flag::NameChanged)) {
|
|
|
|
if (mergeFrom.flags & PeerUpdate::Flag::NameChanged) {
|
2018-01-22 09:33:09 +00:00
|
|
|
mergeTo.oldNameFirstLetters = mergeFrom.oldNameFirstLetters;
|
2016-05-25 12:09:05 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-24 16:13:07 +00:00
|
|
|
mergeTo.flags |= mergeFrom.flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
void peerUpdatedDelayed(const PeerUpdate &update) {
|
2016-10-12 19:34:25 +00:00
|
|
|
SmallUpdates.createIfNull();
|
|
|
|
AllUpdates.createIfNull();
|
2016-05-25 12:09:05 +00:00
|
|
|
|
2016-06-02 13:02:55 +00:00
|
|
|
Global::RefHandleDelayedPeerUpdates().call();
|
|
|
|
|
2016-05-25 18:49:47 +00:00
|
|
|
int existingUpdatesCount = SmallUpdates->size();
|
2016-05-25 12:09:05 +00:00
|
|
|
for (int i = 0; i < existingUpdatesCount; ++i) {
|
2016-05-25 18:49:47 +00:00
|
|
|
auto &existingUpdate = (*SmallUpdates)[i];
|
2016-05-25 12:09:05 +00:00
|
|
|
if (existingUpdate.peer == update.peer) {
|
2016-05-25 18:49:47 +00:00
|
|
|
mergePeerUpdate(existingUpdate, update);
|
2016-05-24 16:13:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-09-26 18:33:34 +00:00
|
|
|
|
2016-05-25 18:49:47 +00:00
|
|
|
if (AllUpdates->isEmpty()) {
|
2016-05-25 12:09:05 +00:00
|
|
|
if (existingUpdatesCount < 5) {
|
2016-05-25 18:49:47 +00:00
|
|
|
SmallUpdates->push_back(update);
|
2016-05-24 16:13:07 +00:00
|
|
|
} else {
|
2016-05-25 18:49:47 +00:00
|
|
|
AllUpdates->insert(update.peer, update);
|
2016-05-24 16:13:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-05-25 18:49:47 +00:00
|
|
|
auto it = AllUpdates->find(update.peer);
|
|
|
|
if (it != AllUpdates->cend()) {
|
|
|
|
mergePeerUpdate(it.value(), update);
|
2016-05-24 16:13:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-05-25 18:49:47 +00:00
|
|
|
AllUpdates->insert(update.peer, update);
|
2016-05-24 16:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void peerUpdatedSendDelayed() {
|
2016-09-26 18:33:34 +00:00
|
|
|
if (!SmallUpdates || !AllUpdates || SmallUpdates->empty()) return;
|
2016-05-24 16:13:07 +00:00
|
|
|
|
2016-10-07 16:45:45 +00:00
|
|
|
auto smallList = base::take(*SmallUpdates);
|
|
|
|
auto allList = base::take(*AllUpdates);
|
2016-09-26 14:50:24 +00:00
|
|
|
for (auto &update : smallList) {
|
2017-02-21 13:45:56 +00:00
|
|
|
PeerUpdated().notify(std::move(update), true);
|
2016-09-26 14:50:24 +00:00
|
|
|
}
|
|
|
|
for (auto &update : allList) {
|
2017-02-21 13:45:56 +00:00
|
|
|
PeerUpdated().notify(std::move(update), true);
|
2016-09-26 14:50:24 +00:00
|
|
|
}
|
2016-09-26 18:33:34 +00:00
|
|
|
|
2016-05-25 18:49:47 +00:00
|
|
|
if (SmallUpdates->isEmpty()) {
|
|
|
|
std::swap(smallList, *SmallUpdates);
|
|
|
|
SmallUpdates->resize(0);
|
2016-05-24 16:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-26 14:50:24 +00:00
|
|
|
base::Observable<PeerUpdate, PeerUpdatedHandler> &PeerUpdated() {
|
|
|
|
return PeerUpdatedObservable;
|
|
|
|
}
|
|
|
|
|
2017-11-21 09:20:56 +00:00
|
|
|
rpl::producer<PeerUpdate> PeerUpdateViewer(
|
|
|
|
PeerUpdate::Flags flags) {
|
|
|
|
return [=](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;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<PeerUpdate> PeerUpdateViewer(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
PeerUpdate::Flags flags) {
|
2017-12-22 07:05:20 +00:00
|
|
|
return PeerUpdateViewer(
|
|
|
|
flags
|
|
|
|
) | rpl::filter([=](const PeerUpdate &update) {
|
|
|
|
return (update.peer == peer);
|
|
|
|
});
|
2017-11-21 09:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<PeerUpdate> PeerUpdateValue(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
PeerUpdate::Flags flags) {
|
|
|
|
auto initial = PeerUpdate(peer);
|
|
|
|
initial.flags = flags;
|
2017-12-22 07:05:20 +00:00
|
|
|
return rpl::single(
|
|
|
|
initial
|
|
|
|
) | rpl::then(PeerUpdateViewer(peer, flags));
|
2017-11-21 09:20:56 +00:00
|
|
|
}
|
|
|
|
|
2016-05-24 16:13:07 +00:00
|
|
|
} // namespace Notify
|