Files
evening_detective_server/internal/app/devices.go
T
2026-10-11 16:00:18 +07:00

62 lines
2.0 KiB
Go

package app
import (
"context"
"evening_detective_server/internal/modules/processor_jwt"
"evening_detective_server/internal/modules/roles"
"evening_detective_server/internal/services/device_presence_service"
proto "evening_detective_server/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)
func (s *server) DeviceHeartbeat(ctx context.Context, req *proto.DeviceHeartbeatReq) (*proto.DeviceHeartbeatRsp, error) {
if req == nil {
req = &proto.DeviceHeartbeatReq{}
}
ip, userAgent := clientInfoFromContext(ctx)
claims, authenticated := processor_jwt.ClaimsFromContext(ctx)
input := device_presence_service.Heartbeat{
DeviceID: req.DeviceID,
ConnectionID: req.ConnectionID,
OrganizerGameID: deviceOptionalID(req.OrganizerGameID),
TeamID: req.TeamID,
Password: req.Password,
ScreenID: req.ScreenID,
IP: ip,
UserAgent: userAgent,
IsAuthenticated: authenticated,
CanManage: authenticated && (roles.HasRole(claims, roles.Organizer) || roles.HasRole(claims, roles.Author)),
}
if req.AccessToken != "" {
input.ScreenTokenHash = hashScreenToken(req.AccessToken)
}
enabled, now, err := s.devicePresenceService.Heartbeat(ctx, input)
if err != nil {
return nil, deviceErrorStatus(err)
}
return &proto.DeviceHeartbeatRsp{
Enabled: &enabled,
ServerTime: timestamppb.New(now),
}, nil
}
func (s *server) GetDevices(ctx context.Context, req *proto.GetDevicesReq) (*proto.GetDevicesRsp, error) {
if req == nil {
req = &proto.GetDevicesReq{}
}
claims, authenticated := processor_jwt.ClaimsFromContext(ctx)
canRead := authenticated && (roles.HasRole(claims, roles.Organizer) || roles.HasRole(claims, roles.Developer))
devices, now, err := s.devicePresenceService.List(ctx, deviceOptionalID(req.GameID), canRead)
if err != nil {
return nil, deviceErrorStatus(err)
}
wifi, stale := s.wifiService.Get(ctx)
return &proto.GetDevicesRsp{
Wifi: mapWiFi(wifi, stale),
Devices: mapVenueDevices(devices),
ServerTime: timestamppb.New(now),
}, nil
}