Changeset View
Changeset View
Standalone View
Standalone View
src/client/room/room-model.hpp
| /* | /* | ||||
| * This file is part of libkazv. | * This file is part of libkazv. | ||||
| * SPDX-FileCopyrightText: 2021-2023 tusooa <tusooa@kazv.moe> | * SPDX-FileCopyrightText: 2021-2023 tusooa <tusooa@kazv.moe> | ||||
| * SPDX-License-Identifier: AGPL-3.0-or-later | * SPDX-License-Identifier: AGPL-3.0-or-later | ||||
| */ | */ | ||||
| #pragma once | #pragma once | ||||
| #include <libkazv-config.hpp> | #include <libkazv-config.hpp> | ||||
| #include <string> | #include <string> | ||||
| #include <variant> | #include <variant> | ||||
| #include <immer/flex_vector.hpp> | #include <immer/flex_vector.hpp> | ||||
| #include <immer/map.hpp> | #include <immer/map.hpp> | ||||
| #include <immer/set.hpp> | |||||
| #include <serialization/immer-flex-vector.hpp> | #include <serialization/immer-flex-vector.hpp> | ||||
| #include <serialization/immer-box.hpp> | #include <serialization/immer-box.hpp> | ||||
| #include <serialization/immer-map.hpp> | #include <serialization/immer-map.hpp> | ||||
| #include <serialization/immer-set.hpp> | |||||
| #include <serialization/immer-array.hpp> | #include <serialization/immer-array.hpp> | ||||
| #include <csapi/sync.hpp> | #include <csapi/sync.hpp> | ||||
| #include <event.hpp> | #include <event.hpp> | ||||
| #include <crypto.hpp> | #include <crypto.hpp> | ||||
| #include "push-rules-desc.hpp" | #include "push-rules-desc.hpp" | ||||
| #include "local-echo.hpp" | #include "local-echo.hpp" | ||||
| ▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | struct MaybeAddStateEventsAction | ||||
| immer::flex_vector<Event> stateEvents; | immer::flex_vector<Event> stateEvents; | ||||
| }; | }; | ||||
| /// Add events to the messages map, but not the timeline. | /// Add events to the messages map, but not the timeline. | ||||
| /// Usually because their position in the timeline is not known. | /// Usually because their position in the timeline is not known. | ||||
| struct AddMessagesAction | struct AddMessagesAction | ||||
| { | { | ||||
| EventList events; | EventList events; | ||||
| /// @internal only to be used by AddToTimelineAction | |||||
| bool alsoInTimeline{false}; | |||||
| }; | }; | ||||
| struct AddToTimelineAction | struct AddToTimelineAction | ||||
| { | { | ||||
| /// Events from oldest to latest | /// Events from oldest to latest | ||||
| immer::flex_vector<Event> events; | immer::flex_vector<Event> events; | ||||
| std::optional<std::string> prevBatch; | std::optional<std::string> prevBatch; | ||||
| std::optional<bool> limited; | std::optional<bool> limited; | ||||
| ▲ Show 20 Lines • Show All 200 Lines • ▼ Show 20 Lines | struct RoomModel | ||||
| /// A map from the session id to a list of event ids of events | /// A map from the session id to a list of event ids of events | ||||
| /// that cannot (yet) be decrypted. | /// that cannot (yet) be decrypted. | ||||
| immer::map< | immer::map< | ||||
| std::string /* sessionId */, | std::string /* sessionId */, | ||||
| immer::flex_vector<std::string /* eventId */>> undecryptedEvents; | immer::flex_vector<std::string /* eventId */>> undecryptedEvents; | ||||
| immer::flex_vector<std::string> unreadNotificationEventIds; | immer::flex_vector<std::string> unreadNotificationEventIds; | ||||
| /// The set of event ids that are in `messages` but not in `timeline`. | |||||
| /// The rationale is that non-timeline events are sparse, so | |||||
| /// instead of recording events that are in the timeline, we | |||||
| /// record those not in the timeline. | |||||
| immer::set<std::string> nonTimelineEvents; | |||||
| immer::flex_vector<std::string> joinedMemberIds() const; | immer::flex_vector<std::string> joinedMemberIds() const; | ||||
| immer::flex_vector<std::string> invitedMemberIds() const; | immer::flex_vector<std::string> invitedMemberIds() const; | ||||
| immer::flex_vector<std::string> knockedMemberIds() const; | immer::flex_vector<std::string> knockedMemberIds() const; | ||||
| immer::flex_vector<std::string> leftMemberIds() const; | immer::flex_vector<std::string> leftMemberIds() const; | ||||
| immer::flex_vector<std::string> bannedMemberIds() const; | immer::flex_vector<std::string> bannedMemberIds() const; | ||||
| EventList joinedMemberEvents() const; | EventList joinedMemberEvents() const; | ||||
| Show All 37 Lines | struct RoomModel | ||||
| void recalculateUndecryptedEvents(); | void recalculateUndecryptedEvents(); | ||||
| /** | /** | ||||
| * Check if the invariants in the model are satisfied. | * Check if the invariants in the model are satisfied. | ||||
| * @return true iff the invariants are satisfied. | * @return true iff the invariants are satisfied. | ||||
| */ | */ | ||||
| bool checkInvariants() const; | bool checkInvariants() const; | ||||
| /** | |||||
| * Check if the event is in the timeline. | |||||
| * | |||||
| * This function takes constant time. | |||||
| * @param eventId The id of the event to check. | |||||
| * @return true iff the event is in the timeline. | |||||
| */ | |||||
| bool isInTimeline(const std::string &eventId) const; | |||||
| using Action = std::variant< | using Action = std::variant< | ||||
| AddStateEventsAction, | AddStateEventsAction, | ||||
| MaybeAddStateEventsAction, | MaybeAddStateEventsAction, | ||||
| AddMessagesAction, | AddMessagesAction, | ||||
| AddToTimelineAction, | AddToTimelineAction, | ||||
| AddAccountDataAction, | AddAccountDataAction, | ||||
| ChangeMembershipAction, | ChangeMembershipAction, | ||||
| ChangeInviteStateAction, | ChangeInviteStateAction, | ||||
| ▲ Show 20 Lines • Show All 105 Lines • ▼ Show 20 Lines | void serialize(Archive &ar, RoomModel &r, std::uint32_t const version) | ||||
| } else { | } else { | ||||
| if constexpr (typename Archive::is_loading()) { | if constexpr (typename Archive::is_loading()) { | ||||
| r.recalculateUndecryptedEvents(); | r.recalculateUndecryptedEvents(); | ||||
| } | } | ||||
| } | } | ||||
| if (version >= 8) { | if (version >= 8) { | ||||
| ar & r.unreadNotificationEventIds; | ar & r.unreadNotificationEventIds; | ||||
| } | } | ||||
| if (version >= 9) { | |||||
| ar & r.nonTimelineEvents; | |||||
| } | } | ||||
| } | |||||
| template<class Archive> | template<class Archive> | ||||
| void serialize(Archive &ar, RoomListModel &l, std::uint32_t const /*version*/) | void serialize(Archive &ar, RoomListModel &l, std::uint32_t const /*version*/) | ||||
| { | { | ||||
| ar & l.rooms; | ar & l.rooms; | ||||
| } | } | ||||
| } | } | ||||
| BOOST_CLASS_VERSION(Kazv::PendingRoomKeyEvent, 1) | BOOST_CLASS_VERSION(Kazv::PendingRoomKeyEvent, 1) | ||||
| BOOST_CLASS_VERSION(Kazv::ReadReceipt, 0) | BOOST_CLASS_VERSION(Kazv::ReadReceipt, 0) | ||||
| BOOST_CLASS_VERSION(Kazv::RoomModel, 8) | BOOST_CLASS_VERSION(Kazv::RoomModel, 9) | ||||
| BOOST_CLASS_VERSION(Kazv::RoomListModel, 0) | BOOST_CLASS_VERSION(Kazv::RoomListModel, 0) | ||||