mirror of
https://github.com/J3vb/OwnCord.git
synced 2026-09-03 03:50:00 +03:00
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
104 lines
2.8 KiB
Go
104 lines
2.8 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"github.com/owncord/server/db"
|
|
"github.com/owncord/server/telemetry"
|
|
)
|
|
|
|
// DMService handles direct message channel operations.
|
|
type DMService struct {
|
|
st Store
|
|
}
|
|
|
|
// NewDMService creates a DMService.
|
|
func NewDMService(st Store) *DMService {
|
|
return &DMService{st: st}
|
|
}
|
|
|
|
// CreateDMResult holds the result of creating or fetching a DM channel.
|
|
type CreateDMResult struct {
|
|
Channel *db.Channel
|
|
Created bool
|
|
Recipient *db.User
|
|
}
|
|
|
|
// CreateDM creates or retrieves a DM channel between two users.
|
|
// Validates that neither user has blocked the other.
|
|
func (s *DMService) CreateDM(ctx context.Context, userID, recipientID int64) (*CreateDMResult, error) {
|
|
ctx, span := telemetry.GlobalTracer("service/dm").Start(ctx, "DMService.CreateDM",
|
|
telemetry.Int64("user_id", userID),
|
|
telemetry.Int64("recipient_id", recipientID),
|
|
)
|
|
start := time.Now()
|
|
defer func() {
|
|
telemetry.TimeSince(ctx, telemetry.NewAppMetrics().ServiceCallDurationSec, start,
|
|
telemetry.String("method", "CreateDM"))
|
|
span.End()
|
|
}()
|
|
|
|
if recipientID <= 0 {
|
|
return nil, fmt.Errorf("%w: recipient_id must be positive", ErrBadRequest)
|
|
}
|
|
if userID == recipientID {
|
|
return nil, fmt.Errorf("%w: cannot create DM with yourself", ErrBadRequest)
|
|
}
|
|
|
|
recipient, err := s.st.GetUserByID(recipientID)
|
|
if err != nil || recipient == nil {
|
|
return nil, fmt.Errorf("%w: recipient not found", ErrNotFound)
|
|
}
|
|
|
|
blocked, err := s.st.IsEitherBlocked(userID, recipientID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%w: failed to check block status", ErrInternal)
|
|
}
|
|
if blocked {
|
|
return nil, fmt.Errorf("%w: cannot create DM — user is blocked", ErrForbidden)
|
|
}
|
|
|
|
ch, created, err := s.st.GetOrCreateDMChannel(userID, recipientID)
|
|
if err != nil {
|
|
slog.Error("DMService.CreateDM", "err", err)
|
|
return nil, fmt.Errorf("%w: failed to create DM channel", ErrInternal)
|
|
}
|
|
|
|
return &CreateDMResult{
|
|
Channel: ch,
|
|
Created: created,
|
|
Recipient: recipient,
|
|
}, nil
|
|
}
|
|
|
|
// ListDMs returns all open DM channels for a user.
|
|
func (s *DMService) ListDMs(userID int64) ([]db.DMChannelInfo, error) {
|
|
dms, err := s.st.GetUserDMChannels(userID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%w: failed to list DMs", ErrInternal)
|
|
}
|
|
return dms, nil
|
|
}
|
|
|
|
// CloseDM closes a DM channel for a user.
|
|
func (s *DMService) CloseDM(userID, channelID int64) error {
|
|
if channelID <= 0 {
|
|
return fmt.Errorf("%w: channel_id must be positive", ErrBadRequest)
|
|
}
|
|
|
|
ok, err := s.st.IsDMParticipant(userID, channelID)
|
|
if err != nil || !ok {
|
|
return fmt.Errorf("%w: not a participant in this DM", ErrNotFound)
|
|
}
|
|
|
|
if err := s.st.CloseDM(userID, channelID); err != nil {
|
|
return fmt.Errorf("%w: failed to close DM", ErrInternal)
|
|
}
|
|
|
|
slog.Debug("DM closed", "user_id", userID, "channel_id", channelID)
|
|
return nil
|
|
}
|