diff --git a/src/core/autotests/messagetest.cpp b/src/core/autotests/messagetest.cpp index cc53e100..6ff435c5 100644 --- a/src/core/autotests/messagetest.cpp +++ b/src/core/autotests/messagetest.cpp @@ -1,416 +1,418 @@ /* Copyright (c) 2017-2020 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "messagetest.h" #include "messages/message.h" #include "ruqola_autotest_helper.h" #include #include QTEST_GUILESS_MAIN(MessageTest) MessageTest::MessageTest(QObject *parent) : QObject(parent) { } //TODO add check for default value ??? void MessageTest::shouldParseMessage_data() { QTest::addColumn("name"); QTest::addColumn("expectedMessage"); { Message firstMessageRef; firstMessageRef.setMessageId(QStringLiteral("blaid")); firstMessageRef.setAlias(QStringLiteral("aliasname")); firstMessageRef.setGroupable(false); firstMessageRef.setTimeStamp(1504509615057); firstMessageRef.setUsername(QStringLiteral("foo.ff")); firstMessageRef.setUserId(QStringLiteral("qt9uNY9FxyL5QT5no")); firstMessageRef.setMessageType(Message::File); firstMessageRef.setUpdatedAt(1504509615063); firstMessageRef.setEditedAt(-1); firstMessageRef.setStarred(false); firstMessageRef.setRoomId(QStringLiteral("qt9uNY9FxyL5QT5nouKK39zoewTkdacidH")); MessageAttachment att; att.setLink(QStringLiteral("/file-upload/3zfLR3Cjr8YnvD6cS/dd.pdf")); att.setTitle(QStringLiteral("dd.pdf")); firstMessageRef.setAttachements({att}); QTest::addRow("first") << QStringLiteral("first") << firstMessageRef; } { Message urlMessageRef; urlMessageRef.setMessageId(QStringLiteral("9kHnbbjbHKHjXXQp7")); urlMessageRef.setAlias(QStringLiteral("alias")); urlMessageRef.setGroupable(false); urlMessageRef.setTimeStamp(1504596899771); urlMessageRef.setUsername(QStringLiteral("username")); urlMessageRef.setUserId(QStringLiteral("bjBueJtHsHQECdkmH")); urlMessageRef.setMessageType(Message::NormalText); urlMessageRef.setUpdatedAt(1504596901803); urlMessageRef.setEditedAt(-1); urlMessageRef.setStarred(false); urlMessageRef.setRoomId(QStringLiteral("dBWXYy4nyBHn8Q7dv")); urlMessageRef.setText(QStringLiteral("https://foo.com/event/whoa")); MessageUrl url; url.setUrl(QStringLiteral("https://foo.com/event/C8CT")); url.setPageTitle(QStringLiteral(" Trainers Panel I")); url.setDescription(QStringLiteral("Description")); urlMessageRef.setUrls({url}); QTest::addRow("url") << QStringLiteral("url") << urlMessageRef; } { //Image Message imageMessageRef; imageMessageRef.setMessageId(QStringLiteral("CD2LsyS4dNbj6TTjQ")); imageMessageRef.setAlias(QStringLiteral("aliasname")); imageMessageRef.setGroupable(false); imageMessageRef.setTimeStamp(1505399177844); imageMessageRef.setUsername(QStringLiteral("username")); imageMessageRef.setUserId(QStringLiteral("FwHz8w8PWWQRqrJjM")); imageMessageRef.setMessageType(Message::NormalText); imageMessageRef.setUpdatedAt(1505399177846); imageMessageRef.setEditedAt(-1); imageMessageRef.setStarred(false); imageMessageRef.setRoomId(QStringLiteral("dBWXYy4nyBHn8Q7dv")); MessageAttachment attImage; attImage.setTitle(QStringLiteral("Clipboard")); attImage.setLink(QStringLiteral("/file-upload/Kt7DBWPe7pnadXDQH/test file")); attImage.setMimeType(QStringLiteral("image/png")); + attImage.setImageWidth(120); + attImage.setImageHeight(120); imageMessageRef.setAttachements({attImage}); QTest::addRow("image") << QStringLiteral("image") << imageMessageRef; //TODO add Mentions } { //Video Message videoMessageRef; videoMessageRef.setMessageId(QStringLiteral("KCy2KiFYmS5NuakPm")); videoMessageRef.setAlias(QStringLiteral("Laurent Montel")); videoMessageRef.setGroupable(false); videoMessageRef.setTimeStamp(1515580853098); videoMessageRef.setUsername(QStringLiteral("laurent")); videoMessageRef.setUserId(QStringLiteral("uKK39zoewTkdacidH")); videoMessageRef.setMessageType(Message::Video); videoMessageRef.setUpdatedAt(1515580853102); videoMessageRef.setEditedAt(-1); videoMessageRef.setStarred(false); videoMessageRef.setRoomId(QStringLiteral("kGtPa6bu7xHrS5xz6")); MessageAttachment attVideo; attVideo.setTitle(QStringLiteral("SampleVideo_1280x720_1mb.mp4")); attVideo.setLink(QStringLiteral("/file-upload/sLgmaWowyttg4d2ZD/SampleVideo_1280x720_1mb.mp4")); attVideo.setDescription(QStringLiteral("test")); //Add video size/video type etc. videoMessageRef.setAttachements({attVideo}); QTest::addRow("video") << QStringLiteral("video") << videoMessageRef; } { //Audio Message audioMessageRef; audioMessageRef.setMessageId(QStringLiteral("AwDsjWKJaW2wCP2ht")); audioMessageRef.setAlias(QStringLiteral("Laurent Montel")); audioMessageRef.setGroupable(false); audioMessageRef.setTimeStamp(1515588347098); audioMessageRef.setUsername(QStringLiteral("laurent")); audioMessageRef.setUserId(QStringLiteral("uKK39zoewTkdacidH")); audioMessageRef.setMessageType(Message::Audio); audioMessageRef.setUpdatedAt(1515588347102); audioMessageRef.setEditedAt(-1); audioMessageRef.setStarred(false); audioMessageRef.setRoomId(QStringLiteral("kGtPa6bu7xHrS5xz6")); MessageAttachment attAudio; attAudio.setTitle(QStringLiteral("joint.wav")); attAudio.setLink(QStringLiteral("/file-upload/9E8YBGgq3H6GbASf3/joint.wav")); attAudio.setDescription(QStringLiteral("dfgsdfgsdfg sdfgd dfsg sfd g")); //Add video size/video type etc. audioMessageRef.setAttachements({attAudio}); QTest::addRow("audio") << QStringLiteral("audio") << audioMessageRef; } { //Message Starred Message messageStarredRef; messageStarredRef.setMessageId(QStringLiteral("R6AkSJ7orEkYHNcZJ")); messageStarredRef.setAlias(QStringLiteral("Laurent")); messageStarredRef.setGroupable(false); messageStarredRef.setTimeStamp(1516711967347); messageStarredRef.setUsername(QStringLiteral("laurent")); messageStarredRef.setUserId(QStringLiteral("uKK39zoewTkdacidH")); messageStarredRef.setMessageType(Message::NormalText); messageStarredRef.setUpdatedAt(1516721464681); messageStarredRef.setEditedAt(1516712012309); messageStarredRef.setEditedByUsername(QStringLiteral("laurent")); messageStarredRef.setEditedByUserId(QStringLiteral("uKK39zoewTkdacidH")); messageStarredRef.setRoomId(QStringLiteral("kGtPa6bu7xHrS5xz6")); messageStarredRef.setStarred(true); messageStarredRef.setText(QStringLiteral("sefssssdfsefdsdfsd")); QTest::addRow("messagestarred") << QStringLiteral("messagestarred") << messageStarredRef; } { //Message Starred Message messagePinnedRef; messagePinnedRef.setMessageId(QStringLiteral("4eawfwyFnt7L8wTwE")); messagePinnedRef.setAlias(QStringLiteral("Laurent")); messagePinnedRef.setGroupable(false); messagePinnedRef.setTimeStamp(1550240990669); messagePinnedRef.setUsername(QStringLiteral("laurent")); messagePinnedRef.setUserId(QStringLiteral("uKK39zoewTkdacidH")); messagePinnedRef.setMessageType(Message::NormalText); messagePinnedRef.setUpdatedAt(1550240994061); messagePinnedRef.setRoomId(QStringLiteral("zMHhMfsEPvKjgFuyE")); messagePinnedRef.setStarred(false); MessagePinned pinned; pinned.setPinned(true); messagePinnedRef.setMessagePinned(pinned); messagePinnedRef.setText(QStringLiteral("sdfgsdfg")); QTest::addRow("messagepinned") << QStringLiteral("messagepinned") << messagePinnedRef; } { //Message Remove Starred status Message messageStarredRemovedRef; messageStarredRemovedRef.setMessageId(QStringLiteral("R6AkSJ7orEkYHNcZJ")); messageStarredRemovedRef.setAlias(QStringLiteral("Laurent")); messageStarredRemovedRef.setGroupable(false); messageStarredRemovedRef.setTimeStamp(1516711967347); messageStarredRemovedRef.setUsername(QStringLiteral("laurent")); messageStarredRemovedRef.setUserId(QStringLiteral("uKK39zoewTkdacidH")); messageStarredRemovedRef.setMessageType(Message::NormalText); messageStarredRemovedRef.setUpdatedAt(1516721464681); messageStarredRemovedRef.setEditedAt(1516712012309); messageStarredRemovedRef.setEditedByUsername(QStringLiteral("laurent")); messageStarredRemovedRef.setEditedByUserId(QStringLiteral("uKK39zoewTkdacidH")); messageStarredRemovedRef.setRoomId(QStringLiteral("kGtPa6bu7xHrS5xz6")); messageStarredRemovedRef.setStarred(false); messageStarredRemovedRef.setText(QStringLiteral("sefssssdfsefdsdfsd")); QTest::addRow("messageremovedstarred") << QStringLiteral("messageremovedstarred") << messageStarredRemovedRef; } { //messageattachmentfile Message messageAttachmentFileRef; messageAttachmentFileRef.setMessageId(QStringLiteral("messageid")); messageAttachmentFileRef.setAlias(QStringLiteral("Laurent")); messageAttachmentFileRef.setGroupable(false); messageAttachmentFileRef.setTimeStamp(1520540888833); messageAttachmentFileRef.setUsername(QStringLiteral("laurent")); messageAttachmentFileRef.setUserId(QStringLiteral("39zoewTkdacidH")); messageAttachmentFileRef.setMessageType(Message::File); messageAttachmentFileRef.setUpdatedAt(1520540888836); messageAttachmentFileRef.setEditedAt(-1); messageAttachmentFileRef.setRoomId(QStringLiteral("Pa6bu7xHrS5xz6")); messageAttachmentFileRef.setStarred(false); MessageAttachment fileAttachment; fileAttachment.setTitle(QStringLiteral("webkit.txt")); fileAttachment.setLink(QStringLiteral("/file-upload/tmqfdJTCmJ3oqG/webkit.txt")); fileAttachment.setDescription(QStringLiteral("description")); //Add video size/video type etc. messageAttachmentFileRef.setAttachements({fileAttachment}); QTest::addRow("messageattachmentfile") << QStringLiteral("messageattachmentfile") << messageAttachmentFileRef; } } void MessageTest::shouldParseMessage() { QFETCH(QString, name); QFETCH(Message, expectedMessage); const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/json/") + name + QLatin1String(".json"); QFile f(originalJsonFile); QVERIFY(f.open(QIODevice::ReadOnly)); const QByteArray content = f.readAll(); f.close(); const QJsonDocument doc = QJsonDocument::fromJson(content); QJsonObject obj = doc.object(); Message originalMessage; originalMessage.parseMessage(obj); const bool messageIsEqual = (originalMessage == expectedMessage); if (!messageIsEqual) { qDebug() << "originalMessage "< lstAttachement; MessageAttachment attachment; attachment.setDescription(QStringLiteral("foo1")); attachment.setTitle(QStringLiteral("foo2")); attachment.setLink(QStringLiteral("foo3")); lstAttachement.append(attachment); MessageAttachment attachment2; attachment2.setDescription(QStringLiteral("foo5")); attachment2.setTitle(QStringLiteral("foo6")); attachment2.setLink(QStringLiteral("foo7")); lstAttachement.append(attachment2); input.setAttachements(lstAttachement); QVector lstUrls; MessageUrl url1; url1.setUrl(QStringLiteral("foo1")); url1.setPageTitle(QStringLiteral("foo2")); lstUrls.append(url1); MessageUrl url2; url2.setUrl(QStringLiteral("foo5")); url2.setPageTitle(QStringLiteral("foo6")); lstUrls.append(url2); input.setUrls(lstUrls); const QByteArray ba = Message::serialize(input); Message output = Message::fromJSon(QJsonObject(QJsonDocument::fromBinaryData(ba).object())); QCOMPARE(input, output); //TODO add Mentions QVERIFY(output.wasEdited()); } void MessageTest::shouldParseJsonMessage_data() { QTest::addColumn("fileName"); QTest::newRow("empty") << QStringLiteral("empty"); QTest::newRow("standardmessage") << QStringLiteral("standardmessage"); QTest::newRow("attachmentimage") << QStringLiteral("attachmentimage"); QTest::newRow("attachment-author") << QStringLiteral("attachment-author"); QTest::newRow("message-url") << QStringLiteral("message-url"); QTest::newRow("message-url-meta-info") << QStringLiteral("message-url-meta-info"); QTest::newRow("message-reactions") << QStringLiteral("message-reactions"); QTest::newRow("animatedimage") << QStringLiteral("animatedimage"); } void MessageTest::shouldParseJsonMessage() { QFETCH(QString, fileName); const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/messages/") + fileName + QLatin1String(".json"); QFile f(originalJsonFile); QVERIFY(f.open(QIODevice::ReadOnly)); const QByteArray content = f.readAll(); f.close(); const QJsonDocument doc = QJsonDocument::fromJson(content); const QJsonObject fields = doc.object(); Message r; r.parseMessage(fields); //qDebug() << " fields"<("fileNameinit"); QTest::addColumn("fileNameupdate"); QTest::newRow("standardmessage") << QStringLiteral("standardmessage") << QStringList(); QTest::newRow("message1-init") << QStringLiteral("message1-init") << QStringList(); QTest::newRow("message1") << QStringLiteral("message1") << (QStringList() << QStringLiteral("message1-updated")); QTest::newRow("message2") << QStringLiteral("message2") << (QStringList() << QStringLiteral("message2-updated") << QStringLiteral("message2-updated-stared")); QTest::newRow("message3") << QStringLiteral("message3") << (QStringList() << QStringLiteral("message3-updated")); //TODO add more ! } void MessageTest::shouldUpdateJsonMessage() { QFETCH(QString, fileNameinit); QFETCH(QStringList, fileNameupdate); const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/messages-updated/") + fileNameinit + QLatin1String(".json"); QFile f(originalJsonFile); QVERIFY(f.open(QIODevice::ReadOnly)); const QByteArray content = f.readAll(); f.close(); const QJsonDocument doc = QJsonDocument::fromJson(content); const QJsonObject fields = doc.object(); Message r; r.parseMessage(fields); for (const QString &updateFile : fileNameupdate) { const QString originalUpdateJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/messages-updated/") + updateFile + QLatin1String(".json"); QFile f(originalUpdateJsonFile); QVERIFY(f.open(QIODevice::ReadOnly)); const QByteArray content = f.readAll(); f.close(); const QJsonDocument doc = QJsonDocument::fromJson(content); const QJsonObject fields = doc.object(); r.parseMessage(fields); } //qDebug() << " fields"< This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "message.h" #include "utils.h" #include "ruqola_debug.h" #include #include #include #include Message::Message(EmojiManager *emojiManager) : mEmojiManager(emojiManager) { } void Message::parseMessage(const QJsonObject &o, bool restApi) { const QString roomId = o.value(QLatin1String("rid")).toString(); //t ? I can't find it. const QString type = o.value(QLatin1String("t")).toString(); mMessageId = o.value(QLatin1String("_id")).toString(); mRoomId = roomId; mText = o.value(QLatin1String("msg")).toString(); if (restApi) { mUpdatedAt = Utils::parseIsoDate(QStringLiteral("_updatedAt"), o); mEditedAt = Utils::parseIsoDate(QStringLiteral("editedAt"), o); setTimeStamp(Utils::parseIsoDate(QStringLiteral("ts"), o)); mThreadLastMessage = Utils::parseIsoDate(QStringLiteral("tlm"), o); mDiscussionLastMessage = Utils::parseIsoDate(QStringLiteral("dlm"), o); } else { setTimeStamp(Utils::parseDate(QStringLiteral("ts"), o)); mUpdatedAt = Utils::parseDate(QStringLiteral("_updatedAt"), o); mEditedAt = Utils::parseDate(QStringLiteral("editedAt"), o); //Verify if a day we will use not restapi for it. mThreadLastMessage = Utils::parseDate(QStringLiteral("tlm"), o); //Verify if a day we will use not restapi for it. mDiscussionLastMessage = Utils::parseDate(QStringLiteral("dlm"), o); } mUsername = o.value(QLatin1String("u")).toObject().value(QLatin1String("username")).toString(); mUserId = o.value(QLatin1String("u")).toObject().value(QLatin1String("_id")).toString(); mEditedByUsername = o.value(QLatin1String("editedBy")).toObject().value(QLatin1String("username")).toString(); mEditedByUserId = o.value(QLatin1String("editedBy")).toObject().value(QLatin1String("_id")).toString(); mAlias = o.value(QLatin1String("alias")).toString(); mAvatar = o.value(QLatin1String("avatar")).toString(); mGroupable = o.value(QLatin1String("groupable")).toBool(/*true*/ false); //Laurent, disable for the moment groupable mParseUrls = o.value(QLatin1String("parseUrls")).toBool(); mRole = o.value(QLatin1String("role")).toString(); mThreadCount = o.value(QLatin1String("tcount")).toInt(); mDiscussionCount = o.value(QLatin1String("dcount")).toInt(); mDiscussionRoomId = o.value(QLatin1String("drid")).toString(); mThreadMessageId = o.value(QLatin1String("tmid")).toString(); mMessageStarred.parse(o); mMessagePinned.parse(o); mMessageTranslation.parse(o); mMessageType = Message::MessageType::NormalText; if (!type.isEmpty()) { mSystemMessageType = type; mMessageType = System; } parseMentions(o.value(QLatin1String("mentions")).toArray()); parseAttachment(o.value(QLatin1String("attachments")).toArray()); parseUrls(o.value(QLatin1String("urls")).toArray()); parseReactions(o.value(QLatin1String("reactions")).toObject()); //TODO unread element } void Message::parseReactions(const QJsonObject &reacts) { if (!reacts.isEmpty()) { mReactions.parseReactions(reacts, mEmojiManager); } } bool Message::showAttachment() const { return mShowAttachment; } void Message::setShowAttachment(bool showAttachment) { mShowAttachment = showAttachment; } bool Message::showTranslatedMessage() const { return mShowTranslatedMessage; } void Message::setShowTranslatedMessage(bool showOriginalMessage) { mShowTranslatedMessage = showOriginalMessage; } MessageTranslation Message::messageTranslation() const { return mMessageTranslation; } void Message::setMessageTranslation(const MessageTranslation &messageTranslation) { mMessageTranslation = messageTranslation; } QString Message::displayTime() const { return mDisplayTime; } QString Message::threadMessageId() const { return mThreadMessageId; } void Message::setThreadMessageId(const QString &threadMessageId) { mThreadMessageId = threadMessageId; } QString Message::discussionRoomId() const { return mDiscussionRoomId; } void Message::setDiscussionRoomId(const QString &discussionRoomId) { mDiscussionRoomId = discussionRoomId; } int Message::discussionCount() const { return mDiscussionCount; } void Message::setDiscussionCount(int discussionCount) { mDiscussionCount = discussionCount; } qint64 Message::discussionLastMessage() const { return mDiscussionLastMessage; } void Message::setDiscussionLastMessage(const qint64 &discussionLastMessage) { mDiscussionLastMessage = discussionLastMessage; } qint64 Message::threadLastMessage() const { return mThreadLastMessage; } void Message::setThreadLastMessage(const qint64 &threadLastMessage) { mThreadLastMessage = threadLastMessage; } int Message::threadCount() const { return mThreadCount; } void Message::setThreadCount(int threadCount) { mThreadCount = threadCount; } MessageStarred Message::messageStarred() const { return mMessageStarred; } void Message::setMessageStarred(const MessageStarred &messageStarred) { mMessageStarred = messageStarred; } MessagePinned Message::messagePinned() const { return mMessagePinned; } void Message::setMessagePinned(const MessagePinned &messagePinned) { mMessagePinned = messagePinned; } bool Message::unread() const { return mUnread; } void Message::setUnread(bool unread) { mUnread = unread; } QString Message::role() const { return mRole; } void Message::setRole(const QString &role) { mRole = role; } void Message::parseMentions(const QJsonArray &mentions) { mMentions.clear(); for (int i = 0; i < mentions.size(); i++) { const QJsonObject mention = mentions.at(i).toObject(); mMentions.insert(mention.value(QLatin1String("username")).toString(), mention.value(QLatin1String("_id")).toString()); } } void Message::parseUrls(const QJsonArray &urls) { mUrls.clear(); if (!urls.isEmpty()) { qCDebug(RUQOLA_LOG) << " void Message::urls(const QJsonObject &attachements)"< Message::mentions() const { return mMentions; } void Message::setMentions(const QMap &mentions) { mMentions = mentions; } void Message::parseAttachment(const QJsonArray &attachments) { mAttachements.clear(); if (!attachments.isEmpty()) { //qDebug() << " void Message::parseAttachment(const QJsonObject &attachements)"<convert to normaltext - mMessageType = Message::MessageType::NormalText; - } + //Use default value + messageAttachement.setImageHeight(120); + messageAttachement.setImageWidth(120); } } messageAttachement.setAuthorName(attachment.value(QLatin1String("author_name")).toString()); //Color const QJsonValue color = attachment.value(QLatin1String("color")); if (!color.isUndefined()) { messageAttachement.setColor(color.toString()); } //MimeType messageAttachement.setMimeType(attachment.value(QLatin1String("image_type")).toString()); if (!messageAttachement.isEmpty()) { mAttachements.append(messageAttachement); } } } } bool Message::operator==(const Message &other) const { return (mMessageId == other.messageId()) && (mRoomId == other.roomId()) && (mText == other.text()) && (mTimeStamp == other.timeStamp()) && (mUsername == other.username()) && (mUserId == other.userId()) && (mUpdatedAt == other.updatedAt()) && (mEditedAt == other.editedAt()) && (mEditedByUsername == other.editedByUsername()) && (mEditedByUserId == other.editedByUserId()) && (mAlias == other.alias()) && (mAvatar == other.avatar()) && (mSystemMessageType == other.systemMessageType()) && (mGroupable == other.groupable()) && (mParseUrls == other.parseUrls()) && (mUrls == other.urls()) && (mAttachements == other.attachements()) && (mMentions == other.mentions()) && (mRole == other.role()) && (mReactions == other.reactions()) && (mUnread == other.unread()) && (mMessagePinned == other.messagePinned()) && (mMessageStarred == other.messageStarred()) && (mThreadCount == other.threadCount()) && (mThreadLastMessage == other.threadLastMessage()) && (mDiscussionCount == other.discussionCount()) && (mDiscussionLastMessage == other.discussionLastMessage()) && (mDiscussionRoomId == other.discussionRoomId()) && (mThreadMessageId == other.threadMessageId()) && (mMessageTranslation == other.messageTranslation()) && (mShowTranslatedMessage == other.showTranslatedMessage()); } bool Message::operator<(const Message &other) const { return mTimeStamp < other.mTimeStamp; } QString Message::systemMessageType() const { return mSystemMessageType; } void Message::setSystemMessageType(const QString &systemMessageType) { mSystemMessageType = systemMessageType; } Message::MessageType Message::messageType() const { return mMessageType; } QString Message::systemMessageText() const { if (mSystemMessageType == QLatin1String("uj")) { return i18n("%1 has joined the channel", mUsername); } else if (mSystemMessageType == QLatin1String("ul")) { return i18n("%1 has left the channel", mUsername); } else if (mSystemMessageType == QLatin1String("room_changed_topic")) { if (mText.isEmpty()) { return i18n("Topic was cleared by: %1", mUsername); } else { return i18n("%2 changed topic to: %1", mText, mUsername); } } else if (mSystemMessageType == QLatin1String("au")) { return i18n("%2 added %1 to the conversation", mText, mUsername); } else if (mSystemMessageType == QLatin1Char('r')) { return i18n("%2 changed room name to #%1", mText, mUsername); } else if (mSystemMessageType == QLatin1String("ru")) { return i18n("%2 removed user %1", mText, mUsername); } else if (mSystemMessageType == QLatin1String("room_changed_description")) { if (mText.isEmpty()) { return i18n("Description was cleared by %1", mUsername); } else { return i18n("%2 changed room description to %1", mText, mUsername); } } else if (mSystemMessageType == QLatin1String("room_changed_announcement")) { if (mText.isEmpty()) { return i18n("Announcement was cleared by %1", mUsername); } else { return i18n("%2 changed room announcement to %1", mText, mUsername); } } else if (mSystemMessageType == QLatin1String("room_changed_privacy")) { return i18n("%2 changed room privacy to %1", mText, mUsername); } else if (mSystemMessageType == QLatin1String("jitsi_call_started")) { return QStringLiteral("") + i18n("Click to join to video") + QStringLiteral(""); } else if (mSystemMessageType == QLatin1String("rm")) { //TODO encrypted message return i18n("Message Deleted"); } else if (mSystemMessageType == QLatin1String("message_pinned")) { return i18n("Message Pinned"); } else if (mSystemMessageType == QLatin1String("otr")) { return i18n("Encrypted Message"); } else if (mSystemMessageType == QLatin1String("user-unmuted")) { //TODO improve it return i18n("%1 was unmuted", mUsername); } else if (mSystemMessageType == QLatin1String("user-muted")) { //TODO improve it return i18n("%1 was muted", mUsername); } else if (mSystemMessageType == QLatin1String("subscription-role-added")) { return i18n("Role \'%3\' was added to %1 by %2", mUsername, mText, mRole); } else if (mSystemMessageType == QLatin1String("subscription-role-removed")) { return i18n("Role \'%3\' was removed to %1 by %2", mUsername, mText, mRole); } else if (mSystemMessageType == QLatin1String("e2e")) { //TODO need to unencrypt it return i18n("Encrypted message: %1", mText); } else if (mSystemMessageType == QLatin1String("discussion-created")) { return i18n("Discussion created about \"%1\"", mText); } else if (mSystemMessageType == QLatin1String("ut")) { return i18n("%1 has joined the conversation", mUsername); } else if (mSystemMessageType == QLatin1String("room-archived")) { return i18n("This room has been archived by %1", mUsername); } else if (mSystemMessageType == QLatin1String("room-unarchived")) { return i18n("This room has been unarchived by %1", mUsername); } else if (mSystemMessageType == QLatin1String("rtc")) { qCWarning(RUQOLA_LOG) << "Need to implement : " << mSystemMessageType << " mText " << mText; return i18n("Unknown action!"); } else if (mSystemMessageType == QLatin1String("wm")) { //TODO verify qCWarning(RUQOLA_LOG) << "Need to implement : " << mSystemMessageType << " mText " << mText; return i18n("Welcome %1!", mUsername); } else { qCWarning(RUQOLA_LOG) << "Unknown type for message: type: " << mSystemMessageType << " mText " << mText; return i18n("Unknown action!"); } } void Message::setMessageType(const MessageType &messageType) { mMessageType = messageType; } QVector Message::attachements() const { return mAttachements; } void Message::setAttachements(const QVector &attachements) { mAttachements = attachements; } QVector Message::urls() const { return mUrls; } void Message::setUrls(const QVector &urls) { mUrls = urls; } QString Message::alias() const { return mAlias; } void Message::setAlias(const QString &alias) { mAlias = alias; } QString Message::editedByUserId() const { return mEditedByUserId; } void Message::setEditedByUserId(const QString &editedByUserId) { mEditedByUserId = editedByUserId; } QString Message::editedByUsername() const { return mEditedByUsername; } void Message::setEditedByUsername(const QString &editedByUsername) { mEditedByUsername = editedByUsername; } bool Message::wasEdited() const { return !mEditedByUsername.isEmpty(); } qint64 Message::editedAt() const { return mEditedAt; } void Message::setEditedAt(const qint64 &editedAt) { mEditedAt = editedAt; } qint64 Message::updatedAt() const { return mUpdatedAt; } void Message::setUpdatedAt(const qint64 &updatedAt) { mUpdatedAt = updatedAt; } QString Message::userId() const { return mUserId; } void Message::setUserId(const QString &userId) { mUserId = userId; } QString Message::username() const { return mUsername; } void Message::setUsername(const QString &username) { mUsername = username; } qint64 Message::timeStamp() const { return mTimeStamp; } void Message::setTimeStamp(const qint64 &timeStamp) { if (mTimeStamp != timeStamp) { mTimeStamp = timeStamp; mDisplayTime = QDateTime::fromMSecsSinceEpoch(mTimeStamp).time().toString(QStringLiteral("hh:mm")); } } QString Message::text() const { return mText; } void Message::setText(const QString &text) { mText = text; } QString Message::messageId() const { return mMessageId; } void Message::setMessageId(const QString &messageId) { mMessageId = messageId; } QString Message::roomId() const { return mRoomId; } void Message::setRoomId(const QString &roomId) { mRoomId = roomId; } QString Message::avatar() const { return mAvatar; } void Message::setAvatar(const QString &avatar) { mAvatar = avatar; } bool Message::parseUrls() const { return mParseUrls; } void Message::setParseUrls(bool parseUrls) { mParseUrls = parseUrls; } bool Message::groupable() const { return mGroupable; } void Message::setGroupable(bool groupable) { mGroupable = groupable; } Message Message::fromJSon(const QJsonObject &o) { Message message; message.mThreadCount = o[QStringLiteral("tcount")].toString().toInt(); message.mDiscussionCount = o[QStringLiteral("dcount")].toString().toInt(); message.mDiscussionRoomId = o[QStringLiteral("drid")].toString(); message.mThreadMessageId = o[QStringLiteral("tmid")].toString(); if (o.contains(QLatin1String("tlm"))) { message.mThreadLastMessage = static_cast(o[QStringLiteral("tlm")].toDouble()); } if (o.contains(QLatin1String("dlm"))) { message.mDiscussionLastMessage = static_cast(o[QStringLiteral("dlm")].toDouble()); } message.mMessageId = o[QStringLiteral("messageID")].toString(); message.mRoomId = o[QStringLiteral("roomID")].toString(); message.mText = o[QStringLiteral("message")].toString(); message.mTimeStamp = static_cast(o[QStringLiteral("timestamp")].toDouble()); message.mUsername = o[QStringLiteral("username")].toString(); message.mUserId = o[QStringLiteral("userID")].toString(); message.mUpdatedAt = static_cast(o[QStringLiteral("updatedAt")].toDouble()); message.mEditedAt = static_cast(o[QStringLiteral("editedAt")].toDouble()); message.mEditedByUsername = o[QStringLiteral("editedByUsername")].toString(); message.mEditedByUserId = o[QStringLiteral("editedByUserID")].toString(); message.mAlias = o[QStringLiteral("alias")].toString(); message.mAvatar = o[QStringLiteral("avatar")].toString(); message.mGroupable = o[QStringLiteral("groupable")].toBool(); message.mParseUrls = o[QStringLiteral("parseUrls")].toBool(); message.mMessageStarred.setIsStarred(o[QStringLiteral("starred")].toBool()); message.mMessagePinned.setPinned(o[QStringLiteral("pinned")].toBool()); message.mRole = o[QStringLiteral("role")].toString(); message.mSystemMessageType = o[QStringLiteral("type")].toString(); message.mMessageType = o[QStringLiteral("messageType")].toVariant().value(); const QJsonArray attachmentsArray = o.value(QLatin1String("attachments")).toArray(); for (int i = 0; i < attachmentsArray.count(); ++i) { const QJsonObject attachment = attachmentsArray.at(i).toObject(); const MessageAttachment att = MessageAttachment::fromJSon(attachment); if (!att.isEmpty()) { message.mAttachements.append(att); } } const QJsonArray urlsArray = o.value(QLatin1String("urls")).toArray(); for (int i = 0; i < urlsArray.count(); ++i) { const QJsonObject urlObj = urlsArray.at(i).toObject(); const MessageUrl url = MessageUrl::fromJSon(urlObj); if (!url.isEmpty()) { message.mUrls.append(url); } } const QJsonObject reactionsArray = o.value(QLatin1String("reactions")).toObject(); message.setReactions(Reactions::fromJSon(reactionsArray)); const QJsonArray mentionsArray = o.value(QLatin1String("mentions")).toArray(); for (int i = 0; i < mentionsArray.count(); ++i) { const QJsonObject mention = mentionsArray.at(i).toObject(); qCDebug(RUQOLA_LOG) << " mention"< -1) { o[QStringLiteral("tlm")] = message.mThreadLastMessage; } if (message.mDiscussionLastMessage > -1) { o[QStringLiteral("dlm")] = message.mDiscussionLastMessage; } o[QStringLiteral("editedByUsername")] = message.mEditedByUsername; o[QStringLiteral("editedByUserID")] = message.mEditedByUserId; o[QStringLiteral("alias")] = message.mAlias; o[QStringLiteral("avatar")] = message.mAvatar; o[QStringLiteral("groupable")] = message.mGroupable; o[QStringLiteral("parseUrls")] = message.mParseUrls; o[QStringLiteral("starred")] = message.mMessageStarred.isStarred(); o[QStringLiteral("pinned")] = message.mMessagePinned.pinned(); //TODO add pinned if (!message.mRole.isEmpty()) { o[QStringLiteral("role")] = message.mRole; } o[QStringLiteral("type")] = message.mSystemMessageType; o[QStringLiteral("messageType")] = QJsonValue::fromVariant(QVariant::fromValue(message.mMessageType)); //TODO add mentions //Attachments if (!message.mAttachements.isEmpty()) { QJsonArray array; const int nbAttachment = message.mAttachements.count(); for (int i = 0; i < nbAttachment; ++i) { array.append(MessageAttachment::serialize(message.mAttachements.at(i))); } o[QStringLiteral("attachments")] = array; } //FIXME save mentions QMapIterator i(message.mentions()); while (i.hasNext()) { i.next(); //TODO } //Urls if (!message.mUrls.isEmpty()) { QJsonArray array; const int nbUrls = message.mUrls.count(); for (int i = 0; i < nbUrls; ++i) { array.append(MessageUrl::serialize(message.mUrls.at(i))); } o[QStringLiteral("urls")] = array; } if (!message.reactions().isEmpty()) { o[QStringLiteral("reactions")] = Reactions::serialize(message.reactions()); } if (message.mThreadCount > 0) { o[QStringLiteral("tcount")] = message.mThreadCount; o[QStringLiteral("tlm")] = message.mThreadLastMessage; } if (message.mDiscussionCount > 0) { o[QStringLiteral("dcount")] = message.mDiscussionCount; o[QStringLiteral("dlm")] = message.mDiscussionLastMessage; } if (!message.mDiscussionRoomId.isEmpty()) { o[QStringLiteral("drid")] = message.mDiscussionRoomId; } if (!message.mThreadMessageId.isEmpty()) { o[QStringLiteral("tmid")] = message.mThreadMessageId; } d.setObject(o); if (toBinary) { return d.toBinaryData(); } //TODO add message translation return d.toJson(QJsonDocument::Indented); } QDebug operator <<(QDebug d, const Message &t) { d << "mMessageId : " << t.messageId(); d << "mText: " << t.text(); d << "mTimeStamp: " << t.timeStamp(); d << "mUsername: " << t.username(); d << "mUserId: " << t.userId(); d << "mUpdatedAt: " << t.updatedAt(); d << "mEditedAt: " << t.editedAt(); d << "mEditedByUsername: " << t.editedByUsername(); d << "mEditedByUserId: " << t.editedByUserId(); d << "mAlias: " << t.alias(); d << "mSystemMessageType: " << t.systemMessageType(); d << "mRoomId: " << t.roomId(); d << "mAvatar: " << t.avatar(); d << "mGroupable: " << t.groupable(); d << "mParseUrls: " << t.parseUrls(); for (int i = 0, total = t.attachements().count(); i < total; ++i) { d << "Attachment :" << t.attachements().at(i); } for (int i = 0, total = t.urls().count(); i < total; ++i) { d << "Urls :" << t.urls().at(i); } d << "Mentions :" << t.mentions(); d << "mMessageType: " << t.messageType(); d << "mRole: " << t.role(); d << "mReaction: " << t.reactions(); d << "mUnread: " << t.unread(); d << "starred " << t.messageStarred(); d << "pinned " << t.messagePinned(); d << "threadcount " << t.threadCount(); d << "threadlastmessage " << t.threadLastMessage(); d << "discussioncount " << t.discussionCount(); d << "discussionlastmessage " << t.discussionLastMessage(); d << "discussionRoomId " << t.discussionRoomId(); d << "threadMessageId " << t.threadMessageId(); d << "messagetranslation" << t.messageTranslation(); d << "mShowOriginalMessage " << t.showTranslatedMessage(); return d; }