2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
2017-06-29 10:27:09 +00:00
|
|
|
#include "history/history.h"
|
2016-04-09 18:45:55 +00:00
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
#include "history/view/history_view_element.h"
|
2018-01-22 16:42:25 +00:00
|
|
|
#include "history/admin_log/history_admin_log_section.h"
|
2017-06-22 15:11:41 +00:00
|
|
|
#include "history/history_message.h"
|
2016-09-28 10:15:03 +00:00
|
|
|
#include "history/history_media_types.h"
|
2017-06-22 15:11:41 +00:00
|
|
|
#include "history/history_service.h"
|
2017-12-18 15:44:50 +00:00
|
|
|
#include "history/history_item_components.h"
|
2018-01-17 16:21:01 +00:00
|
|
|
#include "history/history_inner_widget.h"
|
2016-04-09 18:45:55 +00:00
|
|
|
#include "dialogs/dialogs_indexed_list.h"
|
2016-06-07 19:59:39 +00:00
|
|
|
#include "styles/style_dialogs.h"
|
2016-06-09 14:31:10 +00:00
|
|
|
#include "data/data_drafts.h"
|
2018-01-04 10:22:53 +00:00
|
|
|
#include "data/data_session.h"
|
2018-11-10 23:35:18 +00:00
|
|
|
#include "data/data_media_types.h"
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2017-01-18 10:26:33 +00:00
|
|
|
#include "apiwrap.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
#include "mainwidget.h"
|
2016-04-12 21:31:28 +00:00
|
|
|
#include "mainwindow.h"
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "storage/localstorage.h"
|
2016-06-03 07:20:24 +00:00
|
|
|
#include "observer_peer.h"
|
2017-02-23 09:32:28 +00:00
|
|
|
#include "auth_session.h"
|
2017-03-04 19:36:59 +00:00
|
|
|
#include "window/notifications_manager.h"
|
2017-07-15 12:24:32 +00:00
|
|
|
#include "calls/calls_instance.h"
|
2017-09-04 11:40:02 +00:00
|
|
|
#include "storage/storage_facade.h"
|
|
|
|
#include "storage/storage_shared_media.h"
|
2018-01-23 16:51:12 +00:00
|
|
|
#include "storage/storage_feed_messages.h"
|
2017-12-02 11:07:27 +00:00
|
|
|
#include "data/data_channel_admins.h"
|
2018-01-05 15:57:18 +00:00
|
|
|
#include "data/data_feed.h"
|
2018-10-23 09:44:42 +00:00
|
|
|
#include "ui/image/image.h"
|
2017-12-28 13:06:06 +00:00
|
|
|
#include "ui/text_options.h"
|
2017-12-11 14:45:29 +00:00
|
|
|
#include "core/crash_reports.h"
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2016-09-30 16:40:00 +00:00
|
|
|
namespace {
|
|
|
|
|
2017-05-12 13:53:08 +00:00
|
|
|
constexpr auto kStatusShowClientsideTyping = 6000;
|
|
|
|
constexpr auto kStatusShowClientsideRecordVideo = 6000;
|
|
|
|
constexpr auto kStatusShowClientsideUploadVideo = 6000;
|
|
|
|
constexpr auto kStatusShowClientsideRecordVoice = 6000;
|
|
|
|
constexpr auto kStatusShowClientsideUploadVoice = 6000;
|
|
|
|
constexpr auto kStatusShowClientsideRecordRound = 6000;
|
|
|
|
constexpr auto kStatusShowClientsideUploadRound = 6000;
|
|
|
|
constexpr auto kStatusShowClientsideUploadPhoto = 6000;
|
|
|
|
constexpr auto kStatusShowClientsideUploadFile = 6000;
|
|
|
|
constexpr auto kStatusShowClientsideChooseLocation = 6000;
|
|
|
|
constexpr auto kStatusShowClientsideChooseContact = 6000;
|
|
|
|
constexpr auto kStatusShowClientsidePlayGame = 10000;
|
|
|
|
constexpr auto kSetMyActionForMs = 10000;
|
|
|
|
constexpr auto kNewBlockEachMessage = 50;
|
2018-06-08 20:06:26 +00:00
|
|
|
constexpr auto kSkipCloudDraftsFor = TimeId(3);
|
2016-09-30 16:40:00 +00:00
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
void checkForSwitchInlineButton(HistoryItem *item) {
|
|
|
|
if (item->out() || !item->hasSwitchInlineButton()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (const auto user = item->history()->peer->asUser()) {
|
|
|
|
if (!user->botInfo || !user->botInfo->inlineReturnPeerId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (const auto markup = item->Get<HistoryMessageReplyMarkup>()) {
|
|
|
|
for_const (auto &row, markup->rows) {
|
|
|
|
for_const (auto &button, row) {
|
|
|
|
if (button.type == HistoryMessageMarkupButton::Type::SwitchInline) {
|
|
|
|
Notify::switchInlineBotButtonReceived(QString::fromUtf8(button.data));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-30 16:40:00 +00:00
|
|
|
} // namespace
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
Histories::Histories()
|
|
|
|
: _a_typings(animation(this, &Histories::step_typings))
|
|
|
|
, _selfDestructTimer([this] { checkSelfDestructItems(); }) {
|
|
|
|
}
|
|
|
|
|
|
|
|
History *Histories::find(PeerId peerId) const {
|
|
|
|
if (const auto i = _map.find(peerId); i != _map.end()) {
|
|
|
|
return i->second.get();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
not_null<History*> Histories::findOrInsert(PeerId peerId) {
|
|
|
|
if (const auto result = find(peerId)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
const auto [i, ok] = _map.emplace(
|
|
|
|
peerId,
|
|
|
|
std::make_unique<History>(peerId));
|
|
|
|
return i->second.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::clear() {
|
2018-06-09 08:52:37 +00:00
|
|
|
for (const auto &[peerId, history] : _map) {
|
|
|
|
history->unloadBlocks();
|
|
|
|
}
|
2018-01-31 17:10:29 +00:00
|
|
|
App::historyClearMsgs();
|
|
|
|
_map.clear();
|
|
|
|
|
|
|
|
_unreadFull = _unreadMuted = 0;
|
2018-12-04 10:32:06 +00:00
|
|
|
_unreadEntriesFull = _unreadEntriesMuted = 0;
|
2018-01-31 17:10:29 +00:00
|
|
|
Notify::unreadCounterUpdated();
|
|
|
|
App::historyClearItems();
|
|
|
|
typing.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::registerSendAction(
|
|
|
|
not_null<History*> history,
|
|
|
|
not_null<UserData*> user,
|
|
|
|
const MTPSendMessageAction &action,
|
|
|
|
TimeId when) {
|
|
|
|
if (history->updateSendActionNeedsAnimating(user, action)) {
|
|
|
|
user->madeAction(when);
|
|
|
|
|
|
|
|
auto i = typing.find(history);
|
|
|
|
if (i == typing.cend()) {
|
|
|
|
typing.insert(history, getms());
|
|
|
|
_a_typings.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::step_typings(TimeMs ms, bool timer) {
|
|
|
|
for (auto i = typing.begin(), e = typing.end(); i != e;) {
|
|
|
|
if (i.key()->updateSendActionNeedsAnimating(ms)) {
|
|
|
|
++i;
|
|
|
|
} else {
|
|
|
|
i = typing.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (typing.isEmpty()) {
|
|
|
|
_a_typings.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::remove(const PeerId &peer) {
|
|
|
|
const auto i = _map.find(peer);
|
|
|
|
if (i != _map.cend()) {
|
|
|
|
typing.remove(i->second.get());
|
|
|
|
_map.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HistoryItem *Histories::addNewMessage(
|
|
|
|
const MTPMessage &msg,
|
|
|
|
NewMessageType type) {
|
|
|
|
auto peer = peerFromMessage(msg);
|
|
|
|
if (!peer) return nullptr;
|
|
|
|
|
|
|
|
auto result = App::history(peer)->addNewMessage(msg, type);
|
|
|
|
if (result && type == NewMessageUnread) {
|
|
|
|
checkForSwitchInlineButton(result);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Histories::unreadBadge() const {
|
2018-12-04 10:32:06 +00:00
|
|
|
return computeUnreadBadge(
|
|
|
|
_unreadFull,
|
|
|
|
_unreadMuted,
|
|
|
|
_unreadEntriesFull,
|
|
|
|
_unreadEntriesMuted);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Histories::unreadBadgeMuted() const {
|
|
|
|
return computeUnreadBadgeMuted(
|
|
|
|
_unreadFull,
|
|
|
|
_unreadMuted,
|
|
|
|
_unreadEntriesFull,
|
|
|
|
_unreadEntriesMuted);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Histories::unreadBadgeIgnoreOne(History *history) const {
|
|
|
|
const auto removeCount = (history
|
|
|
|
&& history->inChatList(Dialogs::Mode::All))
|
|
|
|
? history->unreadCount()
|
|
|
|
: 0;
|
|
|
|
if (!removeCount) {
|
|
|
|
return unreadBadge();
|
|
|
|
}
|
|
|
|
const auto removeMuted = history->mute();
|
|
|
|
return computeUnreadBadge(
|
|
|
|
_unreadFull - removeCount,
|
|
|
|
_unreadMuted - (removeMuted ? removeCount : 0),
|
|
|
|
_unreadEntriesFull - 1,
|
|
|
|
_unreadEntriesMuted - (removeMuted ? 1 : 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Histories::unreadBadgeMutedIgnoreOne(History *history) const {
|
|
|
|
const auto removeCount = (history
|
|
|
|
&& history->inChatList(Dialogs::Mode::All))
|
|
|
|
? history->unreadCount()
|
|
|
|
: 0;
|
|
|
|
if (!removeCount) {
|
|
|
|
return unreadBadgeMuted();
|
|
|
|
}
|
|
|
|
const auto removeMuted = history->mute();
|
|
|
|
return computeUnreadBadgeMuted(
|
|
|
|
_unreadFull - removeCount,
|
|
|
|
_unreadMuted - (removeMuted ? removeCount : 0),
|
|
|
|
_unreadEntriesFull - 1,
|
|
|
|
_unreadEntriesMuted - (removeMuted ? 1 : 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
int Histories::unreadOnlyMutedBadge() const {
|
|
|
|
return Auth().settings().countUnreadMessages()
|
|
|
|
? _unreadMuted
|
|
|
|
: _unreadEntriesMuted;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Histories::computeUnreadBadge(
|
|
|
|
int full,
|
|
|
|
int muted,
|
|
|
|
int entriesFull,
|
|
|
|
int entriesMuted) const {
|
|
|
|
const auto withMuted = Auth().settings().includeMutedCounter();
|
|
|
|
return Auth().settings().countUnreadMessages()
|
|
|
|
? (full - (withMuted ? 0 : muted))
|
|
|
|
: (entriesFull - (withMuted ? 0 : entriesMuted));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Histories::computeUnreadBadgeMuted(
|
|
|
|
int full,
|
|
|
|
int muted,
|
|
|
|
int entriesFull,
|
|
|
|
int entriesMuted) const {
|
|
|
|
if (!Auth().settings().includeMutedCounter()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return Auth().settings().countUnreadMessages()
|
|
|
|
? (muted >= full)
|
|
|
|
: (entriesMuted >= entriesFull);
|
2018-02-20 17:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::unreadIncrement(int count, bool muted) {
|
2018-12-04 10:32:06 +00:00
|
|
|
if (!count) {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-20 17:53:55 +00:00
|
|
|
_unreadFull += count;
|
|
|
|
if (muted) {
|
|
|
|
_unreadMuted += count;
|
|
|
|
}
|
2018-12-04 10:32:06 +00:00
|
|
|
if (Auth().settings().countUnreadMessages()) {
|
|
|
|
if (!muted || Auth().settings().includeMutedCounter()) {
|
|
|
|
Notify::unreadCounterUpdated();
|
|
|
|
}
|
2018-02-20 17:53:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::unreadMuteChanged(int count, bool muted) {
|
2018-12-04 10:32:06 +00:00
|
|
|
const auto wasAll = (_unreadMuted == _unreadFull);
|
2018-02-20 17:53:55 +00:00
|
|
|
if (muted) {
|
|
|
|
_unreadMuted += count;
|
|
|
|
} else {
|
|
|
|
_unreadMuted -= count;
|
|
|
|
}
|
2018-12-04 10:32:06 +00:00
|
|
|
if (Auth().settings().countUnreadMessages()) {
|
|
|
|
const auto nowAll = (_unreadMuted == _unreadFull);
|
|
|
|
const auto changed = !Auth().settings().includeMutedCounter()
|
|
|
|
|| (wasAll != nowAll);
|
|
|
|
if (changed) {
|
|
|
|
Notify::unreadCounterUpdated();
|
|
|
|
}
|
|
|
|
}
|
2018-02-20 17:53:55 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 10:32:06 +00:00
|
|
|
void Histories::unreadEntriesChanged(
|
|
|
|
int withUnreadDelta,
|
|
|
|
int mutedWithUnreadDelta) {
|
|
|
|
if (!withUnreadDelta && !mutedWithUnreadDelta) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto wasAll = (_unreadEntriesMuted == _unreadEntriesFull);
|
|
|
|
_unreadEntriesFull += withUnreadDelta;
|
|
|
|
_unreadEntriesMuted += mutedWithUnreadDelta;
|
|
|
|
if (!Auth().settings().countUnreadMessages()) {
|
|
|
|
const auto nowAll = (_unreadEntriesMuted == _unreadEntriesFull);
|
|
|
|
const auto withMuted = Auth().settings().includeMutedCounter();
|
|
|
|
const auto withMutedChanged = withMuted
|
|
|
|
&& (withUnreadDelta != 0 || wasAll != nowAll);
|
|
|
|
const auto withoutMutedChanged = !withMuted
|
|
|
|
&& (withUnreadDelta != mutedWithUnreadDelta);
|
|
|
|
if (withMutedChanged || withoutMutedChanged) {
|
|
|
|
Notify::unreadCounterUpdated();
|
|
|
|
}
|
|
|
|
}
|
2018-01-31 17:10:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::selfDestructIn(not_null<HistoryItem*> item, TimeMs delay) {
|
|
|
|
_selfDestructItems.push_back(item->fullId());
|
|
|
|
if (!_selfDestructTimer.isActive() || _selfDestructTimer.remainingTime() > delay) {
|
|
|
|
_selfDestructTimer.callOnce(delay);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::checkSelfDestructItems() {
|
|
|
|
auto now = getms(true);
|
|
|
|
auto nextDestructIn = TimeMs(0);
|
|
|
|
for (auto i = _selfDestructItems.begin(); i != _selfDestructItems.cend();) {
|
|
|
|
if (auto item = App::histItemById(*i)) {
|
|
|
|
if (auto destructIn = item->getSelfDestructIn(now)) {
|
|
|
|
if (nextDestructIn > 0) {
|
|
|
|
accumulate_min(nextDestructIn, destructIn);
|
|
|
|
} else {
|
|
|
|
nextDestructIn = destructIn;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
} else {
|
|
|
|
i = _selfDestructItems.erase(i);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i = _selfDestructItems.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nextDestructIn > 0) {
|
|
|
|
_selfDestructTimer.callOnce(nextDestructIn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-08 14:16:52 +00:00
|
|
|
History::History(const PeerId &peerId)
|
2018-01-05 15:57:18 +00:00
|
|
|
: Entry(this)
|
|
|
|
, peer(App::peer(peerId))
|
2016-06-07 19:59:39 +00:00
|
|
|
, cloudDraftTextCache(st::dialogsTextWidthMin)
|
2018-04-09 17:48:29 +00:00
|
|
|
, _mute(Auth().data().notifyIsMuted(peer))
|
2016-12-04 19:40:41 +00:00
|
|
|
, _sendActionText(st::dialogsTextWidthMin) {
|
2018-01-13 12:45:11 +00:00
|
|
|
if (const auto user = peer->asUser()) {
|
|
|
|
if (user->botInfo) {
|
2018-01-31 17:10:29 +00:00
|
|
|
_outboxReadBefore = std::numeric_limits<MsgId>::max();
|
2018-01-13 12:45:11 +00:00
|
|
|
}
|
2015-09-08 17:59:36 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 17:24:48 +00:00
|
|
|
void History::clearLastKeyboard() {
|
2015-11-24 16:19:18 +00:00
|
|
|
if (lastKeyboardId) {
|
|
|
|
if (lastKeyboardId == lastKeyboardHiddenId) {
|
|
|
|
lastKeyboardHiddenId = 0;
|
|
|
|
}
|
|
|
|
lastKeyboardId = 0;
|
2016-07-19 11:37:01 +00:00
|
|
|
if (auto main = App::main()) {
|
|
|
|
main->updateBotKeyboard(this);
|
|
|
|
}
|
2015-11-24 16:19:18 +00:00
|
|
|
}
|
2015-06-24 17:24:48 +00:00
|
|
|
lastKeyboardInited = true;
|
|
|
|
lastKeyboardFrom = 0;
|
|
|
|
}
|
|
|
|
|
2018-01-21 19:21:08 +00:00
|
|
|
int History::height() const {
|
|
|
|
return _height;
|
2016-03-23 11:47:32 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
void History::removeNotification(not_null<HistoryItem*> item) {
|
|
|
|
if (!notifies.isEmpty()) {
|
|
|
|
for (auto i = notifies.begin(), e = notifies.end(); i != e; ++i) {
|
|
|
|
if ((*i) == item) {
|
|
|
|
notifies.erase(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HistoryItem *History::currentNotification() {
|
|
|
|
return notifies.isEmpty() ? 0 : notifies.front();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool History::hasNotification() const {
|
|
|
|
return !notifies.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void History::skipNotification() {
|
|
|
|
if (!notifies.isEmpty()) {
|
|
|
|
notifies.pop_front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void History::popNotification(HistoryItem *item) {
|
|
|
|
if (!notifies.isEmpty() && notifies.back() == item) notifies.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool History::hasPendingResizedItems() const {
|
|
|
|
return _flags & Flag::f_has_pending_resized_items;
|
|
|
|
}
|
|
|
|
|
2016-03-19 16:55:15 +00:00
|
|
|
void History::setHasPendingResizedItems() {
|
|
|
|
_flags |= Flag::f_has_pending_resized_items;
|
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
void History::itemRemoved(not_null<HistoryItem*> item) {
|
|
|
|
item->removeMainView();
|
|
|
|
if (lastMessage() == item) {
|
2018-09-21 16:28:46 +00:00
|
|
|
_lastMessage = std::nullopt;
|
2018-01-31 17:10:29 +00:00
|
|
|
if (loadedAtBottom()) {
|
|
|
|
if (const auto last = lastAvailableMessage()) {
|
|
|
|
setLastMessage(last);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (const auto channel = peer->asChannel()) {
|
|
|
|
if (const auto feed = channel->feed()) {
|
|
|
|
// Must be after history->lastMessage() is updated.
|
|
|
|
// Otherwise feed last message will be this value again.
|
|
|
|
feed->messageRemoved(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
itemVanished(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
void History::itemVanished(not_null<HistoryItem*> item) {
|
|
|
|
removeNotification(item);
|
|
|
|
if (lastKeyboardId == item->id) {
|
|
|
|
clearLastKeyboard();
|
|
|
|
}
|
|
|
|
if ((!item->out() || item->isPost())
|
|
|
|
&& item->unread()
|
|
|
|
&& unreadCount() > 0) {
|
2018-02-12 11:10:40 +00:00
|
|
|
changeUnreadCount(-1);
|
2018-01-31 17:10:29 +00:00
|
|
|
}
|
2018-10-31 11:29:14 +00:00
|
|
|
if (peer->pinnedMessageId() == item->id) {
|
|
|
|
peer->clearPinnedMessage();
|
2018-01-31 17:10:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-21 13:45:56 +00:00
|
|
|
void History::setLocalDraft(std::unique_ptr<Data::Draft> &&draft) {
|
|
|
|
_localDraft = std::move(draft);
|
2016-06-09 14:31:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void History::takeLocalDraft(History *from) {
|
|
|
|
if (auto &draft = from->_localDraft) {
|
|
|
|
if (!draft->textWithTags.text.isEmpty() && !_localDraft) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_localDraft = std::move(draft);
|
2016-06-09 14:31:10 +00:00
|
|
|
|
|
|
|
// Edit and reply to drafts can't migrate.
|
|
|
|
// Cloud drafts do not migrate automatically.
|
|
|
|
_localDraft->msgId = 0;
|
|
|
|
}
|
|
|
|
from->clearLocalDraft();
|
2017-08-04 14:54:32 +00:00
|
|
|
Auth().api().saveDraftToCloudDelayed(from);
|
2016-06-09 14:31:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 18:24:27 +00:00
|
|
|
void History::createLocalDraftFromCloud() {
|
|
|
|
auto draft = cloudDraft();
|
2018-11-15 15:36:04 +00:00
|
|
|
if (Data::draftIsNull(draft) || !draft->date || Auth().supportMode()) {
|
2018-02-03 19:52:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-06-03 18:24:27 +00:00
|
|
|
|
|
|
|
auto existing = localDraft();
|
2018-11-15 15:36:04 +00:00
|
|
|
if (Data::draftIsNull(existing)
|
|
|
|
|| !existing->date
|
|
|
|
|| draft->date >= existing->date) {
|
2016-06-03 18:24:27 +00:00
|
|
|
if (!existing) {
|
2018-02-03 19:52:35 +00:00
|
|
|
setLocalDraft(std::make_unique<Data::Draft>(
|
|
|
|
draft->textWithTags,
|
|
|
|
draft->msgId,
|
|
|
|
draft->cursor,
|
|
|
|
draft->previewCancelled));
|
2016-06-03 18:24:27 +00:00
|
|
|
existing = localDraft();
|
|
|
|
} else if (existing != draft) {
|
|
|
|
existing->textWithTags = draft->textWithTags;
|
|
|
|
existing->msgId = draft->msgId;
|
|
|
|
existing->cursor = draft->cursor;
|
|
|
|
existing->previewCancelled = draft->previewCancelled;
|
|
|
|
}
|
|
|
|
existing->date = draft->date;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-21 13:45:56 +00:00
|
|
|
void History::setCloudDraft(std::unique_ptr<Data::Draft> &&draft) {
|
|
|
|
_cloudDraft = std::move(draft);
|
2016-06-09 14:31:10 +00:00
|
|
|
cloudDraftTextCache.clear();
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:36:04 +00:00
|
|
|
Data::Draft *History::createCloudDraft(const Data::Draft *fromDraft) {
|
2016-06-09 14:31:10 +00:00
|
|
|
if (Data::draftIsNull(fromDraft)) {
|
2018-01-10 13:13:33 +00:00
|
|
|
setCloudDraft(std::make_unique<Data::Draft>(
|
|
|
|
TextWithTags(),
|
|
|
|
0,
|
|
|
|
MessageCursor(),
|
|
|
|
false));
|
2018-02-03 19:52:35 +00:00
|
|
|
cloudDraft()->date = TimeId(0);
|
2016-06-03 18:24:27 +00:00
|
|
|
} else {
|
|
|
|
auto existing = cloudDraft();
|
|
|
|
if (!existing) {
|
2018-02-03 19:52:35 +00:00
|
|
|
setCloudDraft(std::make_unique<Data::Draft>(
|
|
|
|
fromDraft->textWithTags,
|
|
|
|
fromDraft->msgId,
|
|
|
|
fromDraft->cursor,
|
|
|
|
fromDraft->previewCancelled));
|
2016-06-03 18:24:27 +00:00
|
|
|
existing = cloudDraft();
|
|
|
|
} else if (existing != fromDraft) {
|
|
|
|
existing->textWithTags = fromDraft->textWithTags;
|
|
|
|
existing->msgId = fromDraft->msgId;
|
|
|
|
existing->cursor = fromDraft->cursor;
|
|
|
|
existing->previewCancelled = fromDraft->previewCancelled;
|
|
|
|
}
|
2018-02-03 19:52:35 +00:00
|
|
|
existing->date = unixtime();
|
2016-06-03 18:24:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cloudDraftTextCache.clear();
|
|
|
|
updateChatListSortPosition();
|
|
|
|
|
|
|
|
return cloudDraft();
|
|
|
|
}
|
|
|
|
|
2018-06-08 20:06:26 +00:00
|
|
|
bool History::skipCloudDraft(const QString &text, TimeId date) const {
|
2018-06-26 15:59:05 +00:00
|
|
|
if (date > 0 && date <= _lastSentDraftTime + kSkipCloudDraftsFor) {
|
2018-06-08 20:06:26 +00:00
|
|
|
return true;
|
2018-06-26 15:59:05 +00:00
|
|
|
} else if (_lastSentDraftText && *_lastSentDraftText == text) {
|
2018-06-08 20:06:26 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void History::setSentDraftText(const QString &text) {
|
|
|
|
_lastSentDraftText = text;
|
|
|
|
}
|
|
|
|
|
2018-06-26 15:59:05 +00:00
|
|
|
void History::clearSentDraftText(const QString &text) {
|
|
|
|
if (_lastSentDraftText && *_lastSentDraftText == text) {
|
2018-09-21 16:28:46 +00:00
|
|
|
_lastSentDraftText = std::nullopt;
|
2018-06-26 15:59:05 +00:00
|
|
|
}
|
2018-06-08 20:06:26 +00:00
|
|
|
accumulate_max(_lastSentDraftTime, unixtime());
|
|
|
|
}
|
|
|
|
|
2017-02-21 13:45:56 +00:00
|
|
|
void History::setEditDraft(std::unique_ptr<Data::Draft> &&draft) {
|
|
|
|
_editDraft = std::move(draft);
|
2016-06-09 14:31:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void History::clearLocalDraft() {
|
|
|
|
_localDraft = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-06-03 18:24:27 +00:00
|
|
|
void History::clearCloudDraft() {
|
|
|
|
if (_cloudDraft) {
|
|
|
|
_cloudDraft = nullptr;
|
|
|
|
cloudDraftTextCache.clear();
|
|
|
|
updateChatListSortPosition();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-09 14:31:10 +00:00
|
|
|
void History::clearEditDraft() {
|
|
|
|
_editDraft = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void History::draftSavedToCloud() {
|
|
|
|
updateChatListEntry();
|
|
|
|
if (App::main()) App::main()->writeDrafts(this);
|
|
|
|
}
|
|
|
|
|
2017-12-06 10:13:38 +00:00
|
|
|
HistoryItemsList History::validateForwardDraft() {
|
|
|
|
auto result = Auth().data().idsToItems(_forwardDraft);
|
2017-06-16 20:33:35 +00:00
|
|
|
if (result.size() != _forwardDraft.size()) {
|
2017-12-06 10:13:38 +00:00
|
|
|
setForwardDraft(Auth().data().itemsToIds(result));
|
2017-06-16 20:33:35 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-12-06 10:13:38 +00:00
|
|
|
void History::setForwardDraft(MessageIdsList &&items) {
|
|
|
|
_forwardDraft = std::move(items);
|
2017-06-16 20:33:35 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 14:27:59 +00:00
|
|
|
bool History::updateSendActionNeedsAnimating(
|
|
|
|
not_null<UserData*> user,
|
|
|
|
const MTPSendMessageAction &action) {
|
|
|
|
if (peer->isSelf()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-01 19:20:33 +00:00
|
|
|
using Type = SendAction::Type;
|
|
|
|
if (action.type() == mtpc_sendMessageCancelAction) {
|
2017-12-07 14:27:59 +00:00
|
|
|
clearSendAction(user);
|
2016-12-01 19:20:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ms = getms();
|
|
|
|
switch (action.type()) {
|
|
|
|
case mtpc_sendMessageTypingAction: _typing.insert(user, ms + kStatusShowClientsideTyping); break;
|
|
|
|
case mtpc_sendMessageRecordVideoAction: _sendActions.insert(user, { Type::RecordVideo, ms + kStatusShowClientsideRecordVideo }); break;
|
|
|
|
case mtpc_sendMessageUploadVideoAction: _sendActions.insert(user, { Type::UploadVideo, ms + kStatusShowClientsideUploadVideo, action.c_sendMessageUploadVideoAction().vprogress.v }); break;
|
|
|
|
case mtpc_sendMessageRecordAudioAction: _sendActions.insert(user, { Type::RecordVoice, ms + kStatusShowClientsideRecordVoice }); break;
|
|
|
|
case mtpc_sendMessageUploadAudioAction: _sendActions.insert(user, { Type::UploadVoice, ms + kStatusShowClientsideUploadVoice, action.c_sendMessageUploadAudioAction().vprogress.v }); break;
|
2017-04-12 19:37:47 +00:00
|
|
|
case mtpc_sendMessageRecordRoundAction: _sendActions.insert(user, { Type::RecordRound, ms + kStatusShowClientsideRecordRound }); break;
|
|
|
|
case mtpc_sendMessageUploadRoundAction: _sendActions.insert(user, { Type::UploadRound, ms + kStatusShowClientsideUploadRound }); break;
|
2016-12-01 19:20:33 +00:00
|
|
|
case mtpc_sendMessageUploadPhotoAction: _sendActions.insert(user, { Type::UploadPhoto, ms + kStatusShowClientsideUploadPhoto, action.c_sendMessageUploadPhotoAction().vprogress.v }); break;
|
|
|
|
case mtpc_sendMessageUploadDocumentAction: _sendActions.insert(user, { Type::UploadFile, ms + kStatusShowClientsideUploadFile, action.c_sendMessageUploadDocumentAction().vprogress.v }); break;
|
|
|
|
case mtpc_sendMessageGeoLocationAction: _sendActions.insert(user, { Type::ChooseLocation, ms + kStatusShowClientsideChooseLocation }); break;
|
|
|
|
case mtpc_sendMessageChooseContactAction: _sendActions.insert(user, { Type::ChooseContact, ms + kStatusShowClientsideChooseContact }); break;
|
2016-12-13 07:59:57 +00:00
|
|
|
case mtpc_sendMessageGamePlayAction: {
|
|
|
|
auto it = _sendActions.find(user);
|
|
|
|
if (it == _sendActions.end() || it->type == Type::PlayGame || it->until <= ms) {
|
|
|
|
_sendActions.insert(user, { Type::PlayGame, ms + kStatusShowClientsidePlayGame });
|
|
|
|
}
|
|
|
|
} break;
|
2016-12-01 19:20:33 +00:00
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
return updateSendActionNeedsAnimating(ms, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool History::mySendActionUpdated(SendAction::Type type, bool doing) {
|
|
|
|
auto ms = getms(true);
|
|
|
|
auto i = _mySendActions.find(type);
|
|
|
|
if (doing) {
|
|
|
|
if (i == _mySendActions.cend()) {
|
|
|
|
_mySendActions.insert(type, ms + kSetMyActionForMs);
|
|
|
|
} else if (i.value() > ms + (kSetMyActionForMs / 2)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
i.value() = ms + kSetMyActionForMs;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (i == _mySendActions.cend()) {
|
|
|
|
return false;
|
|
|
|
} else if (i.value() <= ms) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
_mySendActions.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-21 19:21:08 +00:00
|
|
|
bool History::paintSendAction(
|
|
|
|
Painter &p,
|
|
|
|
int x,
|
|
|
|
int y,
|
|
|
|
int availableWidth,
|
|
|
|
int outerWidth,
|
|
|
|
style::color color,
|
|
|
|
TimeMs ms) {
|
2016-12-01 19:20:33 +00:00
|
|
|
if (_sendActionAnimation) {
|
2018-01-21 19:21:08 +00:00
|
|
|
_sendActionAnimation.paint(
|
|
|
|
p,
|
|
|
|
color,
|
|
|
|
x,
|
|
|
|
y + st::normalFont->ascent,
|
|
|
|
outerWidth,
|
|
|
|
ms);
|
2016-12-01 19:20:33 +00:00
|
|
|
auto animationWidth = _sendActionAnimation.width();
|
|
|
|
x += animationWidth;
|
|
|
|
availableWidth -= animationWidth;
|
|
|
|
p.setPen(color);
|
|
|
|
_sendActionText.drawElided(p, x, y, availableWidth);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool History::updateSendActionNeedsAnimating(TimeMs ms, bool force) {
|
|
|
|
auto changed = force;
|
|
|
|
for (auto i = _typing.begin(), e = _typing.end(); i != e;) {
|
2014-05-30 08:53:19 +00:00
|
|
|
if (ms >= i.value()) {
|
2016-12-01 19:20:33 +00:00
|
|
|
i = _typing.erase(i);
|
2014-05-30 08:53:19 +00:00
|
|
|
changed = true;
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
2016-12-01 19:20:33 +00:00
|
|
|
for (auto i = _sendActions.begin(); i != _sendActions.cend();) {
|
2015-08-01 08:33:00 +00:00
|
|
|
if (ms >= i.value().until) {
|
2016-12-01 19:20:33 +00:00
|
|
|
i = _sendActions.erase(i);
|
2015-08-01 08:33:00 +00:00
|
|
|
changed = true;
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
if (changed) {
|
2016-12-01 19:20:33 +00:00
|
|
|
QString newTypingString;
|
|
|
|
auto typingCount = _typing.size();
|
2016-09-30 13:40:22 +00:00
|
|
|
if (typingCount > 2) {
|
2016-12-01 19:20:33 +00:00
|
|
|
newTypingString = lng_many_typing(lt_count, typingCount);
|
2016-09-30 13:40:22 +00:00
|
|
|
} else if (typingCount > 1) {
|
2016-12-01 19:20:33 +00:00
|
|
|
newTypingString = lng_users_typing(lt_user, _typing.begin().key()->firstName, lt_second_user, (_typing.end() - 1).key()->firstName);
|
2016-09-30 13:40:22 +00:00
|
|
|
} else if (typingCount) {
|
2016-12-01 19:20:33 +00:00
|
|
|
newTypingString = peer->isUser() ? lang(lng_typing) : lng_user_typing(lt_user, _typing.begin().key()->firstName);
|
|
|
|
} else if (!_sendActions.isEmpty()) {
|
2016-09-30 13:40:22 +00:00
|
|
|
// Handles all actions except game playing.
|
2016-12-01 19:20:33 +00:00
|
|
|
using Type = SendAction::Type;
|
|
|
|
auto sendActionString = [](Type type, const QString &name) -> QString {
|
2016-09-30 13:40:22 +00:00
|
|
|
switch (type) {
|
2016-12-01 19:20:33 +00:00
|
|
|
case Type::RecordVideo: return name.isEmpty() ? lang(lng_send_action_record_video) : lng_user_action_record_video(lt_user, name);
|
|
|
|
case Type::UploadVideo: return name.isEmpty() ? lang(lng_send_action_upload_video) : lng_user_action_upload_video(lt_user, name);
|
|
|
|
case Type::RecordVoice: return name.isEmpty() ? lang(lng_send_action_record_audio) : lng_user_action_record_audio(lt_user, name);
|
|
|
|
case Type::UploadVoice: return name.isEmpty() ? lang(lng_send_action_upload_audio) : lng_user_action_upload_audio(lt_user, name);
|
2017-04-12 19:37:47 +00:00
|
|
|
case Type::RecordRound: return name.isEmpty() ? lang(lng_send_action_record_round) : lng_user_action_record_round(lt_user, name);
|
|
|
|
case Type::UploadRound: return name.isEmpty() ? lang(lng_send_action_upload_round) : lng_user_action_upload_round(lt_user, name);
|
2016-12-01 19:20:33 +00:00
|
|
|
case Type::UploadPhoto: return name.isEmpty() ? lang(lng_send_action_upload_photo) : lng_user_action_upload_photo(lt_user, name);
|
|
|
|
case Type::UploadFile: return name.isEmpty() ? lang(lng_send_action_upload_file) : lng_user_action_upload_file(lt_user, name);
|
2017-09-28 20:11:10 +00:00
|
|
|
case Type::ChooseLocation:
|
|
|
|
case Type::ChooseContact: return name.isEmpty() ? lang(lng_typing) : lng_user_typing(lt_user, name);
|
2016-09-30 13:40:22 +00:00
|
|
|
default: break;
|
|
|
|
};
|
|
|
|
return QString();
|
|
|
|
};
|
2016-12-01 19:20:33 +00:00
|
|
|
for (auto i = _sendActions.cbegin(), e = _sendActions.cend(); i != e; ++i) {
|
|
|
|
newTypingString = sendActionString(i->type, peer->isUser() ? QString() : i.key()->firstName);
|
|
|
|
if (!newTypingString.isEmpty()) {
|
|
|
|
_sendActionAnimation.start(i->type);
|
2016-09-30 13:40:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Everyone in sendActions are playing a game.
|
2016-12-01 19:20:33 +00:00
|
|
|
if (newTypingString.isEmpty()) {
|
|
|
|
int playingCount = _sendActions.size();
|
2016-09-30 13:40:22 +00:00
|
|
|
if (playingCount > 2) {
|
2016-12-01 19:20:33 +00:00
|
|
|
newTypingString = lng_many_playing_game(lt_count, playingCount);
|
2016-09-30 13:40:22 +00:00
|
|
|
} else if (playingCount > 1) {
|
2016-12-01 19:20:33 +00:00
|
|
|
newTypingString = lng_users_playing_game(lt_user, _sendActions.begin().key()->firstName, lt_second_user, (_sendActions.end() - 1).key()->firstName);
|
2016-09-30 13:40:22 +00:00
|
|
|
} else {
|
2016-12-01 19:20:33 +00:00
|
|
|
newTypingString = peer->isUser() ? lang(lng_playing_game) : lng_user_playing_game(lt_user, _sendActions.begin().key()->firstName);
|
2016-09-30 13:40:22 +00:00
|
|
|
}
|
2016-12-01 19:20:33 +00:00
|
|
|
_sendActionAnimation.start(Type::PlayGame);
|
2015-08-01 08:33:00 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-12-01 19:20:33 +00:00
|
|
|
if (typingCount > 0) {
|
|
|
|
_sendActionAnimation.start(SendAction::Type::Typing);
|
|
|
|
} else if (newTypingString.isEmpty()) {
|
|
|
|
_sendActionAnimation.stop();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-12-01 19:20:33 +00:00
|
|
|
if (_sendActionString != newTypingString) {
|
|
|
|
_sendActionString = newTypingString;
|
2017-12-28 13:06:06 +00:00
|
|
|
_sendActionText.setText(
|
|
|
|
st::dialogsTextStyle,
|
|
|
|
_sendActionString,
|
|
|
|
Ui::NameTextOptions());
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-01 19:20:33 +00:00
|
|
|
auto result = (!_typing.isEmpty() || !_sendActions.isEmpty());
|
2018-09-20 16:39:59 +00:00
|
|
|
if (changed || (result && !anim::Disabled())) {
|
2016-12-05 08:45:56 +00:00
|
|
|
App::histories().sendActionAnimationUpdated().notify({
|
|
|
|
this,
|
|
|
|
_sendActionAnimation.width(),
|
|
|
|
st::normalFont->height,
|
|
|
|
changed
|
|
|
|
});
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-12-01 19:20:33 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-01-11 13:07:29 +00:00
|
|
|
HistoryItem *History::createItem(
|
2018-01-14 16:02:25 +00:00
|
|
|
const MTPMessage &message,
|
2018-01-11 13:07:29 +00:00
|
|
|
bool detachExistingItem) {
|
2018-01-14 16:02:25 +00:00
|
|
|
const auto messageId = idFromMessage(message);
|
|
|
|
if (!messageId) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-04-30 13:53:36 +00:00
|
|
|
|
2018-01-14 16:02:25 +00:00
|
|
|
if (const auto result = App::histItemById(channelId(), messageId)) {
|
2018-01-11 13:07:29 +00:00
|
|
|
if (detachExistingItem) {
|
|
|
|
result->removeMainView();
|
2015-04-30 23:05:19 +00:00
|
|
|
}
|
2018-01-14 16:02:25 +00:00
|
|
|
if (message.type() == mtpc_message) {
|
|
|
|
const auto media = message.c_message().has_media()
|
|
|
|
? &message.c_message().vmedia
|
2017-12-13 18:10:48 +00:00
|
|
|
: nullptr;
|
2018-01-17 16:21:01 +00:00
|
|
|
result->updateSentMedia(media);
|
2015-04-30 13:53:36 +00:00
|
|
|
}
|
2015-12-27 21:37:48 +00:00
|
|
|
return result;
|
2015-04-30 13:53:36 +00:00
|
|
|
}
|
2018-01-14 16:02:25 +00:00
|
|
|
return HistoryItem::Create(this, message);
|
|
|
|
}
|
|
|
|
|
2018-01-17 16:21:01 +00:00
|
|
|
std::vector<not_null<HistoryItem*>> History::createItems(
|
|
|
|
const QVector<MTPMessage> &data) {
|
|
|
|
auto result = std::vector<not_null<HistoryItem*>>();
|
|
|
|
result.reserve(data.size());
|
|
|
|
for (auto i = data.cend(), e = data.cbegin(); i != e;) {
|
|
|
|
const auto detachExistingItem = true;
|
|
|
|
if (const auto item = createItem(*--i, detachExistingItem)) {
|
|
|
|
result.push_back(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-01-14 16:02:25 +00:00
|
|
|
not_null<HistoryItem*> History::addNewService(
|
|
|
|
MsgId msgId,
|
2018-02-03 19:52:35 +00:00
|
|
|
TimeId date,
|
2018-01-14 16:02:25 +00:00
|
|
|
const QString &text,
|
|
|
|
MTPDmessage::Flags flags,
|
|
|
|
bool unread) {
|
2017-06-18 13:08:49 +00:00
|
|
|
auto message = HistoryService::PreparedText { text };
|
2018-01-14 16:02:25 +00:00
|
|
|
return addNewItem(
|
2018-01-18 20:02:50 +00:00
|
|
|
new HistoryService(this, msgId, date, message, flags),
|
2018-01-14 16:02:25 +00:00
|
|
|
unread);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
HistoryItem *History::addNewMessage(
|
|
|
|
const MTPMessage &msg,
|
|
|
|
NewMessageType type) {
|
2017-12-16 13:24:16 +00:00
|
|
|
if (type == NewMessageExisting) {
|
|
|
|
return addToHistory(msg);
|
|
|
|
}
|
2015-11-13 15:14:33 +00:00
|
|
|
if (!loadedAtBottom() || peer->migrateTo()) {
|
2017-12-21 12:11:33 +00:00
|
|
|
if (const auto item = addToHistory(msg)) {
|
2015-09-20 08:55:41 +00:00
|
|
|
setLastMessage(item);
|
|
|
|
if (type == NewMessageUnread) {
|
2015-10-28 00:29:39 +00:00
|
|
|
newItemAdded(item);
|
2015-09-20 08:55:41 +00:00
|
|
|
}
|
2017-12-21 12:11:33 +00:00
|
|
|
return item;
|
2015-09-20 08:55:41 +00:00
|
|
|
}
|
2017-12-21 12:11:33 +00:00
|
|
|
return nullptr;
|
2015-09-20 08:55:41 +00:00
|
|
|
}
|
|
|
|
|
2016-03-19 18:32:17 +00:00
|
|
|
return addNewToLastBlock(msg, type);
|
|
|
|
}
|
|
|
|
|
2018-01-11 13:07:29 +00:00
|
|
|
HistoryItem *History::addNewToLastBlock(
|
|
|
|
const MTPMessage &msg,
|
|
|
|
NewMessageType type) {
|
2017-12-21 12:11:33 +00:00
|
|
|
Expects(type != NewMessageExisting);
|
|
|
|
|
2017-12-16 13:24:16 +00:00
|
|
|
const auto detachExistingItem = (type != NewMessageLast);
|
2018-01-14 16:02:25 +00:00
|
|
|
const auto item = createItem(msg, detachExistingItem);
|
2018-01-11 13:07:29 +00:00
|
|
|
if (!item || item->mainView()) {
|
2016-03-19 18:32:17 +00:00
|
|
|
return item;
|
2015-12-24 21:27:45 +00:00
|
|
|
}
|
2018-01-14 16:02:25 +00:00
|
|
|
const auto newUnreadMessage = (type == NewMessageUnread);
|
|
|
|
if (newUnreadMessage) {
|
|
|
|
applyMessageChanges(item, msg);
|
|
|
|
}
|
|
|
|
const auto result = addNewItem(item, newUnreadMessage);
|
2017-12-16 13:24:16 +00:00
|
|
|
if (type == NewMessageLast) {
|
|
|
|
// When we add just one last item, like we do while loading dialogs,
|
|
|
|
// we want to remove a single added grouped media, otherwise it will
|
|
|
|
// jump once we open the message history (first we show only that
|
|
|
|
// media, then we load the rest of the group and show the group).
|
|
|
|
//
|
|
|
|
// That way when we open the message history we show nothing until a
|
|
|
|
// whole history part is loaded, it certainly will contain the group.
|
|
|
|
removeOrphanMediaGroupPart();
|
|
|
|
}
|
|
|
|
return result;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2015-09-21 20:57:42 +00:00
|
|
|
HistoryItem *History::addToHistory(const MTPMessage &msg) {
|
2018-01-14 16:02:25 +00:00
|
|
|
const auto detachExistingItem = false;
|
|
|
|
return createItem(msg, detachExistingItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
not_null<HistoryItem*> History::addNewForwarded(
|
|
|
|
MsgId id,
|
|
|
|
MTPDmessage::Flags flags,
|
2018-02-03 19:52:35 +00:00
|
|
|
TimeId date,
|
2018-01-14 16:02:25 +00:00
|
|
|
UserId from,
|
|
|
|
const QString &postAuthor,
|
2018-02-03 19:52:35 +00:00
|
|
|
not_null<HistoryMessage*> original) {
|
2018-01-14 16:02:25 +00:00
|
|
|
return addNewItem(
|
2018-02-03 19:52:35 +00:00
|
|
|
new HistoryMessage(
|
|
|
|
this,
|
|
|
|
id,
|
|
|
|
flags,
|
|
|
|
date,
|
|
|
|
from,
|
|
|
|
postAuthor,
|
|
|
|
original),
|
2018-01-14 16:02:25 +00:00
|
|
|
true);
|
|
|
|
}
|
|
|
|
|
|
|
|
not_null<HistoryItem*> History::addNewDocument(
|
|
|
|
MsgId id,
|
|
|
|
MTPDmessage::Flags flags,
|
|
|
|
UserId viaBotId,
|
|
|
|
MsgId replyTo,
|
2018-02-03 19:52:35 +00:00
|
|
|
TimeId date,
|
2018-01-14 16:02:25 +00:00
|
|
|
UserId from,
|
|
|
|
const QString &postAuthor,
|
2018-02-03 19:52:35 +00:00
|
|
|
not_null<DocumentData*> document,
|
2018-01-18 13:59:22 +00:00
|
|
|
const TextWithEntities &caption,
|
2018-01-14 16:02:25 +00:00
|
|
|
const MTPReplyMarkup &markup) {
|
|
|
|
return addNewItem(
|
2018-02-03 19:52:35 +00:00
|
|
|
new HistoryMessage(
|
|
|
|
this,
|
2018-01-14 16:02:25 +00:00
|
|
|
id,
|
|
|
|
flags,
|
|
|
|
replyTo,
|
2018-02-03 19:52:35 +00:00
|
|
|
viaBotId,
|
2018-01-14 16:02:25 +00:00
|
|
|
date,
|
|
|
|
from,
|
|
|
|
postAuthor,
|
|
|
|
document,
|
|
|
|
caption,
|
|
|
|
markup),
|
|
|
|
true);
|
|
|
|
}
|
|
|
|
|
|
|
|
not_null<HistoryItem*> History::addNewPhoto(
|
|
|
|
MsgId id,
|
|
|
|
MTPDmessage::Flags flags,
|
|
|
|
UserId viaBotId,
|
|
|
|
MsgId replyTo,
|
2018-02-03 19:52:35 +00:00
|
|
|
TimeId date,
|
2018-01-14 16:02:25 +00:00
|
|
|
UserId from,
|
|
|
|
const QString &postAuthor,
|
2018-02-03 19:52:35 +00:00
|
|
|
not_null<PhotoData*> photo,
|
2018-01-18 13:59:22 +00:00
|
|
|
const TextWithEntities &caption,
|
2018-01-14 16:02:25 +00:00
|
|
|
const MTPReplyMarkup &markup) {
|
|
|
|
return addNewItem(
|
2018-02-03 19:52:35 +00:00
|
|
|
new HistoryMessage(
|
|
|
|
this,
|
2018-01-14 16:02:25 +00:00
|
|
|
id,
|
|
|
|
flags,
|
|
|
|
replyTo,
|
2018-02-03 19:52:35 +00:00
|
|
|
viaBotId,
|
2018-01-14 16:02:25 +00:00
|
|
|
date,
|
|
|
|
from,
|
|
|
|
postAuthor,
|
|
|
|
photo,
|
|
|
|
caption,
|
|
|
|
markup),
|
|
|
|
true);
|
|
|
|
}
|
|
|
|
|
|
|
|
not_null<HistoryItem*> History::addNewGame(
|
|
|
|
MsgId id,
|
|
|
|
MTPDmessage::Flags flags,
|
|
|
|
UserId viaBotId,
|
|
|
|
MsgId replyTo,
|
2018-02-03 19:52:35 +00:00
|
|
|
TimeId date,
|
2018-01-14 16:02:25 +00:00
|
|
|
UserId from,
|
|
|
|
const QString &postAuthor,
|
2018-02-03 19:52:35 +00:00
|
|
|
not_null<GameData*> game,
|
2018-01-14 16:02:25 +00:00
|
|
|
const MTPReplyMarkup &markup) {
|
|
|
|
return addNewItem(
|
2018-02-03 19:52:35 +00:00
|
|
|
new HistoryMessage(
|
|
|
|
this,
|
2018-01-14 16:02:25 +00:00
|
|
|
id,
|
|
|
|
flags,
|
|
|
|
replyTo,
|
2018-02-03 19:52:35 +00:00
|
|
|
viaBotId,
|
2018-01-14 16:02:25 +00:00
|
|
|
date,
|
|
|
|
from,
|
|
|
|
postAuthor,
|
|
|
|
game,
|
|
|
|
markup),
|
|
|
|
true);
|
2016-09-28 16:23:25 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 07:16:07 +00:00
|
|
|
void History::setUnreadMentionsCount(int count) {
|
2018-12-13 12:31:03 +00:00
|
|
|
const auto had = _unreadMentionsCount && (*_unreadMentionsCount > 0);
|
2017-08-11 07:16:07 +00:00
|
|
|
if (_unreadMentions.size() > count) {
|
|
|
|
LOG(("API Warning: real mentions count is greater than received mentions count"));
|
|
|
|
count = _unreadMentions.size();
|
|
|
|
}
|
|
|
|
_unreadMentionsCount = count;
|
2018-12-13 12:31:03 +00:00
|
|
|
const auto has = (count > 0);
|
|
|
|
if (has != had && Global::DialogsModeEnabled()) {
|
|
|
|
Notify::historyMuteUpdated(this);
|
|
|
|
updateChatListEntry();
|
|
|
|
}
|
2017-08-11 07:16:07 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 18:27:28 +00:00
|
|
|
bool History::addToUnreadMentions(
|
|
|
|
MsgId msgId,
|
2017-12-21 12:11:33 +00:00
|
|
|
UnreadMentionType type) {
|
2018-03-03 13:09:31 +00:00
|
|
|
if (peer->isChannel() && !peer->isMegagroup()) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-21 12:11:33 +00:00
|
|
|
auto allLoaded = _unreadMentionsCount
|
|
|
|
? (_unreadMentions.size() >= *_unreadMentionsCount)
|
|
|
|
: false;
|
2017-08-11 07:16:07 +00:00
|
|
|
if (allLoaded) {
|
2017-12-21 12:11:33 +00:00
|
|
|
if (type == UnreadMentionType::New) {
|
2017-08-11 07:16:07 +00:00
|
|
|
_unreadMentions.insert(msgId);
|
2018-12-13 12:31:03 +00:00
|
|
|
setUnreadMentionsCount(*_unreadMentionsCount + 1);
|
2017-08-11 07:16:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-12-21 12:11:33 +00:00
|
|
|
} else if (!_unreadMentions.empty() && type != UnreadMentionType::New) {
|
2017-08-11 07:16:07 +00:00
|
|
|
_unreadMentions.insert(msgId);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void History::eraseFromUnreadMentions(MsgId msgId) {
|
|
|
|
_unreadMentions.remove(msgId);
|
2018-12-13 12:31:03 +00:00
|
|
|
if (_unreadMentionsCount && *_unreadMentionsCount > 0) {
|
|
|
|
setUnreadMentionsCount(*_unreadMentionsCount - 1);
|
2017-08-11 07:16:07 +00:00
|
|
|
}
|
|
|
|
Notify::peerUpdatedDelayed(peer, Notify::PeerUpdate::Flag::UnreadMentionsChanged);
|
|
|
|
}
|
|
|
|
|
|
|
|
void History::addUnreadMentionsSlice(const MTPmessages_Messages &result) {
|
|
|
|
auto count = 0;
|
|
|
|
auto messages = (const QVector<MTPMessage>*)nullptr;
|
|
|
|
auto getMessages = [](auto &list) {
|
|
|
|
App::feedUsers(list.vusers);
|
|
|
|
App::feedChats(list.vchats);
|
|
|
|
return &list.vmessages.v;
|
|
|
|
};
|
|
|
|
switch (result.type()) {
|
|
|
|
case mtpc_messages_messages: {
|
|
|
|
auto &d = result.c_messages_messages();
|
|
|
|
messages = getMessages(d);
|
|
|
|
count = messages->size();
|
|
|
|
} break;
|
2016-01-03 01:43:42 +00:00
|
|
|
|
2017-08-11 07:16:07 +00:00
|
|
|
case mtpc_messages_messagesSlice: {
|
|
|
|
auto &d = result.c_messages_messagesSlice();
|
|
|
|
messages = getMessages(d);
|
|
|
|
count = d.vcount.v;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_messages_channelMessages: {
|
2017-11-20 19:54:05 +00:00
|
|
|
LOG(("API Error: unexpected messages.channelMessages! (History::addUnreadMentionsSlice)"));
|
2017-08-11 07:16:07 +00:00
|
|
|
auto &d = result.c_messages_channelMessages();
|
|
|
|
messages = getMessages(d);
|
|
|
|
count = d.vcount.v;
|
|
|
|
} break;
|
|
|
|
|
2017-11-20 19:54:05 +00:00
|
|
|
case mtpc_messages_messagesNotModified: {
|
|
|
|
LOG(("API Error: received messages.messagesNotModified! (History::addUnreadMentionsSlice)"));
|
|
|
|
} break;
|
|
|
|
|
2017-08-11 07:16:07 +00:00
|
|
|
default: Unexpected("type in History::addUnreadMentionsSlice");
|
|
|
|
}
|
|
|
|
|
|
|
|
auto added = false;
|
2017-11-20 19:54:05 +00:00
|
|
|
if (messages) {
|
|
|
|
for (auto &message : *messages) {
|
|
|
|
if (auto item = addToHistory(message)) {
|
|
|
|
if (item->mentionsMe() && item->isMediaUnread()) {
|
|
|
|
_unreadMentions.insert(item->id);
|
|
|
|
added = true;
|
|
|
|
}
|
2016-01-03 01:43:42 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-28 15:15:56 +00:00
|
|
|
}
|
2017-08-11 07:16:07 +00:00
|
|
|
if (!added) {
|
|
|
|
count = _unreadMentions.size();
|
|
|
|
}
|
|
|
|
setUnreadMentionsCount(count);
|
|
|
|
Notify::peerUpdatedDelayed(peer, Notify::PeerUpdate::Flag::UnreadMentionsChanged);
|
2015-08-28 15:15:56 +00:00
|
|
|
}
|
|
|
|
|
2018-01-14 16:02:25 +00:00
|
|
|
not_null<HistoryItem*> History::addNewItem(
|
|
|
|
not_null<HistoryItem*> item,
|
|
|
|
bool unread) {
|
2017-05-12 13:53:08 +00:00
|
|
|
Expects(!isBuildingFrontBlock());
|
2017-12-16 13:24:16 +00:00
|
|
|
|
2018-01-14 16:02:25 +00:00
|
|
|
addItemToBlock(item);
|
2016-03-19 18:32:17 +00:00
|
|
|
|
2018-01-14 16:02:25 +00:00
|
|
|
if (!unread && IsServerMsgId(item->id)) {
|
|
|
|
if (const auto sharedMediaTypes = item->sharedMediaTypes()) {
|
2017-12-21 12:11:33 +00:00
|
|
|
auto from = loadedAtTop() ? 0 : minMsgId();
|
|
|
|
auto till = loadedAtBottom() ? ServerMaxMsgId : maxMsgId();
|
|
|
|
Auth().storage().add(Storage::SharedMediaAddExisting(
|
|
|
|
peer->id,
|
|
|
|
sharedMediaTypes,
|
2018-01-14 16:02:25 +00:00
|
|
|
item->id,
|
2017-12-21 12:11:33 +00:00
|
|
|
{ from, till }));
|
2017-09-04 11:40:02 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-14 16:02:25 +00:00
|
|
|
if (item->from()->id) {
|
|
|
|
if (auto user = item->from()->asUser()) {
|
2017-12-01 18:38:44 +00:00
|
|
|
auto getLastAuthors = [this]() -> std::deque<not_null<UserData*>>* {
|
2017-06-09 16:12:02 +00:00
|
|
|
if (auto chat = peer->asChat()) {
|
|
|
|
return &chat->lastAuthors;
|
|
|
|
} else if (auto channel = peer->asMegagroup()) {
|
|
|
|
return &channel->mgInfo->lastParticipants;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
};
|
2017-10-22 17:06:57 +00:00
|
|
|
if (auto megagroup = peer->asMegagroup()) {
|
|
|
|
if (user->botInfo) {
|
|
|
|
auto mgInfo = megagroup->mgInfo.get();
|
|
|
|
Assert(mgInfo != nullptr);
|
|
|
|
mgInfo->bots.insert(user);
|
|
|
|
if (mgInfo->botStatus != 0 && mgInfo->botStatus < 2) {
|
|
|
|
mgInfo->botStatus = 2;
|
2015-11-20 13:34:37 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-09 09:51:22 +00:00
|
|
|
}
|
2017-06-09 16:12:02 +00:00
|
|
|
if (auto lastAuthors = getLastAuthors()) {
|
2017-12-01 18:38:44 +00:00
|
|
|
auto prev = ranges::find(
|
|
|
|
*lastAuthors,
|
|
|
|
user,
|
|
|
|
[](not_null<UserData*> user) { return user.get(); });
|
|
|
|
auto index = (prev != lastAuthors->end())
|
|
|
|
? (lastAuthors->end() - prev)
|
|
|
|
: -1;
|
|
|
|
if (index > 0) {
|
|
|
|
lastAuthors->erase(prev);
|
|
|
|
} else if (index < 0 && peer->isMegagroup()) { // nothing is outdated if just reordering
|
2017-12-03 16:43:42 +00:00
|
|
|
// admins information outdated
|
2015-11-09 09:51:22 +00:00
|
|
|
}
|
2017-12-01 18:38:44 +00:00
|
|
|
if (index) {
|
2017-10-22 17:06:57 +00:00
|
|
|
lastAuthors->push_front(user);
|
2015-11-09 09:51:22 +00:00
|
|
|
}
|
2017-10-22 17:06:57 +00:00
|
|
|
if (auto megagroup = peer->asMegagroup()) {
|
2016-06-03 07:20:24 +00:00
|
|
|
Notify::peerUpdatedDelayed(peer, Notify::PeerUpdate::Flag::MembersChanged);
|
2017-10-22 17:06:57 +00:00
|
|
|
Auth().data().addNewMegagroupParticipant(megagroup, user);
|
2016-06-03 07:20:24 +00:00
|
|
|
}
|
2015-06-15 17:19:24 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-14 16:02:25 +00:00
|
|
|
if (item->definesReplyKeyboard()) {
|
|
|
|
auto markupFlags = item->replyKeyboardFlags();
|
|
|
|
if (!(markupFlags & MTPDreplyKeyboardMarkup::Flag::f_selective)
|
|
|
|
|| item->mentionsMe()) {
|
2017-12-01 18:38:44 +00:00
|
|
|
auto getMarkupSenders = [this]() -> base::flat_set<not_null<PeerData*>>* {
|
2017-06-09 16:12:02 +00:00
|
|
|
if (auto chat = peer->asChat()) {
|
|
|
|
return &chat->markupSenders;
|
|
|
|
} else if (auto channel = peer->asMegagroup()) {
|
|
|
|
return &channel->mgInfo->markupSenders;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
if (auto markupSenders = getMarkupSenders()) {
|
2018-01-14 16:02:25 +00:00
|
|
|
markupSenders->insert(item->from());
|
2015-06-22 08:51:39 +00:00
|
|
|
}
|
2016-03-19 16:55:15 +00:00
|
|
|
if (markupFlags & MTPDreplyKeyboardMarkup_ClientFlag::f_zero) { // zero markup means replyKeyboardHide
|
2018-01-14 16:02:25 +00:00
|
|
|
if (lastKeyboardFrom == item->from()->id
|
|
|
|
|| (!lastKeyboardInited
|
|
|
|
&& !peer->isChat()
|
|
|
|
&& !peer->isMegagroup()
|
|
|
|
&& !item->out())) {
|
2015-06-24 17:24:48 +00:00
|
|
|
clearLastKeyboard();
|
2015-06-22 08:51:39 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-11-09 09:51:22 +00:00
|
|
|
bool botNotInChat = false;
|
|
|
|
if (peer->isChat()) {
|
2018-01-14 16:02:25 +00:00
|
|
|
botNotInChat = item->from()->isUser()
|
|
|
|
&& (!peer->asChat()->participants.empty()
|
|
|
|
|| !peer->canWrite())
|
|
|
|
&& !peer->asChat()->participants.contains(
|
|
|
|
item->from()->asUser());
|
2015-11-09 09:51:22 +00:00
|
|
|
} else if (peer->isMegagroup()) {
|
2018-01-14 16:02:25 +00:00
|
|
|
botNotInChat = item->from()->isUser()
|
|
|
|
&& (peer->asChannel()->mgInfo->botStatus != 0
|
|
|
|
|| !peer->canWrite())
|
|
|
|
&& !peer->asChannel()->mgInfo->bots.contains(
|
|
|
|
item->from()->asUser());
|
2015-11-09 09:51:22 +00:00
|
|
|
}
|
|
|
|
if (botNotInChat) {
|
|
|
|
clearLastKeyboard();
|
|
|
|
} else {
|
|
|
|
lastKeyboardInited = true;
|
2018-01-14 16:02:25 +00:00
|
|
|
lastKeyboardId = item->id;
|
|
|
|
lastKeyboardFrom = item->from()->id;
|
2015-11-09 09:51:22 +00:00
|
|
|
lastKeyboardUsed = false;
|
|
|
|
}
|
2015-06-17 19:43:03 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-19 09:18:19 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-21 20:57:42 +00:00
|
|
|
|
2018-01-14 16:02:25 +00:00
|
|
|
setLastMessage(item);
|
|
|
|
if (unread) {
|
|
|
|
newItemAdded(item);
|
2017-12-18 11:17:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-18 09:53:49 +00:00
|
|
|
Auth().data().notifyHistoryChangeDelayed(this);
|
2018-01-14 16:02:25 +00:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
void History::applyMessageChanges(
|
|
|
|
not_null<HistoryItem*> item,
|
|
|
|
const MTPMessage &data) {
|
|
|
|
if (data.type() == mtpc_messageService) {
|
|
|
|
applyServiceChanges(item, data.c_messageService());
|
|
|
|
}
|
|
|
|
App::checkSavedGif(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
void History::applyServiceChanges(
|
|
|
|
not_null<HistoryItem*> item,
|
|
|
|
const MTPDmessageService &data) {
|
|
|
|
auto &action = data.vaction;
|
|
|
|
switch (action.type()) {
|
|
|
|
case mtpc_messageActionChatAddUser: {
|
|
|
|
auto &d = action.c_messageActionChatAddUser();
|
|
|
|
if (auto megagroup = peer->asMegagroup()) {
|
|
|
|
auto mgInfo = megagroup->mgInfo.get();
|
|
|
|
Assert(mgInfo != nullptr);
|
|
|
|
auto &v = d.vusers.v;
|
|
|
|
for (auto i = 0, l = v.size(); i != l; ++i) {
|
|
|
|
if (auto user = App::userLoaded(peerFromUser(v[i]))) {
|
|
|
|
if (!base::contains(mgInfo->lastParticipants, user)) {
|
|
|
|
mgInfo->lastParticipants.push_front(user);
|
|
|
|
Notify::peerUpdatedDelayed(peer, Notify::PeerUpdate::Flag::MembersChanged);
|
|
|
|
Auth().data().addNewMegagroupParticipant(megagroup, user);
|
|
|
|
}
|
|
|
|
if (user->botInfo) {
|
|
|
|
peer->asChannel()->mgInfo->bots.insert(user);
|
|
|
|
if (peer->asChannel()->mgInfo->botStatus != 0 && peer->asChannel()->mgInfo->botStatus < 2) {
|
|
|
|
peer->asChannel()->mgInfo->botStatus = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_messageActionChatJoinedByLink: {
|
|
|
|
auto &d = action.c_messageActionChatJoinedByLink();
|
|
|
|
if (auto megagroup = peer->asMegagroup()) {
|
|
|
|
auto mgInfo = megagroup->mgInfo.get();
|
|
|
|
Assert(mgInfo != nullptr);
|
|
|
|
if (auto user = item->from()->asUser()) {
|
|
|
|
if (!base::contains(mgInfo->lastParticipants, user)) {
|
|
|
|
mgInfo->lastParticipants.push_front(user);
|
|
|
|
Notify::peerUpdatedDelayed(peer, Notify::PeerUpdate::Flag::MembersChanged);
|
|
|
|
Auth().data().addNewMegagroupParticipant(megagroup, user);
|
|
|
|
}
|
|
|
|
if (user->botInfo) {
|
|
|
|
mgInfo->bots.insert(user);
|
|
|
|
if (mgInfo->botStatus != 0 && mgInfo->botStatus < 2) {
|
|
|
|
mgInfo->botStatus = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_messageActionChatDeletePhoto: {
|
|
|
|
if (const auto chat = peer->asChat()) {
|
|
|
|
chat->setPhoto(MTP_chatPhotoEmpty());
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_messageActionChatDeleteUser: {
|
|
|
|
auto &d = action.c_messageActionChatDeleteUser();
|
|
|
|
auto uid = peerFromUser(d.vuser_id);
|
|
|
|
if (lastKeyboardFrom == uid) {
|
|
|
|
clearLastKeyboard();
|
|
|
|
}
|
|
|
|
if (auto megagroup = peer->asMegagroup()) {
|
|
|
|
if (auto user = App::userLoaded(uid)) {
|
|
|
|
auto mgInfo = megagroup->mgInfo.get();
|
|
|
|
Assert(mgInfo != nullptr);
|
|
|
|
auto i = ranges::find(
|
|
|
|
mgInfo->lastParticipants,
|
|
|
|
user,
|
|
|
|
[](not_null<UserData*> user) { return user.get(); });
|
|
|
|
if (i != mgInfo->lastParticipants.end()) {
|
|
|
|
mgInfo->lastParticipants.erase(i);
|
|
|
|
Notify::peerUpdatedDelayed(peer, Notify::PeerUpdate::Flag::MembersChanged);
|
|
|
|
}
|
|
|
|
Auth().data().removeMegagroupParticipant(megagroup, user);
|
|
|
|
if (megagroup->membersCount() > 1) {
|
|
|
|
megagroup->setMembersCount(megagroup->membersCount() - 1);
|
|
|
|
} else {
|
|
|
|
mgInfo->lastParticipantsStatus |= MegagroupInfo::LastParticipantsCountOutdated;
|
|
|
|
mgInfo->lastParticipantsCount = 0;
|
|
|
|
}
|
|
|
|
if (mgInfo->lastAdmins.contains(user)) {
|
|
|
|
mgInfo->lastAdmins.remove(user);
|
|
|
|
if (megagroup->adminsCount() > 1) {
|
|
|
|
megagroup->setAdminsCount(megagroup->adminsCount() - 1);
|
|
|
|
}
|
|
|
|
Notify::peerUpdatedDelayed(peer, Notify::PeerUpdate::Flag::AdminsChanged);
|
|
|
|
}
|
|
|
|
mgInfo->bots.remove(user);
|
|
|
|
if (mgInfo->bots.empty() && mgInfo->botStatus > 0) {
|
|
|
|
mgInfo->botStatus = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Data::ChannelAdminChanges(megagroup).feed(uid, false);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_messageActionChatEditPhoto: {
|
|
|
|
auto &d = action.c_messageActionChatEditPhoto();
|
|
|
|
if (d.vphoto.type() == mtpc_photo) {
|
|
|
|
auto &sizes = d.vphoto.c_photo().vsizes.v;
|
|
|
|
if (!sizes.isEmpty()) {
|
2018-01-17 16:21:01 +00:00
|
|
|
auto photo = Auth().data().photo(d.vphoto.c_photo());
|
2018-01-14 16:02:25 +00:00
|
|
|
if (photo) photo->peer = peer;
|
|
|
|
auto &smallSize = sizes.front();
|
|
|
|
auto &bigSize = sizes.back();
|
|
|
|
const MTPFileLocation *smallLoc = 0, *bigLoc = 0;
|
|
|
|
switch (smallSize.type()) {
|
|
|
|
case mtpc_photoSize: smallLoc = &smallSize.c_photoSize().vlocation; break;
|
|
|
|
case mtpc_photoCachedSize: smallLoc = &smallSize.c_photoCachedSize().vlocation; break;
|
|
|
|
}
|
|
|
|
switch (bigSize.type()) {
|
|
|
|
case mtpc_photoSize: bigLoc = &bigSize.c_photoSize().vlocation; break;
|
|
|
|
case mtpc_photoCachedSize: bigLoc = &bigSize.c_photoCachedSize().vlocation; break;
|
|
|
|
}
|
|
|
|
if (smallLoc && bigLoc) {
|
|
|
|
const auto newPhotoId = photo ? photo->id : 0;
|
|
|
|
if (const auto chat = peer->asChat()) {
|
|
|
|
chat->setPhoto(newPhotoId, MTP_chatPhoto(*smallLoc, *bigLoc));
|
|
|
|
} else if (const auto channel = peer->asChannel()) {
|
|
|
|
channel->setPhoto(newPhotoId, MTP_chatPhoto(*smallLoc, *bigLoc));
|
|
|
|
}
|
|
|
|
peer->loadUserpic();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_messageActionChatEditTitle: {
|
|
|
|
auto &d = action.c_messageActionChatEditTitle();
|
|
|
|
if (auto chat = peer->asChat()) {
|
|
|
|
chat->setName(qs(d.vtitle));
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_messageActionChatMigrateTo: {
|
|
|
|
if (auto chat = peer->asChat()) {
|
|
|
|
chat->addFlags(MTPDchat::Flag::f_deactivated);
|
|
|
|
}
|
|
|
|
//auto &d = action.c_messageActionChatMigrateTo();
|
|
|
|
//auto channel = App::channelLoaded(d.vchannel_id.v);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_messageActionChannelMigrateFrom: {
|
|
|
|
//auto &d = action.c_messageActionChannelMigrateFrom();
|
|
|
|
//auto chat = App::chatLoaded(d.vchat_id.v);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_messageActionPinMessage: {
|
|
|
|
if (data.has_reply_to_msg_id() && item) {
|
2018-10-31 11:29:14 +00:00
|
|
|
item->history()->peer->setPinnedMessageId(data.vreply_to_msg_id.v);
|
2018-01-14 16:02:25 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_messageActionPhoneCall: {
|
|
|
|
Calls::Current().newServiceMessage().notify(item->fullId());
|
|
|
|
} break;
|
|
|
|
}
|
2014-07-04 11:12:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 14:27:59 +00:00
|
|
|
void History::clearSendAction(not_null<UserData*> from) {
|
2016-12-01 19:20:33 +00:00
|
|
|
auto updateAtMs = TimeMs(0);
|
|
|
|
auto i = _typing.find(from);
|
|
|
|
if (i != _typing.cend()) {
|
2016-01-05 04:52:40 +00:00
|
|
|
updateAtMs = getms();
|
2015-08-01 08:33:00 +00:00
|
|
|
i.value() = updateAtMs;
|
|
|
|
}
|
2016-12-01 19:20:33 +00:00
|
|
|
auto j = _sendActions.find(from);
|
|
|
|
if (j != _sendActions.cend()) {
|
2016-01-05 04:52:40 +00:00
|
|
|
if (!updateAtMs) updateAtMs = getms();
|
2015-08-01 08:33:00 +00:00
|
|
|
j.value().until = updateAtMs;
|
|
|
|
}
|
|
|
|
if (updateAtMs) {
|
2016-12-01 19:20:33 +00:00
|
|
|
updateSendActionNeedsAnimating(updateAtMs, true);
|
2014-10-22 18:39:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-21 19:21:08 +00:00
|
|
|
void History::mainViewRemoved(
|
|
|
|
not_null<HistoryBlock*> block,
|
|
|
|
not_null<HistoryView::Element*> view) {
|
2018-01-31 17:10:29 +00:00
|
|
|
if (_joinedMessage == view->data()) {
|
|
|
|
_joinedMessage = nullptr;
|
|
|
|
}
|
2018-01-21 19:21:08 +00:00
|
|
|
if (_firstUnreadView == view) {
|
|
|
|
getNextFirstUnreadMessage();
|
|
|
|
}
|
|
|
|
if (_unreadBarView == view) {
|
|
|
|
_unreadBarView = nullptr;
|
|
|
|
}
|
|
|
|
if (scrollTopItem == view) {
|
|
|
|
getNextScrollTopItem(block, view->indexInBlock());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 12:11:33 +00:00
|
|
|
void History::newItemAdded(not_null<HistoryItem*> item) {
|
|
|
|
item->indexAsNewItem();
|
2017-12-07 14:27:59 +00:00
|
|
|
if (const auto from = item->from() ? item->from()->asUser() : nullptr) {
|
|
|
|
if (from == item->author()) {
|
|
|
|
clearSendAction(from);
|
2016-02-17 16:37:21 +00:00
|
|
|
}
|
2018-02-03 19:52:35 +00:00
|
|
|
from->madeAction(item->date());
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2014-07-04 11:12:54 +00:00
|
|
|
if (item->out()) {
|
2018-01-21 19:21:08 +00:00
|
|
|
destroyUnreadBar();
|
2015-10-28 00:29:39 +00:00
|
|
|
if (!item->unread()) {
|
|
|
|
outboxRead(item);
|
|
|
|
}
|
2014-07-04 11:12:54 +00:00
|
|
|
} else if (item->unread()) {
|
2015-09-23 17:43:08 +00:00
|
|
|
if (!isChannel() || peer->asChannel()->amIn()) {
|
|
|
|
notifies.push_back(item);
|
|
|
|
App::main()->newUnreadMsg(this, item);
|
|
|
|
}
|
2015-11-13 15:14:33 +00:00
|
|
|
} else if (!item->isGroupMigrate() || !peer->isMegagroup()) {
|
2015-10-28 00:29:39 +00:00
|
|
|
inboxRead(item);
|
2014-07-04 11:12:54 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 10:37:58 +00:00
|
|
|
HistoryBlock *History::prepareBlockForAddingItem() {
|
|
|
|
if (isBuildingFrontBlock()) {
|
2016-03-31 11:18:21 +00:00
|
|
|
if (_buildingFrontBlock->block) {
|
|
|
|
return _buildingFrontBlock->block;
|
2016-03-31 10:37:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-10 13:13:33 +00:00
|
|
|
blocks.push_front(std::make_unique<HistoryBlock>(this));
|
|
|
|
for (auto i = 0, l = int(blocks.size()); i != l; ++i) {
|
2017-05-12 13:53:08 +00:00
|
|
|
blocks[i]->setIndexInHistory(i);
|
2016-03-31 10:37:58 +00:00
|
|
|
}
|
2018-01-10 13:13:33 +00:00
|
|
|
_buildingFrontBlock->block = blocks.front().get();
|
|
|
|
if (_buildingFrontBlock->expectedItemsCount > 0) {
|
|
|
|
_buildingFrontBlock->block->messages.reserve(
|
|
|
|
_buildingFrontBlock->expectedItemsCount + 1);
|
|
|
|
}
|
|
|
|
return _buildingFrontBlock->block;
|
2016-03-31 10:37:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-10 13:13:33 +00:00
|
|
|
const auto addNewBlock = blocks.empty()
|
|
|
|
|| (blocks.back()->messages.size() >= kNewBlockEachMessage);
|
|
|
|
if (addNewBlock) {
|
|
|
|
blocks.push_back(std::make_unique<HistoryBlock>(this));
|
|
|
|
blocks.back()->setIndexInHistory(blocks.size() - 1);
|
|
|
|
blocks.back()->messages.reserve(kNewBlockEachMessage);
|
2016-03-31 10:37:58 +00:00
|
|
|
}
|
2018-01-10 13:13:33 +00:00
|
|
|
return blocks.back().get();
|
2018-01-23 16:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void History::viewReplaced(not_null<const Element*> was, Element *now) {
|
|
|
|
if (scrollTopItem == was) scrollTopItem= now;
|
|
|
|
if (_firstUnreadView == was) _firstUnreadView= now;
|
|
|
|
if (_unreadBarView == was) _unreadBarView = now;
|
|
|
|
}
|
2016-03-31 10:37:58 +00:00
|
|
|
|
2017-12-13 18:10:48 +00:00
|
|
|
void History::addItemToBlock(not_null<HistoryItem*> item) {
|
2018-01-11 13:07:29 +00:00
|
|
|
Expects(!item->mainView());
|
2016-03-31 10:37:58 +00:00
|
|
|
|
2016-09-23 16:04:26 +00:00
|
|
|
auto block = prepareBlockForAddingItem();
|
2016-03-31 10:37:58 +00:00
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
block->messages.push_back(item->createView(
|
2018-01-17 16:21:01 +00:00
|
|
|
HistoryInner::ElementDelegate()));
|
2018-01-13 12:45:11 +00:00
|
|
|
const auto view = block->messages.back().get();
|
|
|
|
view->attachToBlock(block, block->messages.size() - 1);
|
2015-09-19 09:13:21 +00:00
|
|
|
|
2016-03-31 11:18:21 +00:00
|
|
|
if (isBuildingFrontBlock() && _buildingFrontBlock->expectedItemsCount > 0) {
|
|
|
|
--_buildingFrontBlock->expectedItemsCount;
|
2015-09-19 09:13:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-17 16:21:01 +00:00
|
|
|
void History::addEdgesToSharedMedia() {
|
2017-09-04 11:40:02 +00:00
|
|
|
auto from = loadedAtTop() ? 0 : minMsgId();
|
|
|
|
auto till = loadedAtBottom() ? ServerMaxMsgId : maxMsgId();
|
|
|
|
for (auto i = 0; i != Storage::kSharedMediaTypeCount; ++i) {
|
2018-01-17 16:21:01 +00:00
|
|
|
const auto type = static_cast<Storage::SharedMediaType>(i);
|
|
|
|
Auth().storage().add(Storage::SharedMediaAddSlice(
|
|
|
|
peer->id,
|
|
|
|
type,
|
|
|
|
{},
|
|
|
|
{ from, till }));
|
2017-09-04 11:40:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-27 16:47:46 +00:00
|
|
|
void History::addOlderSlice(const QVector<MTPMessage> &slice) {
|
2014-07-04 11:12:54 +00:00
|
|
|
if (slice.isEmpty()) {
|
2018-02-05 14:37:22 +00:00
|
|
|
_loadedAtTop = true;
|
2018-01-31 17:10:29 +00:00
|
|
|
checkJoinedMessage();
|
2016-05-27 16:47:46 +00:00
|
|
|
return;
|
2014-07-04 11:12:54 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-01-17 16:21:01 +00:00
|
|
|
if (const auto added = createItems(slice); !added.empty()) {
|
|
|
|
startBuildingFrontBlock(added.size());
|
|
|
|
for (const auto item : added) {
|
|
|
|
addItemToBlock(item);
|
2014-08-11 09:03:45 +00:00
|
|
|
}
|
2018-01-30 10:51:35 +00:00
|
|
|
finishBuildingFrontBlock();
|
2017-11-17 20:04:22 +00:00
|
|
|
|
2018-01-17 16:21:01 +00:00
|
|
|
if (loadedAtBottom()) {
|
|
|
|
// Add photos to overview and authors to lastAuthors.
|
|
|
|
addItemsToLists(added);
|
|
|
|
}
|
|
|
|
addToSharedMedia(added);
|
|
|
|
} else {
|
|
|
|
// If no items were added it means we've loaded everything old.
|
2018-02-05 14:37:22 +00:00
|
|
|
_loadedAtTop = true;
|
2018-01-17 16:21:01 +00:00
|
|
|
addEdgesToSharedMedia();
|
2017-12-13 18:10:48 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
checkJoinedMessage();
|
|
|
|
checkLastMessage();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 16:47:46 +00:00
|
|
|
void History::addNewerSlice(const QVector<MTPMessage> &slice) {
|
2015-09-24 16:05:06 +00:00
|
|
|
bool wasEmpty = isEmpty(), wasLoadedAtBottom = loadedAtBottom();
|
|
|
|
|
2014-07-04 11:12:54 +00:00
|
|
|
if (slice.isEmpty()) {
|
2018-02-05 14:37:22 +00:00
|
|
|
_loadedAtBottom = true;
|
2018-01-31 17:10:29 +00:00
|
|
|
if (!lastMessage()) {
|
2017-02-10 14:16:50 +00:00
|
|
|
setLastMessage(lastAvailableMessage());
|
2016-04-11 07:43:40 +00:00
|
|
|
}
|
2014-07-04 11:12:54 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 16:21:01 +00:00
|
|
|
if (const auto added = createItems(slice); !added.empty()) {
|
|
|
|
Assert(!isBuildingFrontBlock());
|
2017-12-13 18:10:48 +00:00
|
|
|
|
2018-01-17 16:21:01 +00:00
|
|
|
for (const auto item : added) {
|
|
|
|
addItemToBlock(item);
|
2015-09-24 16:05:06 +00:00
|
|
|
}
|
2018-01-17 16:21:01 +00:00
|
|
|
|
|
|
|
addToSharedMedia(added);
|
|
|
|
} else {
|
2018-02-05 14:37:22 +00:00
|
|
|
_loadedAtBottom = true;
|
2018-01-17 16:21:01 +00:00
|
|
|
setLastMessage(lastAvailableMessage());
|
|
|
|
addEdgesToSharedMedia();
|
2014-07-04 11:12:54 +00:00
|
|
|
}
|
2015-12-23 14:18:42 +00:00
|
|
|
|
2016-08-14 19:15:45 +00:00
|
|
|
if (!wasLoadedAtBottom) {
|
2017-12-08 18:27:28 +00:00
|
|
|
checkAddAllToUnreadMentions();
|
2014-08-11 09:03:45 +00:00
|
|
|
}
|
2015-09-21 20:57:42 +00:00
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
checkJoinedMessage();
|
|
|
|
checkLastMessage();
|
2016-04-11 07:43:40 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
void History::checkLastMessage() {
|
|
|
|
if (const auto last = lastMessage()) {
|
2018-02-05 14:37:22 +00:00
|
|
|
if (!_loadedAtBottom && last->mainView()) {
|
|
|
|
_loadedAtBottom = true;
|
2017-12-08 18:27:28 +00:00
|
|
|
checkAddAllToUnreadMentions();
|
2016-04-11 07:43:40 +00:00
|
|
|
}
|
2018-02-05 14:37:22 +00:00
|
|
|
} else if (_loadedAtBottom) {
|
2017-02-10 14:16:50 +00:00
|
|
|
setLastMessage(lastAvailableMessage());
|
2016-04-11 07:43:40 +00:00
|
|
|
}
|
2014-07-04 11:12:54 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 16:21:01 +00:00
|
|
|
void History::addItemsToLists(
|
|
|
|
const std::vector<not_null<HistoryItem*>> &items) {
|
|
|
|
std::deque<not_null<UserData*>> *lastAuthors = nullptr;
|
|
|
|
base::flat_set<not_null<PeerData*>> *markupSenders = nullptr;
|
|
|
|
if (peer->isChat()) {
|
|
|
|
lastAuthors = &peer->asChat()->lastAuthors;
|
|
|
|
markupSenders = &peer->asChat()->markupSenders;
|
|
|
|
} else if (peer->isMegagroup()) {
|
|
|
|
// We don't add users to mgInfo->lastParticipants here.
|
|
|
|
// We're scrolling back and we see messages from users that
|
|
|
|
// could be gone from the megagroup already. It is fine for
|
|
|
|
// chat->lastAuthors, because they're used only for field
|
|
|
|
// autocomplete, but this is bad for megagroups, because its
|
|
|
|
// lastParticipants are displayed in Profile as members list.
|
|
|
|
markupSenders = &peer->asChannel()->mgInfo->markupSenders;
|
|
|
|
}
|
|
|
|
for (const auto item : base::reversed(items)) {
|
|
|
|
item->addToUnreadMentions(UnreadMentionType::Existing);
|
|
|
|
if (item->from()->id) {
|
|
|
|
if (lastAuthors) { // chats
|
|
|
|
if (auto user = item->from()->asUser()) {
|
|
|
|
if (!base::contains(*lastAuthors, user)) {
|
|
|
|
lastAuthors->push_back(user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (item->author()->id) {
|
|
|
|
if (markupSenders) { // chats with bots
|
|
|
|
if (!lastKeyboardInited && item->definesReplyKeyboard() && !item->out()) {
|
|
|
|
auto markupFlags = item->replyKeyboardFlags();
|
|
|
|
if (!(markupFlags & MTPDreplyKeyboardMarkup::Flag::f_selective) || item->mentionsMe()) {
|
|
|
|
bool wasKeyboardHide = markupSenders->contains(item->author());
|
|
|
|
if (!wasKeyboardHide) {
|
|
|
|
markupSenders->insert(item->author());
|
|
|
|
}
|
|
|
|
if (!(markupFlags & MTPDreplyKeyboardMarkup_ClientFlag::f_zero)) {
|
|
|
|
if (!lastKeyboardInited) {
|
|
|
|
bool botNotInChat = false;
|
|
|
|
if (peer->isChat()) {
|
|
|
|
botNotInChat = (!peer->canWrite() || !peer->asChat()->participants.empty()) && item->author()->isUser() && !peer->asChat()->participants.contains(item->author()->asUser());
|
|
|
|
} else if (peer->isMegagroup()) {
|
|
|
|
botNotInChat = (!peer->canWrite() || peer->asChannel()->mgInfo->botStatus != 0) && item->author()->isUser() && !peer->asChannel()->mgInfo->bots.contains(item->author()->asUser());
|
|
|
|
}
|
|
|
|
if (wasKeyboardHide || botNotInChat) {
|
|
|
|
clearLastKeyboard();
|
|
|
|
} else {
|
|
|
|
lastKeyboardInited = true;
|
|
|
|
lastKeyboardId = item->id;
|
|
|
|
lastKeyboardFrom = item->author()->id;
|
|
|
|
lastKeyboardUsed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (!lastKeyboardInited && item->definesReplyKeyboard() && !item->out()) { // conversations with bots
|
|
|
|
MTPDreplyKeyboardMarkup::Flags markupFlags = item->replyKeyboardFlags();
|
|
|
|
if (!(markupFlags & MTPDreplyKeyboardMarkup::Flag::f_selective) || item->mentionsMe()) {
|
|
|
|
if (markupFlags & MTPDreplyKeyboardMarkup_ClientFlag::f_zero) {
|
|
|
|
clearLastKeyboard();
|
|
|
|
} else {
|
|
|
|
lastKeyboardInited = true;
|
|
|
|
lastKeyboardId = item->id;
|
|
|
|
lastKeyboardFrom = item->author()->id;
|
|
|
|
lastKeyboardUsed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-12-08 18:27:28 +00:00
|
|
|
void History::checkAddAllToUnreadMentions() {
|
2016-08-14 19:15:45 +00:00
|
|
|
if (!loadedAtBottom()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-10 13:13:33 +00:00
|
|
|
for (const auto &block : blocks) {
|
|
|
|
for (const auto &message : block->messages) {
|
|
|
|
const auto item = message->data();
|
2017-12-21 12:11:33 +00:00
|
|
|
item->addToUnreadMentions(UnreadMentionType::Existing);
|
2016-08-14 19:15:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-17 16:21:01 +00:00
|
|
|
void History::addToSharedMedia(
|
|
|
|
const std::vector<not_null<HistoryItem*>> &items) {
|
2017-09-04 11:40:02 +00:00
|
|
|
std::vector<MsgId> medias[Storage::kSharedMediaTypeCount];
|
2018-01-17 16:21:01 +00:00
|
|
|
for (const auto item : items) {
|
|
|
|
if (const auto types = item->sharedMediaTypes()) {
|
|
|
|
for (auto i = 0; i != Storage::kSharedMediaTypeCount; ++i) {
|
|
|
|
const auto type = static_cast<Storage::SharedMediaType>(i);
|
|
|
|
if (types.test(type)) {
|
|
|
|
if (medias[i].empty()) {
|
|
|
|
medias[i].reserve(items.size());
|
2017-09-04 11:40:02 +00:00
|
|
|
}
|
2018-01-17 16:21:01 +00:00
|
|
|
medias[i].push_back(item->id);
|
2017-09-04 11:40:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-17 16:21:01 +00:00
|
|
|
const auto from = loadedAtTop() ? 0 : minMsgId();
|
|
|
|
const auto till = loadedAtBottom() ? ServerMaxMsgId : maxMsgId();
|
|
|
|
for (auto i = 0; i != Storage::kSharedMediaTypeCount; ++i) {
|
|
|
|
if (!medias[i].empty()) {
|
|
|
|
const auto type = static_cast<Storage::SharedMediaType>(i);
|
|
|
|
Auth().storage().add(Storage::SharedMediaAddSlice(
|
|
|
|
peer->id,
|
|
|
|
type,
|
|
|
|
std::move(medias[i]),
|
|
|
|
{ from, till }));
|
|
|
|
}
|
|
|
|
}
|
2017-09-04 11:40:02 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 15:23:34 +00:00
|
|
|
int History::countUnread(MsgId upTo) {
|
|
|
|
int result = 0;
|
2016-03-15 10:37:56 +00:00
|
|
|
for (auto i = blocks.cend(), e = blocks.cbegin(); i != e;) {
|
2015-09-06 10:17:09 +00:00
|
|
|
--i;
|
2018-01-10 13:13:33 +00:00
|
|
|
const auto &messages = (*i)->messages;
|
|
|
|
for (auto j = messages.cend(), en = messages.cbegin(); j != en;) {
|
2015-09-06 10:17:09 +00:00
|
|
|
--j;
|
2018-01-10 13:13:33 +00:00
|
|
|
const auto item = (*j)->data();
|
|
|
|
if (item->id > 0 && item->id <= upTo) {
|
2015-09-06 10:17:09 +00:00
|
|
|
break;
|
2018-01-10 13:13:33 +00:00
|
|
|
} else if (!item->out() && item->unread() && item->id > upTo) {
|
2015-09-06 10:17:09 +00:00
|
|
|
++result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-01-21 19:21:08 +00:00
|
|
|
void History::calculateFirstUnreadMessage() {
|
2018-01-31 17:10:29 +00:00
|
|
|
if (_firstUnreadView || !_inboxReadBefore) {
|
2018-01-21 19:21:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-09-19 09:13:21 +00:00
|
|
|
|
2016-03-15 10:37:56 +00:00
|
|
|
for (auto i = blocks.cend(); i != blocks.cbegin();) {
|
2015-09-19 09:13:21 +00:00
|
|
|
--i;
|
2018-01-10 13:13:33 +00:00
|
|
|
const auto &messages = (*i)->messages;
|
|
|
|
for (auto j = messages.cend(); j != messages.cbegin();) {
|
2015-09-19 09:13:21 +00:00
|
|
|
--j;
|
2018-01-21 19:21:08 +00:00
|
|
|
const auto view = j->get();
|
|
|
|
const auto item = view->data();
|
|
|
|
if (!IsServerMsgId(item->id)) {
|
|
|
|
continue;
|
|
|
|
} else if (!item->out() || !_firstUnreadView) {
|
2018-01-31 17:10:29 +00:00
|
|
|
if (item->id >= *_inboxReadBefore) {
|
2018-01-21 19:21:08 +00:00
|
|
|
_firstUnreadView = view;
|
2015-09-19 09:13:21 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
MsgId History::readInbox() {
|
|
|
|
const auto upTo = msgIdForRead();
|
2018-02-12 11:10:40 +00:00
|
|
|
changeUnreadCount(-unreadCount());
|
2018-01-31 17:10:29 +00:00
|
|
|
if (upTo) {
|
|
|
|
inboxRead(upTo);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2018-01-31 17:10:29 +00:00
|
|
|
return upTo;
|
|
|
|
}
|
2015-09-06 10:17:09 +00:00
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
void History::inboxRead(MsgId upTo) {
|
2018-02-12 11:10:40 +00:00
|
|
|
if (const auto nowUnreadCount = unreadCount()) {
|
2018-01-31 17:10:29 +00:00
|
|
|
if (loadedAtBottom()) {
|
|
|
|
App::main()->historyToDown(this);
|
|
|
|
}
|
2018-02-12 11:10:40 +00:00
|
|
|
changeUnreadCount(countUnread(upTo) - nowUnreadCount);
|
2018-01-31 17:10:29 +00:00
|
|
|
}
|
|
|
|
setInboxReadTill(upTo);
|
2016-02-28 11:58:30 +00:00
|
|
|
updateChatListEntry();
|
|
|
|
if (peer->migrateTo()) {
|
2016-09-25 19:04:02 +00:00
|
|
|
if (auto migrateTo = App::historyLoaded(peer->migrateTo()->id)) {
|
|
|
|
migrateTo->updateChatListEntry();
|
2015-11-13 15:14:33 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-03 10:09:09 +00:00
|
|
|
|
2018-01-21 19:21:08 +00:00
|
|
|
_firstUnreadView = nullptr;
|
2017-08-04 14:54:32 +00:00
|
|
|
Auth().notifications().clearFromHistory(this);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
void History::inboxRead(not_null<const HistoryItem*> wasRead) {
|
|
|
|
if (IsServerMsgId(wasRead->id)) {
|
|
|
|
inboxRead(wasRead->id);
|
|
|
|
}
|
2015-03-13 13:01:25 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
void History::outboxRead(MsgId upTo) {
|
|
|
|
setOutboxReadTill(upTo);
|
|
|
|
if (const auto last = lastMessage()) {
|
|
|
|
if (last->out() && IsServerMsgId(last->id) && last->id <= upTo) {
|
|
|
|
if (const auto main = App::main()) {
|
|
|
|
main->repaintDialogRow(this, last->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updateChatListEntry();
|
|
|
|
}
|
2015-09-06 10:17:09 +00:00
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
void History::outboxRead(not_null<const HistoryItem*> wasRead) {
|
|
|
|
if (IsServerMsgId(wasRead->id)) {
|
|
|
|
outboxRead(wasRead->id);
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
MsgId History::loadAroundId() const {
|
|
|
|
if (_unreadCount && *_unreadCount > 0 && _inboxReadBefore) {
|
|
|
|
return *_inboxReadBefore;
|
|
|
|
}
|
|
|
|
return MsgId(0);
|
2015-03-13 13:01:25 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 14:16:50 +00:00
|
|
|
HistoryItem *History::lastAvailableMessage() const {
|
2018-01-10 13:13:33 +00:00
|
|
|
return isEmpty() ? nullptr : blocks.back()->messages.back()->data().get();
|
2015-09-20 08:55:41 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
int History::unreadCount() const {
|
|
|
|
return _unreadCount ? *_unreadCount : 0;
|
|
|
|
}
|
|
|
|
|
2018-06-26 18:03:45 +00:00
|
|
|
int History::historiesUnreadCount() const {
|
|
|
|
const auto result = unreadCount();
|
|
|
|
return (!result && unreadMark()) ? 1 : result;
|
|
|
|
}
|
|
|
|
|
2018-02-03 19:52:35 +00:00
|
|
|
bool History::unreadCountKnown() const {
|
|
|
|
return !!_unreadCount;
|
|
|
|
}
|
|
|
|
|
2016-04-14 19:24:42 +00:00
|
|
|
void History::setUnreadCount(int newUnreadCount) {
|
2018-01-31 17:10:29 +00:00
|
|
|
if (!_unreadCount || *_unreadCount != newUnreadCount) {
|
2018-12-04 10:32:06 +00:00
|
|
|
const auto wasUnread = _unreadMark || unreadCount();
|
2018-01-31 17:10:29 +00:00
|
|
|
const auto unreadCountDelta = _unreadCount | [&](int count) {
|
|
|
|
return newUnreadCount - count;
|
|
|
|
};
|
2015-09-07 07:52:37 +00:00
|
|
|
if (newUnreadCount == 1) {
|
2018-01-21 19:21:08 +00:00
|
|
|
if (loadedAtBottom()) {
|
|
|
|
_firstUnreadView = !isEmpty()
|
|
|
|
? blocks.back()->messages.back().get()
|
|
|
|
: nullptr;
|
|
|
|
}
|
2018-01-31 17:10:29 +00:00
|
|
|
if (const auto last = msgIdForRead()) {
|
|
|
|
setInboxReadTill(last - 1);
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
} else if (!newUnreadCount) {
|
2018-01-21 19:21:08 +00:00
|
|
|
_firstUnreadView = nullptr;
|
2018-01-31 17:10:29 +00:00
|
|
|
if (const auto last = msgIdForRead()) {
|
|
|
|
setInboxReadTill(last);
|
|
|
|
}
|
2016-06-03 12:45:33 +00:00
|
|
|
} else {
|
2018-01-21 19:21:08 +00:00
|
|
|
if (!_firstUnreadView && !_unreadBarView && loadedAtBottom()) {
|
|
|
|
calculateFirstUnreadMessage();
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2018-06-26 18:03:45 +00:00
|
|
|
const auto unreadMarkDelta = [&] {
|
|
|
|
if (_unreadMark) {
|
|
|
|
const auto was = _unreadCount && (*_unreadCount > 0);
|
|
|
|
const auto now = (newUnreadCount > 0);
|
|
|
|
if (was != now) {
|
|
|
|
return was ? 1 : -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}();
|
2016-04-11 10:59:01 +00:00
|
|
|
_unreadCount = newUnreadCount;
|
2018-01-31 17:10:29 +00:00
|
|
|
|
2018-01-21 19:21:08 +00:00
|
|
|
if (_unreadBarView) {
|
|
|
|
const auto count = chatListUnreadCount();
|
2016-03-18 19:05:08 +00:00
|
|
|
if (count > 0) {
|
2018-01-21 19:21:08 +00:00
|
|
|
_unreadBarView->setUnreadBarCount(count);
|
2016-03-18 19:05:08 +00:00
|
|
|
} else {
|
2018-01-21 19:21:08 +00:00
|
|
|
_unreadBarView->setUnreadBarFreezed();
|
2016-03-18 19:05:08 +00:00
|
|
|
}
|
2015-11-13 15:14:33 +00:00
|
|
|
}
|
2018-01-31 17:10:29 +00:00
|
|
|
|
|
|
|
if (inChatList(Dialogs::Mode::All)) {
|
2018-12-04 10:32:06 +00:00
|
|
|
const auto delta = unreadMarkDelta + (unreadCountDelta
|
2018-06-26 18:03:45 +00:00
|
|
|
? *unreadCountDelta
|
2018-12-04 10:32:06 +00:00
|
|
|
: newUnreadCount);
|
|
|
|
App::histories().unreadIncrement(delta, mute());
|
|
|
|
|
|
|
|
const auto nowUnread = (*_unreadCount > 0) || _unreadMark;
|
|
|
|
const auto entriesDelta = (wasUnread && !nowUnread)
|
|
|
|
? -1
|
|
|
|
: (nowUnread && !wasUnread)
|
|
|
|
? 1
|
|
|
|
: 0;
|
|
|
|
App::histories().unreadEntriesChanged(
|
|
|
|
entriesDelta,
|
|
|
|
mute() ? entriesDelta : 0);
|
2018-01-31 17:10:29 +00:00
|
|
|
}
|
2018-06-26 18:03:45 +00:00
|
|
|
Notify::peerUpdatedDelayed(
|
|
|
|
peer,
|
|
|
|
Notify::PeerUpdate::Flag::UnreadViewChanged);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void History::setUnreadMark(bool unread) {
|
2018-10-08 13:41:14 +00:00
|
|
|
if (clearUnreadOnClientSide()) {
|
|
|
|
unread = false;
|
|
|
|
}
|
2018-06-26 18:03:45 +00:00
|
|
|
if (_unreadMark != unread) {
|
|
|
|
_unreadMark = unread;
|
|
|
|
if (!_unreadCount || !*_unreadCount) {
|
|
|
|
if (inChatList(Dialogs::Mode::All)) {
|
|
|
|
const auto delta = _unreadMark ? 1 : -1;
|
|
|
|
App::histories().unreadIncrement(delta, mute());
|
2018-12-04 10:32:06 +00:00
|
|
|
App::histories().unreadEntriesChanged(
|
|
|
|
delta,
|
|
|
|
mute() ? delta : 0);
|
2018-06-26 18:03:45 +00:00
|
|
|
|
|
|
|
updateChatListEntry();
|
|
|
|
}
|
2018-01-21 19:21:08 +00:00
|
|
|
}
|
2018-06-26 18:03:45 +00:00
|
|
|
Notify::peerUpdatedDelayed(
|
|
|
|
peer,
|
|
|
|
Notify::PeerUpdate::Flag::UnreadViewChanged);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 18:03:45 +00:00
|
|
|
bool History::unreadMark() const {
|
|
|
|
return _unreadMark;
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:10:40 +00:00
|
|
|
void History::changeUnreadCount(int delta) {
|
|
|
|
if (_unreadCount) {
|
|
|
|
setUnreadCount(std::max(*_unreadCount + delta, 0));
|
|
|
|
}
|
|
|
|
if (const auto channel = peer->asChannel()) {
|
|
|
|
if (const auto feed = channel->feed()) {
|
|
|
|
const auto mutedCountDelta = mute() ? delta : 0;
|
|
|
|
feed->unreadCountChanged(delta, mutedCountDelta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
bool History::mute() const {
|
|
|
|
return _mute;
|
|
|
|
}
|
|
|
|
|
2017-12-04 17:46:03 +00:00
|
|
|
bool History::changeMute(bool newMute) {
|
|
|
|
if (_mute == newMute) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_mute = newMute;
|
2018-01-31 17:10:29 +00:00
|
|
|
|
|
|
|
const auto feed = peer->isChannel()
|
|
|
|
? peer->asChannel()->feed()
|
|
|
|
: nullptr;
|
2018-02-12 11:10:40 +00:00
|
|
|
if (feed) {
|
|
|
|
if (_unreadCount) {
|
|
|
|
if (*_unreadCount) {
|
|
|
|
const auto unreadCountDelta = 0;
|
|
|
|
const auto mutedCountDelta = _mute ? *_unreadCount : -*_unreadCount;
|
|
|
|
feed->unreadCountChanged(unreadCountDelta, mutedCountDelta);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Auth().api().requestDialogEntry(this);
|
|
|
|
Auth().api().requestDialogEntry(feed);
|
|
|
|
}
|
2018-01-31 17:10:29 +00:00
|
|
|
}
|
2017-12-04 17:46:03 +00:00
|
|
|
if (inChatList(Dialogs::Mode::All)) {
|
2018-06-26 18:03:45 +00:00
|
|
|
if (const auto count = historiesUnreadCount()) {
|
|
|
|
App::histories().unreadMuteChanged(count, _mute);
|
2018-12-04 10:32:06 +00:00
|
|
|
|
|
|
|
const auto entriesWithUnreadDelta = 0;
|
|
|
|
const auto mutedEntriesWithUnreadDelta = _mute ? 1 : -1;
|
|
|
|
App::histories().unreadEntriesChanged(
|
|
|
|
entriesWithUnreadDelta,
|
|
|
|
mutedEntriesWithUnreadDelta);
|
|
|
|
|
2017-12-04 17:46:03 +00:00
|
|
|
Notify::unreadCounterUpdated();
|
2016-02-28 11:58:30 +00:00
|
|
|
}
|
2017-12-04 17:46:03 +00:00
|
|
|
Notify::historyMuteUpdated(this);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2017-12-04 17:46:03 +00:00
|
|
|
updateChatListEntry();
|
|
|
|
Notify::peerUpdatedDelayed(
|
|
|
|
peer,
|
|
|
|
Notify::PeerUpdate::Flag::NotificationsEnabled);
|
|
|
|
return true;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 19:21:08 +00:00
|
|
|
void History::getNextFirstUnreadMessage() {
|
|
|
|
Expects(_firstUnreadView != nullptr);
|
|
|
|
|
|
|
|
const auto block = _firstUnreadView->block();
|
|
|
|
const auto index = _firstUnreadView->indexInBlock();
|
|
|
|
const auto setFromMessage = [&](const auto &view) {
|
|
|
|
if (IsServerMsgId(view->data()->id)) {
|
|
|
|
_firstUnreadView = view.get();
|
2018-01-10 13:13:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2018-01-21 19:21:08 +00:00
|
|
|
if (index >= 0) {
|
|
|
|
const auto count = int(block->messages.size());
|
|
|
|
for (auto i = index + 1; i != count; ++i) {
|
2018-01-10 13:13:33 +00:00
|
|
|
const auto &message = block->messages[i];
|
2018-01-23 16:51:12 +00:00
|
|
|
if (setFromMessage(message)) {
|
2014-05-30 08:53:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-21 19:21:08 +00:00
|
|
|
const auto count = int(blocks.size());
|
|
|
|
for (auto j = block->indexInHistory() + 1; j != count; ++j) {
|
|
|
|
for (const auto &message : blocks[j]->messages) {
|
2018-01-10 13:13:33 +00:00
|
|
|
if (setFromMessage(message)) {
|
2016-03-21 18:40:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-21 19:21:08 +00:00
|
|
|
_firstUnreadView = nullptr;
|
2016-03-21 18:40:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-22 16:42:25 +00:00
|
|
|
std::shared_ptr<AdminLog::LocalIdManager> History::adminLogIdManager() {
|
|
|
|
if (const auto strong = _adminLogIdManager.lock()) {
|
|
|
|
return strong;
|
|
|
|
}
|
|
|
|
auto result = std::make_shared<AdminLog::LocalIdManager>();
|
|
|
|
_adminLogIdManager = result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-07-31 14:34:14 +00:00
|
|
|
TimeId History::adjustChatListTimeId() const {
|
|
|
|
const auto result = chatsListTimeId();
|
2018-01-05 15:57:18 +00:00
|
|
|
if (const auto draft = cloudDraft()) {
|
2018-11-15 15:36:04 +00:00
|
|
|
if (!Data::draftIsNull(draft) && !Auth().supportMode()) {
|
2018-07-31 14:34:14 +00:00
|
|
|
return std::max(result, draft->date);
|
2018-01-05 15:57:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-03-21 18:40:00 +00:00
|
|
|
void History::countScrollState(int top) {
|
|
|
|
countScrollTopItem(top);
|
|
|
|
if (scrollTopItem) {
|
2018-01-11 13:07:29 +00:00
|
|
|
scrollTopOffset = (top - scrollTopItem->block()->y() - scrollTopItem->y());
|
2016-03-21 18:40:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void History::countScrollTopItem(int top) {
|
|
|
|
if (isEmpty()) {
|
|
|
|
forgetScrollState();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-10 13:13:33 +00:00
|
|
|
auto itemIndex = 0;
|
|
|
|
auto blockIndex = 0;
|
|
|
|
auto itemTop = 0;
|
|
|
|
if (scrollTopItem) {
|
2018-01-11 13:07:29 +00:00
|
|
|
itemIndex = scrollTopItem->indexInBlock();
|
|
|
|
blockIndex = scrollTopItem->block()->indexInHistory();
|
2017-05-12 13:53:08 +00:00
|
|
|
itemTop = blocks[blockIndex]->y() + scrollTopItem->y();
|
2016-03-21 18:40:00 +00:00
|
|
|
}
|
|
|
|
if (itemTop > top) {
|
|
|
|
// go backward through history while we don't find an item that starts above
|
|
|
|
do {
|
2018-01-10 13:13:33 +00:00
|
|
|
const auto &block = blocks[blockIndex];
|
2016-03-21 18:40:00 +00:00
|
|
|
for (--itemIndex; itemIndex >= 0; --itemIndex) {
|
2018-01-10 13:13:33 +00:00
|
|
|
const auto view = block->messages[itemIndex].get();
|
|
|
|
itemTop = block->y() + view->y();
|
2016-03-21 18:40:00 +00:00
|
|
|
if (itemTop <= top) {
|
2018-01-10 13:13:33 +00:00
|
|
|
scrollTopItem = view;
|
2014-05-30 08:53:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-03-21 18:40:00 +00:00
|
|
|
if (--blockIndex >= 0) {
|
2018-01-10 13:13:33 +00:00
|
|
|
itemIndex = blocks[blockIndex]->messages.size();
|
2016-03-21 18:40:00 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (true);
|
|
|
|
|
2018-01-10 13:13:33 +00:00
|
|
|
scrollTopItem = blocks.front()->messages.front().get();
|
2016-03-21 18:40:00 +00:00
|
|
|
} else {
|
|
|
|
// go forward through history while we don't find the last item that starts above
|
2018-01-10 13:13:33 +00:00
|
|
|
for (auto blocksCount = int(blocks.size()); blockIndex < blocksCount; ++blockIndex) {
|
|
|
|
const auto &block = blocks[blockIndex];
|
|
|
|
for (auto itemsCount = int(block->messages.size()); itemIndex < itemsCount; ++itemIndex) {
|
|
|
|
itemTop = block->y() + block->messages[itemIndex]->y();
|
2016-03-21 18:40:00 +00:00
|
|
|
if (itemTop > top) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(itemIndex > 0 || blockIndex > 0);
|
2016-03-21 18:40:00 +00:00
|
|
|
if (itemIndex > 0) {
|
2018-01-10 13:13:33 +00:00
|
|
|
scrollTopItem = block->messages[itemIndex - 1].get();
|
2016-03-21 18:40:00 +00:00
|
|
|
} else {
|
2018-01-10 13:13:33 +00:00
|
|
|
scrollTopItem = blocks[blockIndex - 1]->messages.back().get();
|
2016-03-21 18:40:00 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
itemIndex = 0;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2018-01-10 13:13:33 +00:00
|
|
|
scrollTopItem = blocks.back()->messages.back().get();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-03-21 18:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void History::getNextScrollTopItem(HistoryBlock *block, int32 i) {
|
|
|
|
++i;
|
2018-01-10 13:13:33 +00:00
|
|
|
if (i > 0 && i < block->messages.size()) {
|
|
|
|
scrollTopItem = block->messages[i].get();
|
2016-03-21 18:40:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
int j = block->indexInHistory() + 1;
|
|
|
|
if (j > 0 && j < blocks.size()) {
|
2018-01-10 13:13:33 +00:00
|
|
|
scrollTopItem = blocks[j]->messages.front().get();
|
2016-03-21 18:40:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
scrollTopItem = nullptr;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void History::addUnreadBar() {
|
2018-01-21 19:21:08 +00:00
|
|
|
if (_unreadBarView || !_firstUnreadView || !unreadCount()) {
|
2018-01-11 13:07:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-01-21 19:21:08 +00:00
|
|
|
if (const auto count = chatListUnreadCount()) {
|
|
|
|
_unreadBarView = _firstUnreadView;
|
|
|
|
_unreadBarView->setUnreadBarCount(count);
|
2015-11-13 15:14:33 +00:00
|
|
|
}
|
2016-03-18 19:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void History::destroyUnreadBar() {
|
2018-01-21 19:21:08 +00:00
|
|
|
if (const auto view = base::take(_unreadBarView)) {
|
|
|
|
view->destroyUnreadBar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool History::hasNotFreezedUnreadBar() const {
|
|
|
|
if (_firstUnreadView) {
|
|
|
|
if (const auto view = _unreadBarView) {
|
|
|
|
if (const auto bar = view->Get<HistoryView::UnreadBar>()) {
|
|
|
|
return !bar->freezed;
|
|
|
|
}
|
|
|
|
}
|
2015-09-19 09:13:21 +00:00
|
|
|
}
|
2018-01-21 19:21:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void History::unsetFirstUnreadMessage() {
|
|
|
|
_firstUnreadView = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
HistoryView::Element *History::unreadBar() const {
|
|
|
|
return _unreadBarView;
|
|
|
|
}
|
|
|
|
|
|
|
|
HistoryView::Element *History::firstUnreadMessage() const {
|
|
|
|
return _firstUnreadView;
|
2015-09-19 09:13:21 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-01-10 13:13:33 +00:00
|
|
|
not_null<HistoryItem*> History::addNewInTheMiddle(
|
2018-01-14 16:02:25 +00:00
|
|
|
not_null<HistoryItem*> item,
|
2018-01-10 13:13:33 +00:00
|
|
|
int blockIndex,
|
|
|
|
int itemIndex) {
|
2017-05-12 13:53:08 +00:00
|
|
|
Expects(blockIndex >= 0);
|
|
|
|
Expects(blockIndex < blocks.size());
|
|
|
|
Expects(itemIndex >= 0);
|
2018-01-10 13:13:33 +00:00
|
|
|
Expects(itemIndex <= blocks[blockIndex]->messages.size());
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-01-10 13:13:33 +00:00
|
|
|
const auto &block = blocks[blockIndex];
|
2016-03-18 19:05:08 +00:00
|
|
|
|
2018-01-11 13:07:29 +00:00
|
|
|
const auto it = block->messages.insert(
|
2018-01-10 13:13:33 +00:00
|
|
|
block->messages.begin() + itemIndex,
|
2018-01-14 16:02:25 +00:00
|
|
|
item->createView(
|
2018-01-17 16:21:01 +00:00
|
|
|
HistoryInner::ElementDelegate()));
|
2018-01-11 13:07:29 +00:00
|
|
|
(*it)->attachToBlock(block.get(), itemIndex);
|
2018-01-10 13:13:33 +00:00
|
|
|
if (itemIndex + 1 < block->messages.size()) {
|
|
|
|
for (auto i = itemIndex + 1, l = int(block->messages.size()); i != l; ++i) {
|
2018-01-11 13:07:29 +00:00
|
|
|
block->messages[i]->setIndexInBlock(i);
|
2016-11-18 16:27:47 +00:00
|
|
|
}
|
2018-01-13 12:45:11 +00:00
|
|
|
block->messages[itemIndex + 1]->previousInBlocksChanged();
|
2018-01-10 13:13:33 +00:00
|
|
|
} else if (blockIndex + 1 < blocks.size() && !blocks[blockIndex + 1]->messages.empty()) {
|
2018-01-13 12:45:11 +00:00
|
|
|
blocks[blockIndex + 1]->messages.front()->previousInBlocksChanged();
|
2016-11-18 16:27:47 +00:00
|
|
|
} else {
|
2018-01-19 17:10:58 +00:00
|
|
|
(*it)->nextInBlocksRemoved();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-03-21 18:40:00 +00:00
|
|
|
|
2018-01-14 16:02:25 +00:00
|
|
|
return item;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2017-12-16 15:25:21 +00:00
|
|
|
|
2018-06-26 18:03:45 +00:00
|
|
|
History *History::migrateSibling() const {
|
2018-01-21 19:21:08 +00:00
|
|
|
const auto addFromId = [&] {
|
|
|
|
if (const auto from = peer->migrateFrom()) {
|
|
|
|
return from->id;
|
|
|
|
} else if (const auto to = peer->migrateTo()) {
|
|
|
|
return to->id;
|
2018-01-05 15:57:18 +00:00
|
|
|
}
|
2018-01-21 19:21:08 +00:00
|
|
|
return PeerId(0);
|
|
|
|
}();
|
2018-06-26 18:03:45 +00:00
|
|
|
return App::historyLoaded(addFromId);
|
|
|
|
}
|
|
|
|
|
|
|
|
int History::chatListUnreadCount() const {
|
|
|
|
const auto result = unreadCount();
|
|
|
|
if (const auto migrated = migrateSibling()) {
|
2018-01-21 19:21:08 +00:00
|
|
|
return result + migrated->unreadCount();
|
2018-01-05 15:57:18 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-06-26 18:03:45 +00:00
|
|
|
bool History::chatListUnreadMark() const {
|
|
|
|
if (unreadMark()) {
|
|
|
|
return true;
|
|
|
|
} else if (const auto migrated = migrateSibling()) {
|
|
|
|
return migrated->unreadMark();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-05 15:57:18 +00:00
|
|
|
bool History::chatListMutedBadge() const {
|
|
|
|
return mute();
|
|
|
|
}
|
|
|
|
|
|
|
|
HistoryItem *History::chatsListItem() const {
|
2018-01-31 17:10:29 +00:00
|
|
|
return lastMessage();
|
2018-01-05 15:57:18 +00:00
|
|
|
}
|
|
|
|
|
2018-01-22 09:33:09 +00:00
|
|
|
const QString &History::chatsListName() const {
|
|
|
|
return peer->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const base::flat_set<QString> &History::chatsListNameWords() const {
|
|
|
|
return peer->nameWords();
|
|
|
|
}
|
|
|
|
|
|
|
|
const base::flat_set<QChar> &History::chatsListFirstLetters() const {
|
|
|
|
return peer->nameFirstLetters();
|
|
|
|
}
|
|
|
|
|
2018-01-05 15:57:18 +00:00
|
|
|
void History::loadUserpic() {
|
|
|
|
peer->loadUserpic();
|
|
|
|
}
|
|
|
|
|
|
|
|
void History::paintUserpic(
|
|
|
|
Painter &p,
|
|
|
|
int x,
|
|
|
|
int y,
|
|
|
|
int size) const {
|
|
|
|
peer->paintUserpic(p, x, y, size);
|
|
|
|
}
|
2017-12-13 18:10:48 +00:00
|
|
|
|
2016-03-31 10:37:58 +00:00
|
|
|
void History::startBuildingFrontBlock(int expectedItemsCount) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(!isBuildingFrontBlock());
|
|
|
|
Assert(expectedItemsCount > 0);
|
2016-03-31 10:37:58 +00:00
|
|
|
|
2016-03-31 11:18:21 +00:00
|
|
|
_buildingFrontBlock.reset(new BuildingBlock());
|
|
|
|
_buildingFrontBlock->expectedItemsCount = expectedItemsCount;
|
2016-03-31 10:37:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 10:51:35 +00:00
|
|
|
void History::finishBuildingFrontBlock() {
|
|
|
|
Expects(isBuildingFrontBlock());
|
2016-03-31 10:37:58 +00:00
|
|
|
|
|
|
|
// Some checks if there was some message history already
|
2018-01-30 10:51:35 +00:00
|
|
|
if (const auto block = base::take(_buildingFrontBlock)->block) {
|
2016-11-18 16:27:47 +00:00
|
|
|
if (blocks.size() > 1) {
|
2018-01-13 12:45:11 +00:00
|
|
|
// ... item, item, item, last ], [ first, item, item ...
|
|
|
|
const auto last = block->messages.back().get();
|
|
|
|
const auto first = blocks[1]->messages.front().get();
|
2016-11-18 16:27:47 +00:00
|
|
|
|
|
|
|
// we've added a new front block, so previous item for
|
|
|
|
// the old first item of a first block was changed
|
2018-01-13 12:45:11 +00:00
|
|
|
first->previousInBlocksChanged();
|
2016-11-18 16:27:47 +00:00
|
|
|
} else {
|
2018-01-19 17:10:58 +00:00
|
|
|
block->messages.back()->nextInBlocksRemoved();
|
2016-11-18 16:27:47 +00:00
|
|
|
}
|
2016-03-22 09:51:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-04 11:12:54 +00:00
|
|
|
void History::clearNotifications() {
|
|
|
|
notifies.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool History::loadedAtBottom() const {
|
2018-02-05 14:37:22 +00:00
|
|
|
return _loadedAtBottom;
|
2014-07-04 11:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool History::loadedAtTop() const {
|
2018-02-05 14:37:22 +00:00
|
|
|
return _loadedAtTop;
|
2014-07-04 11:12:54 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 16:47:46 +00:00
|
|
|
bool History::isReadyFor(MsgId msgId) {
|
2018-01-31 17:10:29 +00:00
|
|
|
if (msgId < 0 && -msgId < ServerMaxMsgId && peer->migrateFrom()) {
|
|
|
|
// Old group history.
|
2016-05-27 16:47:46 +00:00
|
|
|
return App::history(peer->migrateFrom()->id)->isReadyFor(-msgId);
|
2015-11-13 15:14:33 +00:00
|
|
|
}
|
|
|
|
|
2015-07-17 19:17:37 +00:00
|
|
|
if (msgId == ShowAtTheEndMsgId) {
|
|
|
|
return loadedAtBottom();
|
2015-09-19 09:13:21 +00:00
|
|
|
}
|
|
|
|
if (msgId == ShowAtUnreadMsgId) {
|
2018-01-21 19:21:08 +00:00
|
|
|
if (const auto migratePeer = peer->migrateFrom()) {
|
|
|
|
if (const auto migrated = App::historyLoaded(migratePeer)) {
|
|
|
|
if (migrated->unreadCount()) {
|
|
|
|
return migrated->isReadyFor(msgId);
|
2015-11-13 15:14:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-31 17:10:29 +00:00
|
|
|
if (unreadCount() && _inboxReadBefore) {
|
2015-09-19 09:13:21 +00:00
|
|
|
if (!isEmpty()) {
|
2018-01-31 17:10:29 +00:00
|
|
|
return (loadedAtTop() || minMsgId() <= *_inboxReadBefore)
|
|
|
|
&& (loadedAtBottom() || maxMsgId() >= *_inboxReadBefore);
|
2015-09-08 13:34:22 +00:00
|
|
|
}
|
2015-09-19 09:13:21 +00:00
|
|
|
return false;
|
2015-09-08 13:34:22 +00:00
|
|
|
}
|
2015-09-19 09:13:21 +00:00
|
|
|
return loadedAtBottom();
|
2015-07-17 19:17:37 +00:00
|
|
|
}
|
2015-09-19 09:13:21 +00:00
|
|
|
HistoryItem *item = App::histItemById(channelId(), msgId);
|
2018-01-11 13:07:29 +00:00
|
|
|
return item && (item->history() == this) && item->mainView();
|
2015-07-17 19:17:37 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 16:47:46 +00:00
|
|
|
void History::getReadyFor(MsgId msgId) {
|
2015-11-13 15:14:33 +00:00
|
|
|
if (msgId < 0 && -msgId < ServerMaxMsgId && peer->migrateFrom()) {
|
2018-01-31 17:10:29 +00:00
|
|
|
const auto migrated = App::history(peer->migrateFrom()->id);
|
|
|
|
migrated->getReadyFor(-msgId);
|
|
|
|
if (migrated->isEmpty()) {
|
|
|
|
unloadBlocks();
|
2015-11-13 15:14:33 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2018-01-21 19:21:08 +00:00
|
|
|
if (msgId == ShowAtUnreadMsgId) {
|
|
|
|
if (const auto migratePeer = peer->migrateFrom()) {
|
|
|
|
if (const auto migrated = App::historyLoaded(migratePeer)) {
|
|
|
|
if (migrated->unreadCount()) {
|
2018-01-31 17:10:29 +00:00
|
|
|
unloadBlocks();
|
2018-01-21 19:21:08 +00:00
|
|
|
migrated->getReadyFor(msgId);
|
|
|
|
return;
|
|
|
|
}
|
2015-11-13 15:14:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-27 16:47:46 +00:00
|
|
|
if (!isReadyFor(msgId)) {
|
2018-01-31 17:10:29 +00:00
|
|
|
unloadBlocks();
|
2018-09-20 07:02:02 +00:00
|
|
|
if (const auto migratePeer = peer->migrateFrom()) {
|
|
|
|
if (const auto migrated = App::historyLoaded(migratePeer)) {
|
|
|
|
migrated->unloadBlocks();
|
|
|
|
}
|
|
|
|
}
|
2016-03-21 18:40:00 +00:00
|
|
|
if (msgId == ShowAtTheEndMsgId) {
|
2018-02-05 14:37:22 +00:00
|
|
|
_loadedAtBottom = true;
|
2016-03-21 18:40:00 +00:00
|
|
|
}
|
2015-07-17 19:17:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-13 08:41:27 +00:00
|
|
|
void History::setNotLoadedAtBottom() {
|
2018-02-05 14:37:22 +00:00
|
|
|
_loadedAtBottom = false;
|
2018-01-04 19:54:35 +00:00
|
|
|
|
2018-02-05 14:37:22 +00:00
|
|
|
Auth().storage().invalidate(
|
|
|
|
Storage::SharedMediaInvalidateBottom(peer->id));
|
|
|
|
if (const auto channel = peer->asChannel()) {
|
|
|
|
if (const auto feed = channel->feed()) {
|
|
|
|
Auth().storage().invalidate(
|
|
|
|
Storage::FeedMessagesInvalidateBottom(
|
|
|
|
feed->id()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-04 19:54:35 +00:00
|
|
|
|
2018-02-05 14:37:22 +00:00
|
|
|
void History::markFullyLoaded() {
|
|
|
|
_loadedAtTop = _loadedAtBottom = true;
|
|
|
|
if (isEmpty()) {
|
|
|
|
Auth().storage().remove(Storage::SharedMediaRemoveAll(peer->id));
|
|
|
|
if (const auto channel = peer->asChannel()) {
|
|
|
|
if (const auto feed = channel->feed()) {
|
|
|
|
Auth().storage().remove(Storage::FeedMessagesRemoveAll(
|
|
|
|
feed->id(),
|
|
|
|
channel->bareId()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-06 10:17:09 +00:00
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
void History::setLastMessage(HistoryItem *item) {
|
|
|
|
if (item) {
|
2018-09-27 20:31:48 +00:00
|
|
|
if (_lastMessage) {
|
|
|
|
if (!*_lastMessage) {
|
|
|
|
Local::removeSavedPeer(peer);
|
|
|
|
} else if (!IsServerMsgId((*_lastMessage)->id)
|
|
|
|
&& (*_lastMessage)->date() > item->date()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-05 15:57:18 +00:00
|
|
|
}
|
2018-01-31 17:10:29 +00:00
|
|
|
_lastMessage = item;
|
2018-01-05 15:57:18 +00:00
|
|
|
if (const auto feed = peer->feed()) {
|
2018-01-31 17:10:29 +00:00
|
|
|
feed->updateLastMessage(item);
|
2018-01-05 15:57:18 +00:00
|
|
|
}
|
2018-07-31 14:34:14 +00:00
|
|
|
setChatsListTimeId(item->date());
|
2018-01-31 17:10:29 +00:00
|
|
|
} else if (!_lastMessage || *_lastMessage) {
|
|
|
|
_lastMessage = nullptr;
|
2016-06-21 15:58:07 +00:00
|
|
|
updateChatListEntry();
|
2015-08-07 12:11:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
HistoryItem *History::lastMessage() const {
|
|
|
|
return _lastMessage ? (*_lastMessage) : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool History::lastMessageKnown() const {
|
|
|
|
return !!_lastMessage;
|
|
|
|
}
|
|
|
|
|
2018-01-29 17:13:24 +00:00
|
|
|
void History::updateChatListExistence() {
|
|
|
|
Entry::updateChatListExistence();
|
2018-02-03 19:52:35 +00:00
|
|
|
if (!lastMessageKnown() || !unreadCountKnown()) {
|
2018-01-29 17:13:24 +00:00
|
|
|
if (const auto channel = peer->asChannel()) {
|
|
|
|
if (!channel->feed()) {
|
2018-01-31 17:10:29 +00:00
|
|
|
// After ungrouping from a feed we need to load dialog.
|
|
|
|
Auth().api().requestDialogEntry(this);
|
2018-01-29 17:13:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-11 14:03:53 +00:00
|
|
|
bool History::useProxyPromotion() const {
|
|
|
|
if (!isProxyPromoted()) {
|
|
|
|
return false;
|
|
|
|
} else if (const auto channel = peer->asChannel()) {
|
|
|
|
return !isPinnedDialog() && !channel->amIn();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-23 16:51:12 +00:00
|
|
|
bool History::shouldBeInChatList() const {
|
|
|
|
if (peer->migrateTo()) {
|
2016-06-03 18:24:27 +00:00
|
|
|
return false;
|
2017-05-24 19:54:41 +00:00
|
|
|
} else if (isPinnedDialog()) {
|
|
|
|
return true;
|
2018-01-05 15:57:18 +00:00
|
|
|
} else if (const auto channel = peer->asChannel()) {
|
2018-01-23 16:51:12 +00:00
|
|
|
if (!channel->amIn()) {
|
2018-05-11 14:03:53 +00:00
|
|
|
return isProxyPromoted();
|
2018-01-23 16:51:12 +00:00
|
|
|
} else if (const auto feed = channel->feed()) {
|
|
|
|
return !feed->needUpdateInChatList();
|
|
|
|
}
|
2015-11-13 15:14:33 +00:00
|
|
|
}
|
2018-01-05 15:57:18 +00:00
|
|
|
return true;
|
2015-09-06 10:17:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 12:31:03 +00:00
|
|
|
bool History::toImportant() const {
|
|
|
|
return !mute() || hasUnreadMentions();
|
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
void History::unknownMessageDeleted(MsgId messageId) {
|
2018-02-12 11:10:40 +00:00
|
|
|
if (_inboxReadBefore && messageId >= *_inboxReadBefore) {
|
|
|
|
changeUnreadCount(-1);
|
2018-01-31 17:10:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool History::isServerSideUnread(not_null<const HistoryItem*> item) const {
|
|
|
|
Expects(IsServerMsgId(item->id));
|
|
|
|
|
|
|
|
return item->out()
|
|
|
|
? (!_outboxReadBefore || (item->id >= *_outboxReadBefore))
|
|
|
|
: (!_inboxReadBefore || (item->id >= *_inboxReadBefore));
|
|
|
|
}
|
|
|
|
|
|
|
|
void History::applyDialog(const MTPDdialog &data) {
|
|
|
|
applyDialogFields(
|
|
|
|
data.vunread_count.v,
|
|
|
|
data.vread_inbox_max_id.v,
|
|
|
|
data.vread_outbox_max_id.v);
|
|
|
|
applyDialogTopMessage(data.vtop_message.v);
|
2018-06-26 18:03:45 +00:00
|
|
|
setUnreadMark(data.is_unread_mark());
|
2018-01-31 17:10:29 +00:00
|
|
|
setUnreadMentionsCount(data.vunread_mentions_count.v);
|
|
|
|
if (const auto channel = peer->asChannel()) {
|
|
|
|
if (data.has_pts()) {
|
|
|
|
channel->ptsReceived(data.vpts.v);
|
|
|
|
}
|
|
|
|
if (!channel->amCreator()) {
|
|
|
|
const auto topMessageId = FullMsgId(
|
|
|
|
peerToChannel(channel->id),
|
|
|
|
data.vtop_message.v);
|
|
|
|
if (const auto item = App::histItemById(topMessageId)) {
|
2018-02-03 19:52:35 +00:00
|
|
|
if (item->date() <= channel->date) {
|
2018-01-31 17:10:29 +00:00
|
|
|
Auth().api().requestSelfParticipant(channel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-09 17:48:29 +00:00
|
|
|
Auth().data().applyNotifySetting(
|
2018-01-31 17:10:29 +00:00
|
|
|
MTP_notifyPeer(data.vpeer),
|
2018-04-09 17:48:29 +00:00
|
|
|
data.vnotify_settings);
|
2018-06-06 19:56:35 +00:00
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
if (data.has_draft() && data.vdraft.type() == mtpc_draftMessage) {
|
|
|
|
Data::applyPeerCloudDraft(peer->id, data.vdraft.c_draftMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-08 13:41:14 +00:00
|
|
|
bool History::clearUnreadOnClientSide() const {
|
|
|
|
if (!Auth().supportMode()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (const auto user = peer->asUser()) {
|
|
|
|
if (user->flags() & MTPDuser::Flag::f_deleted) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-27 20:31:48 +00:00
|
|
|
bool History::skipUnreadUpdateForClientSideUnread() const {
|
|
|
|
if (peer->id != peerFromUser(ServiceUserId)) {
|
|
|
|
return false;
|
|
|
|
} else if (!_unreadCount || !*_unreadCount) {
|
|
|
|
return false;
|
|
|
|
} else if (!_lastMessage || IsServerMsgId((*_lastMessage)->id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-08 13:41:14 +00:00
|
|
|
bool History::skipUnreadUpdate() const {
|
|
|
|
return skipUnreadUpdateForClientSideUnread()
|
|
|
|
|| clearUnreadOnClientSide();
|
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
void History::applyDialogFields(
|
|
|
|
int unreadCount,
|
|
|
|
MsgId maxInboxRead,
|
|
|
|
MsgId maxOutboxRead) {
|
2018-10-08 13:41:14 +00:00
|
|
|
if (!skipUnreadUpdate()) {
|
2018-09-27 20:31:48 +00:00
|
|
|
setUnreadCount(unreadCount);
|
|
|
|
setInboxReadTill(maxInboxRead);
|
|
|
|
}
|
2018-01-31 17:10:29 +00:00
|
|
|
setOutboxReadTill(maxOutboxRead);
|
|
|
|
}
|
|
|
|
|
|
|
|
void History::applyDialogTopMessage(MsgId topMessageId) {
|
|
|
|
if (topMessageId) {
|
|
|
|
const auto itemId = FullMsgId(
|
|
|
|
channelId(),
|
|
|
|
topMessageId);
|
|
|
|
if (const auto item = App::histItemById(itemId)) {
|
|
|
|
setLastMessage(item);
|
|
|
|
} else {
|
|
|
|
setLastMessage(nullptr);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
setLastMessage(nullptr);
|
|
|
|
}
|
2018-10-08 13:41:14 +00:00
|
|
|
if (clearUnreadOnClientSide()) {
|
|
|
|
setUnreadCount(0);
|
|
|
|
if (const auto last = lastMessage()) {
|
|
|
|
setInboxReadTill(last->id);
|
|
|
|
}
|
|
|
|
}
|
2018-01-31 17:10:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void History::setInboxReadTill(MsgId upTo) {
|
|
|
|
if (_inboxReadBefore) {
|
|
|
|
accumulate_max(*_inboxReadBefore, upTo + 1);
|
|
|
|
} else {
|
|
|
|
_inboxReadBefore = upTo + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void History::setOutboxReadTill(MsgId upTo) {
|
|
|
|
if (_outboxReadBefore) {
|
|
|
|
accumulate_max(*_outboxReadBefore, upTo + 1);
|
|
|
|
} else {
|
|
|
|
_outboxReadBefore = upTo + 1;
|
|
|
|
}
|
2014-07-04 11:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MsgId History::minMsgId() const {
|
2018-01-10 13:13:33 +00:00
|
|
|
for (const auto &block : blocks) {
|
|
|
|
for (const auto &message : block->messages) {
|
|
|
|
const auto item = message->data();
|
2017-09-04 11:40:02 +00:00
|
|
|
if (IsServerMsgId(item->id)) {
|
2016-03-15 10:37:56 +00:00
|
|
|
return item->id;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-04 11:12:54 +00:00
|
|
|
return 0;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2014-07-04 11:12:54 +00:00
|
|
|
MsgId History::maxMsgId() const {
|
2018-01-10 13:13:33 +00:00
|
|
|
for (const auto &block : base::reversed(blocks)) {
|
|
|
|
for (const auto &message : base::reversed(block->messages)) {
|
|
|
|
const auto item = message->data();
|
2017-09-04 11:40:02 +00:00
|
|
|
if (IsServerMsgId(item->id)) {
|
|
|
|
return item->id;
|
2014-07-04 11:12:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 10:17:09 +00:00
|
|
|
MsgId History::msgIdForRead() const {
|
2018-01-31 17:10:29 +00:00
|
|
|
const auto last = lastMessage();
|
|
|
|
const auto result = (last && IsServerMsgId(last->id))
|
|
|
|
? last->id
|
|
|
|
: MsgId(0);
|
|
|
|
return loadedAtBottom()
|
|
|
|
? std::max(result, maxMsgId())
|
|
|
|
: result;
|
2015-09-06 10:17:09 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 12:18:28 +00:00
|
|
|
HistoryItem *History::lastSentMessage() const {
|
|
|
|
if (!loadedAtBottom()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
for (const auto &block : base::reversed(blocks)) {
|
|
|
|
for (const auto &message : base::reversed(block->messages)) {
|
|
|
|
const auto item = message->data();
|
2018-11-10 23:35:18 +00:00
|
|
|
// Skip if message is video message or sticker.
|
|
|
|
if (const auto media = item->media()) {
|
2018-12-04 15:33:00 +00:00
|
|
|
// Skip only if media is not webpage.
|
|
|
|
if (!media->webpage() && !media->allowsEditCaption()) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-11-10 23:35:18 +00:00
|
|
|
}
|
2018-05-15 18:11:29 +00:00
|
|
|
if (IsServerMsgId(item->id)
|
2018-08-21 12:31:13 +00:00
|
|
|
&& !item->serviceMsg()
|
2018-05-15 18:11:29 +00:00
|
|
|
&& (item->out() || peer->isSelf())) {
|
2018-03-09 12:18:28 +00:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-01-21 19:21:08 +00:00
|
|
|
void History::resizeToWidth(int newWidth) {
|
|
|
|
const auto resizeAllItems = (_width != newWidth);
|
2016-03-21 18:40:00 +00:00
|
|
|
|
|
|
|
if (!resizeAllItems && !hasPendingResizedItems()) {
|
2018-01-21 19:21:08 +00:00
|
|
|
return;
|
2016-03-21 18:40:00 +00:00
|
|
|
}
|
2018-01-19 17:10:58 +00:00
|
|
|
_flags &= ~(Flag::f_has_pending_resized_items);
|
2016-03-19 16:55:15 +00:00
|
|
|
|
2018-01-21 19:21:08 +00:00
|
|
|
_width = newWidth;
|
2016-03-21 18:40:00 +00:00
|
|
|
int y = 0;
|
2018-01-10 13:13:33 +00:00
|
|
|
for (const auto &block : blocks) {
|
2017-05-12 13:53:08 +00:00
|
|
|
block->setY(y);
|
2016-03-21 18:40:00 +00:00
|
|
|
y += block->resizeGetHeight(newWidth, resizeAllItems);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2018-01-21 19:21:08 +00:00
|
|
|
_height = y;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
ChannelId History::channelId() const {
|
|
|
|
return peerToChannel(peer->id);
|
2015-09-19 09:13:21 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
bool History::isChannel() const {
|
|
|
|
return peerIsChannel(peer->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool History::isMegagroup() const {
|
|
|
|
return peer->isMegagroup();
|
2015-09-19 09:13:21 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 17:53:03 +00:00
|
|
|
not_null<History*> History::migrateToOrMe() const {
|
|
|
|
if (auto to = peer->migrateTo()) {
|
|
|
|
return App::history(to);
|
|
|
|
}
|
|
|
|
// We could get it by App::history(peer), but we optimize.
|
|
|
|
return const_cast<History*>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
History *History::migrateFrom() const {
|
|
|
|
if (auto from = peer->migrateFrom()) {
|
|
|
|
return App::history(from);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
MsgRange History::rangeForDifferenceRequest() const {
|
|
|
|
auto fromId = MsgId(0);
|
|
|
|
auto toId = MsgId(0);
|
|
|
|
for (auto blockIndex = 0, blocksCount = int(blocks.size()); blockIndex < blocksCount; ++blockIndex) {
|
|
|
|
const auto &block = blocks[blockIndex];
|
|
|
|
for (auto itemIndex = 0, itemsCount = int(block->messages.size()); itemIndex < itemsCount; ++itemIndex) {
|
|
|
|
const auto id = block->messages[itemIndex]->data()->id;
|
|
|
|
if (id > 0) {
|
|
|
|
fromId = id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fromId) break;
|
|
|
|
}
|
|
|
|
if (fromId) {
|
|
|
|
for (auto blockIndex = blocks.size(); blockIndex > 0;) {
|
|
|
|
const auto &block = blocks[--blockIndex];
|
|
|
|
for (auto itemIndex = block->messages.size(); itemIndex > 0;) {
|
|
|
|
const auto id = block->messages[--itemIndex]->data()->id;
|
|
|
|
if (id > 0) {
|
|
|
|
toId = id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (toId) break;
|
|
|
|
}
|
|
|
|
return { fromId, toId + 1 };
|
|
|
|
}
|
|
|
|
return MsgRange();
|
|
|
|
}
|
|
|
|
|
|
|
|
HistoryService *History::insertJoinedMessage(bool unread) {
|
|
|
|
if (!isChannel()
|
|
|
|
|| _joinedMessage
|
|
|
|
|| !peer->asChannel()->amIn()
|
|
|
|
|| (peer->isMegagroup()
|
|
|
|
&& peer->asChannel()->mgInfo->joinedMessageFound)) {
|
|
|
|
return _joinedMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto inviter = (peer->asChannel()->inviter > 0)
|
|
|
|
? App::userLoaded(peer->asChannel()->inviter)
|
|
|
|
: nullptr;
|
|
|
|
if (!inviter) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MTPDmessage::Flags flags = 0;
|
|
|
|
if (inviter->id == Auth().userPeerId()) {
|
|
|
|
unread = false;
|
|
|
|
//} else if (unread) {
|
|
|
|
// flags |= MTPDmessage::Flag::f_unread;
|
|
|
|
}
|
|
|
|
|
2018-02-03 19:52:35 +00:00
|
|
|
const auto inviteDate = peer->asChannel()->inviteDate;
|
2018-01-31 17:10:29 +00:00
|
|
|
if (isEmpty()) {
|
|
|
|
_joinedMessage = GenerateJoinedMessage(
|
|
|
|
this,
|
|
|
|
inviteDate,
|
|
|
|
inviter,
|
|
|
|
flags);
|
|
|
|
addNewItem(_joinedMessage, unread);
|
|
|
|
return _joinedMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto blockIndex = blocks.size(); blockIndex > 0;) {
|
|
|
|
const auto &block = blocks[--blockIndex];
|
|
|
|
for (auto itemIndex = block->messages.size(); itemIndex > 0;) {
|
|
|
|
const auto item = block->messages[--itemIndex]->data();
|
|
|
|
|
|
|
|
// Due to a server bug sometimes inviteDate is less (before) than the
|
|
|
|
// first message in the megagroup (message about migration), let us
|
|
|
|
// ignore that and think, that the inviteDate is always greater-or-equal.
|
|
|
|
if (item->isGroupMigrate()
|
|
|
|
&& peer->isMegagroup()
|
|
|
|
&& peer->migrateFrom()) {
|
|
|
|
peer->asChannel()->mgInfo->joinedMessageFound = true;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-02-03 19:52:35 +00:00
|
|
|
if (item->date() <= inviteDate) {
|
2018-01-31 17:10:29 +00:00
|
|
|
++itemIndex;
|
|
|
|
_joinedMessage = GenerateJoinedMessage(
|
|
|
|
this,
|
|
|
|
inviteDate,
|
|
|
|
inviter,
|
|
|
|
flags);
|
|
|
|
addNewInTheMiddle(_joinedMessage, blockIndex, itemIndex);
|
2018-07-31 14:34:14 +00:00
|
|
|
const auto lastDate = chatsListTimeId();
|
|
|
|
if (!lastDate || inviteDate >= lastDate) {
|
2018-01-31 17:10:29 +00:00
|
|
|
setLastMessage(_joinedMessage);
|
|
|
|
if (unread) {
|
|
|
|
newItemAdded(_joinedMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _joinedMessage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
startBuildingFrontBlock();
|
|
|
|
_joinedMessage = GenerateJoinedMessage(
|
|
|
|
this,
|
|
|
|
inviteDate,
|
|
|
|
inviter,
|
|
|
|
flags);
|
|
|
|
addItemToBlock(_joinedMessage);
|
|
|
|
finishBuildingFrontBlock();
|
|
|
|
|
|
|
|
return _joinedMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
void History::checkJoinedMessage(bool createUnread) {
|
|
|
|
if (!isChannel() || _joinedMessage || peer->asChannel()->inviter <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isEmpty()) {
|
|
|
|
if (loadedAtTop() && loadedAtBottom()) {
|
|
|
|
if (insertJoinedMessage(createUnread)) {
|
|
|
|
if (_joinedMessage->mainView()) {
|
|
|
|
setLastMessage(_joinedMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-03 19:52:35 +00:00
|
|
|
const auto inviteDate = peer->asChannel()->inviteDate;
|
|
|
|
auto firstDate = TimeId(0);
|
|
|
|
auto lastDate = TimeId(0);
|
2018-01-31 17:10:29 +00:00
|
|
|
if (!blocks.empty()) {
|
2018-02-03 19:52:35 +00:00
|
|
|
firstDate = blocks.front()->messages.front()->data()->date();
|
|
|
|
lastDate = blocks.back()->messages.back()->data()->date();
|
2018-01-31 17:10:29 +00:00
|
|
|
}
|
2018-02-03 19:52:35 +00:00
|
|
|
if (firstDate
|
|
|
|
&& lastDate
|
2018-01-31 17:10:29 +00:00
|
|
|
&& (firstDate <= inviteDate || loadedAtTop())
|
|
|
|
&& (lastDate > inviteDate || loadedAtBottom())) {
|
|
|
|
const auto willBeLastMsg = (inviteDate >= lastDate);
|
|
|
|
if (insertJoinedMessage(createUnread && willBeLastMsg)
|
|
|
|
&& willBeLastMsg) {
|
|
|
|
if (_joinedMessage->mainView()) {
|
|
|
|
setLastMessage(_joinedMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-11 14:03:53 +00:00
|
|
|
void History::removeJoinedMessage() {
|
|
|
|
if (_joinedMessage) {
|
|
|
|
base::take(_joinedMessage)->destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
bool History::isEmpty() const {
|
|
|
|
return blocks.empty();
|
|
|
|
}
|
|
|
|
|
2016-06-03 12:45:33 +00:00
|
|
|
bool History::isDisplayedEmpty() const {
|
2018-01-10 13:13:33 +00:00
|
|
|
return isEmpty() || ((blocks.size() == 1)
|
|
|
|
&& blocks.front()->messages.size() == 1
|
|
|
|
&& blocks.front()->messages.front()->data()->isEmpty());
|
2016-06-03 12:45:33 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 13:24:16 +00:00
|
|
|
bool History::hasOrphanMediaGroupPart() const {
|
|
|
|
if (loadedAtTop() || !loadedAtBottom()) {
|
|
|
|
return false;
|
|
|
|
} else if (blocks.size() != 1) {
|
|
|
|
return false;
|
2018-01-10 13:13:33 +00:00
|
|
|
} else if (blocks.front()->messages.size() != 1) {
|
2017-12-16 13:24:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-01-14 16:02:25 +00:00
|
|
|
const auto last = blocks.front()->messages.front()->data();
|
|
|
|
return last->groupId() != MessageGroupId();
|
2017-12-16 13:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool History::removeOrphanMediaGroupPart() {
|
|
|
|
if (hasOrphanMediaGroupPart()) {
|
2018-01-31 17:10:29 +00:00
|
|
|
unloadBlocks();
|
2017-12-16 13:24:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
QVector<MsgId> History::collectMessagesFromUserToDelete(
|
|
|
|
not_null<UserData*> user) const {
|
|
|
|
auto result = QVector<MsgId>();
|
|
|
|
for (const auto &block : blocks) {
|
|
|
|
for (const auto &message : block->messages) {
|
|
|
|
const auto item = message->data();
|
|
|
|
if (item->from() == user && item->canDelete()) {
|
|
|
|
result.push_back(item->id);
|
|
|
|
}
|
|
|
|
}
|
2016-04-23 11:40:42 +00:00
|
|
|
}
|
2018-01-31 17:10:29 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void History::clear() {
|
|
|
|
clearBlocks(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void History::unloadBlocks() {
|
|
|
|
clearBlocks(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void History::clearBlocks(bool leaveItems) {
|
|
|
|
_unreadBarView = nullptr;
|
|
|
|
_firstUnreadView = nullptr;
|
|
|
|
_joinedMessage = nullptr;
|
|
|
|
|
2016-03-21 18:40:00 +00:00
|
|
|
if (scrollTopItem) {
|
|
|
|
forgetScrollState();
|
|
|
|
}
|
2017-10-30 19:24:20 +00:00
|
|
|
if (leaveItems) {
|
2018-01-17 18:20:55 +00:00
|
|
|
Auth().data().notifyHistoryUnloaded(this);
|
2017-10-30 19:24:20 +00:00
|
|
|
} else {
|
2016-03-18 19:05:08 +00:00
|
|
|
setLastMessage(nullptr);
|
2016-04-14 19:24:42 +00:00
|
|
|
notifies.clear();
|
2018-01-17 18:20:55 +00:00
|
|
|
Auth().data().notifyHistoryCleared(this);
|
2014-08-15 11:19:32 +00:00
|
|
|
}
|
2018-01-31 17:10:29 +00:00
|
|
|
blocks.clear();
|
2015-06-17 19:43:03 +00:00
|
|
|
if (leaveItems) {
|
|
|
|
lastKeyboardInited = false;
|
|
|
|
} else {
|
2018-02-12 11:10:40 +00:00
|
|
|
changeUnreadCount(-unreadCount());
|
2017-11-21 13:23:56 +00:00
|
|
|
if (auto channel = peer->asChannel()) {
|
|
|
|
channel->clearPinnedMessage();
|
2018-01-05 15:57:18 +00:00
|
|
|
if (const auto feed = channel->feed()) {
|
|
|
|
// Should be after setLastMessage(nullptr);
|
|
|
|
feed->historyCleared(this);
|
|
|
|
}
|
2016-04-14 19:24:42 +00:00
|
|
|
}
|
|
|
|
clearLastKeyboard();
|
2014-07-04 11:12:54 +00:00
|
|
|
}
|
2018-01-19 17:10:58 +00:00
|
|
|
Auth().data().notifyHistoryChangeDelayed(this);
|
2016-03-21 18:40:00 +00:00
|
|
|
|
2018-02-05 14:37:22 +00:00
|
|
|
_loadedAtTop = false;
|
|
|
|
_loadedAtBottom = !leaveItems;
|
2016-03-21 18:40:00 +00:00
|
|
|
forgetScrollState();
|
2018-01-31 17:10:29 +00:00
|
|
|
if (const auto chat = peer->asChat()) {
|
|
|
|
chat->lastAuthors.clear();
|
|
|
|
chat->markupSenders.clear();
|
|
|
|
} else if (const auto channel = peer->asMegagroup()) {
|
|
|
|
channel->mgInfo->markupSenders.clear();
|
2015-06-17 19:43:03 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 19:54:05 +00:00
|
|
|
void History::clearUpTill(MsgId availableMinId) {
|
|
|
|
auto minId = minMsgId();
|
2018-01-31 17:10:29 +00:00
|
|
|
if (!minId || minId > availableMinId) {
|
2017-11-20 19:54:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
do {
|
2018-01-10 13:13:33 +00:00
|
|
|
const auto item = blocks.front()->messages.front()->data();
|
|
|
|
const auto itemId = item->id;
|
2017-11-20 19:54:05 +00:00
|
|
|
if (IsServerMsgId(itemId) && itemId >= availableMinId) {
|
2017-11-21 16:38:17 +00:00
|
|
|
if (itemId == availableMinId) {
|
|
|
|
auto fromId = 0;
|
|
|
|
auto replyToId = 0;
|
|
|
|
item->applyEdition(MTP_messageService(
|
|
|
|
MTP_flags(0),
|
|
|
|
MTP_int(itemId),
|
|
|
|
MTP_int(fromId),
|
|
|
|
peerToMTP(peer->id),
|
|
|
|
MTP_int(replyToId),
|
2018-02-03 19:52:35 +00:00
|
|
|
MTP_int(item->date()),
|
2017-11-21 16:38:17 +00:00
|
|
|
MTP_messageActionHistoryClear()
|
|
|
|
).c_messageService());
|
|
|
|
}
|
2017-11-20 19:54:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
item->destroy();
|
|
|
|
} while (!isEmpty());
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
if (!lastMessageKnown()) {
|
|
|
|
Auth().api().requestDialogEntry(this);
|
2017-11-20 19:54:05 +00:00
|
|
|
}
|
2018-01-18 13:59:22 +00:00
|
|
|
Auth().data().sendHistoryChangeNotifications();
|
2017-11-20 19:54:05 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 18:38:44 +00:00
|
|
|
void History::applyGroupAdminChanges(
|
|
|
|
const base::flat_map<UserId, bool> &changes) {
|
2018-01-10 13:13:33 +00:00
|
|
|
for (const auto &block : blocks) {
|
|
|
|
for (const auto &message : block->messages) {
|
|
|
|
message->data()->applyGroupAdminChanges(changes);
|
2017-12-01 18:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-05 15:57:18 +00:00
|
|
|
void History::changedInChatListHook(Dialogs::Mode list, bool added) {
|
2018-06-26 18:03:45 +00:00
|
|
|
if (list == Dialogs::Mode::All) {
|
|
|
|
if (const auto delta = historiesUnreadCount() * (added ? 1 : -1)) {
|
|
|
|
App::histories().unreadIncrement(delta, mute());
|
2018-12-04 10:32:06 +00:00
|
|
|
|
|
|
|
const auto entriesDelta = added ? 1 : -1;
|
|
|
|
App::histories().unreadEntriesChanged(
|
|
|
|
entriesDelta,
|
|
|
|
mute() ? entriesDelta : 0);
|
2018-06-26 18:03:45 +00:00
|
|
|
}
|
2016-02-28 11:58:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-05 15:57:18 +00:00
|
|
|
void History::changedChatListPinHook() {
|
|
|
|
Notify::peerUpdatedDelayed(
|
|
|
|
peer,
|
2018-10-31 11:29:14 +00:00
|
|
|
Notify::PeerUpdate::Flag::ChatPinnedChanged);
|
2016-12-13 07:59:57 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 18:10:48 +00:00
|
|
|
void History::removeBlock(not_null<HistoryBlock*> block) {
|
2018-01-10 13:13:33 +00:00
|
|
|
Expects(block->messages.empty());
|
2016-03-25 16:39:58 +00:00
|
|
|
|
2016-03-31 11:18:21 +00:00
|
|
|
if (_buildingFrontBlock && block == _buildingFrontBlock->block) {
|
|
|
|
_buildingFrontBlock->block = nullptr;
|
2016-03-31 10:37:58 +00:00
|
|
|
}
|
|
|
|
|
2016-03-21 18:40:00 +00:00
|
|
|
int index = block->indexInHistory();
|
2017-09-27 18:45:26 +00:00
|
|
|
blocks.erase(blocks.begin() + index);
|
2016-03-21 18:40:00 +00:00
|
|
|
if (index < blocks.size()) {
|
2016-11-18 16:27:47 +00:00
|
|
|
for (int i = index, l = blocks.size(); i < l; ++i) {
|
2017-05-12 13:53:08 +00:00
|
|
|
blocks[i]->setIndexInHistory(i);
|
2016-11-18 16:27:47 +00:00
|
|
|
}
|
2018-01-13 12:45:11 +00:00
|
|
|
blocks[index]->messages.front()->previousInBlocksChanged();
|
2018-01-10 13:13:33 +00:00
|
|
|
} else if (!blocks.empty() && !blocks.back()->messages.empty()) {
|
2018-01-19 17:10:58 +00:00
|
|
|
blocks.back()->messages.back()->nextInBlocksRemoved();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
History::~History() = default;
|
2016-02-25 10:32:31 +00:00
|
|
|
|
2018-01-10 13:13:33 +00:00
|
|
|
HistoryBlock::HistoryBlock(not_null<History*> history)
|
|
|
|
: _history(history) {
|
|
|
|
}
|
|
|
|
|
2016-03-21 18:40:00 +00:00
|
|
|
int HistoryBlock::resizeGetHeight(int newWidth, bool resizeAllItems) {
|
2017-05-12 13:53:08 +00:00
|
|
|
auto y = 0;
|
2018-01-10 13:13:33 +00:00
|
|
|
for (const auto &message : messages) {
|
|
|
|
message->setY(y);
|
2018-01-13 12:45:11 +00:00
|
|
|
if (resizeAllItems || message->pendingResize()) {
|
|
|
|
y += message->resizeGetHeight(newWidth);
|
2015-09-10 10:30:59 +00:00
|
|
|
} else {
|
2018-01-13 12:45:11 +00:00
|
|
|
y += message->height();
|
2015-09-10 10:30:59 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2017-05-12 13:53:08 +00:00
|
|
|
_height = y;
|
|
|
|
return _height;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
void HistoryBlock::remove(not_null<Element*> view) {
|
2018-01-11 13:07:29 +00:00
|
|
|
Expects(view->block() == this);
|
2016-03-21 18:40:00 +00:00
|
|
|
|
2018-01-21 19:21:08 +00:00
|
|
|
_history->mainViewRemoved(this, view);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-01-21 19:21:08 +00:00
|
|
|
const auto blockIndex = indexInHistory();
|
|
|
|
const auto itemIndex = view->indexInBlock();
|
|
|
|
const auto item = view->data();
|
2018-01-11 13:07:29 +00:00
|
|
|
item->clearMainView();
|
2018-01-10 13:13:33 +00:00
|
|
|
messages.erase(messages.begin() + itemIndex);
|
|
|
|
for (auto i = itemIndex, l = int(messages.size()); i < l; ++i) {
|
2018-01-11 13:07:29 +00:00
|
|
|
messages[i]->setIndexInBlock(i);
|
2016-03-21 18:40:00 +00:00
|
|
|
}
|
2018-01-10 13:13:33 +00:00
|
|
|
if (messages.empty()) {
|
|
|
|
// Deletes this.
|
2017-05-12 13:53:08 +00:00
|
|
|
_history->removeBlock(this);
|
2018-01-10 13:13:33 +00:00
|
|
|
} else if (itemIndex < messages.size()) {
|
2018-01-13 12:45:11 +00:00
|
|
|
messages[itemIndex]->previousInBlocksChanged();
|
2017-05-12 13:53:08 +00:00
|
|
|
} else if (blockIndex + 1 < _history->blocks.size()) {
|
2018-01-13 12:45:11 +00:00
|
|
|
_history->blocks[blockIndex + 1]->messages.front()->previousInBlocksChanged();
|
2018-01-10 13:13:33 +00:00
|
|
|
} else if (!_history->blocks.empty() && !_history->blocks.back()->messages.empty()) {
|
2018-01-19 17:10:58 +00:00
|
|
|
_history->blocks.back()->messages.back()->nextInBlocksRemoved();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-10 13:13:33 +00:00
|
|
|
|
2018-01-18 13:59:22 +00:00
|
|
|
void HistoryBlock::refreshView(not_null<Element*> view) {
|
|
|
|
Expects(view->block() == this);
|
|
|
|
|
|
|
|
const auto item = view->data();
|
|
|
|
auto refreshed = item->createView(HistoryInner::ElementDelegate());
|
|
|
|
|
|
|
|
auto blockIndex = indexInHistory();
|
|
|
|
auto itemIndex = view->indexInBlock();
|
2018-01-23 16:51:12 +00:00
|
|
|
_history->viewReplaced(view, refreshed.get());
|
2018-01-18 13:59:22 +00:00
|
|
|
|
|
|
|
messages[itemIndex] = std::move(refreshed);
|
|
|
|
messages[itemIndex]->attachToBlock(this, itemIndex);
|
|
|
|
if (itemIndex + 1 < messages.size()) {
|
|
|
|
messages[itemIndex + 1]->previousInBlocksChanged();
|
|
|
|
} else if (blockIndex + 1 < _history->blocks.size()) {
|
|
|
|
_history->blocks[blockIndex + 1]->messages.front()->previousInBlocksChanged();
|
|
|
|
} else if (!_history->blocks.empty() && !_history->blocks.back()->messages.empty()) {
|
2018-01-19 17:10:58 +00:00
|
|
|
_history->blocks.back()->messages.back()->nextInBlocksRemoved();
|
2018-01-18 13:59:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 17:10:29 +00:00
|
|
|
HistoryBlock::~HistoryBlock() = default;
|