2017-09-26 11:49:16 +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.
|
2017-09-26 11:49:16 +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
|
2017-09-26 11:49:16 +00:00
|
|
|
*/
|
|
|
|
#include "data/data_peer.h"
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "data/data_user.h"
|
|
|
|
#include "data/data_chat.h"
|
|
|
|
#include "data/data_channel.h"
|
2017-12-05 06:41:43 +00:00
|
|
|
#include "data/data_photo.h"
|
2019-04-15 11:54:03 +00:00
|
|
|
#include "data/data_folder.h"
|
2018-01-04 10:22:53 +00:00
|
|
|
#include "data/data_session.h"
|
2017-09-26 11:49:16 +00:00
|
|
|
#include "lang/lang_keys.h"
|
|
|
|
#include "observer_peer.h"
|
|
|
|
#include "apiwrap.h"
|
|
|
|
#include "boxes/confirm_box.h"
|
|
|
|
#include "auth_session.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "core/application.h"
|
2017-10-03 13:05:58 +00:00
|
|
|
#include "mainwindow.h"
|
|
|
|
#include "window/window_controller.h"
|
2018-10-23 09:44:42 +00:00
|
|
|
#include "ui/image/image.h"
|
2017-12-05 08:43:18 +00:00
|
|
|
#include "ui/empty_userpic.h"
|
2017-12-28 13:06:06 +00:00
|
|
|
#include "ui/text_options.h"
|
2019-03-21 18:14:48 +00:00
|
|
|
#include "history/history.h"
|
|
|
|
#include "history/view/history_view_element.h"
|
|
|
|
#include "history/history_item.h"
|
2017-09-26 11:49:16 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
constexpr auto kUpdateFullPeerTimeout = crl::time(5000); // Not more than once in 5 seconds.
|
2017-12-05 06:41:43 +00:00
|
|
|
constexpr auto kUserpicSize = 160;
|
2017-09-26 11:49:16 +00:00
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
using UpdateFlag = Notify::PeerUpdate::Flag;
|
|
|
|
|
2017-12-05 08:43:18 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace Data {
|
|
|
|
|
|
|
|
int PeerColorIndex(int32 bareId) {
|
|
|
|
const auto index = std::abs(bareId) % 7;
|
|
|
|
const int map[] = { 0, 7, 4, 1, 6, 3, 5 };
|
|
|
|
return map[index];
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 08:43:18 +00:00
|
|
|
int PeerColorIndex(PeerId peerId) {
|
|
|
|
return PeerColorIndex(peerToBareInt(peerId));
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 10:21:40 +00:00
|
|
|
style::color PeerUserpicColor(PeerId peerId) {
|
|
|
|
const style::color colors[] = {
|
2017-09-26 11:49:16 +00:00
|
|
|
st::historyPeer1UserpicBg,
|
|
|
|
st::historyPeer2UserpicBg,
|
|
|
|
st::historyPeer3UserpicBg,
|
|
|
|
st::historyPeer4UserpicBg,
|
|
|
|
st::historyPeer5UserpicBg,
|
|
|
|
st::historyPeer6UserpicBg,
|
|
|
|
st::historyPeer7UserpicBg,
|
|
|
|
st::historyPeer8UserpicBg,
|
|
|
|
};
|
2017-12-01 10:21:40 +00:00
|
|
|
return colors[PeerColorIndex(peerId)];
|
|
|
|
}
|
|
|
|
|
2019-03-15 15:15:56 +00:00
|
|
|
PeerId FakePeerIdForJustName(const QString &name) {
|
|
|
|
return peerFromUser(name.isEmpty()
|
|
|
|
? 777
|
|
|
|
: hashCrc32(name.constData(), name.size() * sizeof(QChar)));
|
|
|
|
}
|
|
|
|
|
2017-12-05 08:43:18 +00:00
|
|
|
} // namespace Data
|
2017-09-26 11:49:16 +00:00
|
|
|
|
2017-11-08 16:45:30 +00:00
|
|
|
PeerClickHandler::PeerClickHandler(not_null<PeerData*> peer)
|
|
|
|
: _peer(peer) {
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2018-07-09 18:13:48 +00:00
|
|
|
void PeerClickHandler::onClick(ClickContext context) const {
|
|
|
|
if (context.button == Qt::LeftButton && App::wnd()) {
|
2017-11-08 16:45:30 +00:00
|
|
|
auto controller = App::wnd()->controller();
|
2018-02-14 19:38:01 +00:00
|
|
|
if (_peer
|
|
|
|
&& _peer->isChannel()
|
|
|
|
&& controller->activeChatCurrent().peer() != _peer) {
|
2017-09-26 11:49:16 +00:00
|
|
|
if (!_peer->asChannel()->isPublic() && !_peer->asChannel()->amIn()) {
|
2017-11-08 16:45:30 +00:00
|
|
|
Ui::show(Box<InformBox>(lang(_peer->isMegagroup()
|
|
|
|
? lng_group_not_accessible
|
|
|
|
: lng_channel_not_accessible)));
|
2017-09-26 11:49:16 +00:00
|
|
|
} else {
|
2017-11-08 16:45:30 +00:00
|
|
|
controller->showPeerHistory(
|
2017-10-03 13:05:58 +00:00
|
|
|
_peer,
|
|
|
|
Window::SectionShow::Way::Forward);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ui::showPeerProfile(_peer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 12:36:01 +00:00
|
|
|
PeerData::PeerData(not_null<Data::Session*> owner, PeerId id)
|
2017-12-05 08:43:18 +00:00
|
|
|
: id(id)
|
2019-01-03 12:36:01 +00:00
|
|
|
, _owner(owner)
|
2017-12-05 08:43:18 +00:00
|
|
|
, _userpicEmpty(createEmptyUserpic()) {
|
2017-12-28 13:06:06 +00:00
|
|
|
nameText.setText(st::msgNameStyle, QString(), Ui::NameTextOptions());
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 12:36:01 +00:00
|
|
|
Data::Session &PeerData::owner() const {
|
|
|
|
return *_owner;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuthSession &PeerData::session() const {
|
|
|
|
return _owner->session();
|
|
|
|
}
|
|
|
|
|
2017-11-08 16:45:30 +00:00
|
|
|
void PeerData::updateNameDelayed(
|
|
|
|
const QString &newName,
|
|
|
|
const QString &newNameOrPhone,
|
|
|
|
const QString &newUsername) {
|
2019-04-23 13:28:44 +00:00
|
|
|
if (name == newName && nameVersion > 1) {
|
2017-09-26 11:49:16 +00:00
|
|
|
if (isUser()) {
|
2017-11-08 16:45:30 +00:00
|
|
|
if (asUser()->nameOrPhone == newNameOrPhone
|
|
|
|
&& asUser()->username == newUsername) {
|
2017-09-26 11:49:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (isChannel()) {
|
|
|
|
if (asChannel()->username == newUsername) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (isChat()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
name = newName;
|
2017-12-28 13:06:06 +00:00
|
|
|
nameText.setText(st::msgNameStyle, name, Ui::NameTextOptions());
|
2017-12-05 08:43:18 +00:00
|
|
|
refreshEmptyUserpic();
|
2017-09-26 11:49:16 +00:00
|
|
|
Notify::PeerUpdate update(this);
|
2019-04-23 13:28:44 +00:00
|
|
|
if (nameVersion++ > 1) {
|
|
|
|
update.flags |= UpdateFlag::NameChanged;
|
|
|
|
update.oldNameFirstLetters = nameFirstLetters();
|
|
|
|
}
|
2017-09-26 11:49:16 +00:00
|
|
|
if (isUser()) {
|
|
|
|
if (asUser()->username != newUsername) {
|
|
|
|
asUser()->username = newUsername;
|
|
|
|
update.flags |= UpdateFlag::UsernameChanged;
|
|
|
|
}
|
|
|
|
asUser()->setNameOrPhone(newNameOrPhone);
|
|
|
|
} else if (isChannel()) {
|
|
|
|
if (asChannel()->username != newUsername) {
|
|
|
|
asChannel()->username = newUsername;
|
|
|
|
if (newUsername.isEmpty()) {
|
|
|
|
asChannel()->removeFlags(
|
|
|
|
MTPDchannel::Flag::f_username);
|
|
|
|
} else {
|
|
|
|
asChannel()->addFlags(MTPDchannel::Flag::f_username);
|
|
|
|
}
|
|
|
|
update.flags |= UpdateFlag::UsernameChanged;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fillNames();
|
2019-04-23 13:28:44 +00:00
|
|
|
if (update.flags) {
|
|
|
|
Notify::PeerUpdated().notify(update, true);
|
|
|
|
}
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 08:43:18 +00:00
|
|
|
std::unique_ptr<Ui::EmptyUserpic> PeerData::createEmptyUserpic() const {
|
|
|
|
return std::make_unique<Ui::EmptyUserpic>(
|
|
|
|
Data::PeerUserpicColor(id),
|
|
|
|
name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerData::refreshEmptyUserpic() const {
|
|
|
|
_userpicEmpty = useEmptyUserpic() ? createEmptyUserpic() : nullptr;
|
|
|
|
}
|
|
|
|
|
2017-09-26 11:49:16 +00:00
|
|
|
ClickHandlerPtr PeerData::createOpenLink() {
|
2017-12-18 09:07:18 +00:00
|
|
|
return std::make_shared<PeerClickHandler>(this);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 19:54:05 +00:00
|
|
|
void PeerData::setUserpic(
|
2017-12-05 06:41:43 +00:00
|
|
|
PhotoId photoId,
|
|
|
|
const StorageImageLocation &location,
|
|
|
|
ImagePtr userpic) {
|
|
|
|
_userpicPhotoId = photoId;
|
2017-09-26 11:49:16 +00:00
|
|
|
_userpic = userpic;
|
2017-11-20 19:54:05 +00:00
|
|
|
_userpicLocation = location;
|
2017-12-05 08:43:18 +00:00
|
|
|
refreshEmptyUserpic();
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 06:41:43 +00:00
|
|
|
void PeerData::setUserpicPhoto(const MTPPhoto &data) {
|
2018-11-02 18:35:57 +00:00
|
|
|
const auto photoId = data.match([&](const MTPDphoto &data) {
|
2019-01-18 12:27:37 +00:00
|
|
|
const auto photo = owner().processPhoto(data);
|
2018-11-02 18:35:57 +00:00
|
|
|
photo->peer = this;
|
|
|
|
return photo->id;
|
|
|
|
}, [](const MTPDphotoEmpty &data) {
|
|
|
|
return PhotoId(0);
|
|
|
|
});
|
2017-12-05 06:41:43 +00:00
|
|
|
if (_userpicPhotoId != photoId) {
|
|
|
|
_userpicPhotoId = photoId;
|
|
|
|
Notify::peerUpdatedDelayed(this, UpdateFlag::PhotoChanged);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-26 11:49:16 +00:00
|
|
|
ImagePtr PeerData::currentUserpic() const {
|
|
|
|
if (_userpic) {
|
2018-10-31 09:55:22 +00:00
|
|
|
_userpic->load(userpicOrigin());
|
2017-09-26 11:49:16 +00:00
|
|
|
if (_userpic->loaded()) {
|
2017-11-20 19:54:05 +00:00
|
|
|
if (!useEmptyUserpic()) {
|
2017-12-05 08:43:18 +00:00
|
|
|
_userpicEmpty = nullptr;
|
2017-11-20 19:54:05 +00:00
|
|
|
}
|
2017-09-26 11:49:16 +00:00
|
|
|
return _userpic;
|
|
|
|
}
|
|
|
|
}
|
2018-10-12 16:41:51 +00:00
|
|
|
if (!_userpicEmpty) {
|
|
|
|
refreshEmptyUserpic();
|
|
|
|
}
|
2017-09-26 11:49:16 +00:00
|
|
|
return ImagePtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerData::paintUserpic(Painter &p, int x, int y, int size) const {
|
|
|
|
if (auto userpic = currentUserpic()) {
|
2018-10-31 09:55:22 +00:00
|
|
|
p.drawPixmap(x, y, userpic->pixCircled(userpicOrigin(), size, size));
|
2017-09-26 11:49:16 +00:00
|
|
|
} else {
|
2017-12-05 08:43:18 +00:00
|
|
|
_userpicEmpty->paint(p, x, y, x + size + x, size);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerData::paintUserpicRounded(Painter &p, int x, int y, int size) const {
|
|
|
|
if (auto userpic = currentUserpic()) {
|
2018-10-31 09:55:22 +00:00
|
|
|
p.drawPixmap(x, y, userpic->pixRounded(userpicOrigin(), size, size, ImageRoundRadius::Small));
|
2017-09-26 11:49:16 +00:00
|
|
|
} else {
|
2017-12-05 08:43:18 +00:00
|
|
|
_userpicEmpty->paintRounded(p, x, y, x + size + x, size);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerData::paintUserpicSquare(Painter &p, int x, int y, int size) const {
|
|
|
|
if (auto userpic = currentUserpic()) {
|
2018-10-31 09:55:22 +00:00
|
|
|
p.drawPixmap(x, y, userpic->pix(userpicOrigin(), size, size));
|
2017-09-26 11:49:16 +00:00
|
|
|
} else {
|
2017-12-05 08:43:18 +00:00
|
|
|
_userpicEmpty->paintSquare(p, x, y, x + size + x, size);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-11 15:54:57 +00:00
|
|
|
void PeerData::loadUserpic(bool loadFirst, bool prior) {
|
2018-10-31 09:55:22 +00:00
|
|
|
_userpic->load(userpicOrigin(), loadFirst, prior);
|
2018-10-11 15:54:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PeerData::userpicLoaded() const {
|
|
|
|
return _userpic->loaded();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PeerData::useEmptyUserpic() const {
|
2019-03-22 13:43:34 +00:00
|
|
|
return !_userpicLocation.valid()
|
2018-10-11 15:54:57 +00:00
|
|
|
|| !_userpic
|
|
|
|
|| !_userpic->loaded();
|
|
|
|
}
|
|
|
|
|
2019-03-22 13:43:34 +00:00
|
|
|
InMemoryKey PeerData::userpicUniqueKey() const {
|
2017-11-20 19:54:05 +00:00
|
|
|
if (useEmptyUserpic()) {
|
2018-11-09 14:01:54 +00:00
|
|
|
if (!_userpicEmpty) {
|
|
|
|
refreshEmptyUserpic();
|
|
|
|
}
|
2017-12-05 08:43:18 +00:00
|
|
|
return _userpicEmpty->uniqueKey();
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
2019-03-22 13:43:34 +00:00
|
|
|
return inMemoryKey(_userpicLocation);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PeerData::saveUserpic(const QString &path, int size) const {
|
|
|
|
genUserpic(size).save(path, "PNG");
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerData::saveUserpicRounded(const QString &path, int size) const {
|
|
|
|
genUserpicRounded(size).save(path, "PNG");
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap PeerData::genUserpic(int size) const {
|
|
|
|
if (auto userpic = currentUserpic()) {
|
2018-10-31 09:55:22 +00:00
|
|
|
return userpic->pixCircled(userpicOrigin(), size, size);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
auto result = QImage(QSize(size, size) * cIntRetinaFactor(), QImage::Format_ARGB32_Premultiplied);
|
|
|
|
result.setDevicePixelRatio(cRetinaFactor());
|
|
|
|
result.fill(Qt::transparent);
|
|
|
|
{
|
|
|
|
Painter p(&result);
|
|
|
|
paintUserpic(p, 0, 0, size);
|
|
|
|
}
|
|
|
|
return App::pixmapFromImageInPlace(std::move(result));
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap PeerData::genUserpicRounded(int size) const {
|
|
|
|
if (auto userpic = currentUserpic()) {
|
2018-10-31 09:55:22 +00:00
|
|
|
return userpic->pixRounded(userpicOrigin(), size, size, ImageRoundRadius::Small);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
auto result = QImage(QSize(size, size) * cIntRetinaFactor(), QImage::Format_ARGB32_Premultiplied);
|
|
|
|
result.setDevicePixelRatio(cRetinaFactor());
|
|
|
|
result.fill(Qt::transparent);
|
|
|
|
{
|
|
|
|
Painter p(&result);
|
|
|
|
paintUserpicRounded(p, 0, 0, size);
|
|
|
|
}
|
|
|
|
return App::pixmapFromImageInPlace(std::move(result));
|
|
|
|
}
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
Data::FileOrigin PeerData::userpicOrigin() const {
|
|
|
|
return Data::FileOriginPeerPhoto(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
Data::FileOrigin PeerData::userpicPhotoOrigin() const {
|
|
|
|
return (isUser() && userpicPhotoId())
|
|
|
|
? Data::FileOriginUserPhoto(bareId(), userpicPhotoId())
|
|
|
|
: Data::FileOrigin();
|
|
|
|
}
|
|
|
|
|
2017-12-05 06:41:43 +00:00
|
|
|
void PeerData::updateUserpic(
|
|
|
|
PhotoId photoId,
|
2019-03-22 14:19:43 +00:00
|
|
|
MTP::DcId dcId,
|
2017-12-05 06:41:43 +00:00
|
|
|
const MTPFileLocation &location) {
|
|
|
|
const auto size = kUserpicSize;
|
2019-03-22 14:19:43 +00:00
|
|
|
const auto loc = location.match([&](
|
|
|
|
const MTPDfileLocationToBeDeprecated &deprecated) {
|
|
|
|
return StorageImageLocation(
|
|
|
|
StorageFileLocation(
|
|
|
|
dcId,
|
2019-03-29 15:22:56 +00:00
|
|
|
isSelf() ? peerToUser(id) : 0,
|
2019-03-22 14:19:43 +00:00
|
|
|
MTP_inputPeerPhotoFileLocation(
|
|
|
|
MTP_flags(0),
|
|
|
|
input,
|
|
|
|
deprecated.vvolume_id,
|
|
|
|
deprecated.vlocal_id)),
|
|
|
|
size,
|
|
|
|
size);
|
|
|
|
});
|
|
|
|
setUserpicChecked(photoId, loc, Images::Create(loc));
|
2017-12-05 06:41:43 +00:00
|
|
|
}
|
2017-11-20 19:54:05 +00:00
|
|
|
|
2017-12-05 06:41:43 +00:00
|
|
|
void PeerData::clearUserpic() {
|
|
|
|
const auto photoId = PhotoId(0);
|
|
|
|
const auto loc = StorageImageLocation();
|
|
|
|
const auto photo = [&] {
|
2019-03-12 10:36:33 +00:00
|
|
|
if (isNotificationsUser()) {
|
2019-01-21 13:42:21 +00:00
|
|
|
auto image = Core::App().logoNoMargin().scaledToWidth(
|
2017-12-05 06:41:43 +00:00
|
|
|
kUserpicSize,
|
|
|
|
Qt::SmoothTransformation);
|
|
|
|
return _userpic
|
|
|
|
? _userpic
|
2018-10-23 09:08:50 +00:00
|
|
|
: Images::Create(std::move(image), "PNG");
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
2017-12-05 06:41:43 +00:00
|
|
|
return ImagePtr();
|
|
|
|
}();
|
2017-12-12 11:45:40 +00:00
|
|
|
setUserpicChecked(photoId, loc, photo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerData::setUserpicChecked(
|
|
|
|
PhotoId photoId,
|
|
|
|
const StorageImageLocation &location,
|
|
|
|
ImagePtr userpic) {
|
|
|
|
if (_userpicPhotoId != photoId
|
2018-10-11 15:54:57 +00:00
|
|
|
|| _userpic.get() != userpic.get()
|
2017-12-12 11:45:40 +00:00
|
|
|
|| _userpicLocation != location) {
|
|
|
|
setUserpic(photoId, location, userpic);
|
|
|
|
Notify::peerUpdatedDelayed(this, UpdateFlag::PhotoChanged);
|
2019-04-15 11:54:03 +00:00
|
|
|
//if (const auto channel = asChannel()) { // #feed
|
|
|
|
// if (const auto feed = channel->feed()) {
|
|
|
|
// owner().notifyFeedUpdated(
|
|
|
|
// feed,
|
|
|
|
// Data::FeedUpdateFlag::ChannelPhoto);
|
|
|
|
// }
|
|
|
|
//}
|
2017-12-12 11:45:40 +00:00
|
|
|
}
|
2017-12-05 06:41:43 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 11:29:14 +00:00
|
|
|
bool PeerData::canPinMessages() const {
|
|
|
|
if (const auto user = asUser()) {
|
2018-11-02 18:35:57 +00:00
|
|
|
return user->fullFlags() & MTPDuserFull::Flag::f_can_pin_message;
|
2018-10-31 11:29:14 +00:00
|
|
|
} else if (const auto chat = asChat()) {
|
2019-02-01 12:50:57 +00:00
|
|
|
return chat->amIn() && !chat->amRestricted(ChatRestriction::f_pin_messages);
|
2018-10-31 11:29:14 +00:00
|
|
|
} else if (const auto channel = asChannel()) {
|
2019-02-01 12:50:57 +00:00
|
|
|
return channel->isMegagroup()
|
|
|
|
? !channel->amRestricted(ChatRestriction::f_pin_messages)
|
|
|
|
: ((channel->adminRights() & ChatAdminRight::f_edit_messages)
|
|
|
|
|| channel->amCreator());
|
2018-10-31 11:29:14 +00:00
|
|
|
}
|
|
|
|
Unexpected("Peer type in PeerData::canPinMessages.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerData::setPinnedMessageId(MsgId messageId) {
|
|
|
|
const auto min = [&] {
|
|
|
|
if (const auto channel = asChannel()) {
|
|
|
|
return channel->availableMinId();
|
|
|
|
}
|
|
|
|
return MsgId(0);
|
|
|
|
}();
|
|
|
|
messageId = (messageId > min) ? messageId : MsgId(0);
|
|
|
|
if (_pinnedMessageId != messageId) {
|
|
|
|
_pinnedMessageId = messageId;
|
|
|
|
Notify::peerUpdatedDelayed(
|
|
|
|
this,
|
|
|
|
Notify::PeerUpdate::Flag::PinnedMessageChanged);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-21 18:14:48 +00:00
|
|
|
bool PeerData::canExportChatHistory() const {
|
|
|
|
for (const auto &block : _owner->history(id)->blocks) {
|
|
|
|
for (const auto &message : block->messages) {
|
|
|
|
if (!message->data()->serviceMsg()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-08 13:57:22 +00:00
|
|
|
bool PeerData::setAbout(const QString &newAbout) {
|
|
|
|
if (_about == newAbout) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_about = newAbout;
|
|
|
|
Notify::peerUpdatedDelayed(this, UpdateFlag::AboutChanged);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-23 09:40:14 +00:00
|
|
|
void PeerData::checkFolder(FolderId folderId) {
|
|
|
|
const auto folder = folderId
|
|
|
|
? owner().folderLoaded(folderId)
|
|
|
|
: nullptr;
|
|
|
|
if (const auto history = owner().historyLoaded(this)) {
|
|
|
|
if (folder && history->folder() != folder) {
|
|
|
|
session().api().requestDialogEntry(history);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-26 11:49:16 +00:00
|
|
|
void PeerData::fillNames() {
|
2017-10-24 17:11:35 +00:00
|
|
|
_nameWords.clear();
|
2018-01-22 09:33:09 +00:00
|
|
|
_nameFirstLetters.clear();
|
2017-12-05 14:07:01 +00:00
|
|
|
auto toIndexList = QStringList();
|
|
|
|
auto appendToIndex = [&](const QString &value) {
|
|
|
|
if (!value.isEmpty()) {
|
|
|
|
toIndexList.push_back(TextUtilities::RemoveAccents(value));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
appendToIndex(name);
|
|
|
|
const auto appendTranslit = !toIndexList.isEmpty()
|
|
|
|
&& cRussianLetters().match(toIndexList.front()).hasMatch();
|
|
|
|
if (appendTranslit) {
|
|
|
|
appendToIndex(translitRusEng(toIndexList.front()));
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
2018-01-22 09:33:09 +00:00
|
|
|
if (const auto user = asUser()) {
|
2017-12-05 14:07:01 +00:00
|
|
|
if (user->nameOrPhone != name) {
|
|
|
|
appendToIndex(user->nameOrPhone);
|
|
|
|
}
|
|
|
|
appendToIndex(user->username);
|
|
|
|
if (isSelf()) {
|
2018-12-05 10:41:54 +00:00
|
|
|
const auto english = qsl("Saved messages");
|
|
|
|
const auto localized = lang(lng_saved_messages);
|
|
|
|
appendToIndex(english);
|
|
|
|
if (localized != english) {
|
|
|
|
appendToIndex(localized);
|
|
|
|
}
|
2017-12-05 14:07:01 +00:00
|
|
|
}
|
2018-01-22 09:33:09 +00:00
|
|
|
} else if (const auto channel = asChannel()) {
|
2017-12-05 14:07:01 +00:00
|
|
|
appendToIndex(channel->username);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
2017-12-05 14:07:01 +00:00
|
|
|
auto toIndex = toIndexList.join(' ');
|
2017-09-26 11:49:16 +00:00
|
|
|
toIndex += ' ' + rusKeyboardLayoutSwitch(toIndex);
|
|
|
|
|
2017-12-05 14:07:01 +00:00
|
|
|
const auto namesList = TextUtilities::PrepareSearchWords(toIndex);
|
|
|
|
for (const auto &name : namesList) {
|
2017-10-24 17:11:35 +00:00
|
|
|
_nameWords.insert(name);
|
2018-01-22 09:33:09 +00:00
|
|
|
_nameFirstLetters.insert(name[0]);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-05 08:43:18 +00:00
|
|
|
PeerData::~PeerData() = default;
|
|
|
|
|
2017-09-26 11:49:16 +00:00
|
|
|
void PeerData::updateFull() {
|
2019-01-10 06:26:08 +00:00
|
|
|
if (!_lastFullUpdate
|
2019-02-19 06:57:53 +00:00
|
|
|
|| crl::now() > _lastFullUpdate + kUpdateFullPeerTimeout) {
|
2017-09-26 11:49:16 +00:00
|
|
|
updateFullForced();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerData::updateFullForced() {
|
2019-01-03 12:36:01 +00:00
|
|
|
session().api().requestFullPeer(this);
|
2019-01-10 06:26:08 +00:00
|
|
|
if (const auto channel = asChannel()) {
|
2017-09-26 11:49:16 +00:00
|
|
|
if (!channel->amCreator() && !channel->inviter) {
|
2019-01-03 12:36:01 +00:00
|
|
|
session().api().requestSelfParticipant(channel);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerData::fullUpdated() {
|
2019-02-19 06:57:53 +00:00
|
|
|
_lastFullUpdate = crl::now();
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
UserData *PeerData::asUser() {
|
|
|
|
return isUser() ? static_cast<UserData*>(this) : nullptr;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
const UserData *PeerData::asUser() const {
|
|
|
|
return isUser() ? static_cast<const UserData*>(this) : nullptr;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
ChatData *PeerData::asChat() {
|
|
|
|
return isChat() ? static_cast<ChatData*>(this) : nullptr;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
const ChatData *PeerData::asChat() const {
|
|
|
|
return isChat() ? static_cast<const ChatData*>(this) : nullptr;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
ChannelData *PeerData::asChannel() {
|
|
|
|
return isChannel() ? static_cast<ChannelData*>(this) : nullptr;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
const ChannelData *PeerData::asChannel() const {
|
|
|
|
return isChannel()
|
|
|
|
? static_cast<const ChannelData*>(this)
|
|
|
|
: nullptr;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
ChannelData *PeerData::asMegagroup() {
|
|
|
|
return isMegagroup() ? static_cast<ChannelData*>(this) : nullptr;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
const ChannelData *PeerData::asMegagroup() const {
|
|
|
|
return isMegagroup()
|
|
|
|
? static_cast<const ChannelData*>(this)
|
|
|
|
: nullptr;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 13:28:05 +00:00
|
|
|
ChatData *PeerData::asChatNotMigrated() {
|
|
|
|
if (const auto chat = asChat()) {
|
|
|
|
return chat->migrateTo() ? nullptr : chat;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ChatData *PeerData::asChatNotMigrated() const {
|
|
|
|
if (const auto chat = asChat()) {
|
|
|
|
return chat->migrateTo() ? nullptr : chat;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChannelData *PeerData::asChannelOrMigrated() {
|
|
|
|
if (const auto channel = asChannel()) {
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
return migrateTo();
|
|
|
|
}
|
|
|
|
|
|
|
|
const ChannelData *PeerData::asChannelOrMigrated() const {
|
|
|
|
if (const auto channel = asChannel()) {
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
return migrateTo();
|
|
|
|
}
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
ChatData *PeerData::migrateFrom() const {
|
|
|
|
if (const auto megagroup = asMegagroup()) {
|
|
|
|
return megagroup->amIn()
|
2019-01-14 06:34:51 +00:00
|
|
|
? megagroup->getMigrateFromChat()
|
2019-01-04 11:09:48 +00:00
|
|
|
: nullptr;
|
2017-12-01 18:38:44 +00:00
|
|
|
}
|
2019-01-04 11:09:48 +00:00
|
|
|
return nullptr;
|
2017-12-01 18:38:44 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
ChannelData *PeerData::migrateTo() const {
|
|
|
|
if (const auto chat = asChat()) {
|
2019-01-14 06:34:51 +00:00
|
|
|
if (const auto result = chat->getMigrateToChannel()) {
|
|
|
|
return result->amIn() ? result : nullptr;
|
2018-01-31 17:10:29 +00:00
|
|
|
}
|
2017-11-20 19:54:05 +00:00
|
|
|
}
|
2019-01-04 11:09:48 +00:00
|
|
|
return nullptr;
|
2017-11-20 19:54:05 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 06:34:51 +00:00
|
|
|
not_null<PeerData*> PeerData::migrateToOrMe() {
|
|
|
|
if (const auto channel = migrateTo()) {
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
not_null<const PeerData*> PeerData::migrateToOrMe() const {
|
|
|
|
if (const auto channel = migrateTo()) {
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
const Text &PeerData::dialogName() const {
|
|
|
|
return migrateTo()
|
|
|
|
? migrateTo()->dialogName()
|
|
|
|
: (isUser() && !asUser()->phoneText.isEmpty())
|
|
|
|
? asUser()->phoneText
|
|
|
|
: nameText;
|
2017-11-21 13:23:56 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
const QString &PeerData::shortName() const {
|
|
|
|
return isUser() ? asUser()->firstName : name;
|
2017-11-21 13:23:56 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
QString PeerData::userName() const {
|
|
|
|
return isUser()
|
|
|
|
? asUser()->username
|
|
|
|
: isChannel()
|
|
|
|
? asChannel()->username
|
|
|
|
: QString();
|
2017-11-22 08:04:45 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
bool PeerData::isVerified() const {
|
|
|
|
return isUser()
|
|
|
|
? asUser()->isVerified()
|
|
|
|
: isChannel()
|
|
|
|
? asChannel()->isVerified()
|
|
|
|
: false;
|
2017-11-21 13:23:56 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
bool PeerData::isMegagroup() const {
|
|
|
|
return isChannel() ? asChannel()->isMegagroup() : false;
|
2017-11-21 13:23:56 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
bool PeerData::canWrite() const {
|
|
|
|
return isChannel()
|
|
|
|
? asChannel()->canWrite()
|
|
|
|
: isChat()
|
|
|
|
? asChat()->canWrite()
|
|
|
|
: isUser()
|
|
|
|
? asUser()->canWrite()
|
|
|
|
: false;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
2019-01-05 10:50:04 +00:00
|
|
|
|
2019-01-14 17:43:06 +00:00
|
|
|
Data::RestrictionCheckResult PeerData::amRestricted(
|
|
|
|
ChatRestriction right) const {
|
|
|
|
using Result = Data::RestrictionCheckResult;
|
2019-01-05 10:50:04 +00:00
|
|
|
const auto allowByAdminRights = [](auto right, auto chat) -> bool {
|
|
|
|
if (right == ChatRestriction::f_invite_users) {
|
|
|
|
return chat->adminRights() & ChatAdminRight::f_invite_users;
|
|
|
|
} else if (right == ChatRestriction::f_change_info) {
|
|
|
|
return chat->adminRights() & ChatAdminRight::f_change_info;
|
|
|
|
} else if (right == ChatRestriction::f_pin_messages) {
|
|
|
|
return chat->adminRights() & ChatAdminRight::f_pin_messages;
|
|
|
|
} else {
|
|
|
|
return chat->hasAdminRights();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (const auto channel = asChannel()) {
|
2019-02-01 12:50:57 +00:00
|
|
|
const auto defaultRestrictions = channel->defaultRestrictions()
|
|
|
|
| (channel->isPublic()
|
|
|
|
? (ChatRestriction::f_pin_messages | ChatRestriction::f_change_info)
|
|
|
|
: ChatRestrictions(0));
|
2019-01-14 17:43:06 +00:00
|
|
|
return (channel->amCreator() || allowByAdminRights(right, channel))
|
|
|
|
? Result::Allowed()
|
2019-02-01 12:50:57 +00:00
|
|
|
: (defaultRestrictions & right)
|
2019-01-14 17:43:06 +00:00
|
|
|
? Result::WithEveryone()
|
|
|
|
: (channel->restrictions() & right)
|
|
|
|
? Result::Explicit()
|
|
|
|
: Result::Allowed();
|
2019-01-05 10:50:04 +00:00
|
|
|
} else if (const auto chat = asChat()) {
|
2019-01-14 17:43:06 +00:00
|
|
|
return (chat->amCreator() || allowByAdminRights(right, chat))
|
|
|
|
? Result::Allowed()
|
|
|
|
: (chat->defaultRestrictions() & right)
|
|
|
|
? Result::WithEveryone()
|
|
|
|
: Result::Allowed();
|
2019-01-05 10:50:04 +00:00
|
|
|
}
|
2019-01-14 17:43:06 +00:00
|
|
|
return Result::Allowed();
|
2019-01-05 10:50:04 +00:00
|
|
|
}
|
2019-01-14 17:43:06 +00:00
|
|
|
|
2019-03-21 13:48:40 +00:00
|
|
|
bool PeerData::canRevokeFullHistory() const {
|
|
|
|
return isUser()
|
|
|
|
&& Global::RevokePrivateInbox()
|
|
|
|
&& (Global::RevokePrivateTimeLimit() == 0x7FFFFFFF);
|
|
|
|
}
|
|
|
|
|
2019-01-14 17:43:06 +00:00
|
|
|
namespace Data {
|
|
|
|
|
2019-03-15 08:20:12 +00:00
|
|
|
std::vector<ChatRestrictions> ListOfRestrictions() {
|
|
|
|
using Flag = ChatRestriction;
|
|
|
|
|
|
|
|
return {
|
|
|
|
Flag::f_send_messages,
|
|
|
|
Flag::f_send_media,
|
|
|
|
Flag::f_send_stickers
|
|
|
|
| Flag::f_send_gifs
|
|
|
|
| Flag::f_send_games
|
|
|
|
| Flag::f_send_inline,
|
|
|
|
Flag::f_embed_links,
|
|
|
|
Flag::f_send_polls,
|
|
|
|
Flag::f_invite_users,
|
|
|
|
Flag::f_pin_messages,
|
|
|
|
Flag::f_change_info,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-14 17:43:06 +00:00
|
|
|
std::optional<LangKey> RestrictionErrorKey(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
ChatRestriction restriction) {
|
|
|
|
using Flag = ChatRestriction;
|
|
|
|
if (const auto restricted = peer->amRestricted(restriction)) {
|
|
|
|
const auto all = restricted.isWithEveryone();
|
|
|
|
switch (restriction) {
|
|
|
|
case Flag::f_send_polls:
|
|
|
|
return all
|
|
|
|
? lng_restricted_send_polls_all
|
|
|
|
: lng_restricted_send_polls;
|
|
|
|
case Flag::f_send_messages:
|
|
|
|
return all
|
|
|
|
? lng_restricted_send_message_all
|
|
|
|
: lng_restricted_send_message;
|
|
|
|
case Flag::f_send_media:
|
|
|
|
return all
|
|
|
|
? lng_restricted_send_media_all
|
|
|
|
: lng_restricted_send_media;
|
|
|
|
case Flag::f_send_stickers:
|
|
|
|
return all
|
|
|
|
? lng_restricted_send_stickers_all
|
|
|
|
: lng_restricted_send_stickers;
|
|
|
|
case Flag::f_send_gifs:
|
|
|
|
return all
|
|
|
|
? lng_restricted_send_gifs_all
|
|
|
|
: lng_restricted_send_gifs;
|
|
|
|
case Flag::f_send_inline:
|
|
|
|
case Flag::f_send_games:
|
|
|
|
return all
|
|
|
|
? lng_restricted_send_inline_all
|
|
|
|
: lng_restricted_send_inline;
|
|
|
|
}
|
|
|
|
Unexpected("Restriction in Data::RestrictionErrorKey.");
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Data
|