fix парсер
This commit is contained in:
@@ -18,9 +18,11 @@ func (d *Database) GetChannels(ctx context.Context) []domain.Channel {
|
|||||||
title,
|
title,
|
||||||
access_hash,
|
access_hash,
|
||||||
pts,
|
pts,
|
||||||
invite_link
|
invite_link,
|
||||||
|
is_accessible
|
||||||
FROM channel
|
FROM channel
|
||||||
WHERE deleted_at IS NULL;`
|
WHERE deleted_at IS NULL
|
||||||
|
AND is_accessible = true;`
|
||||||
|
|
||||||
txOrPool := transaction.TryExtractTX(ctx)
|
txOrPool := transaction.TryExtractTX(ctx)
|
||||||
|
|
||||||
@@ -49,13 +51,14 @@ WHERE deleted_at IS NULL;`
|
|||||||
}
|
}
|
||||||
|
|
||||||
type getChannelsDTO struct {
|
type getChannelsDTO struct {
|
||||||
ID pgtype.UUID
|
ID pgtype.UUID
|
||||||
TelegramID pgtype.Int8
|
TelegramID pgtype.Int8
|
||||||
Username pgtype.Text
|
Username pgtype.Text
|
||||||
Title pgtype.Text
|
Title pgtype.Text
|
||||||
AccessHash pgtype.Int8
|
AccessHash pgtype.Int8
|
||||||
Pts pgtype.Int4
|
Pts pgtype.Int4
|
||||||
InviteLink pgtype.Text
|
InviteLink pgtype.Text
|
||||||
|
IsAccessible pgtype.Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dto *getChannelsDTO) destination() []any {
|
func (dto *getChannelsDTO) destination() []any {
|
||||||
@@ -67,17 +70,19 @@ func (dto *getChannelsDTO) destination() []any {
|
|||||||
&dto.AccessHash,
|
&dto.AccessHash,
|
||||||
&dto.Pts,
|
&dto.Pts,
|
||||||
&dto.InviteLink,
|
&dto.InviteLink,
|
||||||
|
&dto.IsAccessible,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dto *getChannelsDTO) toDomain() domain.Channel {
|
func (dto *getChannelsDTO) toDomain() domain.Channel {
|
||||||
return domain.Channel{
|
return domain.Channel{
|
||||||
ID: dto.ID.Bytes,
|
ID: dto.ID.Bytes,
|
||||||
TelegramID: domain.NormalizeChatID(dto.TelegramID.Int64),
|
TelegramID: domain.NormalizeChatID(dto.TelegramID.Int64),
|
||||||
Username: dto.Username.String,
|
Username: dto.Username.String,
|
||||||
Title: dto.Title.String,
|
Title: dto.Title.String,
|
||||||
AccessHash: dto.AccessHash.Int64,
|
AccessHash: dto.AccessHash.Int64,
|
||||||
Pts: int(dto.Pts.Int32),
|
Pts: int(dto.Pts.Int32),
|
||||||
InviteLink: dto.InviteLink.String,
|
InviteLink: dto.InviteLink.String,
|
||||||
|
IsAccessible: dto.IsAccessible.Bool,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,24 +18,31 @@ SET telegram_id = $2,
|
|||||||
title = $4,
|
title = $4,
|
||||||
access_hash = $5,
|
access_hash = $5,
|
||||||
pts = $6,
|
pts = $6,
|
||||||
|
is_accessible = $7,
|
||||||
updated_at = CURRENT_TIMESTAMP
|
updated_at = CURRENT_TIMESTAMP
|
||||||
WHERE id = $1;`
|
WHERE id = $1;`
|
||||||
|
|
||||||
txOrPool := transaction.TryExtractTX(ctx)
|
txOrPool := transaction.TryExtractTX(ctx)
|
||||||
|
|
||||||
username := strings.TrimSpace(channel.Username)
|
username := strings.TrimSpace(channel.Username)
|
||||||
usernameValid := username != ""
|
var usernameDTO pgtype.Text
|
||||||
|
if username != "" {
|
||||||
dto := updateChannelDTO{
|
usernameDTO = pgtype.Text{String: username, Valid: true}
|
||||||
ID: pgtype.UUID{Bytes: channel.ID, Valid: true},
|
} else {
|
||||||
TelegramID: pgtype.Int8{Int64: channel.TelegramID, Valid: true},
|
usernameDTO = pgtype.Text{Valid: false}
|
||||||
Username: pgtype.Text{String: username, Valid: usernameValid},
|
|
||||||
Title: pgtype.Text{String: channel.Title, Valid: true},
|
|
||||||
AccessHash: pgtype.Int8{Int64: channel.AccessHash, Valid: true},
|
|
||||||
Pts: pgtype.Int4{Int32: int32(channel.Pts), Valid: true},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := txOrPool.Exec(ctx, query, dto.ID, dto.TelegramID, dto.Username, dto.Title, dto.AccessHash, dto.Pts)
|
dto := updateChannelDTO{
|
||||||
|
ID: pgtype.UUID{Bytes: channel.ID, Valid: true},
|
||||||
|
TelegramID: pgtype.Int8{Int64: channel.TelegramID, Valid: true},
|
||||||
|
Username: usernameDTO,
|
||||||
|
Title: pgtype.Text{String: channel.Title, Valid: true},
|
||||||
|
AccessHash: pgtype.Int8{Int64: channel.AccessHash, Valid: true},
|
||||||
|
Pts: pgtype.Int4{Int32: int32(channel.Pts), Valid: true},
|
||||||
|
IsAccessible: pgtype.Bool{Bool: channel.IsAccessible, Valid: true},
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := txOrPool.Exec(ctx, query, dto.ID, dto.TelegramID, dto.Username, dto.Title, dto.AccessHash, dto.Pts, dto.IsAccessible)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("txOrPool.Exec: %w", err)
|
return fmt.Errorf("txOrPool.Exec: %w", err)
|
||||||
}
|
}
|
||||||
@@ -44,10 +51,11 @@ WHERE id = $1;`
|
|||||||
}
|
}
|
||||||
|
|
||||||
type updateChannelDTO struct {
|
type updateChannelDTO struct {
|
||||||
ID pgtype.UUID
|
ID pgtype.UUID
|
||||||
TelegramID pgtype.Int8
|
TelegramID pgtype.Int8
|
||||||
Username pgtype.Text
|
Username pgtype.Text
|
||||||
Title pgtype.Text
|
Title pgtype.Text
|
||||||
AccessHash pgtype.Int8
|
AccessHash pgtype.Int8
|
||||||
Pts pgtype.Int4
|
Pts pgtype.Int4
|
||||||
|
IsAccessible pgtype.Bool
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ func extractChannelMeta(currentChannel domain.Channel, chats []tg.ChatClass) *do
|
|||||||
}
|
}
|
||||||
|
|
||||||
username := ""
|
username := ""
|
||||||
if usernameVal, ok := ch.GetUsername(); ok {
|
if usernameVal, ok := ch.GetUsername(); ok && usernameVal != "" {
|
||||||
username = usernameVal
|
username = usernameVal
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,12 +107,13 @@ func extractChannelMeta(currentChannel domain.Channel, chats []tg.ChatClass) *do
|
|||||||
}
|
}
|
||||||
|
|
||||||
updated := domain.Channel{
|
updated := domain.Channel{
|
||||||
ID: currentChannel.ID,
|
ID: currentChannel.ID,
|
||||||
TelegramID: domain.ChatIDFromChannelID(ch.ID),
|
TelegramID: domain.ChatIDFromChannelID(ch.ID),
|
||||||
Username: username,
|
Username: username,
|
||||||
Title: ch.Title,
|
Title: ch.Title,
|
||||||
AccessHash: accessHash,
|
AccessHash: accessHash,
|
||||||
Pts: currentChannel.Pts,
|
Pts: currentChannel.Pts,
|
||||||
|
IsAccessible: currentChannel.IsAccessible,
|
||||||
}
|
}
|
||||||
|
|
||||||
return &updated
|
return &updated
|
||||||
|
|||||||
@@ -49,11 +49,12 @@ func (t *Telegram) ParseChannelMeta(ctx context.Context, username string) (domai
|
|||||||
}
|
}
|
||||||
|
|
||||||
return domain.Channel{
|
return domain.Channel{
|
||||||
ID: uuid.New(),
|
ID: uuid.New(),
|
||||||
TelegramID: domain.ChatIDFromChannelID(ch.ID),
|
TelegramID: domain.ChatIDFromChannelID(ch.ID),
|
||||||
Username: username,
|
Username: username,
|
||||||
Title: ch.Title,
|
Title: ch.Title,
|
||||||
AccessHash: ch.AccessHash,
|
AccessHash: ch.AccessHash,
|
||||||
Pts: pts,
|
Pts: pts,
|
||||||
|
IsAccessible: true,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,17 +56,18 @@ func (t *Telegram) ParseChannelMetaByInvite(ctx context.Context, inviteLink stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
username := ""
|
username := ""
|
||||||
if usernameVal, ok := channel.GetUsername(); ok {
|
if usernameVal, ok := channel.GetUsername(); ok && usernameVal != "" {
|
||||||
username = usernameVal
|
username = usernameVal
|
||||||
}
|
}
|
||||||
|
|
||||||
return domain.Channel{
|
return domain.Channel{
|
||||||
ID: uuid.New(),
|
ID: uuid.New(),
|
||||||
TelegramID: domain.ChatIDFromChannelID(channel.ID),
|
TelegramID: domain.ChatIDFromChannelID(channel.ID),
|
||||||
Username: username,
|
Username: username,
|
||||||
Title: channel.Title,
|
Title: channel.Title,
|
||||||
AccessHash: channel.AccessHash,
|
AccessHash: channel.AccessHash,
|
||||||
Pts: pts,
|
Pts: pts,
|
||||||
|
IsAccessible: true,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,13 +7,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Channel struct {
|
type Channel struct {
|
||||||
ID uuid.UUID
|
ID uuid.UUID
|
||||||
TelegramID int64
|
TelegramID int64
|
||||||
Username string
|
Username string
|
||||||
Title string
|
Title string
|
||||||
AccessHash int64
|
AccessHash int64
|
||||||
Pts int
|
Pts int
|
||||||
InviteLink string
|
InviteLink string
|
||||||
|
IsAccessible bool
|
||||||
}
|
}
|
||||||
|
|
||||||
const channelIDOffset int64 = 1000000000000
|
const channelIDOffset int64 = 1000000000000
|
||||||
|
|||||||
@@ -44,16 +44,25 @@ func (uc *UseCase) processChannel(ctx context.Context, channel domain.Channel) e
|
|||||||
if isPrivateChannelError(err) {
|
if isPrivateChannelError(err) {
|
||||||
log.Warn().Err(err).Int64("telegram_id", channel.TelegramID).Msg("Private channel, attempting rejoin")
|
log.Warn().Err(err).Int64("telegram_id", channel.TelegramID).Msg("Private channel, attempting rejoin")
|
||||||
if channel.InviteLink == "" {
|
if channel.InviteLink == "" {
|
||||||
log.Warn().Int64("telegram_id", channel.TelegramID).Msg("No invite link stored, skipping channel")
|
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
|
return nil
|
||||||
}
|
}
|
||||||
updatedChannel, joinErr := uc.telegram.ParseChannelMetaByInvite(ctx, channel.InviteLink)
|
updatedChannel, joinErr := uc.telegram.ParseChannelMetaByInvite(ctx, channel.InviteLink)
|
||||||
if joinErr != nil {
|
if joinErr != nil {
|
||||||
log.Warn().Err(joinErr).Int64("telegram_id", channel.TelegramID).Msg("Invite rejoin failed")
|
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
|
return nil
|
||||||
}
|
}
|
||||||
updatedChannel.ID = channel.ID
|
updatedChannel.ID = channel.ID
|
||||||
updatedChannel.InviteLink = channel.InviteLink
|
updatedChannel.InviteLink = channel.InviteLink
|
||||||
|
updatedChannel.IsAccessible = true
|
||||||
updatedChannel.Pts = 0
|
updatedChannel.Pts = 0
|
||||||
if err := uc.database.UpdateChannel(ctx, updatedChannel); err != nil {
|
if err := uc.database.UpdateChannel(ctx, updatedChannel); err != nil {
|
||||||
return fmt.Errorf("database.UpdateChannel: %w", err)
|
return fmt.Errorf("database.UpdateChannel: %w", err)
|
||||||
|
|||||||
Reference in New Issue
Block a user