Files
OwnCord/Server/service/block.go
T
Claude 5f1d6fc287 refactor(server): remove the store abstraction layer (D3)
Deletes Server/store (SQLiteStore, MemStore, the composed Store
interface) and collapses to a single sqlc-backed db package, executing
the prior audit's P4 "single data layer" direction (finding #6).

SQLiteStore was a pure pass-through to *db.DB, so consumers now depend
on narrow interfaces that *db.DB satisfies directly:

  - service.Store   (service/datastore.go, renamed from store/store.go)
  - ws.EventStore   (ws/eventstore.go)
  - plugin.PluginStore (plugin/pluginstore.go)

The event- and plugin-KV methods that lived in the store's SQLite
implementation move into the db package (db/event_queries.go,
db/plugin_queries.go), keeping their raw-SQL form.

Tests: the MemStore-based unit tests now run against a real in-memory
SQLite db opened per-test with migrations applied, via package-local
seed helpers. Fault-injection tests embed a real *db.DB and override the
single method under test, preserving error-path coverage. Full server
suite and sqlc-verify are green.

Docs: audit finding #6 and A-2026-07-06 marked resolved; decisions D3
updated; architecture server.md / data-model.md diagrams and prose
updated to the api -> service -> db layering.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UA17KPvqGBX3XbXYnMf1rA
2026-07-19 16:33:58 +00:00

79 lines
2.1 KiB
Go

package service
import (
"context"
"fmt"
"log/slog"
"time"
"github.com/owncord/server/telemetry"
)
// BlockService handles user block/unblock operations.
type BlockService struct {
st Store
}
// NewBlockService creates a BlockService.
func NewBlockService(st Store) *BlockService {
return &BlockService{st: st}
}
// BlockUser blocks a target user. Validates the target exists and
// prevents self-blocking.
func (s *BlockService) BlockUser(ctx context.Context, blockerID, targetID int64) error {
ctx, span := telemetry.GlobalTracer("service/block").Start(ctx, "BlockService.BlockUser",
telemetry.Int64("blocker_id", blockerID),
telemetry.Int64("target_id", targetID),
)
start := time.Now()
defer func() {
telemetry.TimeSince(ctx, telemetry.NewAppMetrics().ServiceCallDurationSec, start,
telemetry.String("method", "BlockUser"))
span.End()
}()
if targetID <= 0 {
return fmt.Errorf("%w: user_id must be positive", ErrBadRequest)
}
if blockerID == targetID {
return fmt.Errorf("%w: cannot block yourself", ErrBadRequest)
}
target, err := s.st.GetUserByID(targetID)
if err != nil || target == nil {
return fmt.Errorf("%w: user not found", ErrNotFound)
}
if err := s.st.BlockUser(blockerID, targetID); err != nil {
return fmt.Errorf("%w: failed to block user", ErrInternal)
}
slog.Info("user blocked", "blocker_id", blockerID, "target_id", targetID)
return nil
}
// UnblockUser removes a block on a target user.
func (s *BlockService) UnblockUser(blockerID, targetID int64) error {
if targetID <= 0 {
return fmt.Errorf("%w: user_id must be positive", ErrBadRequest)
}
if err := s.st.UnblockUser(blockerID, targetID); err != nil {
return fmt.Errorf("%w: failed to unblock user", ErrInternal)
}
slog.Info("user unblocked", "blocker_id", blockerID, "target_id", targetID)
return nil
}
// ListBlocked returns all user IDs blocked by the given user.
func (s *BlockService) ListBlocked(blockerID int64) ([]int64, error) {
ids, err := s.st.ListBlockedUsers(blockerID)
if err != nil {
return nil, fmt.Errorf("%w: failed to list blocked users", ErrInternal)
}
if ids == nil {
ids = []int64{}
}
return ids, nil
}