Verified Commit 03d35df4 authored by Linus Jahn's avatar Linus Jahn 🍙

media-sharing: Check incoming messages for media links

If we had something like a download manager and a button to start
downloads in the UI, we could now also download audio files, images and
videos from manually inserted https-URLs.
parent f6e523f4
Pipeline #231 passed with stage
in 2 minutes and 27 seconds
......@@ -31,7 +31,9 @@
#include "MessageHandler.h"
// Qt
#include <QDateTime>
#include <QMimeDatabase>
#include <QString>
#include <QUrl>
// QXmpp
#include <QXmppClient.h>
#include <QXmppUtils.h>
......@@ -100,7 +102,33 @@ void MessageHandler::handleMessage(const QXmppMessage &msg)
entry.id = msg.id();
entry.sentByMe = (entry.author == client->configuration().jidBare());
entry.message = msg.body();
entry.type = MessageType::MessageText; // text message without media
entry.type = MessageType::MessageText; // default to text message without media
// check if message contains a link
QList<QString> bodyWords = msg.body().split(" ");
for (const QString &word : bodyWords) {
bool isLink = word.startsWith("https://") || word.startsWith("http://");
if (!isLink)
continue;
// check message type by file name in link
// This is hacky, but needed without SIMS or an additional HTTP request.
// Also, this can be useful when a user manually posts an HTTP url.
QUrl url(word);
QList<QMimeType> mediaTypes = QMimeDatabase().mimeTypesForFileName(url.fileName());
for (const QMimeType &type : mediaTypes) {
MessageType mType = MessageModel::messageTypeFromMimeType(type);
if (mType == MessageType::MessageImage ||
mType == MessageType::MessageAudio ||
mType == MessageType::MessageVideo) {
entry.type = mType;
entry.mediaContentType = type.name();
entry.mediaUrl = url.toEncoded();
break;
}
}
break; // we can only handle one link
}
// get possible delay (timestamp)
QDateTime stamp = msg.stamp();
......
......@@ -75,13 +75,6 @@ public:
static MessageType messageTypeFromMimeType(const QMimeType &);
signals:
/**
* Emit when the data has been changed
*
* This is somehow necessary because some changes don't have an effect on the QML side.
*/
void modelChanged();
/**
* Emitted when the user opens another chat to apply a filter to the db
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment