Page MenuHomePhorge

No OneTemporary

Size
9 KB
Referenced Files
None
Subscribers
None
diff --git a/src/base/event.cpp b/src/base/event.cpp
index 2ef21bc..fa33d1e 100644
--- a/src/base/event.cpp
+++ b/src/base/event.cpp
@@ -1,126 +1,131 @@
/*
* Copyright (C) 2020 Tusooa Zhu
*
* This file is part of libkazv.
*
* libkazv is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* libkazv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with libkazv. If not, see <https://www.gnu.org/licenses/>.
*/
#include "libkazv-config.hpp"
#include "event.hpp"
namespace Kazv
{
const JsonWrap Event::notYetDecryptedEvent = {
json{{"type", "m.room.message"},
{"content", {
{"msgtype", "xyz.tusooa.kazv.not.yet.decrypted"},
{"body", "**This message has not yet been decrypted.**"}}}}};
Event::Event() : m_json(json::object()) {}
Event::Event(JsonWrap j)
: m_json(j) {
using namespace std::string_literals;
if (m_json.get().contains("type"s)) {
auto type = m_json.get().at("type"s).get<std::string>();
if (type == "m.room.encrypted"s) {
m_encrypted = true;
m_decryptedJson = notYetDecryptedEvent;
}
}
}
Event Event::fromSync(Event e, std::string roomId) {
auto j = e.originalJson().get();
j["room_id"] = roomId;
return Event(j);
}
std::string Event::id() const {
// the decrypted json does not have an event id
return originalJson().get().contains("event_id")
? originalJson().get().at("event_id")
: "";
}
std::string Event::sender() const {
return originalJson().get().contains("sender")
? originalJson().get().at("sender")
: "";
}
Timestamp Event::originServerTs() const {
return originalJson().get().contains("origin_server_ts")
? originalJson().get().at("origin_server_ts")
: "";
}
std::string Event::type() const {
return raw().get().contains("type")
? raw().get().at("type")
: "";
}
JsonWrap Event::content() const {
return raw().get().contains("content")
? raw().get().at("content")
: json::object();
}
std::string Event::stateKey() const {
return raw().get().contains("state_key")
? raw().get().at("state_key")
: "";
}
+ bool Event::isState() const
+ {
+ return raw().get().contains("state_key");
+ }
+
/// returns the decrypted json
JsonWrap Event::raw() const {
return m_encrypted ? decryptedJson() : m_json;
}
/// returns the original json we fetched, probably encrypted.
JsonWrap Event::originalJson() const {
return m_json;
}
JsonWrap Event::decryptedJson() const {
return m_decryptedJson;
}
bool Event::encrypted() const {
return m_encrypted;
}
bool Event::decrypted() const {
return m_decrypted == Decrypted;
}
Event Event::setDecryptedJson(JsonWrap decryptedJson, DecryptionStatus decrypted) const {
Event e(*this);
e.m_decryptedJson = decryptedJson;
e.m_decrypted = decrypted;
return e;
}
bool operator==(Event a, Event b)
{
return a.id() == b.id()
&& a.originalJson() == b.originalJson()
&& a.raw() == b.raw();
}
}
diff --git a/src/base/event.hpp b/src/base/event.hpp
index 7e81f37..23a4ce9 100644
--- a/src/base/event.hpp
+++ b/src/base/event.hpp
@@ -1,113 +1,120 @@
/*
* Copyright (C) 2020 Tusooa Zhu
*
* This file is part of libkazv.
*
* libkazv is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* libkazv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with libkazv. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "libkazv-config.hpp"
#include <string>
#include <cstdint>
#include "jsonwrap.hpp"
namespace Kazv
{
using Timestamp = std::int_fast64_t;
class Event
{
public:
static const JsonWrap notYetDecryptedEvent;
enum DecryptionStatus {
NotDecrypted,
Decrypted
};
Event();
Event(JsonWrap j);
static Event fromSync(Event e, std::string roomId);
/// returns the id of this event
std::string id() const;
std::string sender() const;
Timestamp originServerTs() const;
std::string type() const;
std::string stateKey() const;
+ /**
+ * @return whether this event is a state event.
+ * An event is considered a state event if and only if
+ * it has a maybe empty stateKey.
+ */
+ bool isState() const;
+
JsonWrap content() const;
/// returns the decrypted json
JsonWrap raw() const;
/// returns the original json we fetched, probably encrypted.
JsonWrap originalJson() const;
JsonWrap decryptedJson() const;
bool encrypted() const;
bool decrypted() const;
/// internal. only to be called from inside the client.
Event setDecryptedJson(JsonWrap decryptedJson, DecryptionStatus decrypted) const;
template <class Archive>
void save(Archive & ar, std::uint32_t const /*version*/ ) const {
ar( m_json, m_decryptedJson, m_decrypted, m_encrypted );
}
template <class Archive>
void load( Archive & ar, std::uint32_t const /*version*/ ) {
ar( m_json, m_decryptedJson, m_decrypted, m_encrypted );
}
private:
JsonWrap m_json;
JsonWrap m_decryptedJson;
DecryptionStatus m_decrypted{NotDecrypted};
bool m_encrypted{false};
};
bool operator==(Event a, Event b);
inline bool operator!=(Event a, Event b) { return !(a == b); };
}
CEREAL_CLASS_VERSION(Kazv::Event, 0);
namespace nlohmann
{
template <>
struct adl_serializer<Kazv::Event> {
static void to_json(json& j, Kazv::Event w) {
j = w.originalJson();
}
static void from_json(const json& j, Kazv::Event &w) {
w = Kazv::Event(Kazv::JsonWrap(j));
}
};
}
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt
index 38405ca..00f5067 100644
--- a/src/tests/CMakeLists.txt
+++ b/src/tests/CMakeLists.txt
@@ -1,37 +1,40 @@
include(CTest)
set(KAZVTEST_RESPATH ${CMAKE_CURRENT_SOURCE_DIR}/resources)
configure_file(kazvtest-respath.hpp.in kazvtest-respath.hpp)
add_executable(kazvtest
testmain.cpp
basejobtest.cpp
+ event-test.cpp
cursorutiltest.cpp
+
client/client-test-util.cpp
client/sync-test.cpp
client/content-test.cpp
client/paginate-test.cpp
client/util-test.cpp
+
kazvjobtest.cpp
event-emitter-test.cpp
crypto-test.cpp
promise-test.cpp
store-test.cpp
file-desc-test.cpp
)
target_include_directories(
kazvtest
PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(kazvtest
PRIVATE Catch2::Catch2
PRIVATE kazv
PRIVATE kazveventemitter
PRIVATE kazvjob
PRIVATE nlohmann_json::nlohmann_json
PRIVATE immer
PRIVATE lager
PRIVATE zug)
diff --git a/src/tests/event-test.cpp b/src/tests/event-test.cpp
new file mode 100644
index 0000000..cebe18a
--- /dev/null
+++ b/src/tests/event-test.cpp
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2021 Tusooa Zhu <tusooa@kazv.moe>
+ *
+ * This file is part of libkazv.
+ *
+ * libkazv is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * libkazv is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with libkazv. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <libkazv-config.hpp>
+
+#include <catch2/catch.hpp>
+
+#include <event.hpp>
+
+using namespace Kazv;
+
+
+TEST_CASE("Should distinguish between state events and other events", "[base][event]")
+{
+ Event state = R"({
+ "state_key": "",
+ "sender": "@example:example.org",
+ "type": "moe.kazv.mxc.custom.state.type",
+ "event_id": "!dummy",
+ "origin_server_ts": 1234,
+ "content": {}
+})"_json;
+
+ REQUIRE(state.isState());
+
+ Event nonState = R"({
+ "sender": "@example:example.org",
+ "type": "moe.kazv.mxc.custom.non.state.type",
+ "event_id": "!dummy",
+ "origin_server_ts": 1234,
+ "content": {}
+})"_json;
+
+ REQUIRE(!nonState.isState());
+}

File Metadata

Mime Type
text/x-diff
Expires
Sat, Nov 23, 8:44 PM (1 d, 3 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
39077
Default Alt Text
(9 KB)

Event Timeline