Skip to content

Commit

Permalink
Add functionality so that sendEvent can handle events with file content.
Browse files Browse the repository at this point in the history
  • Loading branch information
nvrWhere committed Nov 17, 2024
1 parent 8f5b4ea commit ae470e4
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions Quotient/room.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@
#include "events/callevents.h"
#include "events/encryptionevent.h"
#include "events/event.h"
#include "events/eventcontent.h"
#include "events/reactionevent.h"
#include "events/receiptevent.h"
#include "events/redactionevent.h"
#include "events/roomavatarevent.h"
#include "events/roomcanonicalaliasevent.h"
#include "events/roomcreateevent.h"
#include "events/roommemberevent.h"
#include "events/roommessageevent.h"
#include "events/roompowerlevelsevent.h"
#include "events/roomtombstoneevent.h"
#include "events/simplestateevents.h"
Expand Down Expand Up @@ -293,7 +295,7 @@ class Q_DECL_HIDDEN Room::Private {

const PendingEventItem& sendEvent(RoomEventPtr&& event);

QString doPostFile(event_ptr_tt<RoomMessageEvent> fileEvent, const QUrl& localUrl);
const PendingEventItem& doPostFile(PendingEvents::iterator eventItemIter, const QUrl& localUrl);

PendingEvents::iterator addAsPending(RoomEventPtr&& event);

Expand Down Expand Up @@ -1938,6 +1940,16 @@ Room::PendingEvents::iterator Room::Private::addAsPending(RoomEventPtr&& event)

const PendingEventItem& Room::Private::sendEvent(RoomEventPtr&& event)
{
if (const auto rme = eventCast<const RoomMessageEvent>(event);
rme->has<EventContent::FileContentBase>()
) {
const auto url = rme->get<EventContent::FileContentBase>()->url();
// toLocalFile() doesn't work on Android and toString() doesn't work on the desktop
QFileInfo localFile(url.isLocalFile() ? url.toLocalFile() : url.toString());
Q_ASSERT(localFile.isFile());
return doPostFile(addAsPending(std::move(event)), url);
}

return doSendEvent(addAsPending(std::move(event)));
}

Expand Down Expand Up @@ -2149,9 +2161,9 @@ QString Room::postReaction(const QString& eventId, const QString& key)
return post<ReactionEvent>(eventId, key)->transactionId();
}

QString Room::Private::doPostFile(event_ptr_tt<RoomMessageEvent> fileEvent, const QUrl& localUrl)
const PendingEventItem& Room::Private::doPostFile(PendingEvents::iterator eventItemIter, const QUrl& localUrl)
{
const auto txnId = addAsPending(std::move(fileEvent))->event()->transactionId();
const auto txnId = eventItemIter->event()->transactionId();
// Remote URL will only be known after upload; fill in the local path
// to enable the preview while the event is pending.
q->uploadFile(txnId, localUrl);
Expand Down Expand Up @@ -2194,7 +2206,7 @@ QString Room::Private::doPostFile(event_ptr_tt<RoomMessageEvent> fileEvent, cons
emit q->pendingEventDiscarded();
});

return txnId;
return *eventItemIter;
}

QString Room::postFile(const QString& plainText,
Expand All @@ -2206,10 +2218,10 @@ QString Room::postFile(const QString& plainText,
QFileInfo localFile(url.isLocalFile() ? url.toLocalFile() : url.toString());
Q_ASSERT(localFile.isFile());

return d->doPostFile(makeEvent<RoomMessageEvent>(plainText,
return d->doPostFile(d->addAsPending(makeEvent<RoomMessageEvent>(plainText,
RoomMessageEvent::rawMsgTypeForFile(localFile),
std::move(fileContent)),
url);
std::move(fileContent))),
url).event()->transactionId();
}

QString Room::postEvent(RoomEvent* event)
Expand Down

0 comments on commit ae470e4

Please sign in to comment.