File indexing completed on 2024-04-28 16:11:05

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "parsemessageurlutils.h"
0008 #include "ruqola_debug.h"
0009 #include <QUrl>
0010 #include <QUrlQuery>
0011 
0012 ParseMessageUrlUtils::ParseMessageUrlUtils() = default;
0013 
0014 ParseMessageUrlUtils::~ParseMessageUrlUtils() = default;
0015 
0016 bool ParseMessageUrlUtils::parseUrl(const QString &messageUrl)
0017 {
0018     if (messageUrl.isEmpty()) {
0019         return false;
0020     }
0021     QUrl url(messageUrl);
0022     const QUrlQuery query(url);
0023     // const QList<QPair<QString, QString>> queryItems = query.queryItems();
0024     if (messageUrl.startsWith(QStringLiteral("https://go.rocket.chat/"))) {
0025         // qDebug() << "queryItems " << queryItems;
0026 
0027         mServerHost = query.queryItemValue(QStringLiteral("host"));
0028         mRoomId = query.queryItemValue(QStringLiteral("rid"));
0029         mMessageId = query.queryItemValue(QStringLiteral("mid"));
0030         mPath = query.queryItemValue(QStringLiteral("path"), QUrl::FullyDecoded);
0031         if (!mPath.isEmpty()) {
0032             mRoomIdType = RoomIdType::RoomId;
0033             if (mPath.startsWith(QStringLiteral("direct"))) {
0034                 mChannelType = ChannelType::Direct;
0035             } else if (mPath.startsWith(QStringLiteral("channel"))) {
0036                 mChannelType = ChannelType::Channel;
0037             } else if (mPath.startsWith(QStringLiteral("group"))) {
0038                 mChannelType = ChannelType::Group;
0039             } else {
0040                 qCWarning(RUQOLA_LOG) << "Unknown channel type " << mPath;
0041                 return false;
0042             }
0043         } else {
0044             return false;
0045         }
0046         return true;
0047     } else {
0048         // Example https://<server url>/channel/python?msg=sn3gEQom7NcLxTg5h
0049         mMessageId = query.queryItemValue(QStringLiteral("msg"));
0050         mServerHost = url.host();
0051         mPath = url.path(QUrl::FullyDecoded);
0052         mRoomIdType = RoomIdType::RoomName;
0053         url.setQuery(QUrlQuery());
0054         QString urlPathDecoded{url.path(QUrl::FullyDecoded)};
0055         if (urlPathDecoded.contains(QStringLiteral("/channel/"))) {
0056             mRoomId = urlPathDecoded.remove(QStringLiteral("/channel/"));
0057             mChannelType = ChannelType::Channel;
0058         } else if (urlPathDecoded.contains(QStringLiteral("/direct/"))) {
0059             mRoomId = urlPathDecoded.remove(QStringLiteral("/direct/"));
0060             mChannelType = ChannelType::Direct;
0061         } else if (urlPathDecoded.contains(QStringLiteral("/group/"))) {
0062             mRoomId = urlPathDecoded.remove(QStringLiteral("/group/"));
0063             mChannelType = ChannelType::Group;
0064         } else {
0065             mServerHost.clear();
0066             mPath.clear();
0067             mRoomIdType = RoomIdType::Unknown;
0068             return false;
0069         }
0070         return true;
0071     }
0072     return false;
0073 }
0074 
0075 const QString &ParseMessageUrlUtils::messageId() const
0076 {
0077     return mMessageId;
0078 }
0079 
0080 void ParseMessageUrlUtils::setMessageId(const QString &newMessageId)
0081 {
0082     mMessageId = newMessageId;
0083 }
0084 
0085 const QString &ParseMessageUrlUtils::roomId() const
0086 {
0087     return mRoomId;
0088 }
0089 
0090 void ParseMessageUrlUtils::setRoomId(const QString &newRoomId)
0091 {
0092     mRoomId = newRoomId;
0093 }
0094 
0095 const QString &ParseMessageUrlUtils::serverHost() const
0096 {
0097     return mServerHost;
0098 }
0099 
0100 void ParseMessageUrlUtils::setServerHost(const QString &newServerPath)
0101 {
0102     mServerHost = newServerPath;
0103 }
0104 
0105 const QString &ParseMessageUrlUtils::path() const
0106 {
0107     return mPath;
0108 }
0109 
0110 void ParseMessageUrlUtils::setPath(const QString &newPath)
0111 {
0112     mPath = newPath;
0113 }
0114 
0115 ParseMessageUrlUtils::RoomIdType ParseMessageUrlUtils::roomIdType() const
0116 {
0117     return mRoomIdType;
0118 }
0119 
0120 void ParseMessageUrlUtils::setRoomIdType(ParseMessageUrlUtils::RoomIdType newRoomIdType)
0121 {
0122     mRoomIdType = newRoomIdType;
0123 }
0124 
0125 ParseMessageUrlUtils::ChannelType ParseMessageUrlUtils::channelType() const
0126 {
0127     return mChannelType;
0128 }
0129 
0130 void ParseMessageUrlUtils::setChannelType(ChannelType newChannelType)
0131 {
0132     mChannelType = newChannelType;
0133 }
0134 
0135 QDebug operator<<(QDebug d, const ParseMessageUrlUtils &t)
0136 {
0137     d << "mServerPath " << t.serverHost();
0138     d << "mRoomId " << t.roomId();
0139     d << "mMessageId " << t.messageId();
0140     d << "mPath " << t.path();
0141     d << "roomIdType " << t.roomIdType();
0142     d << "channelType " << t.channelType();
0143     return d;
0144 }
0145 
0146 #include "moc_parsemessageurlutils.cpp"