Support reading me_url_prefix from config.

The default value 'https://t.me' can be overriden from server config.
This commit is contained in:
John Preston 2017-03-10 20:25:43 +03:00
parent 70b6335b11
commit 5bb68cfd8f
16 changed files with 74 additions and 30 deletions

View File

@ -1163,7 +1163,7 @@ void RevokePublicLinkBox::mouseReleaseEvent(QMouseEvent *e) {
setCursor((_selected || _pressed) ? style::cur_pointer : style::cur_default);
if (pressed && pressed == _selected) {
auto text_method = pressed->isMegagroup() ? lng_channels_too_much_public_revoke_confirm_group : lng_channels_too_much_public_revoke_confirm_channel;
auto text = text_method(lt_link, CreateInternalLink(pressed->userName()), lt_group, pressed->name);
auto text = text_method(lt_link, Messenger::Instance().createInternalLink(pressed->userName()), lt_group, pressed->name);
auto confirmText = lang(lng_channels_too_much_public_revoke);
_weakRevokeConfirmBox = Ui::show(Box<ConfirmBox>(text, confirmText, base::lambda_guarded(this, [this, pressed]() {
if (_revokeRequestId) return;
@ -1223,7 +1223,7 @@ void RevokePublicLinkBox::getPublicDone(const MTPmessages_Chats &result) {
ChatRow row;
row.peer = peer;
row.name.setText(st::contactsNameStyle, peer->name, _textNameOptions);
row.status.setText(st::defaultTextStyle, CreateInternalLink(textcmdLink(1, peer->userName())), _textDlgOptions);
row.status.setText(st::defaultTextStyle, Messenger::Instance().createInternalLink(textcmdLink(1, peer->userName())), _textDlgOptions);
_rows.push_back(std::move(row));
}
}

View File

@ -46,7 +46,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "storage/file_download.h"
QString PeerFloodErrorText(PeerFloodType type) {
auto link = textcmdLink(CreateInternalLinkHttps(qsl("spambot")), lang(lng_cant_more_info));
auto link = textcmdLink(Messenger::Instance().createInternalLinkFull(qsl("spambot")), lang(lng_cant_more_info));
if (type == PeerFloodType::InviteGroup) {
return lng_cant_invite_not_contact(lt_more_info, link);
}

View File

@ -39,6 +39,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "window/themes/window_theme.h"
#include "boxes/contactsbox.h"
#include "auth_session.h"
#include "messenger.h"
ShareBox::ShareBox(QWidget*, CopyCallback &&copyCallback, SubmitCallback &&submitCallback, FilterCallback &&filterCallback)
: _copyCallback(std::move(copyCallback))
@ -870,7 +871,7 @@ void shareGameScoreFromItem(HistoryItem *item) {
if (media->type() == MediaTypeGame) {
auto shortName = static_cast<HistoryGame*>(media)->game()->shortName;
QApplication::clipboard()->setText(CreateInternalLinkHttps(bot->username + qsl("?game=") + shortName));
QApplication::clipboard()->setText(Messenger::Instance().createInternalLinkFull(bot->username + qsl("?game=") + shortName));
Ui::Toast::Show(lang(lng_share_game_link_copied));
}

View File

@ -33,6 +33,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "ui/widgets/buttons.h"
#include "ui/widgets/scroll_area.h"
#include "auth_session.h"
#include "messenger.h"
StickerSetBox::StickerSetBox(QWidget*, const MTPInputStickerSet &set)
: _set(set) {
@ -68,7 +69,7 @@ void StickerSetBox::onAddStickers() {
}
void StickerSetBox::onShareStickers() {
auto url = CreateInternalLinkHttps(qsl("addstickers/") + _inner->shortName());
auto url = Messenger::Instance().createInternalLinkFull(qsl("addstickers/") + _inner->shortName());
QApplication::clipboard()->setText(url);
Ui::show(Box<InformBox>(lang(lng_stickers_copied)));
}

View File

@ -28,6 +28,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "ui/widgets/input_fields.h"
#include "ui/toast/toast.h"
#include "styles/style_boxes.h"
#include "messenger.h"
UsernameBox::UsernameBox(QWidget*)
: _username(this, st::defaultInputField, qsl("@username"), App::self()->username, false)
@ -85,7 +86,7 @@ void UsernameBox::paintEvent(QPaintEvent *e) {
if (_link->isHidden()) {
p.drawTextLeft(st::usernamePadding.left(), linky, width(), lang(lng_username_link_willbe));
p.setPen(st::usernameDefaultFg);
p.drawTextLeft(st::usernamePadding.left(), linky + st::usernameTextStyle.lineHeight + ((st::usernameTextStyle.lineHeight - st::boxTextFont->height) / 2), width(), CreateInternalLinkHttps(qsl("username")));
p.drawTextLeft(st::usernamePadding.left(), linky + st::usernameTextStyle.lineHeight + ((st::usernameTextStyle.lineHeight - st::boxTextFont->height) / 2), width(), Messenger::Instance().createInternalLinkFull(qsl("username")));
} else {
p.drawTextLeft(st::usernamePadding.left(), linky, width(), lang(lng_username_link));
}
@ -159,7 +160,7 @@ void UsernameBox::onChanged() {
}
void UsernameBox::onLinkClick() {
Application::clipboard()->setText(CreateInternalLinkHttps(getName()));
Application::clipboard()->setText(Messenger::Instance().createInternalLinkFull(getName()));
Ui::Toast::Show(lang(lng_username_copied));
}
@ -230,7 +231,7 @@ QString UsernameBox::getName() const {
void UsernameBox::updateLinkText() {
QString uname = getName();
_link->setText(st::boxTextFont->elided(CreateInternalLinkHttps(uname), st::boxWidth - st::usernamePadding.left() - st::usernamePadding.right()));
_link->setText(st::boxTextFont->elided(Messenger::Instance().createInternalLinkFull(uname), st::boxWidth - st::usernamePadding.left() - st::usernamePadding.right()));
if (uname.isEmpty()) {
if (!_link->isHidden()) {
_link->hide();

View File

@ -305,16 +305,6 @@ inline QString cApiAppVersion() {
return QString::number(AppVersion);
}
constexpr str_const AppLinksDomain = "t.me";
inline QString CreateInternalLink(const QString &query) {
return str_const_toString(AppLinksDomain) + '/' + query;
}
inline QString CreateInternalLinkHttps(const QString &query) {
return qsl("https://") + CreateInternalLink(query);
}
extern QString gKeyFile;
inline const QString &cDataFile() {
if (!gKeyFile.isEmpty()) return gKeyFile;

View File

@ -657,6 +657,7 @@ struct Data {
int32 EditTimeLimit = 172800;
int32 StickersRecentLimit = 30;
int32 PinnedDialogsCountMax = 5;
QString InternalLinksDomain = qsl("https://t.me/");
HiddenPinnedMessagesMap HiddenPinnedMessages;
@ -779,6 +780,7 @@ DefineVar(Global, int32, SavedGifsLimit);
DefineVar(Global, int32, EditTimeLimit);
DefineVar(Global, int32, StickersRecentLimit);
DefineVar(Global, int32, PinnedDialogsCountMax);
DefineVar(Global, QString, InternalLinksDomain);
DefineVar(Global, HiddenPinnedMessagesMap, HiddenPinnedMessages);

View File

@ -342,6 +342,7 @@ DeclareVar(int32, SavedGifsLimit);
DeclareVar(int32, EditTimeLimit);
DeclareVar(int32, StickersRecentLimit);
DeclareVar(int32, PinnedDialogsCountMax);
DeclareVar(QString, InternalLinksDomain);
typedef QMap<PeerId, MsgId> HiddenPinnedMessagesMap;
DeclareVar(HiddenPinnedMessagesMap, HiddenPinnedMessages);

View File

@ -29,6 +29,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "ui/effects/ripple_animation.h"
#include "storage/file_upload.h"
#include "auth_session.h"
#include "messenger.h"
namespace {
@ -793,6 +794,9 @@ bool HistoryItem::canDeleteForEveryone(const QDateTime &cur) const {
return false;
}
QString HistoryItem::directLink() const {
return hasDirectLink() ? Messenger::Instance().createInternalLinkFull(_history->peer->asChannel()->username + '/' + QString::number(id)) : QString();
}
bool HistoryItem::unread() const {
// Messages from myself are always read.
if (history()->peer->isSelf()) return false;

View File

@ -713,9 +713,7 @@ public:
bool hasDirectLink() const {
return id > 0 && _history->peer->isChannel() && _history->peer->asChannel()->isPublic() && !_history->peer->isMegagroup();
}
QString directLink() const {
return hasDirectLink() ? CreateInternalLinkHttps(_history->peer->asChannel()->username + '/' + QString::number(id)) : QString();
}
QString directLink() const;
int32 y;
MsgId id;

View File

@ -590,6 +590,44 @@ void Messenger::authSessionDestroy() {
authSessionChanged().notify();
}
void Messenger::setInternalLinkDomain(const QString &domain) const {
// This domain should start with 'http[s]://' and end with '/', like 'https://t.me/'.
auto validate = [](auto &domain) {
auto prefixes = {
qstr("https://"),
qstr("http://"),
};
for (auto &prefix : prefixes) {
if (domain.startsWith(prefix, Qt::CaseInsensitive)) {
return domain.endsWith('/');
}
}
return false;
};
if (validate(domain) && domain != Global::InternalLinksDomain()) {
Global::SetInternalLinksDomain(domain);
}
}
QString Messenger::createInternalLink(const QString &query) const {
auto result = createInternalLinkFull(query);
auto prefixes = {
qstr("https://"),
qstr("http://"),
};
for (auto &prefix : prefixes) {
if (result.startsWith(prefix, Qt::CaseInsensitive)) {
return result.mid(prefix.size());
}
}
LOG(("Warning: bad internal url '%1'").arg(result));
return result;
}
QString Messenger::createInternalLinkFull(const QString &query) const {
return Global::InternalLinksDomain() + query;
}
FileUploader *Messenger::uploader() {
if (!_uploader && !App::quitting()) _uploader = new FileUploader();
return _uploader;

View File

@ -77,6 +77,10 @@ public:
return _authSessionChanged;
}
void setInternalLinkDomain(const QString &domain) const;
QString createInternalLink(const QString &query) const;
QString createInternalLinkFull(const QString &query) const;
FileUploader *uploader();
void uploadProfilePhoto(const QImage &tosend, const PeerId &peerId);
void regPhotoUpdate(const PeerId &peer, const FullMsgId &msgId);

View File

@ -560,6 +560,7 @@ void Instance::Private::configLoadDone(const MTPConfig &result) {
Global::SetEditTimeLimit(data.vedit_time_limit.v); // ?
Global::SetStickersRecentLimit(data.vstickers_recent_limit.v);
Global::SetPinnedDialogsCountMax(data.vpinned_dialogs_count_max.v);
Messenger::Instance().setInternalLinkDomain(qs(data.vme_url_prefix));
Local::writeSettings();

View File

@ -29,6 +29,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "observer_peer.h"
#include "apiwrap.h"
#include "lang.h"
#include "messenger.h"
namespace Profile {
@ -191,10 +192,10 @@ void InfoWidget::refreshChannelLink() {
TextWithEntities channelLinkTextShort;
if (auto channel = peer()->asChannel()) {
if (!channel->username.isEmpty()) {
channelLinkText.text = CreateInternalLinkHttps(channel->username);
channelLinkText.text = Messenger::Instance().createInternalLinkFull(channel->username);
channelLinkText.entities.push_back(EntityInText(EntityInTextUrl, 0, channelLinkText.text.size()));
channelLinkTextShort.text = CreateInternalLink(channel->username);
channelLinkTextShort.entities.push_back(EntityInText(EntityInTextCustomUrl, 0, channelLinkTextShort.text.size(), CreateInternalLinkHttps(channel->username)));
channelLinkTextShort.text = Messenger::Instance().createInternalLink(channel->username);
channelLinkTextShort.entities.push_back(EntityInText(EntityInTextCustomUrl, 0, channelLinkTextShort.text.size(), Messenger::Instance().createInternalLinkFull(channel->username)));
}
}
setLabeledText(nullptr, lang(lng_profile_link), &_channelLink, channelLinkText, QString());

View File

@ -27,6 +27,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "boxes/usernamebox.h"
#include "boxes/change_phone_box.h"
#include "observer_peer.h"
#include "messenger.h"
namespace Settings {
@ -86,7 +87,7 @@ void InfoWidget::refreshUsername() {
usernameText.text = '@' + self()->username;
copyText = lang(lng_context_copy_mention);
}
usernameText.entities.push_back(EntityInText(EntityInTextCustomUrl, 0, usernameText.text.size(), CreateInternalLinkHttps(self()->username)));
usernameText.entities.push_back(EntityInText(EntityInTextCustomUrl, 0, usernameText.text.size(), Messenger::Instance().createInternalLinkFull(self()->username)));
setLabeledText(_username, lang(lng_profile_username), usernameText, TextWithEntities(), copyText);
if (auto text = _username->entity()->textLabel()) {
text->setClickHandlerHook([](const ClickHandlerPtr &handler, Qt::MouseButton button) {
@ -100,10 +101,10 @@ void InfoWidget::refreshLink() {
TextWithEntities linkText;
TextWithEntities linkTextShort;
if (!self()->username.isEmpty()) {
linkText.text = CreateInternalLinkHttps(self()->username);
linkText.text = Messenger::Instance().createInternalLinkFull(self()->username);
linkText.entities.push_back(EntityInText(EntityInTextUrl, 0, linkText.text.size()));
linkTextShort.text = CreateInternalLink(self()->username);
linkTextShort.entities.push_back(EntityInText(EntityInTextCustomUrl, 0, linkTextShort.text.size(), CreateInternalLinkHttps(self()->username)));
linkTextShort.text = Messenger::Instance().createInternalLink(self()->username);
linkTextShort.entities.push_back(EntityInText(EntityInTextCustomUrl, 0, linkTextShort.text.size(), Messenger::Instance().createInternalLinkFull(self()->username)));
}
setLabeledText(_link, lang(lng_profile_link), linkText, linkTextShort, QString());
if (auto text = _link->entity()->textLabel()) {

View File

@ -26,6 +26,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "window/themes/window_theme.h"
#include "lang.h"
#include "numbers.h"
#include "messenger.h"
namespace Ui {
namespace {
@ -3828,8 +3829,8 @@ void PortInput::correctValue(const QString &was, int32 wasCursor, QString &now,
setCorrectedText(now, nowCursor, newText, newPos);
}
UsernameInput::UsernameInput(QWidget *parent, const style::InputField &st, const QString &ph, const QString &val, bool isLink) : MaskedInputField(parent, st, ph, val),
_linkPlaceholder(isLink ? CreateInternalLink(QString()) : QString()) {
UsernameInput::UsernameInput(QWidget *parent, const style::InputField &st, const QString &ph, const QString &val, bool isLink) : MaskedInputField(parent, st, ph, val)
, _linkPlaceholder(isLink ? Messenger::Instance().createInternalLink(QString()) : QString()) {
if (!_linkPlaceholder.isEmpty()) {
setTextMargins(style::margins(_st.textMargins.left() + _st.font->width(_linkPlaceholder), _st.textMargins.top(), _st.textMargins.right(), _st.textMargins.bottom()));
setPlaceholderHidden(true);