Skip to content

Commit 0a04bc0

Browse files
committed
fix item tracking & better cleanup
1 parent 408bea5 commit 0a04bc0

File tree

9 files changed

+95
-42
lines changed

9 files changed

+95
-42
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ RUN go mod verify
1111

1212
COPY . .
1313

14-
RUN go build
14+
ARG VERSION
15+
RUN go build -ldflags "-X github.com/robherley/gw-bot/internal/meta.Version=${VERSION}"
1516

1617
FROM alpine
1718

database/queries/items.sql

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ INSERT INTO items (id, subscription_id, goodwill_id, created_at, started_at, end
33
VALUES (?, ?, ?, CURRENT_TIMESTAMP, ?, ?)
44
RETURNING *;
55

6-
-- name: FindItemInSubscription :one
7-
SELECT * FROM items
8-
WHERE subscription_id = ? AND goodwill_id = ?;
6+
-- name: IsItemTracked :one
7+
SELECT EXISTS (
8+
SELECT 1
9+
FROM items
10+
WHERE subscription_id = ? AND goodwill_id = ?
11+
) AS is_tracked;
912

1013
-- name: FindItemsEndingSoon :many
1114
SELECT * FROM items
@@ -22,3 +25,7 @@ DELETE FROM items
2225
WHERE ends_at < datetime('now', '-1 day')
2326
LIMIT 1000
2427
RETURNING COUNT(*);
28+
29+
-- name: DeleteItemsInSubscriptions :exec
30+
DELETE FROM items
31+
WHERE subscription_id IN (sqlc.slice('ids'));

internal/bot/bot.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,8 @@ func (b *Bot) NotifyEndingSoonItems(sub sqlgen.Subscription, items []sqlgen.Item
233233
}
234234

235235
func (b *Bot) ItemToEmbed(item gw.Item) *discordgo.MessageEmbed {
236-
return &discordgo.MessageEmbed{
236+
embed := &discordgo.MessageEmbed{
237237
Title: item.Title,
238-
Image: &discordgo.MessageEmbedImage{
239-
URL: item.ImageURL,
240-
},
241238
Fields: []*discordgo.MessageEmbedField{
242239
{
243240
Name: "Current Price",
@@ -267,6 +264,20 @@ func (b *Bot) ItemToEmbed(item gw.Item) *discordgo.MessageEmbed {
267264
},
268265
URL: item.URL(),
269266
}
267+
268+
if item.ImageURL != "" {
269+
embed.Image = &discordgo.MessageEmbedImage{
270+
URL: item.ImageURL,
271+
}
272+
}
273+
274+
slog.Info("embedding item",
275+
"item_id", item.ItemID,
276+
"url", item.URL(),
277+
"image_url", item.ImageURL,
278+
)
279+
280+
return embed
270281
}
271282

272283
func Chunk[T any](slice []T, chunkSize int) [][]T {

internal/bot/cmd/ping.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"os"
87

98
"github.com/bwmarrin/discordgo"
9+
"github.com/robherley/gw-bot/internal/meta"
1010
)
1111

1212
func NewPing() Handler {
@@ -35,17 +35,12 @@ func (cmd *ping) Handle(ctx context.Context, s *discordgo.Session, i *discordgo.
3535
user = i.Member.User.String()
3636
}
3737

38-
version := "unknown"
39-
if v, ok := os.LookupEnv("VERSION"); ok {
40-
version = v
41-
}
42-
4338
payload := map[string]interface{}{
4439
"user": user,
4540
"guild_id": i.GuildID,
4641
"dm": i.GuildID == "",
4742
"channel_id": i.ChannelID,
48-
"version": version,
43+
"version": meta.Version,
4944
}
5045

5146
bytes, err := json.MarshalIndent(payload, "", " ")

internal/bot/cmd/unsubscribe.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ func (cmd *Unsubscribe) Handle(ctx context.Context, s *discordgo.Session, i *dis
8989
return err
9090
}
9191

92+
if err := cmd.db.DeleteItemsInSubscriptions(ctx, subIDs); err != nil {
93+
return err
94+
}
95+
9296
if err := cmd.db.DeleteUserSubscriptions(ctx, sqlgen.DeleteUserSubscriptionsParams{
9397
UserID: userID,
9498
Ids: subIDs,

internal/db/sqlgen/items.sql.go

Lines changed: 36 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/db/sqlgen/querier.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/looper/looper.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,28 +51,44 @@ func (l *Looper) NotifyNewItems(ctx context.Context) {
5151

5252
log := log.With("subscription_id", sub.ID, "user_id", sub.UserID)
5353

54-
items, err := l.gw.Search(ctx, sub.Term, opts...)
54+
foundItems, err := l.gw.Search(ctx, sub.Term, opts...)
5555
if err != nil {
5656
log.Error("failed to search for items", "error", err)
5757
continue
5858
}
5959

60-
if len(items) == 0 {
60+
newItems := make([]gw.Item, 0, len(foundItems))
61+
for _, item := range foundItems {
62+
tracked, err := l.db.IsItemTracked(ctx, sqlgen.IsItemTrackedParams{
63+
SubscriptionID: sub.ID,
64+
GoodwillID: item.ItemID,
65+
})
66+
if err != nil {
67+
log.Error("failed to check if item is tracked", "error", err)
68+
continue
69+
}
70+
71+
if tracked != 1 {
72+
newItems = append(newItems, item)
73+
}
74+
}
75+
76+
if len(newItems) == 0 {
6177
log.Info("no new items found")
6278
continue
6379
}
6480

65-
log.Info("new items found", "count", len(items))
81+
log.Info("new items found", "count", len(newItems))
6682

67-
for _, item := range items {
83+
for _, item := range newItems {
6884
_, err := l.db.CreateItem(ctx, item.NewCreateItemParams(sub))
6985
if err != nil {
7086
log.Error("failed to create item", "error", err)
7187
continue
7288
}
7389
}
7490

75-
if err := l.bot.NotifyNewItems(sub, items); err != nil {
91+
if err := l.bot.NotifyNewItems(sub, newItems); err != nil {
7692
log.Error("failed to notify new items", "error", err)
7793
}
7894

internal/meta/version.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package meta
2+
3+
var Version = "unknown"

0 commit comments

Comments
 (0)