libkazv
event.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of libkazv.
3  * SPDX-FileCopyrightText: 2020-2023 tusooa <tusooa@kazv.moe>
4  * SPDX-License-Identifier: AGPL-3.0-or-later
5  */
6 
7 
8 #pragma once
9 #include "libkazv-config.hpp"
10 
11 #include <string>
12 #include <cstdint>
13 
14 #include "jsonwrap.hpp"
15 
16 namespace Kazv
17 {
18  using Timestamp = std::int_fast64_t;
19 
20  class Event
21  {
22  public:
24 
28  };
29 
30  Event();
31 
32  Event(JsonWrap j);
33 
34  static Event fromSync(Event e, std::string roomId);
35 
37  std::string id() const;
38 
39  std::string sender() const;
40 
41  Timestamp originServerTs() const;
42 
43  std::string type() const;
44 
45  std::string stateKey() const;
46 
52  bool isState() const;
53 
54  JsonWrap content() const;
55 
57  JsonWrap raw() const;
58 
60  JsonWrap originalJson() const;
61 
62  JsonWrap decryptedJson() const;
63 
64  bool encrypted() const;
65 
66  bool decrypted() const;
67 
70 
72  bool redacted() const;
73 
80  std::string replyingTo() const;
81 
88  std::pair<std::string/* relType */, std::string/* eventId */> relationship() const;
89 
90  template<class Archive>
91  void serialize(Archive &ar, std::uint32_t const /*version*/ ) {
92  ar & m_json & m_decryptedJson & m_decrypted & m_encrypted;
93  }
94 
95  private:
96  JsonWrap m_json;
97  JsonWrap m_decryptedJson;
98  DecryptionStatus m_decrypted{NotDecrypted};
99  bool m_encrypted{false};
100  };
101 
102  bool operator==(const Event &a, const Event &b);
103  inline bool operator!=(const Event &a, const Event &b) { return !(a == b); };
104 
105 }
106 
107 BOOST_CLASS_VERSION(Kazv::Event, 0)
108 
109 namespace nlohmann
110 {
111  template <>
112  struct adl_serializer<Kazv::Event> {
113  static void to_json(json& j, Kazv::Event w) {
114  j = w.originalJson();
115  }
116 
117  static void from_json(const json& j, Kazv::Event &w) {
118  w = Kazv::Event(Kazv::JsonWrap(j));
119  }
120  };
121 }
Kazv::Event::redacted
bool redacted() const
returns whether this event has been redacted.
Definition: event.cpp:114
Kazv::Event::fromSync
static Event fromSync(Event e, std::string roomId)
Definition: event.cpp:36
nlohmann::adl_serializer< Kazv::Event >::to_json
static void to_json(json &j, Kazv::Event w)
Definition: event.hpp:113
Kazv::Event::replyingTo
std::string replyingTo() const
Get the event id this event is replying to.
Definition: event.cpp:121
Kazv::Event::DecryptionStatus
DecryptionStatus
Definition: event.hpp:25
Kazv::Event::type
std::string type() const
Definition: event.cpp:62
Kazv::Event::serialize
void serialize(Archive &ar, std::uint32_t const)
Definition: event.hpp:91
Kazv::operator!=
bool operator!=(BaseJob a, BaseJob b)
Definition: basejob.cpp:292
nlohmann
Definition: location.hpp:26
Kazv::operator==
bool operator==(BaseJob a, BaseJob b)
Definition: basejob.cpp:280
Kazv
Definition: location.hpp:10
Kazv::Event::content
JsonWrap content() const
Definition: event.cpp:68
Kazv::Timestamp
std::int_fast64_t Timestamp
Definition: event.hpp:18
Kazv::Event::setDecryptedJson
Event setDecryptedJson(JsonWrap decryptedJson, DecryptionStatus decrypted) const
internal. only to be called from inside the client.
Definition: event.cpp:107
Kazv::json
nlohmann::json json
Definition: jsonwrap.hpp:20
Kazv::Event::relationship
std::pair< std::string, std::string > relationship() const
Get the relationship that this event contains.
Definition: event.cpp:155
Kazv::Event::decrypted
bool decrypted() const
Definition: event.cpp:103
Kazv::Event::NotDecrypted
@ NotDecrypted
Definition: event.hpp:26
Kazv::Event::Event
Event()
Definition: event.cpp:22
Kazv::JsonWrap
Definition: jsonwrap.hpp:22
Kazv::Event::Decrypted
@ Decrypted
Definition: event.hpp:27
nlohmann::adl_serializer< Kazv::Event >::from_json
static void from_json(const json &j, Kazv::Event &w)
Definition: event.hpp:117
jsonwrap.hpp
Kazv::Event
Definition: event.hpp:20
Kazv::Event::originalJson
JsonWrap originalJson() const
returns the original json we fetched, probably encrypted.
Definition: event.cpp:91
libkazv-config.hpp
Kazv::Event::stateKey
std::string stateKey() const
Definition: event.cpp:74
Kazv::Event::isState
bool isState() const
Definition: event.cpp:80
Kazv::Event::decryptedJson
JsonWrap decryptedJson() const
Definition: event.cpp:95
Kazv::Event::originServerTs
Timestamp originServerTs() const
Definition: event.cpp:55
Kazv::Event::notYetDecryptedEvent
static const JsonWrap notYetDecryptedEvent
Definition: event.hpp:23
Kazv::Event::id
std::string id() const
returns the id of this event
Definition: event.cpp:42
Kazv::Event::sender
std::string sender() const
Definition: event.cpp:49
Kazv::Event::raw
JsonWrap raw() const
returns the decrypted json
Definition: event.cpp:86
Kazv::Event::encrypted
bool encrypted() const
Definition: event.cpp:99