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 
27  Decrypted
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 
95  JsonWrap mRelatesTo() const;
96 
97  template<class Archive>
98  void serialize(Archive &ar, std::uint32_t const /*version*/ ) {
99  ar & m_json & m_decryptedJson & m_decrypted & m_encrypted;
100  }
101 
102  private:
103  JsonWrap m_json;
104  JsonWrap m_decryptedJson;
105  DecryptionStatus m_decrypted{NotDecrypted};
106  bool m_encrypted{false};
107  };
108 
109  bool operator==(const Event &a, const Event &b);
110  inline bool operator!=(const Event &a, const Event &b) { return !(a == b); };
111 
112 }
113 
114 BOOST_CLASS_VERSION(Kazv::Event, 0)
115 
116 namespace nlohmann
117 {
118  template <>
119  struct adl_serializer<Kazv::Event> {
120  static void to_json(json& j, Kazv::Event w) {
121  j = w.originalJson();
122  }
123 
124  static void from_json(const json& j, Kazv::Event &w) {
125  w = Kazv::Event(Kazv::JsonWrap(j));
126  }
127  };
128 }
Definition: event.hpp:21
static const JsonWrap notYetDecryptedEvent
Definition: event.hpp:23
std::string stateKey() const
Definition: event.cpp:74
std::string replyingTo() const
Get the event id this event is replying to.
Definition: event.cpp:121
std::string type() const
Definition: event.cpp:62
void serialize(Archive &ar, std::uint32_t const)
Definition: event.hpp:98
std::pair< std::string, std::string > relationship() const
Get the relationship that this event contains.
Definition: event.cpp:155
std::string id() const
returns the id of this event
Definition: event.cpp:42
JsonWrap originalJson() const
returns the original json we fetched, probably encrypted.
Definition: event.cpp:91
bool isState() const
Definition: event.cpp:80
bool decrypted() const
Definition: event.cpp:103
Event()
Definition: event.cpp:22
JsonWrap mRelatesTo() const
Get the m.relates_to object in the event.
Definition: event.cpp:166
JsonWrap decryptedJson() const
Definition: event.cpp:95
Timestamp originServerTs() const
Definition: event.cpp:55
bool redacted() const
returns whether this event has been redacted.
Definition: event.cpp:114
JsonWrap content() const
Definition: event.cpp:68
DecryptionStatus
Definition: event.hpp:25
@ Decrypted
Definition: event.hpp:27
@ NotDecrypted
Definition: event.hpp:26
bool encrypted() const
Definition: event.cpp:99
static Event fromSync(Event e, std::string roomId)
Definition: event.cpp:36
JsonWrap raw() const
returns the decrypted json
Definition: event.cpp:86
std::string sender() const
Definition: event.cpp:49
Event setDecryptedJson(JsonWrap decryptedJson, DecryptionStatus decrypted) const
internal. only to be called from inside the client.
Definition: event.cpp:107
Definition: jsonwrap.hpp:23
Definition: location.hpp:10
bool operator!=(BaseJob a, BaseJob b)
Definition: basejob.cpp:292
nlohmann::json json
Definition: jsonwrap.hpp:20
std::int_fast64_t Timestamp
Definition: event.hpp:18
bool operator==(BaseJob a, BaseJob b)
Definition: basejob.cpp:280
Definition: location.hpp:27
static void to_json(json &j, Kazv::Event w)
Definition: event.hpp:120
static void from_json(const json &j, Kazv::Event &w)
Definition: event.hpp:124