2026-03-14 21:31:03 +01:00
|
|
|
package db
|
|
|
|
|
|
|
|
|
|
import (
|
2026-07-23 17:03:52 +02:00
|
|
|
"context"
|
2026-03-14 21:31:03 +01:00
|
|
|
"database/sql"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2026-03-31 11:41:59 +02:00
|
|
|
"sync/atomic"
|
|
|
|
|
"time"
|
2026-07-19 15:43:46 +00:00
|
|
|
|
2026-08-28 06:54:32 +02:00
|
|
|
"github.com/J3vb/OwnCord/Server/db/dbgen"
|
2026-03-14 21:31:03 +01:00
|
|
|
)
|
|
|
|
|
|
2026-03-31 19:10:42 +02:00
|
|
|
// ErrChannelFull is returned when a voice channel is at capacity.
|
|
|
|
|
var ErrChannelFull = errors.New("voice channel is full")
|
|
|
|
|
|
2026-03-31 11:41:59 +02:00
|
|
|
var voiceJoinSeq uint64
|
|
|
|
|
|
|
|
|
|
func newVoiceJoinToken() string {
|
|
|
|
|
seq := atomic.AddUint64(&voiceJoinSeq, 1)
|
|
|
|
|
return fmt.Sprintf("%s-%020d", time.Now().UTC().Format("2006-01-02T15:04:05.000000000Z"), seq)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-14 21:31:03 +01:00
|
|
|
// JoinVoiceChannel inserts or replaces the user's voice state for the given
|
|
|
|
|
// channel. If the user is already in a different channel, the old row is
|
|
|
|
|
// replaced. Muted, deafened, and speaking are reset to false on join.
|
2026-03-31 11:41:59 +02:00
|
|
|
//
|
|
|
|
|
// joined_at doubles as an opaque join-instance token so stale cleanup can
|
|
|
|
|
// target one specific voice session even if the user later rejoins the same
|
|
|
|
|
// channel.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) JoinVoiceChannel(ctx context.Context, userID, channelID int64) error {
|
|
|
|
|
if err := d.q.JoinVoiceChannel(ctx, dbgen.JoinVoiceChannelParams{
|
2026-07-19 15:43:46 +00:00
|
|
|
UserID: userID,
|
|
|
|
|
ChannelID: channelID,
|
|
|
|
|
JoinedAt: newVoiceJoinToken(),
|
|
|
|
|
}); err != nil {
|
2026-03-14 21:31:03 +01:00
|
|
|
return fmt.Errorf("JoinVoiceChannel: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 19:10:42 +02:00
|
|
|
// JoinVoiceChannelIfCapacity atomically inserts a voice state only if the
|
|
|
|
|
// channel has fewer than maxUsers participants. Returns ErrChannelFull when
|
|
|
|
|
// the channel is at capacity. This prevents the TOCTOU race where two
|
|
|
|
|
// concurrent joins both observe capacity and both succeed.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) JoinVoiceChannelIfCapacity(ctx context.Context, userID, channelID int64, maxUsers int) error {
|
|
|
|
|
res, err := d.q.JoinVoiceChannelIfCapacity(ctx, dbgen.JoinVoiceChannelIfCapacityParams{
|
2026-07-19 15:43:46 +00:00
|
|
|
UserID: userID,
|
|
|
|
|
ChannelID: channelID,
|
|
|
|
|
JoinedAt: newVoiceJoinToken(),
|
|
|
|
|
ChannelID_2: channelID,
|
2026-08-28 06:54:32 +02:00
|
|
|
UserID_2: userID,
|
2026-07-19 15:43:46 +00:00
|
|
|
ChannelID_3: int64(maxUsers),
|
|
|
|
|
})
|
2026-03-31 19:10:42 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("JoinVoiceChannelIfCapacity: %w", err)
|
|
|
|
|
}
|
|
|
|
|
n, _ := res.RowsAffected()
|
|
|
|
|
if n == 0 {
|
|
|
|
|
return ErrChannelFull
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-14 21:31:03 +01:00
|
|
|
// LeaveVoiceChannel removes the user's voice state entirely.
|
|
|
|
|
// It is safe to call when the user is not in any voice channel.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) LeaveVoiceChannel(ctx context.Context, userID int64) error {
|
|
|
|
|
if err := d.q.LeaveVoiceChannel(ctx, userID); err != nil {
|
2026-03-14 21:31:03 +01:00
|
|
|
return fmt.Errorf("LeaveVoiceChannel: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 11:41:59 +02:00
|
|
|
// LeaveVoiceChannelIfMatch removes the user's voice state only if the row
|
|
|
|
|
// still points at expectedChannelID and matches the expected join token.
|
|
|
|
|
// Returns true if a row was deleted.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) LeaveVoiceChannelIfMatch(ctx context.Context, userID, expectedChannelID int64, expectedJoinedAt string) (bool, error) {
|
|
|
|
|
result, err := d.q.LeaveVoiceChannelIfMatch(ctx, dbgen.LeaveVoiceChannelIfMatchParams{
|
2026-07-19 15:43:46 +00:00
|
|
|
UserID: userID,
|
|
|
|
|
ChannelID: expectedChannelID,
|
|
|
|
|
JoinedAt: expectedJoinedAt,
|
|
|
|
|
})
|
2026-03-31 11:41:59 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return false, fmt.Errorf("LeaveVoiceChannelIfMatch: %w", err)
|
|
|
|
|
}
|
|
|
|
|
n, _ := result.RowsAffected()
|
|
|
|
|
return n > 0, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-14 21:31:03 +01:00
|
|
|
// GetVoiceState returns the current voice state for the given user,
|
|
|
|
|
// or nil if the user is not in any voice channel.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) GetVoiceState(ctx context.Context, userID int64) (*VoiceState, error) {
|
|
|
|
|
r, err := d.q.GetUserVoiceState(ctx, userID)
|
2026-07-19 15:43:46 +00:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("GetVoiceState: %w", err)
|
|
|
|
|
}
|
|
|
|
|
vs := VoiceState{
|
2026-08-01 22:06:14 +02:00
|
|
|
UserID: r.UserID,
|
|
|
|
|
ChannelID: r.ChannelID,
|
|
|
|
|
Username: r.Username,
|
|
|
|
|
Muted: r.Muted != 0,
|
|
|
|
|
Deafened: r.Deafened != 0,
|
|
|
|
|
Speaking: r.Speaking != 0,
|
|
|
|
|
Camera: r.Camera != 0,
|
|
|
|
|
Screenshare: r.Screenshare != 0,
|
|
|
|
|
ServerMuted: r.ServerMuted != 0,
|
|
|
|
|
ServerDeafened: r.ServerDeafened != 0,
|
|
|
|
|
JoinedAt: r.JoinedAt,
|
2026-07-19 15:43:46 +00:00
|
|
|
}
|
|
|
|
|
return &vs, nil
|
2026-03-14 21:31:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetChannelVoiceStates returns all voice states for users currently in the
|
|
|
|
|
// given voice channel.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) GetChannelVoiceStates(ctx context.Context, channelID int64) ([]VoiceState, error) {
|
|
|
|
|
rows, err := d.q.GetChannelVoiceStates(ctx, channelID)
|
2026-03-14 21:31:03 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("GetChannelVoiceStates: %w", err)
|
|
|
|
|
}
|
2026-07-19 15:43:46 +00:00
|
|
|
states := make([]VoiceState, 0, len(rows))
|
|
|
|
|
for _, r := range rows {
|
|
|
|
|
states = append(states, VoiceState{
|
2026-08-01 22:06:14 +02:00
|
|
|
UserID: r.UserID,
|
|
|
|
|
ChannelID: r.ChannelID,
|
|
|
|
|
Username: r.Username,
|
|
|
|
|
Muted: r.Muted != 0,
|
|
|
|
|
Deafened: r.Deafened != 0,
|
|
|
|
|
Speaking: r.Speaking != 0,
|
|
|
|
|
Camera: r.Camera != 0,
|
|
|
|
|
Screenshare: r.Screenshare != 0,
|
|
|
|
|
ServerMuted: r.ServerMuted != 0,
|
|
|
|
|
ServerDeafened: r.ServerDeafened != 0,
|
|
|
|
|
JoinedAt: r.JoinedAt,
|
2026-07-19 15:43:46 +00:00
|
|
|
})
|
2026-03-14 21:31:03 +01:00
|
|
|
}
|
|
|
|
|
return states, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-19 04:04:53 +01:00
|
|
|
// GetAllVoiceStates returns voice states across all voice channels in a single
|
|
|
|
|
// query. Used at startup to build the ready payload without N+1 per-channel queries.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) GetAllVoiceStates(ctx context.Context) ([]VoiceState, error) {
|
|
|
|
|
rows, err := d.q.GetAllVoiceStates(ctx)
|
2026-03-19 04:04:53 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("GetAllVoiceStates: %w", err)
|
|
|
|
|
}
|
2026-07-19 15:43:46 +00:00
|
|
|
states := make([]VoiceState, 0, len(rows))
|
|
|
|
|
for _, r := range rows {
|
|
|
|
|
states = append(states, VoiceState{
|
2026-08-01 22:06:14 +02:00
|
|
|
UserID: r.UserID,
|
|
|
|
|
ChannelID: r.ChannelID,
|
|
|
|
|
Username: r.Username,
|
|
|
|
|
Muted: r.Muted != 0,
|
|
|
|
|
Deafened: r.Deafened != 0,
|
|
|
|
|
Speaking: r.Speaking != 0,
|
|
|
|
|
Camera: r.Camera != 0,
|
|
|
|
|
Screenshare: r.Screenshare != 0,
|
|
|
|
|
ServerMuted: r.ServerMuted != 0,
|
|
|
|
|
ServerDeafened: r.ServerDeafened != 0,
|
|
|
|
|
JoinedAt: r.JoinedAt,
|
2026-07-19 15:43:46 +00:00
|
|
|
})
|
2026-03-19 04:04:53 +01:00
|
|
|
}
|
|
|
|
|
return states, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-14 21:31:03 +01:00
|
|
|
// UpdateVoiceMute sets the muted field for the given user's voice state.
|
|
|
|
|
// It is safe to call when the user is not in any channel (no-op).
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) UpdateVoiceMute(ctx context.Context, userID int64, muted bool) error {
|
|
|
|
|
if err := d.q.UpdateVoiceMute(ctx, dbgen.UpdateVoiceMuteParams{
|
2026-07-19 15:43:46 +00:00
|
|
|
Muted: b2i64(muted),
|
|
|
|
|
UserID: userID,
|
|
|
|
|
}); err != nil {
|
2026-03-14 21:31:03 +01:00
|
|
|
return fmt.Errorf("UpdateVoiceMute: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UpdateVoiceDeafen sets the deafened field for the given user's voice state.
|
|
|
|
|
// It is safe to call when the user is not in any channel (no-op).
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) UpdateVoiceDeafen(ctx context.Context, userID int64, deafened bool) error {
|
|
|
|
|
if err := d.q.UpdateVoiceDeafen(ctx, dbgen.UpdateVoiceDeafenParams{
|
2026-07-19 15:43:46 +00:00
|
|
|
Deafened: b2i64(deafened),
|
|
|
|
|
UserID: userID,
|
|
|
|
|
}); err != nil {
|
2026-03-14 21:31:03 +01:00
|
|
|
return fmt.Errorf("UpdateVoiceDeafen: %w", err)
|
2026-08-01 22:06:14 +02:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-15 12:57:51 +02:00
|
|
|
// SetVoiceServerMute applies or clears the moderator-imposed mute, scoped to
|
|
|
|
|
// channelID -- the channel the caller's authorization check passed for.
|
|
|
|
|
// Applying it also sets muted so the client state matches immediately;
|
|
|
|
|
// clearing it leaves muted alone, so a user who was muted before the
|
|
|
|
|
// moderator acted stays muted until they unmute themselves.
|
|
|
|
|
//
|
|
|
|
|
// Reports matched=false when the target's voice_states row is no longer in
|
|
|
|
|
// channelID (OC-0005): a channel switch racing the moderator's DB round
|
|
|
|
|
// trips must not let this write land on whatever channel the target moved
|
|
|
|
|
// to, including a DM call nobody was authorized against. The row is left
|
|
|
|
|
// untouched in that case, same as if the write had never happened.
|
|
|
|
|
func (d *DB) SetVoiceServerMute(ctx context.Context, userID, channelID int64, serverMuted bool) (matched bool, err error) {
|
|
|
|
|
var res sql.Result
|
2026-08-01 22:06:14 +02:00
|
|
|
if serverMuted {
|
2026-08-15 12:57:51 +02:00
|
|
|
res, err = d.q.ApplyVoiceServerMute(ctx, dbgen.ApplyVoiceServerMuteParams{UserID: userID, ChannelID: channelID})
|
2026-08-01 22:06:14 +02:00
|
|
|
} else {
|
2026-08-15 12:57:51 +02:00
|
|
|
res, err = d.q.ClearVoiceServerMute(ctx, dbgen.ClearVoiceServerMuteParams{UserID: userID, ChannelID: channelID})
|
2026-08-01 22:06:14 +02:00
|
|
|
}
|
|
|
|
|
if err != nil {
|
2026-08-15 12:57:51 +02:00
|
|
|
return false, fmt.Errorf("SetVoiceServerMute: %w", err)
|
2026-08-01 22:06:14 +02:00
|
|
|
}
|
2026-08-15 12:57:51 +02:00
|
|
|
n, _ := res.RowsAffected()
|
|
|
|
|
return n > 0, nil
|
2026-08-01 22:06:14 +02:00
|
|
|
}
|
|
|
|
|
|
2026-08-15 12:57:51 +02:00
|
|
|
// SetVoiceServerDeafen applies or clears the moderator-imposed deafen, scoped
|
|
|
|
|
// to channelID. Mirrors SetVoiceServerMute, including the asymmetric handling
|
|
|
|
|
// of deafened and the channel-scoped matched result.
|
|
|
|
|
func (d *DB) SetVoiceServerDeafen(ctx context.Context, userID, channelID int64, serverDeafened bool) (matched bool, err error) {
|
|
|
|
|
var res sql.Result
|
2026-08-01 22:06:14 +02:00
|
|
|
if serverDeafened {
|
2026-08-15 12:57:51 +02:00
|
|
|
res, err = d.q.ApplyVoiceServerDeafen(ctx, dbgen.ApplyVoiceServerDeafenParams{UserID: userID, ChannelID: channelID})
|
2026-08-01 22:06:14 +02:00
|
|
|
} else {
|
2026-08-15 12:57:51 +02:00
|
|
|
res, err = d.q.ClearVoiceServerDeafen(ctx, dbgen.ClearVoiceServerDeafenParams{UserID: userID, ChannelID: channelID})
|
2026-08-01 22:06:14 +02:00
|
|
|
}
|
|
|
|
|
if err != nil {
|
2026-08-15 12:57:51 +02:00
|
|
|
return false, fmt.Errorf("SetVoiceServerDeafen: %w", err)
|
2026-03-14 21:31:03 +01:00
|
|
|
}
|
2026-08-15 12:57:51 +02:00
|
|
|
n, _ := res.RowsAffected()
|
|
|
|
|
return n > 0, nil
|
2026-03-14 21:31:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ClearVoiceState removes a user's voice state on disconnect.
|
|
|
|
|
// Equivalent to LeaveVoiceChannel but named to clarify the disconnect use case.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) ClearVoiceState(ctx context.Context, userID int64) error {
|
|
|
|
|
if err := d.q.ClearVoiceState(ctx, userID); err != nil {
|
2026-03-14 21:31:03 +01:00
|
|
|
return fmt.Errorf("ClearVoiceState: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 11:42:25 +01:00
|
|
|
// ClearAllVoiceStates removes all voice state rows. Called on server startup
|
|
|
|
|
// to clear stale state from a previous run.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) ClearAllVoiceStates(ctx context.Context) error {
|
|
|
|
|
if err := d.q.ClearAllVoiceStates(ctx); err != nil {
|
2026-03-15 11:42:25 +01:00
|
|
|
return fmt.Errorf("ClearAllVoiceStates: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-21 11:59:14 +01:00
|
|
|
// CountActiveCameras returns the number of users with camera enabled in the
|
|
|
|
|
// given voice channel. Uses the DB as source of truth (race-free via SQLite
|
|
|
|
|
// serialization) rather than querying LiveKit.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) CountActiveCameras(ctx context.Context, channelID int64) (int, error) {
|
|
|
|
|
count, err := d.q.CountActiveCameras(ctx, channelID)
|
2026-03-21 11:59:14 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return 0, fmt.Errorf("CountActiveCameras: %w", err)
|
|
|
|
|
}
|
2026-07-19 15:43:46 +00:00
|
|
|
return int(count), nil
|
2026-03-21 11:59:14 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-15 11:42:25 +01:00
|
|
|
// UpdateVoiceCamera sets the camera field for the given user's voice state.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) UpdateVoiceCamera(ctx context.Context, userID int64, camera bool) error {
|
|
|
|
|
if err := d.q.UpdateVoiceCamera(ctx, dbgen.UpdateVoiceCameraParams{
|
2026-07-19 15:43:46 +00:00
|
|
|
Camera: b2i64(camera),
|
|
|
|
|
UserID: userID,
|
|
|
|
|
}); err != nil {
|
2026-03-15 11:42:25 +01:00
|
|
|
return fmt.Errorf("UpdateVoiceCamera: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 09:23:17 +02:00
|
|
|
// EnableCameraIfUnderLimit atomically enables a user's camera only if the
|
|
|
|
|
// channel has not yet reached maxVideo active cameras. Returns true if the
|
|
|
|
|
// camera was enabled, false if the limit was already reached.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) EnableCameraIfUnderLimit(ctx context.Context, userID, channelID int64, maxVideo int) (bool, error) {
|
|
|
|
|
res, err := d.q.EnableCameraIfUnderLimit(ctx, dbgen.EnableCameraIfUnderLimitParams{
|
2026-07-19 15:43:46 +00:00
|
|
|
UserID: userID,
|
|
|
|
|
ChannelID: channelID,
|
|
|
|
|
ChannelID_2: channelID,
|
2026-08-16 17:06:11 +02:00
|
|
|
MaxVideo: int64(maxVideo),
|
2026-07-19 15:43:46 +00:00
|
|
|
})
|
2026-04-01 09:23:17 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return false, fmt.Errorf("EnableCameraIfUnderLimit: %w", err)
|
|
|
|
|
}
|
|
|
|
|
rows, err := res.RowsAffected()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, fmt.Errorf("EnableCameraIfUnderLimit RowsAffected: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return rows > 0, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 11:42:25 +01:00
|
|
|
// UpdateVoiceScreenshare sets the screenshare field for the given user's voice state.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) UpdateVoiceScreenshare(ctx context.Context, userID int64, screenshare bool) error {
|
|
|
|
|
if err := d.q.UpdateVoiceScreenshare(ctx, dbgen.UpdateVoiceScreenshareParams{
|
2026-07-19 15:43:46 +00:00
|
|
|
Screenshare: b2i64(screenshare),
|
|
|
|
|
UserID: userID,
|
|
|
|
|
}); err != nil {
|
2026-03-15 11:42:25 +01:00
|
|
|
return fmt.Errorf("UpdateVoiceScreenshare: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-15 12:57:51 +02:00
|
|
|
// EnableScreenshareIfUnderLimit atomically enables a user's screenshare only
|
|
|
|
|
// if the channel has not yet reached maxVideo active video streams — camera
|
|
|
|
|
// and screenshare draw from the same voice_max_video budget (OC-0023).
|
|
|
|
|
// Returns true if the screenshare was enabled, false if the limit was
|
|
|
|
|
// already reached.
|
|
|
|
|
func (d *DB) EnableScreenshareIfUnderLimit(ctx context.Context, userID, channelID int64, maxVideo int) (bool, error) {
|
|
|
|
|
res, err := d.q.EnableScreenshareIfUnderLimit(ctx, dbgen.EnableScreenshareIfUnderLimitParams{
|
|
|
|
|
UserID: userID,
|
|
|
|
|
ChannelID: channelID,
|
|
|
|
|
ChannelID_2: channelID,
|
2026-08-16 17:06:11 +02:00
|
|
|
MaxVideo: int64(maxVideo),
|
2026-08-15 12:57:51 +02:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, fmt.Errorf("EnableScreenshareIfUnderLimit: %w", err)
|
|
|
|
|
}
|
|
|
|
|
rows, err := res.RowsAffected()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, fmt.Errorf("EnableScreenshareIfUnderLimit RowsAffected: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return rows > 0, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 11:42:25 +01:00
|
|
|
// CountChannelVoiceUsers returns the number of users currently in the given
|
|
|
|
|
// voice channel.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) CountChannelVoiceUsers(ctx context.Context, channelID int64) (int, error) {
|
2026-03-15 11:42:25 +01:00
|
|
|
var count int
|
2026-07-31 15:41:57 +02:00
|
|
|
err := d.reader.QueryRowContext(ctx,
|
2026-03-15 11:42:25 +01:00
|
|
|
`SELECT COUNT(*) FROM voice_states WHERE channel_id = ?`,
|
|
|
|
|
channelID,
|
|
|
|
|
).Scan(&count)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, fmt.Errorf("CountChannelVoiceUsers: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return count, nil
|
|
|
|
|
}
|