Skip to content

Commit

Permalink
Skip draft updates while sending with clear_draft.
Browse files Browse the repository at this point in the history
I hope fixes #4845, fixes #4852, fixes #4861.
  • Loading branch information
john-preston committed Jun 27, 2018
1 parent 3309596 commit 372cf27
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
21 changes: 16 additions & 5 deletions Telegram/SourceFiles/apiwrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2064,15 +2064,16 @@ void ApiWrap::saveDraftsToCloud() {
ConvertTextTagsToEntities(textWithTags.tags),
TextUtilities::ConvertOption::SkipLocal);

history->setSentDraftText(textWithTags.text);
const auto draftText = textWithTags.text;
history->setSentDraftText(draftText);
cloudDraft->saveRequestId = request(MTPmessages_SaveDraft(
MTP_flags(flags),
MTP_int(cloudDraft->msgId),
history->peer->input,
MTP_string(textWithTags.text),
entities
)).done([=](const MTPBool &result, mtpRequestId requestId) {
history->clearSentDraftText();
history->clearSentDraftText(draftText);

if (const auto cloudDraft = history->cloudDraft()) {
if (cloudDraft->saveRequestId == requestId) {
Expand All @@ -2086,7 +2087,7 @@ void ApiWrap::saveDraftsToCloud() {
checkQuitPreventFinished();
}
}).fail([=](const RPCError &error, mtpRequestId requestId) {
history->clearSentDraftText();
history->clearSentDraftText(draftText);

if (const auto cloudDraft = history->cloudDraft()) {
if (cloudDraft->saveRequestId == requestId) {
Expand Down Expand Up @@ -4042,6 +4043,7 @@ void ApiWrap::sendMessage(MessageToSend &&message) {
if (message.clearDraft) {
sendFlags |= MTPmessages_SendMessage::Flag::f_clear_draft;
history->clearCloudDraft();
history->setSentDraftText(QString());
}
auto messageFromId = channelPost ? 0 : _session->userId();
auto messagePostAuthor = channelPost
Expand Down Expand Up @@ -4076,7 +4078,10 @@ void ApiWrap::sendMessage(MessageToSend &&message) {
sentEntities
)).done([=](const MTPUpdates &result) {
applyUpdates(result, randomId);
}).fail([=](const RPCError &error) { sendMessageFail(error);
history->clearSentDraftText(QString());
}).fail([=](const RPCError &error) {
sendMessageFail(error);
history->clearSentDraftText(QString());
}).afterRequest(history->sendRequestId
).send();
}
Expand Down Expand Up @@ -4141,6 +4146,9 @@ void ApiWrap::sendInlineResult(
options.replyTo,
messagePostAuthor);

history->clearCloudDraft();
history->setSentDraftText(QString());

history->sendRequestId = request(MTPmessages_SendInlineBotResult(
MTP_flags(sendFlags),
peer->input,
Expand All @@ -4150,7 +4158,10 @@ void ApiWrap::sendInlineResult(
MTP_string(data->getId())
)).done([=](const MTPUpdates &result) {
applyUpdates(result, randomId);
}).fail([=](const RPCError &error) { sendMessageFail(error);
history->clearSentDraftText(QString());
}).fail([=](const RPCError &error) {
sendMessageFail(error);
history->clearSentDraftText(QString());
}).afterRequest(history->sendRequestId
).send();

Expand Down
10 changes: 6 additions & 4 deletions Telegram/SourceFiles/history/history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,9 @@ Data::Draft *History::createCloudDraft(Data::Draft *fromDraft) {
}

bool History::skipCloudDraft(const QString &text, TimeId date) const {
if (_lastSentDraftText && *_lastSentDraftText == text) {
if (date > 0 && date <= _lastSentDraftTime + kSkipCloudDraftsFor) {
return true;
} else if (date <= _lastSentDraftTime + kSkipCloudDraftsFor) {
} else if (_lastSentDraftText && *_lastSentDraftText == text) {
return true;
}
return false;
Expand All @@ -421,8 +421,10 @@ void History::setSentDraftText(const QString &text) {
_lastSentDraftText = text;
}

void History::clearSentDraftText() {
_lastSentDraftText = base::none;
void History::clearSentDraftText(const QString &text) {
if (_lastSentDraftText && *_lastSentDraftText == text) {
_lastSentDraftText = base::none;
}
accumulate_max(_lastSentDraftTime, unixtime());
}

Expand Down
2 changes: 1 addition & 1 deletion Telegram/SourceFiles/history/history.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ class History : public Dialogs::Entry {
Data::Draft *createCloudDraft(Data::Draft *fromDraft);
bool skipCloudDraft(const QString &text, TimeId date) const;
void setSentDraftText(const QString &text);
void clearSentDraftText();
void clearSentDraftText(const QString &text);
void setEditDraft(std::unique_ptr<Data::Draft> &&draft);
void clearLocalDraft();
void clearCloudDraft();
Expand Down

1 comment on commit 372cf27

@Lonami
Copy link
Contributor

@Lonami Lonami commented on 372cf27 Jun 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thanks :)

Please sign in to comment.