приватные каналы

This commit is contained in:
Artem Tsyrulnikov
2026-01-21 02:41:20 +03:00
parent 233afb5451
commit b5695360c2
27 changed files with 1026 additions and 217 deletions

View File

@@ -0,0 +1,11 @@
package usecase
import (
"context"
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/domain"
)
func (uc *UseCase) FetchChannelMetaByInvite(ctx context.Context, inviteLink string) (domain.Channel, error) {
return uc.telegram.ParseChannelMetaByInvite(ctx, inviteLink)
}

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"github.com/TelegramExchange/pkg/transaction"
"github.com/gotd/td/tgerr"
"github.com/rs/zerolog/log"
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/domain"
@@ -23,6 +24,11 @@ func (uc *UseCase) FetchChannels(ctx context.Context) error {
c = updatedChannel
}
if c.Pts == 0 {
log.Warn().Int64("telegram_id", c.TelegramID).Msg("Channel pts still empty, skipping diff")
continue
}
err := uc.processChannel(ctx, c)
if err != nil {
return fmt.Errorf("uc.processChannel: %w", err)
@@ -35,6 +41,25 @@ func (uc *UseCase) FetchChannels(ctx context.Context) error {
func (uc *UseCase) processChannel(ctx context.Context, channel domain.Channel) error {
diff, err := uc.telegram.GetChannelDiff(ctx, channel, 20)
if err != nil {
if isPrivateChannelError(err) {
log.Warn().Err(err).Int64("telegram_id", channel.TelegramID).Msg("Private channel, attempting rejoin")
if channel.InviteLink == "" {
log.Warn().Int64("telegram_id", channel.TelegramID).Msg("No invite link stored, skipping channel")
return nil
}
updatedChannel, joinErr := uc.telegram.ParseChannelMetaByInvite(ctx, channel.InviteLink)
if joinErr != nil {
log.Warn().Err(joinErr).Int64("telegram_id", channel.TelegramID).Msg("Invite rejoin failed")
return nil
}
updatedChannel.ID = channel.ID
updatedChannel.InviteLink = channel.InviteLink
updatedChannel.Pts = 0
if err := uc.database.UpdateChannel(ctx, updatedChannel); err != nil {
return fmt.Errorf("database.UpdateChannel: %w", err)
}
return nil
}
return fmt.Errorf("telegram.GetChannelDiff: %w", err)
}
@@ -74,10 +99,26 @@ func (uc *UseCase) processChannel(ctx context.Context, channel domain.Channel) e
return nil
}
func isPrivateChannelError(err error) bool {
return tgerr.Is(err, "CHANNEL_PRIVATE", "CHANNEL_INVALID", "CHANNEL_FORBIDDEN")
}
func (uc *UseCase) initializeChannel(ctx context.Context, channel domain.Channel) (domain.Channel, error) {
c, err := uc.telegram.ParseChannelMeta(ctx, channel.Username)
if err != nil {
return domain.Channel{}, fmt.Errorf("telegram.ParseChannelMeta: %w", err)
c := channel
if channel.Username != "" {
updated, err := uc.telegram.ParseChannelMeta(ctx, channel.Username)
if err != nil {
return domain.Channel{}, fmt.Errorf("telegram.ParseChannelMeta: %w", err)
}
c = updated
} else if channel.AccessHash == 0 {
return domain.Channel{}, fmt.Errorf("channel %s missing access hash", channel.ID)
} else {
pts, err := uc.telegram.GetChannelPTS(ctx, channel)
if err != nil {
return domain.Channel{}, fmt.Errorf("telegram.GetChannelPTS: %w", err)
}
c.Pts = pts
}
// Preserve persistent channel ID from the database so foreign keys remain valid.

View File

@@ -8,6 +8,8 @@ import (
type Telegram interface {
ParseChannelMeta(ctx context.Context, username string) (domain.Channel, error)
ParseChannelMetaByInvite(ctx context.Context, inviteLink string) (domain.Channel, error)
GetChannelPTS(ctx context.Context, channel domain.Channel) (int, error)
GetChannelHistory(ctx context.Context, channel domain.Channel, limit int) ([]domain.Post, error)
GetChannelDiff(ctx context.Context, channel domain.Channel, limit int) (domain.ChannelDiff, error)
UpdatePostsViews(ctx context.Context, channel domain.Channel, posts []domain.Post) error