2026-04-04 16:48:57 +00:00
|
|
|
package db
|
|
|
|
|
|
2026-07-19 15:01:54 +00:00
|
|
|
import (
|
2026-07-23 17:03:52 +02:00
|
|
|
"context"
|
2026-07-19 15:01:54 +00:00
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"database/sql"
|
|
|
|
|
|
2026-08-28 06:54:32 +02:00
|
|
|
"github.com/J3vb/OwnCord/Server/db/dbgen"
|
2026-07-19 15:01:54 +00:00
|
|
|
)
|
2026-04-04 16:48:57 +00:00
|
|
|
|
|
|
|
|
// BlockUser adds a block from blocker to blocked. Idempotent — re-blocking
|
|
|
|
|
// a user that is already blocked is a no-op (INSERT OR IGNORE).
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) BlockUser(ctx context.Context, blockerID, blockedID int64) error {
|
|
|
|
|
if err := d.q.BlockUser(ctx, dbgen.BlockUserParams{
|
2026-07-19 15:01:54 +00:00
|
|
|
BlockerID: blockerID,
|
|
|
|
|
BlockedID: blockedID,
|
|
|
|
|
}); err != nil {
|
2026-04-04 16:48:57 +00:00
|
|
|
return fmt.Errorf("BlockUser: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UnblockUser removes a block. Idempotent — unblocking a non-blocked user is
|
|
|
|
|
// a no-op.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) UnblockUser(ctx context.Context, blockerID, blockedID int64) error {
|
|
|
|
|
if err := d.q.UnblockUser(ctx, dbgen.UnblockUserParams{
|
2026-07-19 15:01:54 +00:00
|
|
|
BlockerID: blockerID,
|
|
|
|
|
BlockedID: blockedID,
|
|
|
|
|
}); err != nil {
|
2026-04-04 16:48:57 +00:00
|
|
|
return fmt.Errorf("UnblockUser: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsBlocked returns true if blockerID has blocked blockedID.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) IsBlocked(ctx context.Context, blockerID, blockedID int64) (bool, error) {
|
|
|
|
|
_, err := d.q.IsBlocked(ctx, dbgen.IsBlockedParams{
|
2026-07-19 15:01:54 +00:00
|
|
|
BlockerID: blockerID,
|
|
|
|
|
BlockedID: blockedID,
|
|
|
|
|
})
|
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
2026-04-04 16:48:57 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return false, fmt.Errorf("IsBlocked: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsEitherBlocked returns true if either user has blocked the other.
|
|
|
|
|
// Used for DM authorization — if either party has blocked the other,
|
|
|
|
|
// messaging is denied.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) IsEitherBlocked(ctx context.Context, userA, userB int64) (bool, error) {
|
|
|
|
|
_, err := d.q.IsEitherBlocked(ctx, dbgen.IsEitherBlockedParams{
|
2026-07-19 15:01:54 +00:00
|
|
|
BlockerID: userA,
|
|
|
|
|
BlockedID: userB,
|
|
|
|
|
BlockerID_2: userB,
|
|
|
|
|
BlockedID_2: userA,
|
|
|
|
|
})
|
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
2026-04-04 16:48:57 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return false, fmt.Errorf("IsEitherBlocked: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ListBlockedUsers returns the IDs of all users blocked by the given user.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (d *DB) ListBlockedUsers(ctx context.Context, blockerID int64) ([]int64, error) {
|
|
|
|
|
ids, err := d.q.ListBlockedUsers(ctx, blockerID)
|
2026-04-04 16:48:57 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("ListBlockedUsers: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return ids, nil
|
|
|
|
|
}
|