Changeset View
Changeset View
Standalone View
Standalone View
src/client/client.cpp
| /* | /* | ||||
| * This file is part of libkazv. | * This file is part of libkazv. | ||||
| * SPDX-FileCopyrightText: 2020-2023 tusooa <tusooa@kazv.moe> | * SPDX-FileCopyrightText: 2020-2023 tusooa <tusooa@kazv.moe> | ||||
| * SPDX-License-Identifier: AGPL-3.0-or-later | * SPDX-License-Identifier: AGPL-3.0-or-later | ||||
| */ | */ | ||||
| #include <libkazv-config.hpp> | #include <libkazv-config.hpp> | ||||
| #include <filesystem> | #include <filesystem> | ||||
| #include <algorithm> | |||||
| #include <lager/constant.hpp> | #include <lager/constant.hpp> | ||||
| #include "client.hpp" | #include "client.hpp" | ||||
| namespace Kazv | namespace Kazv | ||||
| { | { | ||||
| Client::Client(lager::reader<SdkModel> sdk, | Client::Client(lager::reader<SdkModel> sdk, | ||||
| ▲ Show 20 Lines • Show All 443 Lines • ▼ Show 20 Lines | #endif | ||||
| auto Client::getVersions(std::string homeserver) const -> PromiseT | auto Client::getVersions(std::string homeserver) const -> PromiseT | ||||
| { | { | ||||
| return m_ctx.dispatch(GetVersionsAction{homeserver}); | return m_ctx.dispatch(GetVersionsAction{homeserver}); | ||||
| } | } | ||||
| auto Client::supportVersions() const -> lager::reader<immer::array<std::string>> | auto Client::supportVersions() const -> lager::reader<immer::array<std::string>> | ||||
| { | { | ||||
| return clientCursor()[&ClientModel::versions]; | return clientCursor()[&ClientModel::versions]; | ||||
| } | |||||
| auto Client::addDirectRoom(std::string userId, std::string roomId) const -> PromiseT | |||||
| { | |||||
| auto content = this->accountData().get()["m.direct"].content().get(); | |||||
| auto rooms = content[userId]; | |||||
tusooa: did not check whether userId is contained in content, can cause UB. | |||||
| if (std::find(rooms.begin(), rooms.end(), roomId) != rooms.end()) { | |||||
Done Inline Actionsdid not check whether rooms is a json array tusooa: did not check whether rooms is a json array | |||||
| // The roomId is already in the m.direct, do nothing | |||||
| return m_ctx.createResolvedPromise(true); | |||||
| } | |||||
| content[userId].push_back(roomId); | |||||
| return Client::setAccountData(json{ | |||||
| {"type", "m.direct"}, | |||||
| {"content", std::move(content)} | |||||
| }); | |||||
| } | } | ||||
| } | } | ||||
Done Inline ActionsYou should be reusing the content json of the original account data event, not directRoomMap(). directRoomMap is intended to be used as a way to quickly find which user a direct room is associated with. Your code here just convert things back and forth, and is inefficient and error-prone. tusooa: You should be reusing the content json of the original account data event, not `directRoomMap… | |||||
did not check whether userId is contained in content, can cause UB.