Files
voice-cat/core/src/session/session.cpp

88 lines
2.7 KiB
C++
Raw Normal View History

#include "session/session.h"
2026-06-15 23:48:44 +02:00
#include <algorithm>
namespace voicecat::session {
2026-06-15 23:48:44 +02:00
const Channel* SessionModel::find_channel(uint32_t id) const {
for (auto& ch : channels_) if (ch.id == id) return &ch;
return nullptr;
}
const User* SessionModel::find_user(uint32_t id) const {
for (auto& u : users_) if (u.id == id) return &u;
return nullptr;
}
#ifdef VOICECAT_HAS_NET
void SessionModel::apply_snapshot(const voicecat::v1::ServerStateSnapshot& snap) {
channels_.clear();
for (const auto& pb : snap.channels()) {
Channel ch;
ch.id = pb.id();
ch.name = pb.name();
channels_.push_back(std::move(ch));
}
users_.clear();
for (const auto& pb : snap.users()) {
User u;
u.id = pb.id();
u.nickname = pb.nickname();
u.is_guest = pb.is_guest();
u.channel_id = pb.channel_id();
users_.push_back(std::move(u));
}
}
void SessionModel::apply_user_event(const voicecat::v1::UserEvent& ev) {
using Kind = voicecat::v1::UserEvent;
if (ev.kind() == Kind::JOINED || ev.kind() == Kind::UPDATED) {
const auto& pb = ev.user();
User u;
u.id = pb.id();
u.nickname = pb.nickname();
u.is_guest = pb.is_guest();
u.channel_id = pb.channel_id();
auto it = std::find_if(users_.begin(), users_.end(),
[&](const User& x) { return x.id == u.id; });
if (it != users_.end()) *it = std::move(u);
else users_.push_back(std::move(u));
} else if (ev.kind() == Kind::LEFT) {
uint32_t uid = ev.user().id();
users_.erase(std::remove_if(users_.begin(), users_.end(),
[uid](const User& x) { return x.id == uid; }),
users_.end());
}
}
void SessionModel::apply_channel_event(const voicecat::v1::ChannelEvent& ev) {
using Kind = voicecat::v1::ChannelEvent;
if (ev.kind() == Kind::CREATED || ev.kind() == Kind::UPDATED) {
const auto& pb = ev.channel();
Channel ch;
ch.id = pb.id();
ch.name = pb.name();
auto it = std::find_if(channels_.begin(), channels_.end(),
[&](const Channel& x) { return x.id == ch.id; });
if (it != channels_.end()) *it = std::move(ch);
else channels_.push_back(std::move(ch));
} else if (ev.kind() == Kind::DELETED) {
uint32_t cid = ev.channel().id();
channels_.erase(std::remove_if(channels_.begin(), channels_.end(),
[cid](const Channel& x) { return x.id == cid; }),
channels_.end());
}
}
#endif // VOICECAT_HAS_NET
} // namespace voicecat::session