2018-11-15 15:36:04 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "support/support_helper.h"
|
|
|
|
|
|
|
|
#include "dialogs/dialogs_key.h"
|
|
|
|
#include "data/data_drafts.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "data/data_user.h"
|
2019-01-18 12:27:37 +00:00
|
|
|
#include "data/data_session.h"
|
2019-09-16 11:14:06 +00:00
|
|
|
#include "api/api_text_entities.h"
|
2018-11-15 15:36:04 +00:00
|
|
|
#include "history/history.h"
|
2018-11-20 15:36:36 +00:00
|
|
|
#include "boxes/abstract_box.h"
|
|
|
|
#include "ui/toast/toast.h"
|
|
|
|
#include "ui/widgets/input_fields.h"
|
|
|
|
#include "ui/text/text_entity.h"
|
|
|
|
#include "ui/text_options.h"
|
|
|
|
#include "chat_helpers/message_field.h"
|
2018-11-21 10:09:46 +00:00
|
|
|
#include "chat_helpers/emoji_suggestions_widget.h"
|
2019-07-10 17:28:33 +00:00
|
|
|
#include "base/unixtime.h"
|
2018-11-20 15:36:36 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2019-06-06 10:21:40 +00:00
|
|
|
#include "window/window_session_controller.h"
|
2018-12-03 11:20:31 +00:00
|
|
|
#include "storage/storage_media_prepare.h"
|
|
|
|
#include "storage/localimageloader.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "core/sandbox.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2018-11-15 15:36:04 +00:00
|
|
|
#include "observer_peer.h"
|
|
|
|
#include "apiwrap.h"
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "facades.h"
|
2019-09-18 11:19:05 +00:00
|
|
|
#include "styles/style_layers.h"
|
2018-11-20 15:36:36 +00:00
|
|
|
#include "styles/style_boxes.h"
|
2018-11-15 15:36:04 +00:00
|
|
|
|
2019-08-02 10:40:35 +00:00
|
|
|
namespace Main {
|
|
|
|
class Session;
|
|
|
|
} // namespace Main
|
|
|
|
|
2018-11-15 15:36:04 +00:00
|
|
|
namespace Support {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kOccupyFor = TimeId(60);
|
2019-02-19 06:57:53 +00:00
|
|
|
constexpr auto kReoccupyEach = 30 * crl::time(1000);
|
2018-11-21 06:43:35 +00:00
|
|
|
constexpr auto kMaxSupportInfoLength = MaxMessageSize * 4;
|
2018-11-15 15:36:04 +00:00
|
|
|
|
2019-09-18 11:19:05 +00:00
|
|
|
class EditInfoBox : public Ui::BoxContent {
|
2018-11-20 15:36:36 +00:00
|
|
|
public:
|
|
|
|
EditInfoBox(
|
|
|
|
QWidget*,
|
2019-08-02 10:40:35 +00:00
|
|
|
not_null<Main::Session*> session,
|
2018-11-20 15:36:36 +00:00
|
|
|
const TextWithTags &text,
|
|
|
|
Fn<void(TextWithTags, Fn<void(bool success)>)> submit);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void prepare() override;
|
|
|
|
void setInnerFocus() override;
|
|
|
|
|
|
|
|
private:
|
2019-08-02 10:40:35 +00:00
|
|
|
not_null<Main::Session*> _session;
|
2018-11-20 15:36:36 +00:00
|
|
|
object_ptr<Ui::InputField> _field = { nullptr };
|
|
|
|
Fn<void(TextWithTags, Fn<void(bool success)>)> _submit;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
EditInfoBox::EditInfoBox(
|
|
|
|
QWidget*,
|
2019-08-02 10:40:35 +00:00
|
|
|
not_null<Main::Session*> session,
|
2018-11-20 15:36:36 +00:00
|
|
|
const TextWithTags &text,
|
|
|
|
Fn<void(TextWithTags, Fn<void(bool success)>)> submit)
|
2019-08-02 10:40:35 +00:00
|
|
|
: _session(session)
|
|
|
|
, _field(
|
2018-11-20 15:36:36 +00:00
|
|
|
this,
|
|
|
|
st::supportInfoField,
|
|
|
|
Ui::InputField::Mode::MultiLine,
|
2019-06-18 14:07:45 +00:00
|
|
|
rpl::single(qsl("Support information")), // #TODO hard_lang
|
2018-11-20 15:36:36 +00:00
|
|
|
text)
|
|
|
|
, _submit(std::move(submit)) {
|
2018-11-21 06:43:35 +00:00
|
|
|
_field->setMaxLength(kMaxSupportInfoLength);
|
2018-11-20 15:36:36 +00:00
|
|
|
_field->setSubmitSettings(Ui::InputField::SubmitSettings::Both);
|
|
|
|
_field->setInstantReplaces(Ui::InstantReplaces::Default());
|
2019-08-02 10:40:35 +00:00
|
|
|
_field->setInstantReplacesEnabled(
|
|
|
|
session->settings().replaceEmojiValue());
|
2018-11-20 15:36:36 +00:00
|
|
|
_field->setMarkdownReplacesEnabled(rpl::single(true));
|
2019-08-02 10:40:35 +00:00
|
|
|
_field->setEditLinkCallback(DefaultEditLinkCallback(session, _field));
|
2018-11-20 15:36:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditInfoBox::prepare() {
|
2019-06-18 15:00:55 +00:00
|
|
|
setTitle(rpl::single(qsl("Edit support information"))); // #TODO hard_lang
|
2018-11-20 15:36:36 +00:00
|
|
|
|
|
|
|
const auto save = [=] {
|
|
|
|
const auto done = crl::guard(this, [=](bool success) {
|
|
|
|
if (success) {
|
|
|
|
closeBox();
|
|
|
|
} else {
|
|
|
|
_field->showError();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
_submit(_field->getTextWithAppliedMarkdown(), done);
|
|
|
|
};
|
2019-06-18 16:53:27 +00:00
|
|
|
addButton(tr::lng_settings_save(), save);
|
|
|
|
addButton(tr::lng_cancel(), [=] { closeBox(); });
|
2018-11-20 15:36:36 +00:00
|
|
|
|
|
|
|
connect(_field, &Ui::InputField::submitted, save);
|
|
|
|
connect(_field, &Ui::InputField::cancelled, [=] { closeBox(); });
|
2018-11-21 10:09:46 +00:00
|
|
|
Ui::Emoji::SuggestionsController::Init(
|
|
|
|
getDelegate()->outerContainer(),
|
2019-08-02 10:40:35 +00:00
|
|
|
_field,
|
|
|
|
_session);
|
2018-11-20 15:36:36 +00:00
|
|
|
|
|
|
|
auto cursor = _field->textCursor();
|
|
|
|
cursor.movePosition(QTextCursor::End);
|
|
|
|
_field->setTextCursor(cursor);
|
|
|
|
|
|
|
|
widthValue(
|
|
|
|
) | rpl::start_with_next([=](int width) {
|
|
|
|
_field->resizeToWidth(
|
|
|
|
width - st::boxPadding.left() - st::boxPadding.right());
|
|
|
|
_field->moveToLeft(st::boxPadding.left(), st::boxPadding.bottom());
|
|
|
|
}, _field->lifetime());
|
|
|
|
|
|
|
|
_field->heightValue(
|
|
|
|
) | rpl::start_with_next([=](int height) {
|
|
|
|
setDimensions(
|
|
|
|
st::boxWideWidth,
|
|
|
|
st::boxPadding.bottom() + height + st::boxPadding.bottom());
|
|
|
|
}, _field->lifetime());
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditInfoBox::setInnerFocus() {
|
|
|
|
_field->setFocusFast();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FormatDateTime(TimeId value) {
|
|
|
|
const auto now = QDateTime::currentDateTime();
|
2019-07-10 17:28:33 +00:00
|
|
|
const auto date = base::unixtime::parse(value);
|
2018-11-20 15:36:36 +00:00
|
|
|
if (date.date() == now.date()) {
|
2019-06-19 16:39:25 +00:00
|
|
|
return tr::lng_mediaview_today(
|
|
|
|
tr::now,
|
2018-11-20 15:36:36 +00:00
|
|
|
lt_time,
|
|
|
|
date.time().toString(cTimeFormat()));
|
|
|
|
} else if (date.date().addDays(1) == now.date()) {
|
2019-06-19 16:39:25 +00:00
|
|
|
return tr::lng_mediaview_yesterday(
|
|
|
|
tr::now,
|
2018-11-20 15:36:36 +00:00
|
|
|
lt_time,
|
|
|
|
date.time().toString(cTimeFormat()));
|
|
|
|
} else {
|
2019-06-19 16:39:25 +00:00
|
|
|
return tr::lng_mediaview_date_time(
|
|
|
|
tr::now,
|
2018-11-20 15:36:36 +00:00
|
|
|
lt_date,
|
|
|
|
date.date().toString(qsl("dd.MM.yy")),
|
|
|
|
lt_time,
|
|
|
|
date.time().toString(cTimeFormat()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:36:04 +00:00
|
|
|
uint32 OccupationTag() {
|
2019-01-21 13:42:21 +00:00
|
|
|
return uint32(Core::Sandbox::Instance().installationTag() & 0xFFFFFFFF);
|
2018-11-15 15:36:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-20 13:34:07 +00:00
|
|
|
QString NormalizeName(QString name) {
|
|
|
|
return name.replace(':', '_').replace(';', '_');
|
|
|
|
}
|
|
|
|
|
|
|
|
Data::Draft OccupiedDraft(const QString &normalizedName) {
|
2019-07-10 17:28:33 +00:00
|
|
|
const auto now = base::unixtime::now(), till = now + kOccupyFor;
|
2018-11-15 15:36:04 +00:00
|
|
|
return {
|
|
|
|
TextWithTags{ "t:"
|
|
|
|
+ QString::number(till)
|
|
|
|
+ ";u:"
|
2018-11-20 13:34:07 +00:00
|
|
|
+ QString::number(OccupationTag())
|
|
|
|
+ ";n:"
|
|
|
|
+ normalizedName },
|
2018-11-15 15:36:04 +00:00
|
|
|
MsgId(0),
|
|
|
|
MessageCursor(),
|
|
|
|
false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-11-22 13:00:14 +00:00
|
|
|
[[nodiscard]] bool TrackHistoryOccupation(History *history) {
|
|
|
|
if (!history) {
|
|
|
|
return false;
|
|
|
|
} else if (const auto user = history->peer->asUser()) {
|
2019-07-18 08:51:11 +00:00
|
|
|
return !user->isBot();
|
2018-11-22 13:00:14 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:36:04 +00:00
|
|
|
uint32 ParseOccupationTag(History *history) {
|
2018-11-22 13:00:14 +00:00
|
|
|
if (!TrackHistoryOccupation(history)) {
|
2018-11-15 15:36:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const auto draft = history->cloudDraft();
|
|
|
|
if (!draft) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const auto &text = draft->textWithTags.text;
|
|
|
|
#ifndef OS_MAC_OLD
|
|
|
|
const auto parts = text.splitRef(';');
|
|
|
|
#else // OS_MAC_OLD
|
|
|
|
const auto parts = text.split(';');
|
|
|
|
#endif // OS_MAC_OLD
|
|
|
|
auto valid = false;
|
|
|
|
auto result = uint32();
|
|
|
|
for (const auto &part : parts) {
|
|
|
|
if (part.startsWith(qstr("t:"))) {
|
2019-07-10 17:28:33 +00:00
|
|
|
if (part.mid(2).toInt() >= base::unixtime::now()) {
|
2018-11-15 15:36:04 +00:00
|
|
|
valid = true;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if (part.startsWith(qstr("u:"))) {
|
|
|
|
result = part.mid(2).toUInt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return valid ? result : 0;
|
|
|
|
}
|
|
|
|
|
2018-11-20 13:34:07 +00:00
|
|
|
QString ParseOccupationName(History *history) {
|
2018-11-22 13:00:14 +00:00
|
|
|
if (!TrackHistoryOccupation(history)) {
|
2018-11-20 13:34:07 +00:00
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
const auto draft = history->cloudDraft();
|
|
|
|
if (!draft) {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
const auto &text = draft->textWithTags.text;
|
|
|
|
#ifndef OS_MAC_OLD
|
|
|
|
const auto parts = text.splitRef(';');
|
|
|
|
#else // OS_MAC_OLD
|
|
|
|
const auto parts = text.split(';');
|
|
|
|
#endif // OS_MAC_OLD
|
|
|
|
auto valid = false;
|
|
|
|
auto result = QString();
|
|
|
|
for (const auto &part : parts) {
|
|
|
|
if (part.startsWith(qstr("t:"))) {
|
2019-07-10 17:28:33 +00:00
|
|
|
if (part.mid(2).toInt() >= base::unixtime::now()) {
|
2018-11-20 13:34:07 +00:00
|
|
|
valid = true;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if (part.startsWith(qstr("n:"))) {
|
2018-11-29 12:52:00 +00:00
|
|
|
#ifndef OS_MAC_OLD
|
2018-11-20 13:34:07 +00:00
|
|
|
result = part.mid(2).toString();
|
2018-11-29 12:52:00 +00:00
|
|
|
#else // OS_MAC_OLD
|
|
|
|
result = part.mid(2);
|
|
|
|
#endif // OS_MAC_OLD
|
2018-11-20 13:34:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return valid ? result : QString();
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:36:04 +00:00
|
|
|
TimeId OccupiedBySomeoneTill(History *history) {
|
2018-11-22 13:00:14 +00:00
|
|
|
if (!TrackHistoryOccupation(history)) {
|
2018-11-15 15:36:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const auto draft = history->cloudDraft();
|
|
|
|
if (!draft) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const auto &text = draft->textWithTags.text;
|
|
|
|
#ifndef OS_MAC_OLD
|
|
|
|
const auto parts = text.splitRef(';');
|
|
|
|
#else // OS_MAC_OLD
|
|
|
|
const auto parts = text.split(';');
|
|
|
|
#endif // OS_MAC_OLD
|
|
|
|
auto valid = false;
|
|
|
|
auto result = TimeId();
|
|
|
|
for (const auto &part : parts) {
|
|
|
|
if (part.startsWith(qstr("t:"))) {
|
2019-07-10 17:28:33 +00:00
|
|
|
if (part.mid(2).toInt() >= base::unixtime::now()) {
|
2018-11-15 15:36:04 +00:00
|
|
|
result = part.mid(2).toInt();
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if (part.startsWith(qstr("u:"))) {
|
|
|
|
if (part.mid(2).toUInt() != OccupationTag()) {
|
|
|
|
valid = true;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return valid ? result : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
Helper::Helper(not_null<Main::Session*> session)
|
2018-11-15 15:36:04 +00:00
|
|
|
: _session(session)
|
2019-11-27 08:02:56 +00:00
|
|
|
, _api(_session->api().instance())
|
2018-11-15 15:36:04 +00:00
|
|
|
, _templates(_session)
|
|
|
|
, _reoccupyTimer([=] { reoccupy(); })
|
|
|
|
, _checkOccupiedTimer([=] { checkOccupiedChats(); }) {
|
2019-11-27 08:02:56 +00:00
|
|
|
_api.request(MTPhelp_GetSupportName(
|
2018-11-20 13:34:07 +00:00
|
|
|
)).done([=](const MTPhelp_SupportName &result) {
|
|
|
|
result.match([&](const MTPDhelp_supportName &data) {
|
2019-07-05 13:38:38 +00:00
|
|
|
setSupportName(qs(data.vname()));
|
2018-11-20 13:34:07 +00:00
|
|
|
});
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
setSupportName(
|
2019-01-21 13:42:21 +00:00
|
|
|
qsl("[rand^")
|
|
|
|
+ QString::number(Core::Sandbox::Instance().installationTag())
|
|
|
|
+ ']');
|
2018-11-20 13:34:07 +00:00
|
|
|
}).send();
|
2018-11-15 15:36:04 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
std::unique_ptr<Helper> Helper::Create(not_null<Main::Session*> session) {
|
2019-01-03 12:36:01 +00:00
|
|
|
//return std::make_unique<Helper>(session); AssertIsDebug();
|
|
|
|
const auto valid = session->user()->phone().startsWith(qstr("424"));
|
|
|
|
return valid ? std::make_unique<Helper>(session) : nullptr;
|
|
|
|
}
|
|
|
|
|
2019-06-06 10:21:40 +00:00
|
|
|
void Helper::registerWindow(not_null<Window::SessionController*> controller) {
|
2018-11-15 15:36:04 +00:00
|
|
|
controller->activeChatValue(
|
|
|
|
) | rpl::map([](Dialogs::Key key) {
|
2018-11-20 15:36:36 +00:00
|
|
|
const auto history = key.history();
|
2018-11-22 13:00:14 +00:00
|
|
|
return TrackHistoryOccupation(history) ? history : nullptr;
|
2018-11-15 15:36:04 +00:00
|
|
|
}) | rpl::distinct_until_changed(
|
|
|
|
) | rpl::start_with_next([=](History *history) {
|
|
|
|
updateOccupiedHistory(controller, history);
|
|
|
|
}, controller->lifetime());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Helper::cloudDraftChanged(not_null<History*> history) {
|
|
|
|
chatOccupiedUpdated(history);
|
|
|
|
if (history != _occupiedHistory) {
|
|
|
|
return;
|
|
|
|
}
|
2018-11-20 13:34:07 +00:00
|
|
|
occupyIfNotYet();
|
2018-11-15 15:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Helper::chatOccupiedUpdated(not_null<History*> history) {
|
|
|
|
if (const auto till = OccupiedBySomeoneTill(history)) {
|
|
|
|
_occupiedChats[history] = till + 2;
|
|
|
|
Notify::peerUpdatedDelayed(
|
|
|
|
history->peer,
|
2018-11-20 15:36:36 +00:00
|
|
|
Notify::PeerUpdate::Flag::UserOccupiedChanged);
|
2018-11-15 15:36:04 +00:00
|
|
|
checkOccupiedChats();
|
|
|
|
} else if (_occupiedChats.take(history)) {
|
|
|
|
Notify::peerUpdatedDelayed(
|
|
|
|
history->peer,
|
2018-11-20 15:36:36 +00:00
|
|
|
Notify::PeerUpdate::Flag::UserOccupiedChanged);
|
2018-11-15 15:36:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Helper::checkOccupiedChats() {
|
2019-07-10 17:28:33 +00:00
|
|
|
const auto now = base::unixtime::now();
|
2018-11-15 15:36:04 +00:00
|
|
|
while (!_occupiedChats.empty()) {
|
|
|
|
const auto nearest = ranges::min_element(
|
|
|
|
_occupiedChats,
|
|
|
|
std::less<>(),
|
|
|
|
[](const auto &pair) { return pair.second; });
|
|
|
|
if (nearest->second <= now) {
|
|
|
|
const auto history = nearest->first;
|
|
|
|
_occupiedChats.erase(nearest);
|
|
|
|
Notify::peerUpdatedDelayed(
|
|
|
|
history->peer,
|
2018-11-20 15:36:36 +00:00
|
|
|
Notify::PeerUpdate::Flag::UserOccupiedChanged);
|
2018-11-15 15:36:04 +00:00
|
|
|
} else {
|
|
|
|
_checkOccupiedTimer.callOnce(
|
2019-02-19 06:57:53 +00:00
|
|
|
(nearest->second - now) * crl::time(1000));
|
2018-11-15 15:36:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_checkOccupiedTimer.cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Helper::updateOccupiedHistory(
|
2019-06-06 10:21:40 +00:00
|
|
|
not_null<Window::SessionController*> controller,
|
2018-11-15 15:36:04 +00:00
|
|
|
History *history) {
|
2018-11-20 13:34:07 +00:00
|
|
|
if (isOccupiedByMe(_occupiedHistory)) {
|
2018-11-15 15:36:04 +00:00
|
|
|
_occupiedHistory->clearCloudDraft();
|
|
|
|
_session->api().saveDraftToCloudDelayed(_occupiedHistory);
|
|
|
|
}
|
|
|
|
_occupiedHistory = history;
|
|
|
|
occupyInDraft();
|
|
|
|
}
|
|
|
|
|
2018-11-20 13:34:07 +00:00
|
|
|
void Helper::setSupportName(const QString &name) {
|
|
|
|
_supportName = name;
|
|
|
|
_supportNameNormalized = NormalizeName(name);
|
|
|
|
occupyIfNotYet();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Helper::occupyIfNotYet() {
|
|
|
|
if (!isOccupiedByMe(_occupiedHistory)) {
|
|
|
|
occupyInDraft();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:36:04 +00:00
|
|
|
void Helper::occupyInDraft() {
|
2018-11-20 13:34:07 +00:00
|
|
|
if (_occupiedHistory
|
|
|
|
&& !isOccupiedBySomeone(_occupiedHistory)
|
|
|
|
&& !_supportName.isEmpty()) {
|
|
|
|
const auto draft = OccupiedDraft(_supportNameNormalized);
|
2018-11-15 15:36:04 +00:00
|
|
|
_occupiedHistory->createCloudDraft(&draft);
|
|
|
|
_session->api().saveDraftToCloudDelayed(_occupiedHistory);
|
|
|
|
_reoccupyTimer.callEach(kReoccupyEach);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Helper::reoccupy() {
|
2018-11-20 13:34:07 +00:00
|
|
|
if (isOccupiedByMe(_occupiedHistory)) {
|
|
|
|
const auto draft = OccupiedDraft(_supportNameNormalized);
|
2018-11-15 15:36:04 +00:00
|
|
|
_occupiedHistory->createCloudDraft(&draft);
|
|
|
|
_session->api().saveDraftToCloudDelayed(_occupiedHistory);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-20 13:34:07 +00:00
|
|
|
bool Helper::isOccupiedByMe(History *history) const {
|
2018-11-15 15:36:04 +00:00
|
|
|
if (const auto tag = ParseOccupationTag(history)) {
|
|
|
|
return (tag == OccupationTag());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-20 13:34:07 +00:00
|
|
|
bool Helper::isOccupiedBySomeone(History *history) const {
|
2018-11-15 15:36:04 +00:00
|
|
|
if (const auto tag = ParseOccupationTag(history)) {
|
|
|
|
return (tag != OccupationTag());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-20 15:36:36 +00:00
|
|
|
void Helper::refreshInfo(not_null<UserData*> user) {
|
2019-11-27 08:02:56 +00:00
|
|
|
_api.request(MTPhelp_GetUserInfo(
|
2018-11-20 15:36:36 +00:00
|
|
|
user->inputUser
|
|
|
|
)).done([=](const MTPhelp_UserInfo &result) {
|
|
|
|
applyInfo(user, result);
|
|
|
|
if (_userInfoEditPending.contains(user)) {
|
|
|
|
_userInfoEditPending.erase(user);
|
|
|
|
showEditInfoBox(user);
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Helper::applyInfo(
|
|
|
|
not_null<UserData*> user,
|
|
|
|
const MTPhelp_UserInfo &result) {
|
|
|
|
const auto notify = [&] {
|
|
|
|
Notify::peerUpdatedDelayed(
|
|
|
|
user,
|
|
|
|
Notify::PeerUpdate::Flag::UserSupportInfoChanged);
|
|
|
|
};
|
|
|
|
const auto remove = [&] {
|
|
|
|
if (_userInformation.take(user)) {
|
|
|
|
notify();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
result.match([&](const MTPDhelp_userInfo &data) {
|
|
|
|
auto info = UserInfo();
|
2019-07-05 13:38:38 +00:00
|
|
|
info.author = qs(data.vauthor());
|
|
|
|
info.date = data.vdate().v;
|
2018-11-20 15:36:36 +00:00
|
|
|
info.text = TextWithEntities{
|
2019-07-05 13:38:38 +00:00
|
|
|
qs(data.vmessage()),
|
2019-09-16 11:14:06 +00:00
|
|
|
Api::EntitiesFromMTP(data.ventities().v) };
|
2018-11-20 15:36:36 +00:00
|
|
|
if (info.text.empty()) {
|
|
|
|
remove();
|
|
|
|
} else if (_userInformation[user] != info) {
|
|
|
|
_userInformation[user] = info;
|
|
|
|
notify();
|
|
|
|
}
|
|
|
|
}, [&](const MTPDhelp_userInfoEmpty &) {
|
|
|
|
remove();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<UserInfo> Helper::infoValue(not_null<UserData*> user) const {
|
|
|
|
return Notify::PeerUpdateValue(
|
|
|
|
user,
|
|
|
|
Notify::PeerUpdate::Flag::UserSupportInfoChanged
|
|
|
|
) | rpl::map([=] {
|
|
|
|
return infoCurrent(user);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<QString> Helper::infoLabelValue(
|
|
|
|
not_null<UserData*> user) const {
|
|
|
|
return infoValue(
|
|
|
|
user
|
|
|
|
) | rpl::map([](const Support::UserInfo &info) {
|
|
|
|
return info.author + ", " + FormatDateTime(info.date);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<TextWithEntities> Helper::infoTextValue(
|
|
|
|
not_null<UserData*> user) const {
|
|
|
|
return infoValue(
|
|
|
|
user
|
|
|
|
) | rpl::map([](const Support::UserInfo &info) {
|
|
|
|
return info.text;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
UserInfo Helper::infoCurrent(not_null<UserData*> user) const {
|
|
|
|
const auto i = _userInformation.find(user);
|
|
|
|
return (i != end(_userInformation)) ? i->second : UserInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Helper::editInfo(not_null<UserData*> user) {
|
|
|
|
if (!_userInfoEditPending.contains(user)) {
|
|
|
|
_userInfoEditPending.emplace(user);
|
|
|
|
refreshInfo(user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Helper::showEditInfoBox(not_null<UserData*> user) {
|
|
|
|
const auto info = infoCurrent(user);
|
|
|
|
const auto editData = TextWithTags{
|
|
|
|
info.text.text,
|
2019-09-16 11:14:06 +00:00
|
|
|
TextUtilities::ConvertEntitiesToTextTags(info.text.entities)
|
2018-11-20 15:36:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto save = [=](TextWithTags result, Fn<void(bool)> done) {
|
|
|
|
saveInfo(user, TextWithEntities{
|
|
|
|
result.text,
|
2019-09-16 11:14:06 +00:00
|
|
|
TextUtilities::ConvertTextTagsToEntities(result.tags)
|
2018-11-20 15:36:36 +00:00
|
|
|
}, done);
|
|
|
|
};
|
2019-08-02 10:40:35 +00:00
|
|
|
Ui::show(
|
|
|
|
Box<EditInfoBox>(&user->session(), editData, save),
|
2019-09-18 11:19:05 +00:00
|
|
|
Ui::LayerOption::KeepOther);
|
2018-11-20 15:36:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Helper::saveInfo(
|
|
|
|
not_null<UserData*> user,
|
|
|
|
TextWithEntities text,
|
|
|
|
Fn<void(bool success)> done) {
|
|
|
|
const auto i = _userInfoSaving.find(user);
|
|
|
|
if (i != end(_userInfoSaving)) {
|
|
|
|
if (i->second.data == text) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
i->second.data = text;
|
2019-11-27 08:02:56 +00:00
|
|
|
_api.request(base::take(i->second.requestId)).cancel();
|
2018-11-20 15:36:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_userInfoSaving.emplace(user, SavingInfo{ text });
|
|
|
|
}
|
|
|
|
|
|
|
|
TextUtilities::PrepareForSending(
|
|
|
|
text,
|
|
|
|
Ui::ItemTextDefaultOptions().flags);
|
|
|
|
TextUtilities::Trim(text);
|
|
|
|
|
2019-09-16 11:14:06 +00:00
|
|
|
const auto entities = Api::EntitiesToMTP(
|
2018-11-20 15:36:36 +00:00
|
|
|
text.entities,
|
2019-09-16 11:14:06 +00:00
|
|
|
Api::ConvertOption::SkipLocal);
|
2019-11-27 08:02:56 +00:00
|
|
|
_userInfoSaving[user].requestId = _api.request(MTPhelp_EditUserInfo(
|
2018-11-20 15:36:36 +00:00
|
|
|
user->inputUser,
|
|
|
|
MTP_string(text.text),
|
|
|
|
entities
|
|
|
|
)).done([=](const MTPhelp_UserInfo &result) {
|
|
|
|
applyInfo(user, result);
|
|
|
|
done(true);
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
done(false);
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2018-11-20 13:34:07 +00:00
|
|
|
Templates &Helper::templates() {
|
|
|
|
return _templates;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ChatOccupiedString(not_null<History*> history) {
|
|
|
|
const auto hand = QString::fromUtf8("\xe2\x9c\x8b\xef\xb8\x8f");
|
|
|
|
const auto name = ParseOccupationName(history);
|
|
|
|
return (name.isEmpty() || name.startsWith(qstr("[rand^")))
|
|
|
|
? hand + " chat taken"
|
|
|
|
: hand + ' ' + name + " is here";
|
2018-11-15 15:36:04 +00:00
|
|
|
}
|
|
|
|
|
2018-12-03 11:20:31 +00:00
|
|
|
QString InterpretSendPath(const QString &path) {
|
|
|
|
QFile f(path);
|
|
|
|
if (!f.open(QIODevice::ReadOnly)) {
|
|
|
|
return "App Error: Could not open interpret file: " + path;
|
|
|
|
}
|
|
|
|
const auto content = QString::fromUtf8(f.readAll());
|
|
|
|
f.close();
|
|
|
|
const auto lines = content.split('\n');
|
|
|
|
auto toId = PeerId(0);
|
|
|
|
auto filePath = QString();
|
|
|
|
auto caption = QString();
|
|
|
|
for (const auto &line : lines) {
|
|
|
|
if (line.startsWith(qstr("from: "))) {
|
|
|
|
if (Auth().userId() != line.mid(qstr("from: ").size()).toInt()) {
|
|
|
|
return "App Error: Wrong current user.";
|
|
|
|
}
|
|
|
|
} else if (line.startsWith(qstr("channel: "))) {
|
|
|
|
const auto channelId = line.mid(qstr("channel: ").size()).toInt();
|
|
|
|
toId = peerFromChannel(channelId);
|
|
|
|
} else if (line.startsWith(qstr("file: "))) {
|
|
|
|
const auto path = line.mid(qstr("file: ").size());
|
|
|
|
if (!QFile(path).exists()) {
|
|
|
|
return "App Error: Could not find file with path: " + path;
|
|
|
|
}
|
|
|
|
filePath = path;
|
|
|
|
} else if (line.startsWith(qstr("caption: "))) {
|
|
|
|
caption = line.mid(qstr("caption: ").size());
|
|
|
|
} else if (!caption.isEmpty()) {
|
|
|
|
caption += '\n' + line;
|
|
|
|
} else {
|
|
|
|
return "App Error: Invalid command: " + line;
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 12:27:37 +00:00
|
|
|
const auto history = Auth().data().historyLoaded(toId);
|
2018-12-03 11:20:31 +00:00
|
|
|
if (!history) {
|
|
|
|
return "App Error: Could not find channel with id: " + QString::number(peerToChannel(toId));
|
|
|
|
}
|
|
|
|
Ui::showPeerHistory(history, ShowAtUnreadMsgId);
|
2019-08-30 10:17:18 +00:00
|
|
|
history->session().api().sendFiles(
|
2018-12-03 11:20:31 +00:00
|
|
|
Storage::PrepareMediaList(QStringList(filePath), st::sendMediaPreviewSize),
|
|
|
|
SendMediaType::File,
|
|
|
|
{ caption },
|
|
|
|
nullptr,
|
2019-08-12 12:11:34 +00:00
|
|
|
Api::SendAction(history));
|
2018-12-03 11:20:31 +00:00
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:36:04 +00:00
|
|
|
} // namespace Support
|