From dc33cea58f4b920f60648e81379c58bd62861b9b Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 11 Apr 2017 16:14:24 +0200 Subject: Copy mail back from mailtransport --- .../mailtransportresource.cpp | 46 +++++++++++++++++++++- .../tests/mailtransporttest.cpp | 34 +++++++++++++++- 2 files changed, 76 insertions(+), 4 deletions(-) diff --git a/examples/mailtransportresource/mailtransportresource.cpp b/examples/mailtransportresource/mailtransportresource.cpp index 75d9898..8e5b22e 100644 --- a/examples/mailtransportresource/mailtransportresource.cpp +++ b/examples/mailtransportresource/mailtransportresource.cpp @@ -43,6 +43,44 @@ SINK_DEBUG_AREA("mailtransportresource") using namespace Sink; +class MailtransportPreprocessor : public Sink::Preprocessor +{ +public: + MailtransportPreprocessor() : Sink::Preprocessor() {} + + QByteArray getTargetResource() + { + using namespace Sink::ApplicationDomain; + + auto resource = Store::readOne(Query{}.filter(resourceInstanceIdentifier()).request()); + if (resource.identifier().isEmpty()) { + SinkWarning() << "Failed to retrieve this resource: " << resourceInstanceIdentifier(); + } + Query query; + query.containsFilter(ApplicationDomain::ResourceCapabilities::Mail::sent); + query.filter(resource.getAccount()); + auto targetResource = Store::readOne(query); + if (targetResource.identifier().isEmpty()) { + SinkWarning() << "Failed to find target resource: " << targetResource.identifier(); + } + return targetResource.identifier(); + } + + void modifiedEntity(const Sink::ApplicationDomain::ApplicationDomainType &oldEntity, Sink::ApplicationDomain::ApplicationDomainType &newEntity) Q_DECL_OVERRIDE + { + using namespace Sink::ApplicationDomain; + auto mail = newEntity.cast(); + if (mail.changedProperties().contains(Mail::Trash::name)) { + //Move back to regular resource + newEntity.setResource(getTargetResource()); + } else if (mail.changedProperties().contains(Mail::Draft::name)) { + //Move back to regular resource + newEntity.setResource(getTargetResource()); + } + //TODO this will only copy it back, but not yet move + } +}; + class MailtransportSynchronizer : public Sink::Synchronizer { public: MailtransportSynchronizer(const Sink::ResourceContext &resourceContext) @@ -65,7 +103,11 @@ public: msg->setHead(KMime::CRLFtoLF(data)); msg->parse(); if (settings.testMode) { - SinkLog() << "I would totally send that mail, but I'm in test mode." << mail.identifier(); + auto subject = msg->subject(true)->asUnicodeString(); + SinkLog() << "I would totally send that mail, but I'm in test mode." << mail.identifier() << subject; + if (!subject.contains("send")) { + return KAsync::error("Failed to send the message."); + } auto path = resourceStorageLocation(mResourceInstanceIdentifier) + "/test/"; SinkTrace() << path; QDir dir; @@ -194,7 +236,7 @@ MailtransportResource::MailtransportResource(const Sink::ResourceContext &resour setupSynchronizer(synchronizer); setupInspector(QSharedPointer::create(resourceContext)); - setupPreprocessors(ENTITY_TYPE_MAIL, QVector() << new MailPropertyExtractor); + setupPreprocessors(ENTITY_TYPE_MAIL, QVector() << new MailPropertyExtractor << new MailtransportPreprocessor); } MailtransportResourceFactory::MailtransportResourceFactory(QObject *parent) diff --git a/examples/mailtransportresource/tests/mailtransporttest.cpp b/examples/mailtransportresource/tests/mailtransporttest.cpp index 23a61b8..a30fc20 100644 --- a/examples/mailtransportresource/tests/mailtransporttest.cpp +++ b/examples/mailtransportresource/tests/mailtransporttest.cpp @@ -59,7 +59,7 @@ private slots: { auto message = KMime::Message::Ptr::create(); message->messageID(true)->generate("foo.com"); - message->subject(true)->fromUnicodeString(QString::fromLatin1("Foobar"), "utf8"); + message->subject(true)->fromUnicodeString(QString::fromLatin1("send: Foobar"), "utf8"); message->assemble(); auto mail = ApplicationDomain::Mail::create(mResourceInstanceIdentifier); @@ -83,7 +83,37 @@ private slots: QVERIFY(!mailInSentMailFolder.getSubject().isEmpty()); } - //TODO test mail that fails to be sent. add a special header to the mail and have the resource fail sending. Ensure we can modify the mail to fix sending of the message. + void testSendFailure() + { + auto message = KMime::Message::Ptr::create(); + message->messageID(true)->generate("foo.com"); + message->subject(true)->fromUnicodeString(QString::fromLatin1("error: Foobar"), "utf8"); + message->assemble(); + + auto mail = ApplicationDomain::Mail::create(mResourceInstanceIdentifier); + mail.setMimeMessage(message->encodedContent()); + + VERIFYEXEC(Store::create(mail)); + VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier)); + + //Ensure the mail is queryable in the outbox + auto mailInOutbox = Store::readOne(Query().resourceFilter(mResourceInstanceIdentifier).filter(false)); + QVERIFY(!mailInOutbox.identifier().isEmpty()); + + //Modify back to drafts + auto modifiedMail = mailInOutbox; + modifiedMail.setDraft(true); + VERIFYEXEC(Store::modify(modifiedMail)); + VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier)); + + QTest::qWait(100); + // auto mailsInOutbox = Store::read(Query().resourceFilter(mResourceInstanceIdentifier)); + // QCOMPARE(mailsInOutbox.size(), 0); + + auto mailsInDrafts = Store::read(Query().resourceFilter(mStorageResource)); + QCOMPARE(mailsInDrafts.size(), 1); + + } }; -- cgit v1.2.3