ручное парсер
This commit is contained in:
@@ -2,6 +2,7 @@ package usecase
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/TelegramExchange/pkg/transaction"
|
||||
@@ -16,21 +17,18 @@ func (uc *UseCase) FetchChannels(ctx context.Context) error {
|
||||
|
||||
for _, c := range channels {
|
||||
if c.Pts == 0 {
|
||||
updatedChannel, err := uc.initializeChannel(ctx, c)
|
||||
if err != nil {
|
||||
return fmt.Errorf("uc.initializeChannel: %w", err)
|
||||
}
|
||||
|
||||
c = updatedChannel
|
||||
}
|
||||
|
||||
if c.Pts == 0 {
|
||||
log.Warn().Int64("telegram_id", c.TelegramID).Msg("Channel pts still empty, skipping diff")
|
||||
go uc.initializeChannel(ctx, c)
|
||||
continue
|
||||
}
|
||||
|
||||
err := uc.processChannel(ctx, c)
|
||||
if err != nil {
|
||||
if isPrivateChannelError(err) {
|
||||
log.Warn().Int64("telegram_id", c.TelegramID).Msg("Private channel, attempting rejoin")
|
||||
go uc.rejoinChannel(ctx, c)
|
||||
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("uc.processChannel: %w", err)
|
||||
}
|
||||
}
|
||||
@@ -38,37 +36,13 @@ func (uc *UseCase) FetchChannels(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func isPrivateChannelError(err error) bool {
|
||||
return tgerr.Is(err, "CHANNEL_PRIVATE", "CHANNEL_INVALID", "CHANNEL_FORBIDDEN")
|
||||
}
|
||||
|
||||
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, marking inaccessible")
|
||||
channel.IsAccessible = false
|
||||
if err := uc.database.UpdateChannel(ctx, channel); err != nil {
|
||||
return fmt.Errorf("database.UpdateChannel (mark inaccessible): %w", err)
|
||||
}
|
||||
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, marking inaccessible")
|
||||
channel.IsAccessible = false
|
||||
if err := uc.database.UpdateChannel(ctx, channel); err != nil {
|
||||
return fmt.Errorf("database.UpdateChannel (mark inaccessible): %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
updatedChannel.ID = channel.ID
|
||||
updatedChannel.InviteLink = channel.InviteLink
|
||||
updatedChannel.IsAccessible = true
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -97,49 +71,90 @@ func (uc *UseCase) processChannel(ctx context.Context, channel domain.Channel) e
|
||||
if err != nil {
|
||||
return fmt.Errorf("database.UpdateChannel: %w", err)
|
||||
}
|
||||
} else {
|
||||
channel.Pts = diff.NewPts
|
||||
err = uc.database.UpdateChannel(ctx, channel)
|
||||
if err != nil {
|
||||
return fmt.Errorf("database.UpdateChannel: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
channel.Pts = diff.NewPts
|
||||
err = uc.database.UpdateChannel(ctx, channel)
|
||||
if err != nil {
|
||||
return fmt.Errorf("database.UpdateChannel: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func isPrivateChannelError(err error) bool {
|
||||
return tgerr.Is(err, "CHANNEL_PRIVATE", "CHANNEL_INVALID", "CHANNEL_FORBIDDEN")
|
||||
}
|
||||
func (uc *UseCase) rejoinChannel(ctx context.Context, channel domain.Channel) {
|
||||
defer func() {
|
||||
err := uc.database.UpdateChannel(ctx, channel)
|
||||
if err != nil {
|
||||
log.Err(err).Msg("database.UpdateChannel (rejoinChannel)")
|
||||
}
|
||||
}()
|
||||
|
||||
func (uc *UseCase) initializeChannel(ctx context.Context, channel domain.Channel) (domain.Channel, error) {
|
||||
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
|
||||
if channel.InviteLink == "" {
|
||||
log.Warn().Int64("telegram_id", channel.TelegramID).Msg("No invite link stored, marking inaccessible")
|
||||
channel.IsAccessible = false
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Preserve persistent channel ID from the database so foreign keys remain valid.
|
||||
c.ID = channel.ID
|
||||
|
||||
posts, err := uc.telegram.GetChannelHistory(ctx, c, 20)
|
||||
updated, err := uc.telegram.ParseChannelMetaByInvite(ctx, channel.InviteLink)
|
||||
if err != nil {
|
||||
return domain.Channel{}, fmt.Errorf("telegram.GetChannelHistory: %w", err)
|
||||
log.Warn().Err(err).Int64("telegram_id", channel.TelegramID).Msg("Invite rejoin failed, marking inaccessible")
|
||||
channel.IsAccessible = false
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
channel.TelegramID = updated.TelegramID
|
||||
channel.Title = updated.Title
|
||||
channel.AccessHash = 0
|
||||
channel.Pts = 0
|
||||
channel.IsAccessible = true
|
||||
}
|
||||
|
||||
func (uc *UseCase) initializeChannel(ctx context.Context, channel domain.Channel) {
|
||||
var (
|
||||
updated domain.Channel
|
||||
err error
|
||||
)
|
||||
|
||||
switch {
|
||||
case channel.Username != "":
|
||||
updated, err = uc.telegram.ParseChannelMeta(ctx, channel.Username)
|
||||
case channel.InviteLink != "":
|
||||
updated, err = uc.telegram.ParseChannelMetaByInvite(ctx, channel.InviteLink)
|
||||
default:
|
||||
err = errors.New("channel state invalid")
|
||||
}
|
||||
if err != nil {
|
||||
if isPrivateChannelError(err) {
|
||||
channel.IsAccessible = false
|
||||
|
||||
err = uc.database.UpdateChannel(ctx, channel)
|
||||
if err != nil {
|
||||
log.Err(err).Msg("initializeChannel.uc.database.UpdateChannel")
|
||||
}
|
||||
}
|
||||
|
||||
log.Error().Err(err).Msg("initializeChannel.ParseChannel")
|
||||
return
|
||||
}
|
||||
|
||||
channel.Title = updated.Title
|
||||
channel.AccessHash = updated.AccessHash
|
||||
channel.Pts = updated.Pts
|
||||
|
||||
const initPosts = 20
|
||||
posts, err := uc.telegram.GetChannelHistory(ctx, channel, initPosts)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("telegram.GetChannelHistory")
|
||||
return
|
||||
}
|
||||
|
||||
err = transaction.Wrap(ctx, func(ctx context.Context) error {
|
||||
err = uc.database.UpdateChannel(ctx, c)
|
||||
err = uc.database.UpdateChannel(ctx, channel)
|
||||
if err != nil {
|
||||
return fmt.Errorf("database.UpdateChannel: %w", err)
|
||||
}
|
||||
@@ -155,8 +170,6 @@ func (uc *UseCase) initializeChannel(ctx context.Context, channel domain.Channel
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return domain.Channel{}, fmt.Errorf("transaction.Wrap: %w", err)
|
||||
log.Error().Err(err).Msg("transaction.Wrap")
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user