Unified checking for editable message.

This commit is contained in:
23rd 2021-01-11 00:10:29 +03:00
parent 1607752cf9
commit 15254599e2
9 changed files with 19 additions and 36 deletions

View file

@ -543,7 +543,8 @@ int32 ScheduledMessages::countListHash(const List &list) const {
return HashFinalize(hash);
}
HistoryItem *ScheduledMessages::lastSentMessage(not_null<History*> history) {
HistoryItem *ScheduledMessages::lastEditableMessage(
not_null<History*> history) {
const auto i = _data.find(history);
if (i == end(_data)) {
return nullptr;
@ -552,9 +553,12 @@ HistoryItem *ScheduledMessages::lastSentMessage(not_null<History*> history) {
sort(list);
const auto items = ranges::view::reverse(list.items);
const auto it = ranges::find_if(
items,
&HistoryItem::canBeEditedFromHistory);
const auto now = base::unixtime::now();
auto proj = [&](const OwnedItem &item) {
return item->allowsEdit(now);
};
const auto it = ranges::find_if(items, std::move(proj));
return (it == end(items)) ? nullptr : (*it).get();
}

View file

@ -32,7 +32,8 @@ public:
[[nodiscard]] HistoryItem *lookupItem(PeerId peer, MsgId msg) const;
[[nodiscard]] HistoryItem *lookupItem(FullMsgId itemId) const;
[[nodiscard]] int count(not_null<History*> history) const;
[[nodiscard]] HistoryItem *lastSentMessage(not_null<History*> history);
[[nodiscard]] HistoryItem *lastEditableMessage(
not_null<History*> history);
void checkEntitiesAndUpdate(const MTPDmessage &data);
void apply(const MTPDupdateNewScheduledMessage &update);

View file

@ -2704,14 +2704,15 @@ MsgId History::msgIdForRead() const {
: result;
}
HistoryItem *History::lastSentMessage() const {
HistoryItem *History::lastEditableMessage() const {
if (!loadedAtBottom()) {
return nullptr;
}
const auto now = base::unixtime::now();
for (const auto &block : ranges::view::reverse(blocks)) {
for (const auto &message : ranges::view::reverse(block->messages)) {
const auto item = message->data();
if (item->canBeEditedFromHistory()) {
if (item->allowsEdit(now)) {
return item;
}
}

View file

@ -259,7 +259,7 @@ public:
MsgId minMsgId() const;
MsgId maxMsgId() const;
MsgId msgIdForRead() const;
HistoryItem *lastSentMessage() const;
HistoryItem *lastEditableMessage() const;
void resizeToWidth(int newWidth);
void forceFullResize();

View file

@ -762,27 +762,6 @@ bool HistoryItem::canUpdateDate() const {
return isScheduled();
}
bool HistoryItem::canBeEditedFromHistory() const {
// Skip if message is editing media.
if (isEditingMedia()) {
return false;
}
// Skip if message is video message or sticker.
if (const auto m = media()) {
// Skip only if media is not webpage.
if (!m->webpage() && !m->allowsEditCaption()) {
return false;
}
}
if ((IsServerMsgId(id) || isScheduled())
&& !serviceMsg()
&& (out() || history()->peer->isSelf())
&& !Has<HistoryMessageForwarded>()) {
return true;
}
return false;
}
void HistoryItem::sendFailed() {
Expects(_clientFlags & MTPDmessage_ClientFlag::f_sending);
Expects(!(_clientFlags & MTPDmessage_ClientFlag::f_failed));

View file

@ -390,8 +390,6 @@ public:
void updateDate(TimeId newDate);
[[nodiscard]] bool canUpdateDate() const;
[[nodiscard]] bool canBeEditedFromHistory() const;
virtual ~HistoryItem();
MsgId id;

View file

@ -5022,10 +5022,9 @@ void HistoryWidget::keyPressEvent(QKeyEvent *e) {
_scroll->keyPressEvent(e);
} else if (e->key() == Qt::Key_Up && !commonModifiers) {
const auto item = _history
? _history->lastSentMessage()
? _history->lastEditableMessage()
: nullptr;
if (item
&& item->allowsEdit(base::unixtime::now())
&& _field->empty()
&& !_editMsgId
&& !_replyToId) {

View file

@ -494,7 +494,7 @@ void RepliesWidget::setupComposeControls() {
if (!_composeControls->isEditingMessage()) {
// #TODO replies edit last sent message
//auto &messages = session().data().scheduledMessages();
//if (const auto item = messages.lastSentMessage(_history)) {
//if (const auto item = messages.lastEditableMessage(_history)) {
// _inner->editMessageRequestNotify(item->fullId());
//} else {
_scroll->keyPressEvent(e);

View file

@ -235,8 +235,9 @@ void ScheduledWidget::setupComposeControls() {
) | rpl::start_with_next([=](not_null<QKeyEvent*> e) {
if (e->key() == Qt::Key_Up) {
if (!_composeControls->isEditingMessage()) {
auto &messages = session().data().scheduledMessages();
if (const auto item = messages.lastSentMessage(_history)) {
const auto item = session().data().scheduledMessages()
.lastEditableMessage(_history);
if (item) {
_inner->editMessageRequestNotify(item->fullId());
} else {
_scroll->keyPressEvent(e);