-
Notifications
You must be signed in to change notification settings - Fork 22
/
PrenoteAcknowledgedPushEventHandler.cpp
41 lines (35 loc) · 1.68 KB
/
PrenoteAcknowledgedPushEventHandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "PrenoteAcknowledgedPushEventHandler.h"
#include "PrenoteMessage.h"
#include "PrenoteMessageCollection.h"
#include "PrenoteMessageEventHandlerCollection.h"
#include "push/PushEventSubscription.h"
using UKControllerPlugin::Push::PushEventSubscription;
namespace UKControllerPlugin::Prenote {
PrenoteAcknowledgedPushEventHandler::PrenoteAcknowledgedPushEventHandler(
std::shared_ptr<PrenoteMessageCollection> prenotes, const PrenoteMessageEventHandlerCollection& eventHandlers)
: prenotes(std::move(prenotes)), eventHandlers(eventHandlers)
{
}
void PrenoteAcknowledgedPushEventHandler::ProcessPushEvent(const Push::PushEvent& message)
{
auto messageData = message.data;
if (!MessageValid(messageData)) {
LogError("Invalid acknowledge prenote message");
return;
}
int prenoteId = messageData.at("id").get<int>();
const auto prenote = prenotes->GetById(prenoteId);
prenote->Acknowledge();
eventHandlers.MessageAcknowledged(*prenote);
LogInfo("Acknowledged prenote id " + std::to_string(prenoteId));
}
auto PrenoteAcknowledgedPushEventHandler::GetPushEventSubscriptions() const -> std::set<PushEventSubscription>
{
return {{PushEventSubscription::SUB_TYPE_EVENT, "prenote-message.acknowledged"}};
}
auto PrenoteAcknowledgedPushEventHandler::MessageValid(const nlohmann::json& message) const -> bool
{
return message.contains("id") && message.at("id").is_number_integer() &&
this->prenotes->GetById(message.at("id").get<int>()) != nullptr;
}
} // namespace UKControllerPlugin::Prenote