Remove HistoryItemInstantiated.

This commit is contained in:
John Preston 2018-01-18 23:02:50 +03:00
parent 950126865e
commit d326c7e3fa
6 changed files with 103 additions and 246 deletions

View File

@ -334,7 +334,7 @@ void GenerateItems(
auto addSimpleServiceMessage = [&](const QString &text, PhotoData *photo = nullptr) {
auto message = HistoryService::PreparedText { text };
message.links.push_back(fromLink);
addPart(HistoryService::create(history, idManager.next(), ::date(date), message, 0, peerToUser(from->id), photo));
addPart(new HistoryService(history, idManager.next(), ::date(date), message, 0, peerToUser(from->id), photo));
};
auto createChangeTitle = [&](const MTPDchannelAdminLogEventActionChangeTitle &action) {
@ -355,7 +355,7 @@ void GenerateItems(
auto bodyReplyTo = 0;
auto bodyViaBotId = 0;
auto newDescription = PrepareText(newValue, QString());
auto body = HistoryMessage::create(history, idManager.next(), bodyFlags, bodyReplyTo, bodyViaBotId, ::date(date), peerToUser(from->id), QString(), newDescription);
auto body = new HistoryMessage(history, idManager.next(), bodyFlags, bodyReplyTo, bodyViaBotId, ::date(date), peerToUser(from->id), QString(), newDescription);
if (!oldValue.isEmpty()) {
auto oldDescription = PrepareText(oldValue, QString());
body->addLogEntryOriginal(id, lang(lng_admin_log_previous_description), oldDescription);
@ -376,7 +376,7 @@ void GenerateItems(
auto bodyReplyTo = 0;
auto bodyViaBotId = 0;
auto newLink = newValue.isEmpty() ? TextWithEntities() : PrepareText(Messenger::Instance().createInternalLinkFull(newValue), QString());
auto body = HistoryMessage::create(history, idManager.next(), bodyFlags, bodyReplyTo, bodyViaBotId, ::date(date), peerToUser(from->id), QString(), newLink);
auto body = new HistoryMessage(history, idManager.next(), bodyFlags, bodyReplyTo, bodyViaBotId, ::date(date), peerToUser(from->id), QString(), newLink);
if (!oldValue.isEmpty()) {
auto oldLink = PrepareText(Messenger::Instance().createInternalLinkFull(oldValue), QString());
body->addLogEntryOriginal(id, lang(lng_admin_log_previous_link), oldLink);
@ -486,7 +486,7 @@ void GenerateItems(
auto bodyReplyTo = 0;
auto bodyViaBotId = 0;
auto bodyText = GenerateParticipantChangeText(channel, action.vparticipant);
addPart(HistoryMessage::create(history, idManager.next(), bodyFlags, bodyReplyTo, bodyViaBotId, ::date(date), peerToUser(from->id), QString(), bodyText));
addPart(new HistoryMessage(history, idManager.next(), bodyFlags, bodyReplyTo, bodyViaBotId, ::date(date), peerToUser(from->id), QString(), bodyText));
};
auto createParticipantToggleBan = [&](const MTPDchannelAdminLogEventActionParticipantToggleBan &action) {
@ -494,7 +494,7 @@ void GenerateItems(
auto bodyReplyTo = 0;
auto bodyViaBotId = 0;
auto bodyText = GenerateParticipantChangeText(channel, action.vnew_participant, &action.vprev_participant);
addPart(HistoryMessage::create(history, idManager.next(), bodyFlags, bodyReplyTo, bodyViaBotId, ::date(date), peerToUser(from->id), QString(), bodyText));
addPart(new HistoryMessage(history, idManager.next(), bodyFlags, bodyReplyTo, bodyViaBotId, ::date(date), peerToUser(from->id), QString(), bodyText));
};
auto createParticipantToggleAdmin = [&](const MTPDchannelAdminLogEventActionParticipantToggleAdmin &action) {
@ -502,7 +502,7 @@ void GenerateItems(
auto bodyReplyTo = 0;
auto bodyViaBotId = 0;
auto bodyText = GenerateParticipantChangeText(channel, action.vnew_participant, &action.vprev_participant);
addPart(HistoryMessage::create(history, idManager.next(), bodyFlags, bodyReplyTo, bodyViaBotId, ::date(date), peerToUser(from->id), QString(), bodyText));
addPart(new HistoryMessage(history, idManager.next(), bodyFlags, bodyReplyTo, bodyViaBotId, ::date(date), peerToUser(from->id), QString(), bodyText));
};
auto createChangeStickerSet = [&](const MTPDchannelAdminLogEventActionChangeStickerSet &action) {
@ -523,7 +523,7 @@ void GenerateItems(
auto message = HistoryService::PreparedText { text };
message.links.push_back(fromLink);
message.links.push_back(setLink);
addPart(HistoryService::create(history, idManager.next(), ::date(date), message, 0, peerToUser(from->id)));
addPart(new HistoryService(history, idManager.next(), ::date(date), message, 0, peerToUser(from->id)));
}
};

View File

@ -437,7 +437,7 @@ HistoryJoined *ChannelHistory::insertJoinedMessage(bool unread) {
auto inviteDate = peer->asChannel()->inviteDate;
if (unread) _maxReadMessageDate = inviteDate;
if (isEmpty()) {
_joinedMessage = HistoryJoined::create(this, inviteDate, inviter, flags);
_joinedMessage = new HistoryJoined(this, inviteDate, inviter, flags);
addNewItem(_joinedMessage, unread);
return _joinedMessage;
}
@ -456,7 +456,7 @@ HistoryJoined *ChannelHistory::insertJoinedMessage(bool unread) {
}
if (item->date <= inviteDate) {
++itemIndex;
_joinedMessage = HistoryJoined::create(
_joinedMessage = new HistoryJoined(
this,
inviteDate,
inviter,
@ -476,7 +476,7 @@ HistoryJoined *ChannelHistory::insertJoinedMessage(bool unread) {
startBuildingFrontBlock();
_joinedMessage = HistoryJoined::create(this, inviteDate, inviter, flags);
_joinedMessage = new HistoryJoined(this, inviteDate, inviter, flags);
addItemToBlock(_joinedMessage);
finishBuildingFrontBlock();
@ -766,7 +766,7 @@ not_null<HistoryItem*> History::createItemForwarded(
UserId from,
const QString &postAuthor,
HistoryMessage *original) {
return HistoryMessage::create(
return new HistoryMessage(
this,
id,
flags,
@ -787,7 +787,7 @@ not_null<HistoryItem*> History::createItemDocument(
DocumentData *document,
const TextWithEntities &caption,
const MTPReplyMarkup &markup) {
return HistoryMessage::create(
return new HistoryMessage(
this,
id,
flags,
@ -812,7 +812,7 @@ not_null<HistoryItem*> History::createItemPhoto(
PhotoData *photo,
const TextWithEntities &caption,
const MTPReplyMarkup &markup) {
return HistoryMessage::create(
return new HistoryMessage(
this,
id,
flags,
@ -836,7 +836,7 @@ not_null<HistoryItem*> History::createItemGame(
const QString &postAuthor,
GameData *game,
const MTPReplyMarkup &markup) {
return HistoryMessage::create(
return new HistoryMessage(
this,
id,
flags,
@ -857,7 +857,7 @@ not_null<HistoryItem*> History::addNewService(
bool unread) {
auto message = HistoryService::PreparedText { text };
return addNewItem(
HistoryService::create(this, msgId, date, message, flags),
new HistoryService(this, msgId, date, message, flags),
unread);
}

View File

@ -56,7 +56,7 @@ not_null<HistoryItem*> CreateUnsupportedMessage(
text.entities.push_front(
EntityInText(EntityInTextItalic, 0, text.text.size()));
flags &= ~MTPDmessage::Flag::f_post_author;
return HistoryMessage::create(
return new HistoryMessage(
history,
msgId,
flags,
@ -81,9 +81,6 @@ HistoryItem::HistoryItem(
, _history(history)
, _from(from ? App::user(from) : history->peer)
, _flags(flags) {
}
void HistoryItem::finishCreate() {
App::historyRegItem(this);
}
@ -783,7 +780,7 @@ not_null<HistoryItem*> HistoryItem::Create(
const auto text = HistoryService::PreparedText {
lang(lng_message_empty)
};
return HistoryService::create(history, data.vid.v, ::date(), text);
return new HistoryService(history, data.vid.v, ::date(), text);
} break;
case mtpc_message: {
@ -881,7 +878,7 @@ not_null<HistoryItem*> HistoryItem::Create(
const auto text = HistoryService::PreparedText {
lang(lng_message_empty)
};
return HistoryService::create(
return new HistoryService(
history,
data.vid.v,
::date(data.vdate),
@ -889,17 +886,17 @@ not_null<HistoryItem*> HistoryItem::Create(
data.vflags.v,
data.has_from_id() ? data.vfrom_id.v : UserId(0));
} else if (badMedia == MediaCheckResult::HasTimeToLive) {
return HistoryService::create(history, data);
return new HistoryService(history, data);
}
return HistoryMessage::create(history, data);
return new HistoryMessage(history, data);
} break;
case mtpc_messageService: {
auto &data = message.c_messageService();
if (data.vaction.type() == mtpc_messageActionPhoneCall) {
return HistoryMessage::create(history, data);
return new HistoryMessage(history, data);
}
return HistoryService::create(history, data);
return new HistoryService(history, data);
} break;
}

View File

@ -287,10 +287,6 @@ protected:
QDateTime date,
UserId from);
// To completely create history item we need to call
// a virtual method, it can not be done from constructor.
virtual void finishCreate();
virtual void markMediaAsReadHook() {
}
@ -327,22 +323,6 @@ private:
};
// make all the constructors in HistoryItem children protected
// and wrapped with a static create() call with the same args
// so that history item can not be created directly, without
// calling a virtual finishCreate() method
template <typename T>
class HistoryItemInstantiated {
public:
template <typename ...Args>
static not_null<T*> _create(Args &&... args) {
auto result = new T(std::forward<Args>(args)...);
result->finishCreate();
return result;
}
};
ClickHandlerPtr goToMessageClickHandler(
not_null<PeerData*> peer,
MsgId msgId);

View File

@ -25,182 +25,8 @@ void FastShareMessage(not_null<HistoryItem*> item);
QString FormatViewsCount(int views);
class HistoryMessage
: public HistoryItem
, private HistoryItemInstantiated<HistoryMessage> {
: public HistoryItem {
public:
static not_null<HistoryMessage*> create(
not_null<History*> history,
const MTPDmessage &msg) {
return _create(history, msg);
}
static not_null<HistoryMessage*> create(
not_null<History*> history,
const MTPDmessageService &msg) {
return _create(history, msg);
}
static not_null<HistoryMessage*> create(
not_null<History*> history,
MsgId msgId,
MTPDmessage::Flags flags,
QDateTime date,
UserId from,
const QString &postAuthor,
not_null<HistoryMessage*> fwd) {
return _create(history, msgId, flags, date, from, postAuthor, fwd);
}
static not_null<HistoryMessage*> create(
not_null<History*> history,
MsgId msgId,
MTPDmessage::Flags flags,
MsgId replyTo,
UserId viaBotId,
QDateTime date,
UserId from,
const QString &postAuthor,
const TextWithEntities &textWithEntities) {
return _create(
history,
msgId,
flags,
replyTo,
viaBotId,
date,
from,
postAuthor,
textWithEntities);
}
static not_null<HistoryMessage*> create(
not_null<History*> history,
MsgId msgId,
MTPDmessage::Flags flags,
MsgId replyTo,
UserId viaBotId,
QDateTime date,
UserId from,
const QString &postAuthor,
not_null<DocumentData*> document,
const TextWithEntities &caption,
const MTPReplyMarkup &markup) {
return _create(
history,
msgId,
flags,
replyTo,
viaBotId,
date,
from,
postAuthor,
document,
caption,
markup);
}
static not_null<HistoryMessage*> create(
not_null<History*> history,
MsgId msgId,
MTPDmessage::Flags flags,
MsgId replyTo,
UserId viaBotId,
QDateTime date,
UserId from,
const QString &postAuthor,
not_null<PhotoData*> photo,
const TextWithEntities &caption,
const MTPReplyMarkup &markup) {
return _create(
history,
msgId,
flags,
replyTo,
viaBotId,
date,
from,
postAuthor,
photo,
caption,
markup);
}
static not_null<HistoryMessage*> create(
not_null<History*> history,
MsgId msgId,
MTPDmessage::Flags flags,
MsgId replyTo,
UserId viaBotId,
QDateTime date,
UserId from,
const QString &postAuthor,
not_null<GameData*> game,
const MTPReplyMarkup &markup) {
return _create(
history,
msgId,
flags,
replyTo,
viaBotId,
date,
from,
postAuthor,
game,
markup);
}
void refreshMedia(const MTPMessageMedia *media);
void refreshSentMedia(const MTPMessageMedia *media);
void setMedia(const MTPMessageMedia &media);
static std::unique_ptr<Data::Media> CreateMedia(
not_null<HistoryMessage*> item,
const MTPMessageMedia &media);
int32 plainMaxWidth() const;
bool allowsForward() const override;
bool allowsEdit(const QDateTime &now) const override;
bool uploading() const;
void applyGroupAdminChanges(
const base::flat_map<UserId, bool> &changes) override;
void setViewsCount(int32 count) override;
void setRealId(MsgId newId) override;
void dependencyItemRemoved(HistoryItem *dependency) override;
QString notificationHeader() const override;
void applyEdition(const MTPDmessage &message) override;
void applyEdition(const MTPDmessageService &message) override;
void updateSentMedia(const MTPMessageMedia *media) override;
void updateReplyMarkup(const MTPReplyMarkup *markup) override {
setReplyMarkup(markup);
}
void addToUnreadMentions(UnreadMentionType type) override;
void eraseFromUnreadMentions() override;
Storage::SharedMediaTypesMask sharedMediaTypes() const override;
void setText(const TextWithEntities &textWithEntities) override;
TextWithEntities originalText() const override;
bool textHasLinks() const override;
int viewsCount() const override;
not_null<PeerData*> displayFrom() const;
bool updateDependencyItem() override;
MsgId dependencyMsgId() const override {
return replyToId();
}
HistoryMessage *toHistoryMessage() override { // dynamic_cast optimize
return this;
}
const HistoryMessage *toHistoryMessage() const override { // dynamic_cast optimize
return this;
}
std::unique_ptr<HistoryView::Element> createView(
not_null<HistoryView::ElementDelegate*> delegate) override;
~HistoryMessage();
private:
HistoryMessage(
not_null<History*> history,
const MTPDmessage &msg);
@ -260,8 +86,65 @@ private:
const QString &postAuthor,
not_null<GameData*> game,
const MTPReplyMarkup &markup); // local game
friend class HistoryItemInstantiated<HistoryMessage>;
void refreshMedia(const MTPMessageMedia *media);
void refreshSentMedia(const MTPMessageMedia *media);
void setMedia(const MTPMessageMedia &media);
static std::unique_ptr<Data::Media> CreateMedia(
not_null<HistoryMessage*> item,
const MTPMessageMedia &media);
int32 plainMaxWidth() const;
bool allowsForward() const override;
bool allowsEdit(const QDateTime &now) const override;
bool uploading() const;
void applyGroupAdminChanges(
const base::flat_map<UserId, bool> &changes) override;
void setViewsCount(int32 count) override;
void setRealId(MsgId newId) override;
void dependencyItemRemoved(HistoryItem *dependency) override;
QString notificationHeader() const override;
void applyEdition(const MTPDmessage &message) override;
void applyEdition(const MTPDmessageService &message) override;
void updateSentMedia(const MTPMessageMedia *media) override;
void updateReplyMarkup(const MTPReplyMarkup *markup) override {
setReplyMarkup(markup);
}
void addToUnreadMentions(UnreadMentionType type) override;
void eraseFromUnreadMentions() override;
Storage::SharedMediaTypesMask sharedMediaTypes() const override;
void setText(const TextWithEntities &textWithEntities) override;
TextWithEntities originalText() const override;
bool textHasLinks() const override;
int viewsCount() const override;
not_null<PeerData*> displayFrom() const;
bool updateDependencyItem() override;
MsgId dependencyMsgId() const override {
return replyToId();
}
HistoryMessage *toHistoryMessage() override { // dynamic_cast optimize
return this;
}
const HistoryMessage *toHistoryMessage() const override { // dynamic_cast optimize
return this;
}
std::unique_ptr<HistoryView::Element> createView(
not_null<HistoryView::ElementDelegate*> delegate) override;
~HistoryMessage();
private:
void setEmptyText();
bool hasAdminBadge() const {
return _flags & MTPDmessage_ClientFlag::f_has_admin_badge;

View File

@ -51,22 +51,25 @@ namespace HistoryView {
class ServiceMessagePainter;
} // namespace HistoryView
class HistoryService : public HistoryItem, private HistoryItemInstantiated<HistoryService> {
class HistoryService : public HistoryItem {
public:
struct PreparedText {
QString text;
QList<ClickHandlerPtr> links;
};
static not_null<HistoryService*> create(not_null<History*> history, const MTPDmessage &message) {
return _create(history, message);
}
static not_null<HistoryService*> create(not_null<History*> history, const MTPDmessageService &message) {
return _create(history, message);
}
static not_null<HistoryService*> create(not_null<History*> history, MsgId msgId, QDateTime date, const PreparedText &message, MTPDmessage::Flags flags = 0, UserId from = 0, PhotoData *photo = nullptr) {
return _create(history, msgId, date, message, flags, from, photo);
}
HistoryService(not_null<History*> history, const MTPDmessage &message);
HistoryService(
not_null<History*> history,
const MTPDmessageService &message);
HistoryService(
not_null<History*> history,
MsgId msgId,
QDateTime date,
const PreparedText &message,
MTPDmessage::Flags flags = 0,
UserId from = 0,
PhotoData *photo = nullptr);
bool updateDependencyItem() override;
MsgId dependencyMsgId() const override {
@ -104,11 +107,6 @@ public:
protected:
friend class HistoryView::ServiceMessagePainter;
HistoryService(not_null<History*> history, const MTPDmessage &message);
HistoryService(not_null<History*> history, const MTPDmessageService &message);
HistoryService(not_null<History*> history, MsgId msgId, QDateTime date, const PreparedText &message, MTPDmessage::Flags flags = 0, UserId from = 0, PhotoData *photo = 0);
friend class HistoryItemInstantiated<HistoryService>;
void markMediaAsReadHook() override;
void setServiceText(const PreparedText &prepared);
@ -153,18 +151,17 @@ private:
};
class HistoryJoined : public HistoryService, private HistoryItemInstantiated<HistoryJoined> {
class HistoryJoined : public HistoryService {
public:
static not_null<HistoryJoined*> create(not_null<History*> history, const QDateTime &inviteDate, not_null<UserData*> inviter, MTPDmessage::Flags flags) {
return _create(history, inviteDate, inviter, flags);
}
protected:
HistoryJoined(not_null<History*> history, const QDateTime &inviteDate, not_null<UserData*> inviter, MTPDmessage::Flags flags);
using HistoryItemInstantiated<HistoryJoined>::_create;
friend class HistoryItemInstantiated<HistoryJoined>;
HistoryJoined(
not_null<History*> history,
const QDateTime &inviteDate,
not_null<UserData*> inviter,
MTPDmessage::Flags flags);
private:
static PreparedText GenerateText(not_null<History*> history, not_null<UserData*> inviter);
static PreparedText GenerateText(
not_null<History*> history,
not_null<UserData*> inviter);
};