2026-04-05 21:01:29 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
2026-04-06 09:49:03 +00:00
|
|
|
"context"
|
2026-04-05 21:01:29 +00:00
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
|
2026-08-28 06:54:32 +02:00
|
|
|
"github.com/J3vb/OwnCord/Server/db"
|
|
|
|
|
"github.com/J3vb/OwnCord/Server/telemetry"
|
2026-04-05 21:01:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// InviteService handles invite management.
|
|
|
|
|
type InviteService struct {
|
2026-07-19 16:33:58 +00:00
|
|
|
st Store
|
2026-04-05 21:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewInviteService creates an InviteService.
|
2026-07-19 16:33:58 +00:00
|
|
|
func NewInviteService(st Store) *InviteService {
|
2026-04-05 21:01:29 +00:00
|
|
|
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.
|
2026-04-07 09:10:14 +02:00
|
|
|
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",
|
2026-04-06 09:49:03 +00:00
|
|
|
telemetry.Int64("created_by", createdBy),
|
|
|
|
|
)
|
|
|
|
|
start := time.Now()
|
|
|
|
|
defer func() {
|
2026-04-06 13:48:41 +00:00
|
|
|
telemetry.TimeSince(ctx, telemetry.NewAppMetrics().ServiceCallDurationSec, start,
|
2026-04-06 09:49:03 +00:00
|
|
|
telemetry.String("method", "CreateInvite"))
|
|
|
|
|
span.End()
|
|
|
|
|
}()
|
|
|
|
|
|
2026-04-05 21:01:29 +00:00
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-23 17:03:52 +02:00
|
|
|
code, err := s.st.CreateInvite(ctx, createdBy, maxUses, expiresAt)
|
2026-04-05 21:01:29 +00:00
|
|
|
if err != nil {
|
2026-07-24 11:06:59 +02:00
|
|
|
return nil, fmt.Errorf("%w: failed to create invite: %v", ErrInternal, err)
|
2026-04-05 21:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-23 17:03:52 +02:00
|
|
|
invite, err := s.st.GetInvite(ctx, code)
|
2026-04-06 22:48:59 +02:00
|
|
|
if err != nil || invite == nil {
|
2026-07-24 11:06:59 +02:00
|
|
|
return nil, fmt.Errorf("%w: failed to retrieve invite: %v", ErrInternal, err)
|
2026-04-05 21:01:29 +00:00
|
|
|
}
|
|
|
|
|
return invite, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ListInvites returns all invites.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (s *InviteService) ListInvites(ctx context.Context) ([]*db.Invite, error) {
|
|
|
|
|
invites, err := s.st.ListInvites(ctx)
|
2026-04-05 21:01:29 +00:00
|
|
|
if err != nil {
|
2026-07-24 11:06:59 +02:00
|
|
|
return nil, fmt.Errorf("%w: failed to list invites: %v", ErrInternal, err)
|
2026-04-05 21:01:29 +00:00
|
|
|
}
|
|
|
|
|
return invites, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RevokeInvite revokes an invite by code.
|
2026-07-23 17:03:52 +02:00
|
|
|
func (s *InviteService) RevokeInvite(ctx context.Context, code string) error {
|
|
|
|
|
invite, err := s.st.GetInvite(ctx, code)
|
2026-04-05 21:01:29 +00:00
|
|
|
if err != nil || invite == nil {
|
|
|
|
|
return fmt.Errorf("%w: invite not found", ErrNotFound)
|
|
|
|
|
}
|
2026-07-23 17:03:52 +02:00
|
|
|
if err := s.st.RevokeInvite(ctx, code); err != nil {
|
2026-07-24 11:06:59 +02:00
|
|
|
return fmt.Errorf("%w: failed to revoke invite: %v", ErrInternal, err)
|
2026-04-05 21:01:29 +00:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|