Changeset View
Changeset View
Standalone View
Standalone View
src/client/actions/send.cpp
| /* | /* | ||||
| * This file is part of libkazv. | * This file is part of libkazv. | ||||
| * SPDX-FileCopyrightText: 2020 Tusooa Zhu <tusooa@kazv.moe> | * SPDX-FileCopyrightText: 2020-2026 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 <debug.hpp> | #include <debug.hpp> | ||||
| #include <types.hpp> | #include <types.hpp> | ||||
| #include <immer-utils.hpp> | #include <immer-utils.hpp> | ||||
| #include "send.hpp" | #include "send.hpp" | ||||
| #include "status-utils.hpp" | #include "status-utils.hpp" | ||||
| namespace Kazv | namespace Kazv | ||||
| { | { | ||||
| ClientResult updateClient(ClientModel m, SendMessageAction a) | ClientResult updateClient(ClientModel m, SendMessageAction a) | ||||
| { | { | ||||
| auto event = std::move(a.event); | auto event = std::move(a.event); | ||||
| auto roomId = a.roomId; | auto roomId = a.roomId; | ||||
| auto origJson = event.originalJson().get(); | auto origJson = event.originalJson().get(); | ||||
| if (!origJson.contains("type") || !origJson.contains("content")) { | if (!origJson.contains("type") || !origJson.contains("content")) { | ||||
| m.addTrigger(InvalidMessageFormat{}); | return { std::move(m), failEffect( | ||||
| return { std::move(m), lager::noop }; | "MOE.KAZV.MXC.INVALID_MESSAGE_FORMAT", | ||||
| "Invalid message format" | |||||
| ) }; | |||||
| } | } | ||||
| if (m.roomList.rooms[a.roomId].encrypted && !event.encrypted()) { | if (m.roomList.rooms[a.roomId].encrypted && !event.encrypted()) { | ||||
| if (!event.isState()) { | if (!event.isState()) { | ||||
| return { std::move(m), [](auto &&) { | return { std::move(m), [](auto &&) { | ||||
| return EffectStatus{ | return EffectStatus{ | ||||
| /* succ = */ false, | /* succ = */ false, | ||||
| json{ | json{ | ||||
| ▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | ClientResult processResponse(ClientModel m, SendMessageResponse r) | ||||
| if (!maybeLocalEcho.has_value()) { | if (!maybeLocalEcho.has_value()) { | ||||
| kzo.client.warn() << "We do not have local echo with txnId " << txnId << " . Have we time-travelled?" << std::endl; | kzo.client.warn() << "We do not have local echo with txnId " << txnId << " . Have we time-travelled?" << std::endl; | ||||
| return room; | return room; | ||||
| } | } | ||||
| auto localEcho = std::move(maybeLocalEcho).value(); | auto localEcho = std::move(maybeLocalEcho).value(); | ||||
| localEcho.status = LocalEchoDesc::Failed; | localEcho.status = LocalEchoDesc::Failed; | ||||
| return RoomModel::update(std::move(room), AddLocalEchoAction{localEcho}); | return RoomModel::update(std::move(room), AddLocalEchoAction{localEcho}); | ||||
| }); | }); | ||||
| m.addTrigger(SendMessageFailed{roomId, r.errorCode(), r.errorMessage()}); | |||||
| return { std::move(m), failWithResponse(r) }; | return { std::move(m), failWithResponse(r) }; | ||||
| } | } | ||||
| m.addTrigger(SendMessageSuccessful{roomId, r.eventId()}); | |||||
| return { std::move(m), lager::noop }; | return { std::move(m), lager::noop }; | ||||
| } | } | ||||
| ClientResult updateClient(ClientModel m, SendToDeviceMessageAction a) | ClientResult updateClient(ClientModel m, SendToDeviceMessageAction a) | ||||
| { | { | ||||
| auto origJson = a.event.originalJson().get(); | auto origJson = a.event.originalJson().get(); | ||||
| if (!origJson.contains("type") || !origJson.contains("content")) { | if (!origJson.contains("type") || !origJson.contains("content")) { | ||||
| return { std::move(m), simpleFail }; | return { std::move(m), simpleFail }; | ||||
| ▲ Show 20 Lines • Show All 99 Lines • ▼ Show 20 Lines | namespace Kazv | ||||
| } | } | ||||
| ClientResult processResponse(ClientModel m, SendToDeviceResponse r) | ClientResult processResponse(ClientModel m, SendToDeviceResponse r) | ||||
| { | { | ||||
| auto devicesToSend = r.dataJson("devicesToSend"); | auto devicesToSend = r.dataJson("devicesToSend"); | ||||
| auto txnId = r.dataStr("txnId"); | auto txnId = r.dataStr("txnId"); | ||||
| if (! r.success()) { | if (! r.success()) { | ||||
| m.addTrigger(SendToDeviceMessageFailed{devicesToSend, txnId, r.errorCode(), r.errorMessage()}); | |||||
| return { std::move(m), failWithResponse(r) }; | return { std::move(m), failWithResponse(r) }; | ||||
| } | } | ||||
| m.addTrigger(SendToDeviceMessageSuccessful{devicesToSend, txnId}); | |||||
| return { std::move(m), lager::noop }; | return { std::move(m), lager::noop }; | ||||
| } | } | ||||
| ClientResult updateClient(ClientModel m, SaveLocalEchoAction a) | ClientResult updateClient(ClientModel m, SaveLocalEchoAction a) | ||||
| { | { | ||||
| auto txnId = a.txnId.has_value() ? a.txnId.value() : getTxnId(a.event, m); | auto txnId = a.txnId.has_value() ? a.txnId.value() : getTxnId(a.event, m); | ||||
| m.roomList = RoomListModel::update(m.roomList, UpdateRoomAction{ | m.roomList = RoomListModel::update(m.roomList, UpdateRoomAction{ | ||||
| ▲ Show 20 Lines • Show All 57 Lines • Show Last 20 Lines | |||||