Files
OwnCord/Server/service/invite.go
T
J3vb 81c121c2bd fix(server): resolve golangci-lint contextcheck, errcheck, gocritic findings
contextcheck: add ctx context.Context as first param to ListVisibleChannels,
BlockUser, CreateDM, CreateInvite, UpdateProfile, SendMessage; pass r.Context()
from HTTP handlers and ctx from WS handler; replace context.Background() in
telemetry spans with the propagated ctx.

errcheck: suppress justified Close() errors — defer func(){ _ = rows.Close() }()
in sqlite_events.go (idiomatic; rows.Err() checked), _ = resp.Body.Close() in
host_http.go (body fully consumed), _ = f.Close() in host_ui.go (read-only fd).

gocritic/rangeValCopy: rewrite for _, ch := range all (line 60) to indexed loop
in ChannelService.ListVisibleChannels to avoid 144-byte per-iteration copy.
2026-04-07 09:10:14 +02:00

84 lines
2.3 KiB
Go

package service
import (
"context"
"fmt"
"time"
"github.com/owncord/server/db"
"github.com/owncord/server/store"
"github.com/owncord/server/telemetry"
)
// InviteService handles invite management.
type InviteService struct {
st store.Store
}
// NewInviteService creates an InviteService.
func NewInviteService(st store.Store) *InviteService {
return &InviteService{st: st}
}
// maxInviteExpiryHoursVal caps invite expiry to 30 days (H-4 hardening).
const maxInviteExpiryHoursVal = 720
// MaxInviteExpiryHours returns the maximum invite expiry in hours.
func MaxInviteExpiryHours() int { return maxInviteExpiryHoursVal }
// CreateInvite creates a new invite code with optional max uses and expiry.
func (s *InviteService) CreateInvite(ctx context.Context, createdBy int64, maxUses int, expiresInHours int) (*db.Invite, error) {
ctx, span := telemetry.GlobalTracer("service/invite").Start(ctx, "InviteService.CreateInvite",
telemetry.Int64("created_by", createdBy),
)
start := time.Now()
defer func() {
telemetry.TimeSince(ctx, telemetry.NewAppMetrics().ServiceCallDurationSec, start,
telemetry.String("method", "CreateInvite"))
span.End()
}()
// Cap expiry.
if expiresInHours > maxInviteExpiryHoursVal {
expiresInHours = maxInviteExpiryHoursVal
}
var expiresAt *time.Time
if expiresInHours > 0 {
t := time.Now().Add(time.Duration(expiresInHours) * time.Hour)
expiresAt = &t
}
code, err := s.st.CreateInvite(createdBy, maxUses, expiresAt)
if err != nil {
return nil, fmt.Errorf("%w: failed to create invite", ErrInternal)
}
invite, err := s.st.GetInvite(code)
if err != nil || invite == nil {
return nil, fmt.Errorf("%w: failed to retrieve invite", ErrInternal)
}
return invite, nil
}
// ListInvites returns all invites.
func (s *InviteService) ListInvites() ([]*db.Invite, error) {
invites, err := s.st.ListInvites()
if err != nil {
return nil, fmt.Errorf("%w: failed to list invites", ErrInternal)
}
return invites, nil
}
// RevokeInvite revokes an invite by code.
func (s *InviteService) RevokeInvite(code string) error {
invite, err := s.st.GetInvite(code)
if err != nil || invite == nil {
return fmt.Errorf("%w: invite not found", ErrNotFound)
}
if err := s.st.RevokeInvite(code); err != nil {
return fmt.Errorf("%w: failed to revoke invite", ErrInternal)
}
return nil
}