|
|
@ -627,56 +627,32 @@ TimelineViewManager::forwardMessageToRoom(mtx::events::collections::TimelineEven |
|
|
|
auto elem = *e; |
|
|
|
auto elem = *e; |
|
|
|
auto room = models.find(roomId); |
|
|
|
auto room = models.find(roomId); |
|
|
|
auto messageType = mtx::accessors::msg_type(elem); |
|
|
|
auto messageType = mtx::accessors::msg_type(elem); |
|
|
|
|
|
|
|
auto content = mtx::accessors::url(elem); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (sentFromEncrypted) { |
|
|
|
|
|
|
|
std::optional<mtx::crypto::EncryptedFile> encryptionInfo = |
|
|
|
|
|
|
|
mtx::accessors::file(elem); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
http::client()->download( |
|
|
|
|
|
|
|
content, |
|
|
|
|
|
|
|
[this, roomId, e, encryptionInfo](const std::string &res, |
|
|
|
|
|
|
|
const std::string &content_type, |
|
|
|
|
|
|
|
const std::string &originalFilename, |
|
|
|
|
|
|
|
mtx::http::RequestErr err) { |
|
|
|
|
|
|
|
if (err) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (sentFromEncrypted && messageType == mtx::events::MessageType::Image) { |
|
|
|
assert(encryptionInfo); |
|
|
|
auto body = mtx::accessors::body(elem); |
|
|
|
|
|
|
|
auto mimetype = mtx::accessors::mimetype(elem); |
|
|
|
|
|
|
|
auto imageHeight = mtx::accessors::media_height(elem); |
|
|
|
|
|
|
|
auto imageWidth = mtx::accessors::media_height(elem); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QString mxcUrl = QString::fromStdString(mtx::accessors::url(elem)); |
|
|
|
auto data = mtx::crypto::to_string( |
|
|
|
MxcImageProvider::download( |
|
|
|
mtx::crypto::decrypt_file(res, encryptionInfo.value())); |
|
|
|
mxcUrl.remove("mxc://"), |
|
|
|
|
|
|
|
QSize(imageWidth, imageHeight), |
|
|
|
|
|
|
|
[this, roomId, body, mimetype](QString, QSize, QImage image, QString) { |
|
|
|
|
|
|
|
QByteArray data = |
|
|
|
|
|
|
|
QByteArray::fromRawData((const char *)image.bits(), image.byteCount()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
auto payload = std::string(data.data(), data.size()); |
|
|
|
|
|
|
|
std::optional<mtx::crypto::EncryptedFile> encryptedFile; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QSize dimensions; |
|
|
|
|
|
|
|
QString blurhash; |
|
|
|
|
|
|
|
auto mimeClass = QString::fromStdString(mimetype).split("/")[0]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dimensions = image.size(); |
|
|
|
|
|
|
|
if (image.height() > 200 && image.width() > 360) |
|
|
|
|
|
|
|
image = image.scaled(360, 200, Qt::KeepAspectRatioByExpanding); |
|
|
|
|
|
|
|
std::vector<unsigned char> data_; |
|
|
|
|
|
|
|
for (int y = 0; y < image.height(); y++) { |
|
|
|
|
|
|
|
for (int x = 0; x < image.width(); x++) { |
|
|
|
|
|
|
|
auto p = image.pixel(x, y); |
|
|
|
|
|
|
|
data_.push_back(static_cast<unsigned char>(qRed(p))); |
|
|
|
|
|
|
|
data_.push_back(static_cast<unsigned char>(qGreen(p))); |
|
|
|
|
|
|
|
data_.push_back(static_cast<unsigned char>(qBlue(p))); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
blurhash = QString::fromStdString( |
|
|
|
|
|
|
|
blurhash::encode(data_.data(), image.width(), image.height(), 4, 3)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
http::client()->upload( |
|
|
|
http::client()->upload( |
|
|
|
payload, |
|
|
|
data, |
|
|
|
encryptedFile ? "application/octet-stream" : mimetype, |
|
|
|
content_type, |
|
|
|
body, |
|
|
|
originalFilename, |
|
|
|
[this, |
|
|
|
[this, roomId, e](const mtx::responses::ContentURI &res, |
|
|
|
roomId, |
|
|
|
|
|
|
|
filename = body, |
|
|
|
|
|
|
|
encryptedFile = std::move(encryptedFile), |
|
|
|
|
|
|
|
mimeClass, |
|
|
|
|
|
|
|
mimetype, |
|
|
|
|
|
|
|
size = payload.size(), |
|
|
|
|
|
|
|
dimensions, |
|
|
|
|
|
|
|
blurhash](const mtx::responses::ContentURI &res, |
|
|
|
|
|
|
|
mtx::http::RequestErr err) mutable { |
|
|
|
mtx::http::RequestErr err) mutable { |
|
|
|
if (err) { |
|
|
|
if (err) { |
|
|
|
nhlog::net()->warn("failed to upload media: {} {} ({})", |
|
|
|
nhlog::net()->warn("failed to upload media: {} {} ({})", |
|
|
@ -686,27 +662,37 @@ TimelineViewManager::forwardMessageToRoom(mtx::events::collections::TimelineEven |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
auto url = QString::fromStdString(res.content_uri); |
|
|
|
std::visit( |
|
|
|
if (encryptedFile) |
|
|
|
[this, roomId, e, url = res.content_uri](auto ev) { |
|
|
|
encryptedFile->url = res.content_uri; |
|
|
|
if constexpr (mtx::events::message_content_to_type< |
|
|
|
|
|
|
|
decltype(ev.content)> == |
|
|
|
|
|
|
|
mtx::events::EventType::RoomMessage) { |
|
|
|
|
|
|
|
if constexpr (messageWithFileAndUrl(ev)) { |
|
|
|
|
|
|
|
ev.content.relations.relations.clear(); |
|
|
|
|
|
|
|
ev.content.file.reset(); |
|
|
|
|
|
|
|
ev.content.url = url; |
|
|
|
|
|
|
|
|
|
|
|
auto r = models.find(roomId); |
|
|
|
auto room = models.find(roomId); |
|
|
|
r.value()->input()->image(QString::fromStdString(filename), |
|
|
|
room.value()->sendMessageEvent( |
|
|
|
encryptedFile, |
|
|
|
ev.content, |
|
|
|
url, |
|
|
|
mtx::events::EventType::RoomMessage); |
|
|
|
QString::fromStdString(mimetype), |
|
|
|
} |
|
|
|
size, |
|
|
|
} |
|
|
|
dimensions, |
|
|
|
}, |
|
|
|
blurhash); |
|
|
|
*e); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return; |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
return; |
|
|
|
return; |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::visit( |
|
|
|
std::visit( |
|
|
|
[room](auto e) { |
|
|
|
[room](auto e) { |
|
|
|
if constexpr (mtx::events::message_content_to_type<decltype(e.content)> == |
|
|
|
if constexpr (mtx::events::message_content_to_type<decltype(e.content)> == |
|
|
|
mtx::events::EventType::RoomMessage) { |
|
|
|
mtx::events::EventType::RoomMessage) { |
|
|
|
|
|
|
|
e.content.relations.relations.clear(); |
|
|
|
room.value()->sendMessageEvent(e.content, |
|
|
|
room.value()->sendMessageEvent(e.content, |
|
|
|
mtx::events::EventType::RoomMessage); |
|
|
|
mtx::events::EventType::RoomMessage); |
|
|
|
} |
|
|
|
} |
|
|
|