Tighten up no-worsening of spaces, add saving ShortUrl when scraping from feed

This commit is contained in:
Alessio 2022-12-23 00:00:13 -05:00
parent 1a9a2cb745
commit dac10fa13c
3 changed files with 12 additions and 2 deletions

View File

@ -25,12 +25,12 @@ func (p Profile) SaveSpace(s scraper.Space) error {
set id=:id,
created_by_id=case when created_by_id is not null then created_by_id else nullif(:created_by_id, 0) end,
short_url=case when short_url == "" then :short_url else short_url end,
state=:state,
state=case when :state != "" then :state else state end,
title=case when :is_details_fetched then :title else title end,
updated_at=max(:updated_at, updated_at),
is_available_for_replay=:is_available_for_replay,
replay_watch_count=:replay_watch_count,
live_listeners_count=:live_listeners_count,
live_listeners_count=max(:live_listeners_count, live_listeners_count),
is_details_fetched=(is_details_fetched or :is_details_fetched)
`, &s)
if err != nil {

View File

@ -38,10 +38,12 @@ func TestNoWorseningSpace(t *testing.T) {
space := create_space_from_id(rand.Int())
space.ShortUrl = "Some Short Url"
space.State = "Some State"
space.Title = "Debating Somebody"
space.CreatedAt = scraper.TimestampFromUnix(1000)
space.UpdatedAt = scraper.TimestampFromUnix(2000)
space.CreatedById = scraper.UserID(-1)
space.LiveListenersCount = 100
space.IsDetailsFetched = true
// Save the space
@ -51,9 +53,11 @@ func TestNoWorseningSpace(t *testing.T) {
// Worsen the space, then re-save
space.ShortUrl = ""
space.Title = ""
space.State = ""
space.CreatedAt = scraper.TimestampFromUnix(0)
space.UpdatedAt = scraper.TimestampFromUnix(0)
space.CreatedById = scraper.UserID(0)
space.LiveListenersCount = 0
space.IsDetailsFetched = false
err = profile.SaveSpace(space)
require.NoError(err)
@ -63,9 +67,11 @@ func TestNoWorseningSpace(t *testing.T) {
require.NoError(err)
assert.Equal(new_space.ShortUrl, "Some Short Url")
assert.Equal(new_space.State, "Some State")
assert.Equal(new_space.Title, "Debating Somebody")
assert.Equal(new_space.CreatedAt, scraper.TimestampFromUnix(1000))
assert.Equal(new_space.UpdatedAt, scraper.TimestampFromUnix(2000))
assert.Equal(new_space.CreatedById, scraper.UserID(-1))
assert.Equal(new_space.LiveListenersCount, int64(100))
assert.True(new_space.IsDetailsFetched)
}

View File

@ -130,6 +130,10 @@ func (trove *TweetTrove) FillSpaceDetails() error {
}
// Replace the old space in the trove with the new, updated one
new_space, is_ok := new_trove.Spaces[i]
if new_space.ShortUrl == "" {
// Copy over the short-url, which doesn't seem to exist on a full Space response
new_space.ShortUrl = trove.Spaces[i].ShortUrl
}
if is_ok {
// Necessary to check is_ok because the space response could be empty, in which case
// we don't want to overwrite it