diff --git a/cmd/eksterd/memory.go b/cmd/eksterd/memory.go index 480c484..21b67ee 100644 --- a/cmd/eksterd/memory.go +++ b/cmd/eksterd/memory.go @@ -312,23 +312,23 @@ func (b *memoryBackend) FollowURL(uid string, url string) (microsub.Feed, error) feed := microsub.Feed{Type: "feed", URL: url} var channelID int - if row := b.database.QueryRow(`SELECT "id" FROM "channels" WHERE "uid" = $1`, uid); row != nil { - err := row.Scan(&channelID) - if err != nil { - log.Fatal(err) + err := b.database.QueryRow(`SELECT "id" FROM "channels" WHERE "uid" = $1`, uid).Scan(&channelID) + if err != nil { + if err == sql.ErrNoRows { + return microsub.Feed{}, fmt.Errorf("channel does not exist: %w", err) } + return microsub.Feed{}, err } - row := b.database.QueryRow( + var feedID int + err = b.database.QueryRow( `INSERT INTO "feeds" ("channel_id", "url") VALUES ($1, $2) RETURNING "id"`, channelID, feed.URL, - ) - if row == nil { - return microsub.Feed{}, fmt.Errorf("no feed_id") + ).Scan(&feedID) + if err != nil { + return feed, err } - var feedID int - _ = row.Scan(&feedID) resp, err := b.Fetch3(uid, feed.URL) if err != nil { diff --git a/go.mod b/go.mod index 51316e4..3aa331e 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.16 require ( github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 github.com/blevesearch/bleve/v2 v2.0.3 + github.com/davecgh/go-spew v1.1.1 // indirect github.com/gilliek/go-opml v1.0.0 github.com/golang-migrate/migrate/v4 v4.15.1 github.com/gomodule/redigo v1.8.2 diff --git a/pkg/timeline/postgres.go b/pkg/timeline/postgres.go index 2329581..367a688 100644 --- a/pkg/timeline/postgres.go +++ b/pkg/timeline/postgres.go @@ -212,20 +212,31 @@ func (p *postgresStream) AddItem(item microsub.Item) (bool, error) { t = t2 } if item.UID == "" { + // FIXME: This won't work when we receive the item multiple times h := sha256.Sum256([]byte(fmt.Sprintf("%s:%d", p.channel, time.Now().UnixNano()))) item.UID = hex.EncodeToString(h[:]) } - feedID, err := strconv.ParseInt(item.Source.ID, 10, 64) - if err != nil { - return false, fmt.Errorf("ERROR: item.Source.ID is not an integer %q: %w", item.Source.ID, err) + var optFeedID sql.NullInt64 + if item.Source == nil || item.Source.ID == "" { + optFeedID.Valid = false + optFeedID.Int64 = 0 + } else { + feedID, err := strconv.ParseInt(item.Source.ID, 10, 64) + if err != nil { + optFeedID.Valid = false + optFeedID.Int64 = 0 + } else { + optFeedID.Valid = true + optFeedID.Int64 = feedID + } } result, err := conn.ExecContext(context.Background(), ` INSERT INTO "items" ("channel_id", "feed_id", "uid", "data", "published_at", "created_at") VALUES ($1, $2, $3, $4, $5, DEFAULT) ON CONFLICT ON CONSTRAINT "items_uid_key" DO NOTHING -`, p.channelID, feedID, item.UID, &item, t) +`, p.channelID, optFeedID, item.UID, &item, t) if err != nil { return false, fmt.Errorf("insert item: %w", err) }