время в топе и тэги креативов
This commit is contained in:
@@ -11,22 +11,24 @@ import (
|
||||
)
|
||||
|
||||
func (d *Database) CreatePost(ctx context.Context, post domain.Post) error {
|
||||
query := `INSERT INTO post (id, channel_id, message_id, text)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
query := `INSERT INTO post (id, channel_id, message_id, text, published_at)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
ON CONFLICT (channel_id, message_id) DO UPDATE
|
||||
SET text = EXCLUDED.text,
|
||||
published_at = COALESCE(post.published_at, EXCLUDED.published_at),
|
||||
updated_at = CURRENT_TIMESTAMP;`
|
||||
|
||||
txOrPool := transaction.TryExtractTX(ctx)
|
||||
|
||||
dto := createPostDTO{
|
||||
ID: pgtype.UUID{Bytes: post.ID, Valid: true},
|
||||
ChannelID: pgtype.UUID{Bytes: post.ChannelID, Valid: true},
|
||||
MessageID: pgtype.Int4{Int32: int32(post.MessageID), Valid: true},
|
||||
Text: pgtype.Text{String: post.Text, Valid: true},
|
||||
ID: pgtype.UUID{Bytes: post.ID, Valid: true},
|
||||
ChannelID: pgtype.UUID{Bytes: post.ChannelID, Valid: true},
|
||||
MessageID: pgtype.Int4{Int32: int32(post.MessageID), Valid: true},
|
||||
Text: pgtype.Text{String: post.Text, Valid: true},
|
||||
PublishedAt: pgtype.Timestamptz{Time: post.PublishedAt, Valid: !post.PublishedAt.IsZero()},
|
||||
}
|
||||
|
||||
_, err := txOrPool.Exec(ctx, query, dto.ID, dto.ChannelID, dto.MessageID, dto.Text)
|
||||
_, err := txOrPool.Exec(ctx, query, dto.ID, dto.ChannelID, dto.MessageID, dto.Text, dto.PublishedAt)
|
||||
if err != nil {
|
||||
return fmt.Errorf("txOrPool.Exec: %w", err)
|
||||
}
|
||||
@@ -35,8 +37,9 @@ SET text = EXCLUDED.text,
|
||||
}
|
||||
|
||||
type createPostDTO struct {
|
||||
ID pgtype.UUID
|
||||
ChannelID pgtype.UUID
|
||||
MessageID pgtype.Int4
|
||||
Text pgtype.Text
|
||||
ID pgtype.UUID
|
||||
ChannelID pgtype.UUID
|
||||
MessageID pgtype.Int4
|
||||
Text pgtype.Text
|
||||
PublishedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package telegram
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/domain"
|
||||
"github.com/gotd/td/tg"
|
||||
@@ -60,7 +61,8 @@ func extractPosts(channel domain.Channel, msgs []tg.MessageClass) []domain.Post
|
||||
}
|
||||
|
||||
text := messageToHTML(m.Message, m.Entities)
|
||||
p := domain.NewPost(channel, m.ID, text, m.Views)
|
||||
publishedAt := time.Unix(int64(m.Date), 0).UTC()
|
||||
p := domain.NewPost(channel, m.ID, text, m.Views, publishedAt)
|
||||
posts = append(posts, p)
|
||||
}
|
||||
|
||||
@@ -73,7 +75,7 @@ func extractDeletedPosts(channel domain.Channel, updates []tg.UpdateClass) []dom
|
||||
for _, upd := range updates {
|
||||
if u, ok := upd.(*tg.UpdateDeleteChannelMessages); ok {
|
||||
for _, msgID := range u.Messages {
|
||||
p := domain.NewPost(channel, msgID, "", 0)
|
||||
p := domain.NewPost(channel, msgID, "", 0, time.Time{})
|
||||
deleted = append(deleted, p)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package telegram
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/domain"
|
||||
"github.com/gotd/td/tg"
|
||||
@@ -39,7 +40,8 @@ func (t *Telegram) GetChannelHistory(ctx context.Context, channel domain.Channel
|
||||
}
|
||||
|
||||
text := messageToHTML(m.Message, m.Entities)
|
||||
p := domain.NewPost(channel, m.ID, text, m.Views)
|
||||
publishedAt := time.Unix(int64(m.Date), 0).UTC()
|
||||
p := domain.NewPost(channel, m.ID, text, m.Views, publishedAt)
|
||||
placements = append(placements, p)
|
||||
}
|
||||
|
||||
|
||||
@@ -2,20 +2,22 @@ package domain
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Post struct {
|
||||
ID uuid.UUID
|
||||
ChannelID uuid.UUID
|
||||
MessageID int
|
||||
Text string
|
||||
Link string
|
||||
Views int
|
||||
ID uuid.UUID
|
||||
ChannelID uuid.UUID
|
||||
MessageID int
|
||||
Text string
|
||||
Link string
|
||||
Views int
|
||||
PublishedAt time.Time
|
||||
}
|
||||
|
||||
func NewPost(channel Channel, messageID int, text string, views int) Post {
|
||||
func NewPost(channel Channel, messageID int, text string, views int, publishedAt time.Time) Post {
|
||||
link := ""
|
||||
if channel.Username != "" {
|
||||
link = fmt.Sprintf("https://t.me/%s/%d", channel.Username, messageID)
|
||||
@@ -23,11 +25,12 @@ func NewPost(channel Channel, messageID int, text string, views int) Post {
|
||||
link = fmt.Sprintf("https://t.me/c/%d/%d", ChannelIDFromChatID(channel.TelegramID), messageID)
|
||||
}
|
||||
return Post{
|
||||
ID: uuid.New(),
|
||||
ChannelID: channel.ID,
|
||||
MessageID: messageID,
|
||||
Text: text,
|
||||
Link: link,
|
||||
Views: views,
|
||||
ID: uuid.New(),
|
||||
ChannelID: channel.ID,
|
||||
MessageID: messageID,
|
||||
Text: text,
|
||||
Link: link,
|
||||
Views: views,
|
||||
PublishedAt: publishedAt,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user