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
|
|
|
|
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
|
|
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "observer_peer.h"
|
|
|
|
|
|
|
|
#include "core/observer.h"
|
|
|
|
|
2016-05-25 12:09:05 +00:00
|
|
|
namespace App {
|
|
|
|
// Temp forward declaration (while all peer updates are not done through observers).
|
|
|
|
void emitPeerUpdated();
|
|
|
|
} // namespace App
|
|
|
|
|
2016-05-24 16:13:07 +00:00
|
|
|
namespace Notify {
|
|
|
|
namespace {
|
|
|
|
|
2016-05-25 18:49:47 +00:00
|
|
|
using internal::PeerUpdateHandler;
|
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() {
|
|
|
|
SmallUpdates.makeIfNull();
|
|
|
|
AllUpdates.makeIfNull();
|
|
|
|
}
|
|
|
|
void FinishCallback() {
|
|
|
|
SmallUpdates.clear();
|
|
|
|
AllUpdates.clear();
|
|
|
|
}
|
2016-06-01 20:05:37 +00:00
|
|
|
ObservedEventRegistrator<PeerUpdate::Flags, PeerUpdateHandler> creator(StartCallback, FinishCallback);
|
2016-05-24 16:13:07 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2016-05-25 18:49:47 +00:00
|
|
|
namespace internal {
|
|
|
|
|
2016-06-01 20:05:37 +00:00
|
|
|
ConnectionId plainRegisterPeerObserver(PeerUpdate::Flags events, PeerUpdateHandler &&handler) {
|
2016-05-25 18:49:47 +00:00
|
|
|
return creator.registerObserver(events, std_::forward<PeerUpdateHandler>(handler));
|
2016-05-24 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 18:49:47 +00:00
|
|
|
} // namespace internal
|
|
|
|
|
2016-05-24 16:13:07 +00:00
|
|
|
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) {
|
2016-05-25 12:09:05 +00:00
|
|
|
mergeTo.oldNames = mergeFrom.oldNames;
|
|
|
|
mergeTo.oldNameFirstChars = mergeFrom.oldNameFirstChars;
|
|
|
|
}
|
|
|
|
}
|
2016-06-02 13:02:55 +00:00
|
|
|
if (mergeFrom.flags & PeerUpdate::Flag::SharedMediaChanged) {
|
|
|
|
mergeTo.mediaTypesMask |= mergeFrom.mediaTypesMask;
|
|
|
|
}
|
2016-05-24 16:13:07 +00:00
|
|
|
mergeTo.flags |= mergeFrom.flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
void peerUpdatedDelayed(const PeerUpdate &update) {
|
2016-05-25 18:49:47 +00:00
|
|
|
t_assert(creator.started());
|
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-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-06-02 13:02:55 +00:00
|
|
|
if (!creator.started()) return;
|
2016-05-25 12:09:05 +00:00
|
|
|
|
2016-06-02 13:02:55 +00:00
|
|
|
App::emitPeerUpdated();
|
2016-05-25 12:09:05 +00:00
|
|
|
|
2016-05-25 18:49:47 +00:00
|
|
|
if (SmallUpdates->isEmpty()) return;
|
2016-05-24 16:13:07 +00:00
|
|
|
|
2016-05-25 18:49:47 +00:00
|
|
|
auto smallList = createAndSwap(*SmallUpdates);
|
|
|
|
auto allList = createAndSwap(*AllUpdates);
|
2016-05-24 16:13:07 +00:00
|
|
|
for_const (auto &update, smallList) {
|
2016-05-25 18:49:47 +00:00
|
|
|
creator.notify(update.flags, update);
|
2016-05-24 16:13:07 +00:00
|
|
|
}
|
|
|
|
for_const (auto &update, allList) {
|
2016-05-25 18:49:47 +00:00
|
|
|
creator.notify(update.flags, update);
|
2016-05-24 16:13:07 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Notify
|