summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/Elements')
-rw-r--r--Swiften/Elements/Delay.h2
-rw-r--r--Swiften/Elements/Stanza.cpp16
-rw-r--r--Swiften/Elements/Stanza.h12
-rw-r--r--Swiften/Elements/UnitTest/StanzaTest.cpp57
4 files changed, 80 insertions, 7 deletions
diff --git a/Swiften/Elements/Delay.h b/Swiften/Elements/Delay.h
index f59d729..3213037 100644
--- a/Swiften/Elements/Delay.h
+++ b/Swiften/Elements/Delay.h
@@ -16,7 +16,7 @@ namespace Swift {
class Delay : public Payload {
public:
Delay() {};
- Delay(const boost::posix_time::ptime& time, const JID& from) : time_(time), from_(from) {};
+ Delay(const boost::posix_time::ptime& time, const JID& from = JID()) : time_(time), from_(from) {};
const boost::posix_time::ptime& getStamp() const {return time_;};
void setStamp(const boost::posix_time::ptime& time) {time_ = time;};
diff --git a/Swiften/Elements/Stanza.cpp b/Swiften/Elements/Stanza.cpp
index dd6021d..d15d778 100644
--- a/Swiften/Elements/Stanza.cpp
+++ b/Swiften/Elements/Stanza.cpp
@@ -5,6 +5,7 @@
*/
#include "Swiften/Elements/Stanza.h"
+#include "Swiften/Elements/Delay.h"
#include <typeinfo>
@@ -33,5 +34,20 @@ boost::shared_ptr<Payload> Stanza::getPayloadOfSameType(boost::shared_ptr<Payloa
return boost::shared_ptr<Payload>();
}
+boost::optional<boost::posix_time::ptime> Stanza::getTimestamp() const {
+ boost::shared_ptr<Delay> delay = getPayload<Delay>();
+ return delay ? delay->getStamp() : boost::optional<boost::posix_time::ptime>();
+}
+
+boost::optional<boost::posix_time::ptime> Stanza::getTimestampFrom(const JID& jid) const {
+ std::vector< boost::shared_ptr<Delay> > delays = getPayloads<Delay>();
+ for (size_t i = 0; i < delays.size(); ++i) {
+ if (delays[i]->getFrom() == jid) {
+ return delays[i]->getStamp();
+ }
+ }
+ return getTimestamp();
+}
+
}
diff --git a/Swiften/Elements/Stanza.h b/Swiften/Elements/Stanza.h
index f42048e..20fb557 100644
--- a/Swiften/Elements/Stanza.h
+++ b/Swiften/Elements/Stanza.h
@@ -4,11 +4,12 @@
* See Documentation/Licenses/GPLv3.txt for more information.
*/
-#ifndef SWIFTEN_STANZAS_STANZA_H
-#define SWIFTEN_STANZAS_STANZA_H
+#pragma once
#include <vector>
#include <boost/shared_ptr.hpp>
+#include <boost/optional.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
#include "Swiften/Elements/Element.h"
#include "Swiften/Elements/Payload.h"
@@ -65,6 +66,11 @@ namespace Swift {
const String& getID() const { return id_; }
void setID(const String& id) { id_ = id; }
+
+ boost::optional<boost::posix_time::ptime> getTimestamp() const;
+
+ // Falls back to any timestamp if no specific timestamp for the given JID is found.
+ boost::optional<boost::posix_time::ptime> getTimestampFrom(const JID& jid) const;
private:
String id_;
@@ -75,5 +81,3 @@ namespace Swift {
Payloads payloads_;
};
}
-
-#endif
diff --git a/Swiften/Elements/UnitTest/StanzaTest.cpp b/Swiften/Elements/UnitTest/StanzaTest.cpp
index 1e7e9c4..15f8629 100644
--- a/Swiften/Elements/UnitTest/StanzaTest.cpp
+++ b/Swiften/Elements/UnitTest/StanzaTest.cpp
@@ -11,6 +11,7 @@
#include "Swiften/Elements/Stanza.h"
#include "Swiften/Elements/Payload.h"
#include "Swiften/Elements/Message.h"
+#include "Swiften/Elements/Delay.h"
using namespace Swift;
@@ -27,6 +28,11 @@ class StanzaTest : public CppUnit::TestFixture
CPPUNIT_TEST(testUpdatePayload_NewPayload);
CPPUNIT_TEST(testGetPayloadOfSameType);
CPPUNIT_TEST(testGetPayloadOfSameType_NoSuchPayload);
+ CPPUNIT_TEST(testGetTimestamp);
+ CPPUNIT_TEST(testGetTimestamp_TimestampWithFrom);
+ CPPUNIT_TEST(testGetTimestamp_NoDelay);
+ CPPUNIT_TEST(testGetTimestampFrom);
+ CPPUNIT_TEST(testGetTimestampFrom_Fallsback);
CPPUNIT_TEST_SUITE_END();
public:
@@ -60,8 +66,6 @@ class StanzaTest : public CppUnit::TestFixture
bool* alive_;
};
- StanzaTest() {}
-
void testConstructor_Copy() {
Message m;
m.addPayload(boost::shared_ptr<MyPayload1>(new MyPayload1()));
@@ -172,6 +176,55 @@ class StanzaTest : public CppUnit::TestFixture
CPPUNIT_ASSERT(!m.getPayloadOfSameType(boost::shared_ptr<MyPayload2>(new MyPayload2("bar"))));
}
+
+ void testGetTimestamp() {
+ Message m;
+ m.addPayload(boost::shared_ptr<Delay>(new Delay(boost::posix_time::from_time_t(1))));
+
+ boost::optional<boost::posix_time::ptime> timestamp = m.getTimestamp();
+
+ CPPUNIT_ASSERT(timestamp);
+ CPPUNIT_ASSERT_EQUAL(std::string("1970-Jan-01 00:00:01"), boost::posix_time::to_simple_string(*timestamp));
+ }
+
+ void testGetTimestamp_TimestampWithFrom() {
+ Message m;
+ m.addPayload(boost::shared_ptr<Delay>(new Delay(boost::posix_time::from_time_t(1), JID("foo@bar.com"))));
+
+ boost::optional<boost::posix_time::ptime> timestamp = m.getTimestamp();
+
+ CPPUNIT_ASSERT(timestamp);
+ CPPUNIT_ASSERT_EQUAL(std::string("1970-Jan-01 00:00:01"), boost::posix_time::to_simple_string(*timestamp));
+ }
+
+ void testGetTimestamp_NoDelay() {
+ Message m;
+ CPPUNIT_ASSERT(!m.getTimestamp());
+ }
+
+ void testGetTimestampFrom() {
+ Message m;
+ m.addPayload(boost::shared_ptr<Delay>(new Delay(boost::posix_time::from_time_t(0))));
+ m.addPayload(boost::shared_ptr<Delay>(new Delay(boost::posix_time::from_time_t(1), JID("foo1@bar.com"))));
+ m.addPayload(boost::shared_ptr<Delay>(new Delay(boost::posix_time::from_time_t(2), JID("foo2@bar.com"))));
+ m.addPayload(boost::shared_ptr<Delay>(new Delay(boost::posix_time::from_time_t(3), JID("foo3@bar.com"))));
+
+ boost::optional<boost::posix_time::ptime> timestamp = m.getTimestampFrom(JID("foo2@bar.com"));
+
+ CPPUNIT_ASSERT(timestamp);
+ CPPUNIT_ASSERT_EQUAL(std::string("1970-Jan-01 00:00:02"), boost::posix_time::to_simple_string(*timestamp));
+ }
+
+ void testGetTimestampFrom_Fallsback() {
+ Message m;
+ m.addPayload(boost::shared_ptr<Delay>(new Delay(boost::posix_time::from_time_t(1), JID("foo1@bar.com"))));
+ m.addPayload(boost::shared_ptr<Delay>(new Delay(boost::posix_time::from_time_t(3), JID("foo3@bar.com"))));
+
+ boost::optional<boost::posix_time::ptime> timestamp = m.getTimestampFrom(JID("foo2@bar.com"));
+
+ CPPUNIT_ASSERT(timestamp);
+ CPPUNIT_ASSERT_EQUAL(std::string("1970-Jan-01 00:00:01"), boost::posix_time::to_simple_string(*timestamp));
+ }
};
CPPUNIT_TEST_SUITE_REGISTRATION(StanzaTest);