2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-08-23 02:37:11 +00:00
tdesktop/Telegram/SourceFiles/data/data_drafts.cpp

189 lines
5.3 KiB
C++
Raw Normal View History

2016-05-31 12:46:31 +03:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
2016-05-31 12:46:31 +03:00
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
2016-05-31 12:46:31 +03:00
*/
#include "data/data_drafts.h"
2016-05-31 12:46:31 +03:00
2019-09-16 14:14:06 +03:00
#include "api/api_text_entities.h"
#include "ui/widgets/fields/input_field.h"
#include "chat_helpers/message_field.h"
#include "history/history.h"
2017-06-29 13:27:09 +03:00
#include "history/history_widget.h"
2023-10-10 10:49:22 +04:00
#include "history/history_item_components.h"
#include "main/main_session.h"
2024-03-26 19:13:26 +04:00
#include "data/data_changes.h"
2019-01-18 16:27:37 +04:00
#include "data/data_session.h"
#include "data/data_web_page.h"
#include "mainwidget.h"
2017-03-04 13:23:56 +03:00
#include "storage/localstorage.h"
2016-05-31 12:46:31 +03:00
namespace Data {
2025-06-13 14:39:30 +04:00
namespace {
constexpr auto kMaxSuggestStars = 1'000'000'000;
} // namespace
2016-05-31 12:46:31 +03:00
WebPageDraft WebPageDraft::FromItem(not_null<HistoryItem*> item) {
const auto previewMedia = item->media();
const auto previewPage = previewMedia
? previewMedia->webpage()
: nullptr;
using PageFlag = MediaWebPageFlag;
const auto previewFlags = previewMedia
? previewMedia->webpageFlags()
: PageFlag();
return {
.id = previewPage ? previewPage->id : 0,
.url = previewPage ? previewPage->url : QString(),
.forceLargeMedia = !!(previewFlags & PageFlag::ForceLargeMedia),
.forceSmallMedia = !!(previewFlags & PageFlag::ForceSmallMedia),
.invert = item->invertMedia(),
.manual = !!(previewFlags & PageFlag::Manual),
2023-10-25 21:23:54 +04:00
.removed = !previewPage,
};
}
Draft::Draft(
const TextWithTags &textWithTags,
2023-10-10 10:49:22 +04:00
FullReplyTo reply,
2025-06-13 14:39:30 +04:00
SuggestPostOptions suggest,
const MessageCursor &cursor,
WebPageDraft webpage,
mtpRequestId saveRequestId)
: textWithTags(textWithTags)
2023-10-10 10:49:22 +04:00
, reply(std::move(reply))
, cursor(cursor)
, webpage(webpage)
, saveRequestId(saveRequestId) {
}
Draft::Draft(
2018-05-22 00:31:46 +03:00
not_null<const Ui::InputField*> field,
2023-10-10 10:49:22 +04:00
FullReplyTo reply,
2025-06-13 14:39:30 +04:00
SuggestPostOptions suggest,
WebPageDraft webpage,
mtpRequestId saveRequestId)
: textWithTags(field->getTextWithTags())
2023-10-10 10:49:22 +04:00
, reply(std::move(reply))
, cursor(field)
, webpage(webpage) {
}
2020-06-08 12:03:45 +04:00
void ApplyPeerCloudDraft(
not_null<Main::Session*> session,
PeerId peerId,
MsgId topicRootId,
2025-05-20 20:32:24 +04:00
PeerId monoforumPeerId,
2020-06-08 12:03:45 +04:00
const MTPDdraftMessage &draft) {
const auto history = session->data().history(peerId);
const auto date = draft.vdate().v;
2025-05-20 20:32:24 +04:00
if (history->skipCloudDraftUpdate(topicRootId, monoforumPeerId, date)) {
return;
}
const auto textWithTags = TextWithTags{
2019-07-05 15:38:38 +02:00
qs(draft.vmessage()),
2019-09-16 14:14:06 +03:00
TextUtilities::ConvertEntitiesToTextTags(
2020-06-08 12:03:45 +04:00
Api::EntitiesFromMTP(
session,
draft.ventities().value_or_empty()))
};
2023-10-25 11:00:22 +04:00
auto replyTo = draft.vreply_to()
? ReplyToFromMTP(history, *draft.vreply_to())
: FullReplyTo();
replyTo.topicRootId = topicRootId;
2025-05-20 20:32:24 +04:00
replyTo.monoforumPeerId = monoforumPeerId;
auto webpage = WebPageDraft{
.invert = draft.is_invert_media(),
.removed = draft.is_no_webpage(),
};
if (const auto media = draft.vmedia()) {
media->match([&](const MTPDmessageMediaWebPage &data) {
const auto parsed = session->data().processWebpage(
data.vwebpage());
if (!parsed->failed) {
webpage.forceLargeMedia = data.is_force_large_media();
webpage.forceSmallMedia = data.is_force_small_media();
webpage.manual = data.is_manual();
webpage.url = parsed->url;
webpage.id = parsed->id;
}
}, [](const auto &) {});
}
2025-06-13 14:39:30 +04:00
auto suggest = SuggestPostOptions();
if (!history->suggestDraftAllowed()) {
// Don't apply suggest options in unsupported chats.
} else if (const auto suggested = draft.vsuggested_post()) {
const auto &data = suggested->data();
suggest.exists = 1;
suggest.date = data.vschedule_date().value_or_empty();
suggest.stars = uint32(std::clamp(
data.vstars_amount().v,
uint64(),
uint64(kMaxSuggestStars)));
}
auto cloudDraft = std::make_unique<Draft>(
textWithTags,
2023-10-25 11:00:22 +04:00
replyTo,
2025-06-13 14:39:30 +04:00
suggest,
2023-10-13 10:04:29 +04:00
MessageCursor(Ui::kQFixedMax, Ui::kQFixedMax, Ui::kQFixedMax),
std::move(webpage));
cloudDraft->date = date;
history->setCloudDraft(std::move(cloudDraft));
2025-05-20 20:32:24 +04:00
history->applyCloudDraft(topicRootId, monoforumPeerId);
2016-05-31 12:46:31 +03:00
}
2020-06-08 12:03:45 +04:00
void ClearPeerCloudDraft(
not_null<Main::Session*> session,
PeerId peerId,
MsgId topicRootId,
2025-05-20 20:32:24 +04:00
PeerId monoforumPeerId,
2020-06-08 12:03:45 +04:00
TimeId date) {
const auto history = session->data().history(peerId);
2025-05-20 20:32:24 +04:00
if (history->skipCloudDraftUpdate(topicRootId, monoforumPeerId, date)) {
2018-06-26 14:58:29 +01:00
return;
}
2025-05-20 20:32:24 +04:00
history->clearCloudDraft(topicRootId, monoforumPeerId);
history->applyCloudDraft(topicRootId, monoforumPeerId);
}
2024-04-22 15:06:48 +04:00
void SetChatLinkDraft(not_null<PeerData*> peer, TextWithEntities draft) {
static const auto kInlineStart = QRegularExpression("^@[a-zA-Z0-9_]");
if (kInlineStart.match(draft.text).hasMatch()) {
draft = TextWithEntities().append(' ').append(std::move(draft));
}
2024-03-26 19:13:26 +04:00
const auto textWithTags = TextWithTags{
draft.text,
TextUtilities::ConvertEntitiesToTextTags(draft.entities)
};
const auto cursor = MessageCursor{
int(textWithTags.text.size()),
int(textWithTags.text.size()),
Ui::kQFixedMax
};
const auto history = peer->owner().history(peer->id);
const auto topicRootId = MsgId();
2025-05-20 20:32:24 +04:00
const auto monoforumPeerId = PeerId();
2025-06-13 14:39:30 +04:00
history->setLocalDraft(std::make_unique<Draft>(
2024-03-26 19:13:26 +04:00
textWithTags,
2025-05-20 20:32:24 +04:00
FullReplyTo{
.topicRootId = topicRootId,
.monoforumPeerId = monoforumPeerId,
},
2025-06-13 14:39:30 +04:00
SuggestPostOptions(),
2024-03-26 19:13:26 +04:00
cursor,
2025-06-13 14:39:30 +04:00
WebPageDraft()));
2025-05-20 20:32:24 +04:00
history->clearLocalEditDraft(topicRootId, monoforumPeerId);
2024-03-26 19:13:26 +04:00
history->session().changes().entryUpdated(
history,
2025-06-13 14:39:30 +04:00
EntryUpdate::Flag::LocalDraftSet);
2024-03-26 19:13:26 +04:00
}
2016-05-31 12:46:31 +03:00
} // namespace Data