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"
|
2020-06-12 12:12:34 +00:00
|
|
|
#include "data/data_changes.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"
|
2019-09-16 11:14:06 +00:00
|
|
|
#include "data/data_file_origin.h"
|
2020-02-21 07:58:50 +00:00
|
|
|
#include "data/data_histories.h"
|
2019-07-17 10:37:42 +00:00
|
|
|
#include "base/unixtime.h"
|
2019-09-16 11:14:06 +00:00
|
|
|
#include "base/crc32hash.h"
|
2017-09-26 11:49:16 +00:00
|
|
|
#include "lang/lang_keys.h"
|
|
|
|
#include "apiwrap.h"
|
|
|
|
#include "boxes/confirm_box.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2019-12-09 13:57:33 +00:00
|
|
|
#include "main/main_account.h"
|
2020-06-24 08:28:46 +00:00
|
|
|
#include "main/main_domain.h"
|
2019-12-09 13:57:33 +00:00
|
|
|
#include "main/main_app_config.h"
|
2020-06-17 09:36:25 +00:00
|
|
|
#include "mtproto/mtproto_config.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "core/application.h"
|
2017-10-03 13:05:58 +00:00
|
|
|
#include "mainwindow.h"
|
2019-06-06 10:21:40 +00:00
|
|
|
#include "window/window_session_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"
|
2020-05-28 14:32:10 +00:00
|
|
|
#include "storage/file_download.h"
|
2020-06-17 09:36:25 +00:00
|
|
|
#include "facades.h" // Ui::showPeerProfile
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "app.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
|
|
|
|
2020-06-12 12:12:34 +00:00
|
|
|
using UpdateFlag = Data::PeerUpdate::Flag;
|
2019-01-04 11:09:48 +00:00
|
|
|
|
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
|
2019-09-16 11:14:06 +00:00
|
|
|
: base::crc32(name.constData(), name.size() * sizeof(QChar)));
|
2019-03-15 15:15:56 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2020-06-24 08:28:46 +00:00
|
|
|
if (context.button != Qt::LeftButton) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto &windows = _peer->session().windows();
|
|
|
|
if (windows.empty()) {
|
|
|
|
Core::App().domain().activate(&_peer->session().account());
|
|
|
|
if (windows.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const auto window = windows.front();
|
|
|
|
const auto currentPeer = window->activeChatCurrent().peer();
|
|
|
|
if (_peer && _peer->isChannel() && currentPeer != _peer) {
|
|
|
|
const auto clickedChannel = _peer->asChannel();
|
|
|
|
if (!clickedChannel->isPublic()
|
|
|
|
&& !clickedChannel->amIn()
|
|
|
|
&& (!currentPeer->isChannel()
|
|
|
|
|| currentPeer->asChannel()->linkedChat() != clickedChannel)) {
|
|
|
|
Ui::show(Box<InformBox>(_peer->isMegagroup()
|
|
|
|
? tr::lng_group_not_accessible(tr::now)
|
|
|
|
: tr::lng_channel_not_accessible(tr::now)));
|
2017-09-26 11:49:16 +00:00
|
|
|
} else {
|
2020-06-24 08:28:46 +00:00
|
|
|
window->showPeerHistory(
|
|
|
|
_peer,
|
|
|
|
Window::SectionShow::Way::Forward);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
2020-06-24 08:28:46 +00:00
|
|
|
} else {
|
|
|
|
Ui::showPeerProfile(_peer);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2020-05-28 14:32:10 +00:00
|
|
|
, _owner(owner) {
|
2019-06-12 10:45: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;
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
Main::Session &PeerData::session() const {
|
2019-01-03 12:36:01 +00:00
|
|
|
return _owner->session();
|
|
|
|
}
|
|
|
|
|
2019-06-06 11:59:00 +00:00
|
|
|
Main::Account &PeerData::account() const {
|
|
|
|
return session().account();
|
|
|
|
}
|
|
|
|
|
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;
|
2019-06-12 10:45:06 +00:00
|
|
|
_nameText.setText(st::msgNameStyle, name, Ui::NameTextOptions());
|
2020-05-28 14:32:10 +00:00
|
|
|
_userpicEmpty = nullptr;
|
|
|
|
|
2020-06-12 12:12:34 +00:00
|
|
|
auto flags = UpdateFlag::None | UpdateFlag::None;
|
|
|
|
auto oldFirstLetters = base::flat_set<QChar>();
|
|
|
|
const auto nameUpdated = (nameVersion++ > 1);
|
|
|
|
if (nameUpdated) {
|
|
|
|
oldFirstLetters = nameFirstLetters();
|
|
|
|
flags |= UpdateFlag::Name;
|
2019-04-23 13:28:44 +00:00
|
|
|
}
|
2017-09-26 11:49:16 +00:00
|
|
|
if (isUser()) {
|
|
|
|
if (asUser()->username != newUsername) {
|
|
|
|
asUser()->username = newUsername;
|
2020-06-12 12:12:34 +00:00
|
|
|
flags |= UpdateFlag::Username;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2020-06-12 12:12:34 +00:00
|
|
|
flags |= UpdateFlag::Username;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fillNames();
|
2020-06-12 12:12:34 +00:00
|
|
|
if (nameUpdated) {
|
|
|
|
session().changes().nameUpdated(this, std::move(oldFirstLetters));
|
|
|
|
}
|
|
|
|
if (flags) {
|
|
|
|
session().changes().peerUpdated(this, flags);
|
2019-04-23 13:28:44 +00:00
|
|
|
}
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 14:32:10 +00:00
|
|
|
not_null<Ui::EmptyUserpic*> PeerData::ensureEmptyUserpic() const {
|
|
|
|
if (!_userpicEmpty) {
|
|
|
|
_userpicEmpty = std::make_unique<Ui::EmptyUserpic>(
|
|
|
|
Data::PeerUserpicColor(id),
|
|
|
|
name);
|
|
|
|
}
|
|
|
|
return _userpicEmpty.get();
|
2017-12-05 08:43:18 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-28 14:32:10 +00:00
|
|
|
void PeerData::setUserpic(PhotoId photoId, const ImageLocation &location) {
|
2017-12-05 06:41:43 +00:00
|
|
|
_userpicPhotoId = photoId;
|
2020-05-28 14:32:10 +00:00
|
|
|
_userpic.set(&session(), ImageWithLocation{ .location = location });
|
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;
|
2020-06-12 12:12:34 +00:00
|
|
|
session().changes().peerUpdated(this, UpdateFlag::Photo);
|
2017-12-05 06:41:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 14:32:10 +00:00
|
|
|
Image *PeerData::currentUserpic(
|
|
|
|
std::shared_ptr<Data::CloudImageView> &view) const {
|
|
|
|
if (!_userpic.isCurrentView(view)) {
|
|
|
|
view = _userpic.createView();
|
|
|
|
_userpic.load(&session(), userpicOrigin());
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
2020-05-28 14:32:10 +00:00
|
|
|
const auto image = view ? view->image() : nullptr;
|
|
|
|
if (image) {
|
|
|
|
_userpicEmpty = nullptr;
|
2020-05-29 14:08:18 +00:00
|
|
|
} else if (isNotificationsUser()) {
|
|
|
|
static auto result = Image(
|
2020-05-29 15:10:25 +00:00
|
|
|
Core::App().logoNoMargin().scaledToWidth(
|
|
|
|
kUserpicSize,
|
|
|
|
Qt::SmoothTransformation));
|
2020-05-29 14:08:18 +00:00
|
|
|
return &result;
|
2018-10-12 16:41:51 +00:00
|
|
|
}
|
2020-05-28 14:32:10 +00:00
|
|
|
return image;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 14:32:10 +00:00
|
|
|
void PeerData::paintUserpic(
|
|
|
|
Painter &p,
|
|
|
|
std::shared_ptr<Data::CloudImageView> &view,
|
|
|
|
int x,
|
|
|
|
int y,
|
|
|
|
int size) const {
|
|
|
|
if (const auto userpic = currentUserpic(view)) {
|
2020-05-29 15:10:25 +00:00
|
|
|
p.drawPixmap(x, y, userpic->pixCircled(size, size));
|
2017-09-26 11:49:16 +00:00
|
|
|
} else {
|
2020-05-28 14:32:10 +00:00
|
|
|
ensureEmptyUserpic()->paint(p, x, y, x + size + x, size);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 14:32:10 +00:00
|
|
|
void PeerData::paintUserpicRounded(
|
|
|
|
Painter &p,
|
|
|
|
std::shared_ptr<Data::CloudImageView> &view,
|
|
|
|
int x,
|
|
|
|
int y,
|
|
|
|
int size) const {
|
|
|
|
if (const auto userpic = currentUserpic(view)) {
|
2020-05-29 15:10:25 +00:00
|
|
|
p.drawPixmap(x, y, userpic->pixRounded(size, size, ImageRoundRadius::Small));
|
2017-09-26 11:49:16 +00:00
|
|
|
} else {
|
2020-05-28 14:32:10 +00:00
|
|
|
ensureEmptyUserpic()->paintRounded(p, x, y, x + size + x, size);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 14:32:10 +00:00
|
|
|
void PeerData::paintUserpicSquare(
|
|
|
|
Painter &p,
|
|
|
|
std::shared_ptr<Data::CloudImageView> &view,
|
|
|
|
int x,
|
|
|
|
int y,
|
|
|
|
int size) const {
|
|
|
|
if (const auto userpic = currentUserpic(view)) {
|
2020-05-29 15:10:25 +00:00
|
|
|
p.drawPixmap(x, y, userpic->pix(size, size));
|
2017-09-26 11:49:16 +00:00
|
|
|
} else {
|
2020-05-28 14:32:10 +00:00
|
|
|
ensureEmptyUserpic()->paintSquare(p, x, y, x + size + x, size);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 11:52:39 +00:00
|
|
|
void PeerData::loadUserpic() {
|
2020-05-28 14:32:10 +00:00
|
|
|
_userpic.load(&session(), userpicOrigin());
|
2018-10-11 15:54:57 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 14:32:10 +00:00
|
|
|
bool PeerData::hasUserpic() const {
|
|
|
|
return !_userpic.empty();
|
2018-10-11 15:54:57 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 14:32:10 +00:00
|
|
|
std::shared_ptr<Data::CloudImageView> PeerData::activeUserpicView() {
|
|
|
|
return _userpic.empty() ? nullptr : _userpic.activeView();
|
2018-10-11 15:54:57 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 14:32:10 +00:00
|
|
|
std::shared_ptr<Data::CloudImageView> PeerData::createUserpicView() {
|
|
|
|
if (_userpic.empty()) {
|
|
|
|
return nullptr;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
2020-05-28 14:32:10 +00:00
|
|
|
auto result = _userpic.createView();
|
|
|
|
_userpic.load(&session(), userpicPhotoOrigin());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PeerData::useEmptyUserpic(
|
|
|
|
std::shared_ptr<Data::CloudImageView> &view) const {
|
|
|
|
return !currentUserpic(view);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 14:32:10 +00:00
|
|
|
InMemoryKey PeerData::userpicUniqueKey(
|
|
|
|
std::shared_ptr<Data::CloudImageView> &view) const {
|
|
|
|
return useEmptyUserpic(view)
|
|
|
|
? ensureEmptyUserpic()->uniqueKey()
|
|
|
|
: inMemoryKey(_userpic.location());
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 14:32:10 +00:00
|
|
|
void PeerData::saveUserpic(
|
|
|
|
std::shared_ptr<Data::CloudImageView> &view,
|
|
|
|
const QString &path,
|
|
|
|
int size) const {
|
|
|
|
genUserpic(view, size).save(path, "PNG");
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 14:32:10 +00:00
|
|
|
void PeerData::saveUserpicRounded(
|
|
|
|
std::shared_ptr<Data::CloudImageView> &view,
|
|
|
|
const QString &path,
|
|
|
|
int size) const {
|
|
|
|
genUserpicRounded(view, size).save(path, "PNG");
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap PeerData::genUserpic(
|
|
|
|
std::shared_ptr<Data::CloudImageView> &view,
|
|
|
|
int size) const {
|
|
|
|
if (const auto userpic = currentUserpic(view)) {
|
2020-05-29 15:10:25 +00:00
|
|
|
return userpic->pixCircled(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);
|
2020-05-28 14:32:10 +00:00
|
|
|
paintUserpic(p, view, 0, 0, size);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
return App::pixmapFromImageInPlace(std::move(result));
|
|
|
|
}
|
|
|
|
|
2020-05-28 14:32:10 +00:00
|
|
|
QPixmap PeerData::genUserpicRounded(
|
|
|
|
std::shared_ptr<Data::CloudImageView> &view,
|
|
|
|
int size) const {
|
2020-05-29 15:10:25 +00:00
|
|
|
if (const auto userpic = currentUserpic(view)) {
|
|
|
|
return userpic->pixRounded(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);
|
2020-05-28 14:32:10 +00:00
|
|
|
paintUserpicRounded(p, view, 0, 0, size);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
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) {
|
2020-05-28 14:32:10 +00:00
|
|
|
setUserpicChecked(photoId, location.match([&](
|
2019-03-22 14:19:43 +00:00
|
|
|
const MTPDfileLocationToBeDeprecated &deprecated) {
|
2020-05-28 14:32:10 +00:00
|
|
|
return ImageLocation(
|
|
|
|
{ StorageFileLocation(
|
2019-03-22 14:19:43 +00:00
|
|
|
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,
|
2019-07-05 13:38:38 +00:00
|
|
|
deprecated.vvolume_id(),
|
2020-05-28 14:32:10 +00:00
|
|
|
deprecated.vlocal_id())) },
|
|
|
|
kUserpicSize,
|
|
|
|
kUserpicSize);
|
|
|
|
}));
|
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() {
|
2020-05-28 14:32:10 +00:00
|
|
|
setUserpicChecked(PhotoId(), ImageLocation());
|
2017-12-12 11:45:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PeerData::setUserpicChecked(
|
|
|
|
PhotoId photoId,
|
2020-05-28 14:32:10 +00:00
|
|
|
const ImageLocation &location) {
|
|
|
|
if (_userpicPhotoId != photoId || _userpic.location() != location) {
|
|
|
|
setUserpic(photoId, location);
|
2020-06-12 12:12:34 +00:00
|
|
|
session().changes().peerUpdated(this, UpdateFlag::Photo);
|
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
|
|
|
}
|
|
|
|
|
2019-12-09 13:57:33 +00:00
|
|
|
auto PeerData::unavailableReasons() const
|
|
|
|
-> const std::vector<Data::UnavailableReason> & {
|
|
|
|
static const auto result = std::vector<Data::UnavailableReason>();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString PeerData::computeUnavailableReason() const {
|
|
|
|
const auto &list = unavailableReasons();
|
|
|
|
const auto &config = session().account().appConfig();
|
|
|
|
const auto skip = config.get<std::vector<QString>>(
|
|
|
|
"ignore_restriction_reasons",
|
|
|
|
std::vector<QString>());
|
|
|
|
auto &&filtered = ranges::view::all(
|
|
|
|
list
|
|
|
|
) | ranges::view::filter([&](const Data::UnavailableReason &reason) {
|
2020-05-18 19:33:14 +00:00
|
|
|
return !ranges::contains(skip, reason.reason);
|
2019-12-09 13:57:33 +00:00
|
|
|
});
|
|
|
|
const auto first = filtered.begin();
|
|
|
|
return (first != filtered.end()) ? first->text : QString();
|
|
|
|
}
|
|
|
|
|
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()) {
|
2020-04-23 12:08:50 +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.");
|
|
|
|
}
|
|
|
|
|
2020-04-23 12:08:50 +00:00
|
|
|
bool PeerData::canEditMessagesIndefinitely() const {
|
|
|
|
if (const auto user = asUser()) {
|
|
|
|
return user->isSelf();
|
|
|
|
} else if (const auto chat = asChat()) {
|
|
|
|
return false;
|
|
|
|
} else if (const auto channel = asChannel()) {
|
|
|
|
return channel->isMegagroup()
|
|
|
|
? channel->canPinMessages()
|
|
|
|
: channel->canEditMessages();
|
|
|
|
}
|
|
|
|
Unexpected("Peer type in PeerData::canEditMessagesIndefinitely.");
|
|
|
|
}
|
|
|
|
|
2018-10-31 11:29:14 +00:00
|
|
|
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;
|
2020-06-12 12:12:34 +00:00
|
|
|
session().changes().peerUpdated(this, UpdateFlag::PinnedMessage);
|
2018-10-31 11:29:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-21 18:14:48 +00:00
|
|
|
bool PeerData::canExportChatHistory() const {
|
2020-07-01 14:19:25 +00:00
|
|
|
if (const auto channel = asChannel()) {
|
|
|
|
if (!channel->amIn() && channel->invitePeekExpires()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-03-21 18:14:48 +00:00
|
|
|
for (const auto &block : _owner->history(id)->blocks) {
|
|
|
|
for (const auto &message : block->messages) {
|
|
|
|
if (!message->data()->serviceMsg()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-01 10:15:46 +00:00
|
|
|
if (const auto from = migrateFrom()) {
|
|
|
|
return from->canExportChatHistory();
|
|
|
|
}
|
2019-03-21 18:14:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-08 13:57:22 +00:00
|
|
|
bool PeerData::setAbout(const QString &newAbout) {
|
|
|
|
if (_about == newAbout) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_about = newAbout;
|
2020-06-12 12:12:34 +00:00
|
|
|
session().changes().peerUpdated(this, UpdateFlag::About);
|
2019-01-08 13:57:22 +00:00
|
|
|
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) {
|
2020-02-21 07:58:50 +00:00
|
|
|
owner().histories().requestDialogEntry(history);
|
2019-04-23 09:40:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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");
|
2019-06-19 15:09:03 +00:00
|
|
|
const auto localized = tr::lng_saved_messages(tr::now);
|
2018-12-05 10:41:54 +00:00
|
|
|
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-06-12 13:26:04 +00:00
|
|
|
const Ui::Text::String &PeerData::topBarNameText() const {
|
2019-06-12 10:45:06 +00:00
|
|
|
if (const auto to = migrateTo()) {
|
|
|
|
return to->topBarNameText();
|
|
|
|
} else if (const auto user = asUser()) {
|
|
|
|
if (!user->phoneText.isEmpty()) {
|
|
|
|
return user->phoneText;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _nameText;
|
|
|
|
}
|
|
|
|
|
2019-06-12 13:26:04 +00:00
|
|
|
const Ui::Text::String &PeerData::nameText() const {
|
2019-06-12 10:45:06 +00:00
|
|
|
if (const auto to = migrateTo()) {
|
|
|
|
return to->nameText();
|
|
|
|
}
|
|
|
|
return _nameText;
|
2017-11-21 13:23:56 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
const QString &PeerData::shortName() const {
|
2019-06-12 13:26:04 +00:00
|
|
|
if (const auto user = asUser()) {
|
|
|
|
return user->firstName.isEmpty() ? user->lastName : user->firstName;
|
|
|
|
}
|
|
|
|
return name;
|
2017-11-21 13:23:56 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
QString PeerData::userName() const {
|
2019-06-23 12:18:33 +00:00
|
|
|
if (const auto user = asUser()) {
|
|
|
|
return user->username;
|
|
|
|
} else if (const auto channel = asChannel()) {
|
|
|
|
return channel->username;
|
|
|
|
}
|
|
|
|
return QString();
|
2017-11-22 08:04:45 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
bool PeerData::isVerified() const {
|
2019-06-23 12:18:33 +00:00
|
|
|
if (const auto user = asUser()) {
|
|
|
|
return user->isVerified();
|
|
|
|
} else if (const auto channel = asChannel()) {
|
|
|
|
return channel->isVerified();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PeerData::isScam() const {
|
|
|
|
if (const auto user = asUser()) {
|
|
|
|
return user->isScam();
|
|
|
|
} else if (const auto channel = asChannel()) {
|
|
|
|
return channel->isScam();
|
|
|
|
}
|
|
|
|
return 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 {
|
2019-06-23 12:18:33 +00:00
|
|
|
if (const auto user = asUser()) {
|
|
|
|
return user->canWrite();
|
|
|
|
} else if (const auto channel = asChannel()) {
|
|
|
|
return channel->canWrite();
|
|
|
|
} else if (const auto chat = asChat()) {
|
|
|
|
return chat->canWrite();
|
|
|
|
}
|
|
|
|
return 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()
|
2019-09-27 17:01:36 +00:00
|
|
|
&& !isSelf()
|
2020-06-17 09:36:25 +00:00
|
|
|
&& session().serverConfig().revokePrivateInbox
|
|
|
|
&& (session().serverConfig().revokePrivateTimeLimit == 0x7FFFFFFF);
|
2019-03-21 13:48:40 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 10:37:42 +00:00
|
|
|
bool PeerData::slowmodeApplied() const {
|
|
|
|
if (const auto channel = asChannel()) {
|
|
|
|
return !channel->amCreator()
|
|
|
|
&& !channel->hasAdminRights()
|
|
|
|
&& (channel->flags() & MTPDchannel::Flag::f_slowmode_enabled);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PeerData::slowmodeSecondsLeft() const {
|
|
|
|
if (const auto channel = asChannel()) {
|
2019-08-20 09:42:13 +00:00
|
|
|
if (const auto seconds = channel->slowmodeSeconds()) {
|
|
|
|
if (const auto last = channel->slowmodeLastMessage()) {
|
|
|
|
const auto now = base::unixtime::now();
|
|
|
|
return std::max(seconds - (now - last), 0);
|
|
|
|
}
|
2019-07-17 10:37:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-15 13:30:29 +00:00
|
|
|
bool PeerData::canSendPolls() const {
|
|
|
|
if (const auto user = asUser()) {
|
2020-05-12 07:26:47 +00:00
|
|
|
return user->isBot() && !user->isSupport();
|
2020-01-15 13:30:29 +00:00
|
|
|
} else if (const auto chat = asChat()) {
|
|
|
|
return chat->canSendPolls();
|
|
|
|
} else if (const auto channel = asChannel()) {
|
|
|
|
return channel->canSendPolls();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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-06-19 15:09:03 +00:00
|
|
|
std::optional<QString> RestrictionError(
|
2019-01-14 17:43:06 +00:00
|
|
|
not_null<PeerData*> peer,
|
|
|
|
ChatRestriction restriction) {
|
|
|
|
using Flag = ChatRestriction;
|
|
|
|
if (const auto restricted = peer->amRestricted(restriction)) {
|
|
|
|
const auto all = restricted.isWithEveryone();
|
2019-10-07 14:35:22 +00:00
|
|
|
const auto channel = peer->asChannel();
|
|
|
|
if (!all && channel) {
|
|
|
|
auto restrictedUntil = channel->restrictedUntil();
|
|
|
|
if (restrictedUntil > 0 && !ChannelData::IsRestrictedForever(restrictedUntil)) {
|
|
|
|
auto restrictedUntilDateTime = base::unixtime::parse(channel->restrictedUntil());
|
|
|
|
auto date = restrictedUntilDateTime.toString(qsl("dd.MM.yy"));
|
|
|
|
auto time = restrictedUntilDateTime.toString(cTimeFormat());
|
|
|
|
|
|
|
|
switch (restriction) {
|
|
|
|
case Flag::f_send_polls:
|
|
|
|
return tr::lng_restricted_send_polls_until(
|
|
|
|
tr::now, lt_date, date, lt_time, time);
|
|
|
|
case Flag::f_send_messages:
|
|
|
|
return tr::lng_restricted_send_message_until(
|
|
|
|
tr::now, lt_date, date, lt_time, time);
|
|
|
|
case Flag::f_send_media:
|
|
|
|
return tr::lng_restricted_send_media_until(
|
|
|
|
tr::now, lt_date, date, lt_time, time);
|
|
|
|
case Flag::f_send_stickers:
|
|
|
|
return tr::lng_restricted_send_stickers_until(
|
|
|
|
tr::now, lt_date, date, lt_time, time);
|
|
|
|
case Flag::f_send_gifs:
|
|
|
|
return tr::lng_restricted_send_gifs_until(
|
|
|
|
tr::now, lt_date, date, lt_time, time);
|
|
|
|
case Flag::f_send_inline:
|
|
|
|
case Flag::f_send_games:
|
|
|
|
return tr::lng_restricted_send_inline_until(
|
|
|
|
tr::now, lt_date, date, lt_time, time);
|
|
|
|
}
|
|
|
|
Unexpected("Restriction in Data::RestrictionErrorKey.");
|
|
|
|
}
|
|
|
|
}
|
2019-01-14 17:43:06 +00:00
|
|
|
switch (restriction) {
|
|
|
|
case Flag::f_send_polls:
|
|
|
|
return all
|
2019-06-19 15:09:03 +00:00
|
|
|
? tr::lng_restricted_send_polls_all(tr::now)
|
|
|
|
: tr::lng_restricted_send_polls(tr::now);
|
2019-01-14 17:43:06 +00:00
|
|
|
case Flag::f_send_messages:
|
|
|
|
return all
|
2019-06-19 15:09:03 +00:00
|
|
|
? tr::lng_restricted_send_message_all(tr::now)
|
|
|
|
: tr::lng_restricted_send_message(tr::now);
|
2019-01-14 17:43:06 +00:00
|
|
|
case Flag::f_send_media:
|
|
|
|
return all
|
2019-06-19 15:09:03 +00:00
|
|
|
? tr::lng_restricted_send_media_all(tr::now)
|
|
|
|
: tr::lng_restricted_send_media(tr::now);
|
2019-01-14 17:43:06 +00:00
|
|
|
case Flag::f_send_stickers:
|
|
|
|
return all
|
2019-06-19 15:09:03 +00:00
|
|
|
? tr::lng_restricted_send_stickers_all(tr::now)
|
|
|
|
: tr::lng_restricted_send_stickers(tr::now);
|
2019-01-14 17:43:06 +00:00
|
|
|
case Flag::f_send_gifs:
|
|
|
|
return all
|
2019-06-19 15:09:03 +00:00
|
|
|
? tr::lng_restricted_send_gifs_all(tr::now)
|
|
|
|
: tr::lng_restricted_send_gifs(tr::now);
|
2019-01-14 17:43:06 +00:00
|
|
|
case Flag::f_send_inline:
|
|
|
|
case Flag::f_send_games:
|
|
|
|
return all
|
2019-06-19 15:09:03 +00:00
|
|
|
? tr::lng_restricted_send_inline_all(tr::now)
|
|
|
|
: tr::lng_restricted_send_inline(tr::now);
|
2019-01-14 17:43:06 +00:00
|
|
|
}
|
|
|
|
Unexpected("Restriction in Data::RestrictionErrorKey.");
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Data
|