94 lines
3.6 KiB
Go
94 lines
3.6 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
channelmodel "github.com/gochat/gochat/internal/model/channel"
|
|
"github.com/gochat/gochat/internal/repository"
|
|
applogger "github.com/gochat/gochat/pkg/logger"
|
|
)
|
|
|
|
// ChannelTwitterService implements business logic for Twitter channel operations.
|
|
// Reference: Chatwoot app/controllers/api/v1/accounts/channels/twitter_controller.rb
|
|
type ChannelTwitterService struct {
|
|
repo *repository.ChannelTwitterRepo
|
|
}
|
|
|
|
// NewChannelTwitterService creates a new ChannelTwitter service.
|
|
func NewChannelTwitterService(repo *repository.ChannelTwitterRepo) *ChannelTwitterService {
|
|
return &ChannelTwitterService{repo: repo}
|
|
}
|
|
|
|
// GetByID retrieves a Twitter channel by ID.
|
|
func (s *ChannelTwitterService) GetByID(ctx context.Context, id uint) (*channelmodel.ChannelTwitter, error) {
|
|
return s.repo.FindByID(ctx, id)
|
|
}
|
|
|
|
// GetByInboxID retrieves a Twitter channel by inbox ID.
|
|
func (s *ChannelTwitterService) GetByInboxID(ctx context.Context, inboxID uint) (*channelmodel.ChannelTwitter, error) {
|
|
return s.repo.FindByInboxID(ctx, inboxID)
|
|
}
|
|
|
|
// GetByAccountAndInboxID retrieves a Twitter channel scoped to an account and inbox.
|
|
func (s *ChannelTwitterService) GetByAccountAndInboxID(ctx context.Context, accountID, inboxID uint) (*channelmodel.ChannelTwitter, error) {
|
|
return s.repo.FindByAccountAndInboxID(ctx, accountID, inboxID)
|
|
}
|
|
|
|
// GetByTwitterUserID retrieves a Twitter channel by Twitter user ID.
|
|
func (s *ChannelTwitterService) GetByTwitterUserID(ctx context.Context, twitterUserID string) (*channelmodel.ChannelTwitter, error) {
|
|
return s.repo.FindByTwitterUserID(ctx, twitterUserID)
|
|
}
|
|
|
|
// Create creates a new Twitter channel record.
|
|
func (s *ChannelTwitterService) Create(ctx context.Context, ch *channelmodel.ChannelTwitter) error {
|
|
if err := s.repo.Create(ctx, ch); err != nil {
|
|
applogger.L().Errorf("Failed to create twitter channel: %v", err)
|
|
return fmt.Errorf("failed to create twitter channel: %w", err)
|
|
}
|
|
applogger.L().Infof("Twitter channel created (id=%d, inbox_id=%d)", ch.ID, ch.InboxID)
|
|
return nil
|
|
}
|
|
|
|
// Update updates a Twitter channel record.
|
|
func (s *ChannelTwitterService) Update(ctx context.Context, ch *channelmodel.ChannelTwitter) error {
|
|
if err := s.repo.Update(ctx, ch); err != nil {
|
|
applogger.L().Errorf("Failed to update twitter channel: %v", err)
|
|
return fmt.Errorf("failed to update twitter channel: %w", err)
|
|
}
|
|
applogger.L().Infof("Twitter channel updated (id=%d)", ch.ID)
|
|
return nil
|
|
}
|
|
|
|
// Delete soft-deletes a Twitter channel record.
|
|
func (s *ChannelTwitterService) Delete(ctx context.Context, id uint) error {
|
|
if err := s.repo.Delete(ctx, id); err != nil {
|
|
applogger.L().Errorf("Failed to delete twitter channel: %v", err)
|
|
return err
|
|
}
|
|
applogger.L().Infof("Twitter channel deleted (id=%d)", id)
|
|
return nil
|
|
}
|
|
|
|
// ListByAccount retrieves all Twitter channels for an account.
|
|
func (s *ChannelTwitterService) ListByAccount(ctx context.Context, accountID uint) ([]channelmodel.ChannelTwitter, error) {
|
|
return s.repo.ListByAccount(ctx, accountID)
|
|
}
|
|
|
|
// MarkReauthorizationRequired marks a Twitter channel as needing reauthorization.
|
|
func (s *ChannelTwitterService) MarkReauthorizationRequired(ctx context.Context, inboxID uint) error {
|
|
ch, err := s.repo.FindByInboxID(ctx, inboxID)
|
|
if err != nil {
|
|
return fmt.Errorf("twitter channel not found: %w", err)
|
|
}
|
|
|
|
ch.ReauthorizationRequired = true
|
|
if err := s.repo.Update(ctx, ch); err != nil {
|
|
applogger.L().Errorf("Failed to mark twitter channel for reauthorization: %v", err)
|
|
return err
|
|
}
|
|
|
|
applogger.L().Infof("Twitter channel marked for reauthorization (inbox_id=%d)", inboxID)
|
|
return nil
|
|
}
|