Files
OwnCord/Server/service/channel.go
T
J3vb ba66629077 fix(ci): resolve all golangci-lint and ESLint failures on PR #1132
Client:
- ci.yml: patch auto-generated events.ts to rename Event -> _Event
  so @typescript-eslint/no-unused-vars does not fail on generated code

Server (gocritic):
- service/channel.go: rangeValCopy (line 74), elseif (line 174)
- service/message.go: rangeValCopy (line 648), elseif (lines 438, 496)
- ws/emit.go: caseOrder — ChannelEvent before BroadcastAllEvent
- ws/handlers_command_test.go: stringXbytes — use bytes.Equal
- ws/pubsub_test.go: stringXbytes — use bytes.Equal

Server (nilerr):
- service/channel.go: nolint:nilerr for intentional silent drops in HandleTyping

Server (gosec):
- plugin/host_ui.go: G703 nolint — path already sanitized above
- plugin/registry.go: G302 — tighten plugin file permissions 0o640 → 0o600
- ws/hub.go, ws/serve.go: G115 nolint — seq counters never reach MaxInt64

Server (unused/unparam):
- plugin/registry.go: nolint:unused for wazero-tagged module field
- telemetry/metrics.go: nolint:unused for otel-tagged resetAppMetricsForInit
- ws/command.go: nolint:unparam for map entries whose error return is always nil

Server (staticcheck ST1000/ST1020):
- Add blank line before package declarations in phase-comment files
  (api/plugins_handler.go, plugin/host_{commands,events,http}.go,
   telemetry/metrics.go, telemetry/middleware.go,
   telemetry/telemetry_default.go, ws/event_persister.go)
- Fix GlobalTracer/GlobalMeter doc comments to start with function name
2026-04-07 10:10:38 +02:00

187 lines
5.5 KiB
Go

package service
import (
"context"
"fmt"
"log/slog"
"time"
"github.com/owncord/server/db"
"github.com/owncord/server/permissions"
"github.com/owncord/server/store"
"github.com/owncord/server/telemetry"
)
// ChannelService handles channel-related business logic including
// listing, permission-filtered access, typing, presence, and read state.
type ChannelService struct {
st store.Store
perms *PermissionService
}
// NewChannelService creates a ChannelService.
func NewChannelService(st store.Store, perms *PermissionService) *ChannelService {
return &ChannelService{
st: st,
perms: perms,
}
}
// ListVisibleChannels returns channels the user has ReadMessages permission for.
// DM channels are excluded (they are accessed via DMService).
func (s *ChannelService) ListVisibleChannels(ctx context.Context, userID int64) ([]db.Channel, error) {
// Phase B Step 8 — span the public service entrypoint.
ctx, span := telemetry.GlobalTracer("service/channel").Start(ctx,
"ChannelService.ListVisibleChannels",
telemetry.Int64("user_id", userID),
)
start := time.Now()
defer func() {
telemetry.TimeSince(ctx, telemetry.NewAppMetrics().ServiceCallDurationSec, start,
telemetry.String("method", "ListVisibleChannels"))
span.End()
}()
all, err := s.st.ListChannels()
if err != nil {
slog.Error("ChannelService.ListVisibleChannels", "err", err)
return nil, fmt.Errorf("%w: failed to list channels", ErrInternal)
}
role, err := s.perms.GetRoleForUser(userID)
if err != nil || role == nil {
slog.Error("ChannelService.ListVisibleChannels GetRoleForUser", "err", err, "user_id", userID)
return nil, fmt.Errorf("%w: failed to get role", ErrInternal)
}
isAdmin := permissions.HasAdmin(role.Permissions)
if isAdmin {
// Admin sees all non-DM channels.
var visible []db.Channel
for i := range all {
if all[i].Type != "dm" {
visible = append(visible, all[i])
}
}
return visible, nil
}
overrides, err := s.st.GetAllChannelPermissionsForRole(role.ID)
if err != nil {
overrides = make(map[int64]db.ChannelOverride)
}
var visible []db.Channel
for i := range all {
if all[i].Type == "dm" {
continue
}
o := overrides[all[i].ID]
effective := permissions.EffectivePerms(role.Permissions, o.Allow, o.Deny)
if effective&permissions.ReadMessages == permissions.ReadMessages {
visible = append(visible, all[i])
}
}
if visible == nil {
visible = []db.Channel{}
}
return visible, nil
}
// HandleTyping processes a typing start event for a channel.
// Returns the channel so callers can build broadcast events.
// Silent errors are returned as nil (typing indicators are best-effort).
func (s *ChannelService) HandleTyping(userID, channelID int64, limiter interface {
Allow(key string, limit int, window time.Duration) bool
},
) (*db.Channel, error) {
if channelID <= 0 {
return nil, nil
}
// Per-user-per-channel rate limit.
ratKey := fmt.Sprintf("typing:%d:%d", userID, channelID)
if limiter != nil && !limiter.Allow(ratKey, 1, 3*time.Second) {
return nil, nil
}
ch, err := s.st.GetChannel(channelID)
if err != nil || ch == nil {
return nil, nil //nolint:nilerr // typing indicators are best-effort; errors silently dropped
}
if ch.Type == "dm" {
ok, dmErr := s.st.IsDMParticipant(userID, channelID)
if dmErr != nil || !ok {
return nil, nil //nolint:nilerr // typing indicators are best-effort; errors silently dropped
}
} else if !s.perms.HasChannelPerm(userID, channelID, permissions.ReadMessages) {
return nil, nil // silent drop
}
return ch, nil
}
// GetDMParticipantIDs returns the participant IDs for a DM channel.
// Convenience method for handlers building DM events.
func (s *ChannelService) GetDMParticipantIDs(channelID int64) ([]int64, error) {
return s.st.GetDMParticipantIDs(channelID)
}
// HandlePresenceUpdate validates and persists a presence status change.
func (s *ChannelService) HandlePresenceUpdate(userID int64, status string, limiter interface {
Allow(key string, limit int, window time.Duration) bool
},
) error {
// Rate limit.
ratKey := fmt.Sprintf("presence:%d", userID)
if limiter != nil && !limiter.Allow(ratKey, 1, 10*time.Second) {
return ErrRateLimited
}
validStatuses := map[string]bool{
"online": true, "idle": true, "dnd": true, "offline": true,
}
if !validStatuses[status] {
return fmt.Errorf("%w: invalid status", ErrBadRequest)
}
if err := s.st.UpdateUserStatus(userID, status); err != nil {
slog.Error("ChannelService.HandlePresenceUpdate", "err", err, "user_id", userID)
return fmt.Errorf("%w: failed to update status", ErrInternal)
}
return nil
}
// HandleChannelFocus processes a channel focus event and updates read state.
// Returns the channel for callers to set client state.
func (s *ChannelService) HandleChannelFocus(userID, channelID int64) (*db.Channel, error) {
if channelID <= 0 {
return nil, fmt.Errorf("%w: channel_id must be positive", ErrBadRequest)
}
ch, err := s.st.GetChannel(channelID)
if err != nil || ch == nil {
return nil, fmt.Errorf("%w: channel not found", ErrNotFound)
}
if ch.Type == "dm" {
ok, err := s.st.IsDMParticipant(userID, channelID)
if err != nil || !ok {
return nil, fmt.Errorf("%w: access denied", ErrForbidden)
}
} else if !s.perms.HasChannelPerm(userID, channelID, permissions.ReadMessages) {
return nil, fmt.Errorf("%w: access denied", ErrForbidden)
}
// Mark channel as read.
latestID, err := s.st.GetLatestMessageID(channelID)
if err == nil && latestID > 0 {
_ = s.st.UpdateReadState(userID, channelID, latestID)
}
slog.Debug("channel_focus", "user_id", userID, "channel_id", channelID)
return ch, nil
}