File indexing completed on 2024-10-06 04:34:02

0001 /*
0002    SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "messageurl.h"
0008 #include "ruqolaglobalconfig.h"
0009 #include <QJsonObject>
0010 
0011 MessageUrl::MessageUrl()
0012 {
0013     mShowPreview = RuqolaGlobalConfig::self()->showPreviewUrl();
0014 }
0015 
0016 QStringList MessageUrl::pageTitleElements()
0017 {
0018     return {QStringLiteral("ogTitle"), QStringLiteral("twitterTitle"), QStringLiteral("title"), QStringLiteral("pageTitle"), QStringLiteral("oembedTitle")};
0019 }
0020 
0021 QStringList MessageUrl::descriptionElements()
0022 {
0023     return {QStringLiteral("ogDescription"), QStringLiteral("twitterDescription"), QStringLiteral("description")};
0024 }
0025 
0026 QStringList MessageUrl::imageUrlElements()
0027 {
0028     return {QStringLiteral("ogImage"), QStringLiteral("twitterImage"), QStringLiteral("msapplicationTileImage"), QStringLiteral("oembedThumbnailUrl")};
0029 }
0030 
0031 QStringList MessageUrl::siteUrlElements()
0032 {
0033     return {QStringLiteral("ogUrl"), QStringLiteral("oembedProviderUrl")};
0034 }
0035 
0036 QStringList MessageUrl::siteNameElements()
0037 {
0038     return {QStringLiteral("ogSiteName"), QStringLiteral("oembedProviderName")};
0039 }
0040 
0041 QStringList MessageUrl::imageHeightElements()
0042 {
0043     return {QStringLiteral("ogImageHeight"), QStringLiteral("oembedHeight"), QStringLiteral("oembedThumbnailHeight")};
0044 }
0045 
0046 QStringList MessageUrl::imageWidthElements()
0047 {
0048     return {QStringLiteral("ogImageWidth"), QStringLiteral("oembedWidth"), QStringLiteral("oembedThumbnailWidth")};
0049 }
0050 
0051 bool MessageUrl::showPreview() const
0052 {
0053     return mShowPreview;
0054 }
0055 
0056 void MessageUrl::setShowPreview(bool newShowPreview)
0057 {
0058     mShowPreview = newShowPreview;
0059 }
0060 
0061 QString MessageUrl::cleanText(const QString &str)
0062 {
0063     QString newStr = str;
0064     newStr.remove(QLatin1Char('\n'));
0065     return newStr;
0066 }
0067 
0068 void MessageUrl::generateHtmlDescription()
0069 {
0070     mHtmlDescription.clear();
0071     if (!mPageTitle.isEmpty()) {
0072         mHtmlDescription = QStringLiteral("[%1](%2)").arg(MessageUrl::cleanText(mPageTitle), mUrl);
0073     }
0074     if (!mDescription.isEmpty()) {
0075         mHtmlDescription += QStringLiteral("\n%1").arg(MessageUrl::cleanText(mDescription));
0076     }
0077     if (!mSiteName.isEmpty()) {
0078         mHtmlDescription += QStringLiteral("\n[%1](%2)").arg(mSiteName, mSiteUrl);
0079     }
0080 }
0081 
0082 bool MessageUrl::hasHtmlDescription() const
0083 {
0084     return !mHtmlDescription.isEmpty();
0085 }
0086 
0087 QString MessageUrl::htmlDescription() const
0088 {
0089     return mHtmlDescription;
0090 }
0091 
0092 bool MessageUrl::hasPreviewUrl() const
0093 {
0094     if (!mPageTitle.isEmpty() || !mDescription.isEmpty()) {
0095         return true;
0096     }
0097     if (!mImageUrl.isEmpty()) {
0098         return true;
0099     }
0100     return false;
0101 }
0102 
0103 QString MessageUrl::urlId() const
0104 {
0105     return mUrlId;
0106 }
0107 
0108 void MessageUrl::setUrlId(const QString &newUrlId)
0109 {
0110     mUrlId = newUrlId;
0111 }
0112 
0113 int MessageUrl::imageWidth() const
0114 {
0115     return mImageWidth;
0116 }
0117 
0118 void MessageUrl::setImageWidth(int newImageWidth)
0119 {
0120     mImageWidth = newImageWidth;
0121 }
0122 
0123 int MessageUrl::imageHeight() const
0124 {
0125     return mImageHeight;
0126 }
0127 
0128 void MessageUrl::setImageHeight(int newImageHeight)
0129 {
0130     mImageHeight = newImageHeight;
0131 }
0132 
0133 QString MessageUrl::siteName() const
0134 {
0135     return mSiteName;
0136 }
0137 
0138 void MessageUrl::setSiteName(const QString &newSiteName)
0139 {
0140     mSiteName = newSiteName;
0141 }
0142 
0143 QString MessageUrl::siteUrl() const
0144 {
0145     return mSiteUrl;
0146 }
0147 
0148 void MessageUrl::setSiteUrl(const QString &newSiteUrl)
0149 {
0150     mSiteUrl = newSiteUrl;
0151 }
0152 
0153 QString MessageUrl::authorUrl() const
0154 {
0155     return mAuthorUrl;
0156 }
0157 
0158 void MessageUrl::setAuthorUrl(const QString &newAuthorUrl)
0159 {
0160     mAuthorUrl = newAuthorUrl;
0161 }
0162 
0163 QString MessageUrl::authorName() const
0164 {
0165     return mAuthorName;
0166 }
0167 
0168 void MessageUrl::setAuthorName(const QString &newAuthorName)
0169 {
0170     mAuthorName = newAuthorName;
0171 }
0172 
0173 QString MessageUrl::imageUrl() const
0174 {
0175     return mImageUrl;
0176 }
0177 
0178 void MessageUrl::setImageUrl(const QString &newImageUrl)
0179 {
0180     mImageUrl = newImageUrl;
0181 }
0182 
0183 void MessageUrl::parseUrl(const QJsonObject &url)
0184 {
0185     const QJsonValue urlStr = url.value(QLatin1String("url"));
0186     if (!urlStr.isUndefined()) {
0187         setUrl(urlStr.toString());
0188     }
0189     const QJsonObject meta = url.value(QLatin1String("meta")).toObject();
0190     if (!meta.isEmpty()) {
0191         for (const QString &element : pageTitleElements()) {
0192             const QJsonValue pageTitleStr = meta.value(element);
0193             if (!pageTitleStr.isUndefined()) {
0194                 setPageTitle(pageTitleStr.toString());
0195                 break;
0196             }
0197         }
0198         for (const QString &element : descriptionElements()) {
0199             const QJsonValue descriptionStr = meta.value(element);
0200             if (!descriptionStr.isUndefined()) {
0201                 setDescription(descriptionStr.toString());
0202                 break;
0203             }
0204         }
0205         for (const QString &element : imageUrlElements()) {
0206             const QJsonValue imageUrlStr = meta.value(element);
0207             if (!imageUrlStr.isUndefined()) {
0208                 setImageUrl(imageUrlStr.toString());
0209                 break;
0210             }
0211         }
0212         const QJsonValue authorName = meta.value(QLatin1String("oembedAuthorName"));
0213         if (!authorName.isUndefined()) {
0214             setAuthorName(authorName.toString());
0215         }
0216         const QJsonValue authorUrl = meta.value(QLatin1String("oembedAuthorUrl"));
0217         if (!authorUrl.isUndefined()) {
0218             setAuthorUrl(authorUrl.toString());
0219         }
0220         for (const QString &element : siteUrlElements()) {
0221             const QJsonValue siteUrlStr = meta.value(element);
0222             if (!siteUrlStr.isUndefined()) {
0223                 setSiteUrl(siteUrlStr.toString());
0224                 break;
0225             }
0226         }
0227         for (const QString &element : siteNameElements()) {
0228             const QJsonValue siteNameStr = meta.value(element);
0229             if (!siteNameStr.isUndefined()) {
0230                 setSiteName(siteNameStr.toString());
0231                 break;
0232             }
0233         }
0234         for (const QString &element : imageHeightElements()) {
0235             const QJsonValue imageHeightStr = meta.value(element);
0236             if (!imageHeightStr.isUndefined()) {
0237                 setImageHeight(imageHeightStr.toString().toInt());
0238                 break;
0239             }
0240         }
0241         for (const QString &element : imageWidthElements()) {
0242             const QJsonValue imageWidthStr = meta.value(element);
0243             if (!imageWidthStr.isUndefined()) {
0244                 setImageWidth(imageWidthStr.toString().toInt());
0245                 break;
0246             }
0247         }
0248     }
0249 
0250     generateHtmlDescription();
0251     // qDebug() << " *this " << *this << " is empty " << isEmpty() << " url" << url;
0252     // Use apps/meteor/client/components/message/content/UrlPreviews.tsx
0253 }
0254 
0255 QJsonObject MessageUrl::serialize(const MessageUrl &url)
0256 {
0257     QJsonObject obj;
0258     obj[QLatin1String("pageTitle")] = url.pageTitle();
0259     obj[QLatin1String("url")] = url.url();
0260     obj[QLatin1String("description")] = url.description();
0261     if (!url.imageUrl().isEmpty()) {
0262         obj[QLatin1String("imageUrl")] = url.imageUrl();
0263     }
0264     if (!url.authorName().isEmpty()) {
0265         obj[QLatin1String("authorName")] = url.authorName();
0266     }
0267     if (!url.authorUrl().isEmpty()) {
0268         obj[QLatin1String("authorUrl")] = url.authorUrl();
0269     }
0270     if (!url.siteUrl().isEmpty()) {
0271         obj[QLatin1String("siteUrl")] = url.siteUrl();
0272     }
0273     if (!url.siteName().isEmpty()) {
0274         obj[QLatin1String("siteName")] = url.siteName();
0275     }
0276     if (url.imageHeight() > -1) {
0277         obj[QLatin1String("imageHeight")] = url.imageHeight();
0278     }
0279     if (url.imageWidth() > -1) {
0280         obj[QLatin1String("imageWidth")] = url.imageWidth();
0281     }
0282     return obj;
0283 }
0284 
0285 MessageUrl MessageUrl::deserialize(const QJsonObject &o)
0286 {
0287     MessageUrl url;
0288     url.setPageTitle(o.value(QLatin1String("pageTitle")).toString());
0289     url.setUrl(o.value(QLatin1String("url")).toString());
0290     url.setDescription(o.value(QLatin1String("description")).toString());
0291     url.setImageUrl(o.value(QLatin1String("imageUrl")).toString());
0292     url.setAuthorName(o.value(QLatin1String("authorName")).toString());
0293     url.setAuthorUrl(o.value(QLatin1String("authorUrl")).toString());
0294     url.setSiteUrl(o.value(QLatin1String("siteUrl")).toString());
0295     url.setSiteName(o.value(QLatin1String("siteName")).toString());
0296     url.setImageHeight(o.value(QLatin1String("imageHeight")).toInt(-1));
0297     url.setImageWidth(o.value(QLatin1String("imageWidth")).toInt(-1));
0298     url.generateHtmlDescription();
0299     return url;
0300 }
0301 
0302 QString MessageUrl::url() const
0303 {
0304     return mUrl;
0305 }
0306 
0307 void MessageUrl::setUrl(const QString &url)
0308 {
0309     mUrl = url;
0310 }
0311 
0312 QString MessageUrl::pageTitle() const
0313 {
0314     return mPageTitle;
0315 }
0316 
0317 void MessageUrl::setPageTitle(const QString &pageTitle)
0318 {
0319     mPageTitle = pageTitle;
0320 }
0321 
0322 bool MessageUrl::isEmpty() const
0323 {
0324     return mUrl.isEmpty();
0325 }
0326 
0327 QString MessageUrl::description() const
0328 {
0329     return mDescription;
0330 }
0331 
0332 void MessageUrl::setDescription(const QString &description)
0333 {
0334     mDescription = description;
0335 }
0336 
0337 bool MessageUrl::operator==(const MessageUrl &other) const
0338 {
0339     return (mUrl == other.url()) && (mPageTitle == other.pageTitle()) && (mDescription == other.description()) && (mImageUrl == other.imageUrl())
0340         && (mAuthorName == other.authorName()) && (mAuthorUrl == other.authorUrl()) && (mSiteUrl == other.siteUrl()) && (mSiteName == other.siteName())
0341         && (mImageHeight == other.imageHeight()) && (mImageWidth == other.imageWidth()) && (mUrlId == urlId());
0342 }
0343 
0344 QDebug operator<<(QDebug d, const MessageUrl &t)
0345 {
0346     d.space() << "Url:" << t.url();
0347     d.space() << "Page Title:" << t.pageTitle();
0348     d.space() << "Description:" << t.description();
0349     d.space() << "ImageUrl:" << t.imageUrl();
0350     d.space() << "AuthorName:" << t.authorName();
0351     d.space() << "AuthorUrl:" << t.authorUrl();
0352     d.space() << "SiteUrl:" << t.siteUrl();
0353     d.space() << "SiteName:" << t.siteName();
0354     d.space() << "ImageHeight:" << t.imageHeight();
0355     d.space() << "ImageWidth:" << t.imageWidth();
0356     d.space() << "UrlId:" << t.urlId();
0357     d.space() << "htmlDescription:" << t.htmlDescription();
0358     return d;
0359 }
0360 
0361 #include "moc_messageurl.cpp"