Add persistence for is_banned field

This commit is contained in:
Alessio 2022-01-07 12:38:56 -05:00
parent 1ea97e669a
commit fb174df363
4 changed files with 99 additions and 20 deletions

View File

@ -12,6 +12,7 @@ create table users (rowid integer primary key,
join_date integer,
is_private boolean default 0,
is_verified boolean default 0,
is_banned boolean default 0,
profile_image_url text,
profile_image_local_path text,
banner_image_url text,

View File

@ -17,16 +17,18 @@ func (p Profile) SaveUser(u scraper.User) error {
db := p.DB
_, err := db.Exec(`
insert into users (id, display_name, handle, bio, following_count, followers_count, location, website, join_date, is_private, is_verified, profile_image_url, profile_image_local_path, banner_image_url, banner_image_local_path, pinned_tweet_id, is_content_downloaded)
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
insert into users (id, display_name, handle, bio, following_count, followers_count, location, website, join_date, is_private, is_verified, is_banned, profile_image_url, profile_image_local_path, banner_image_url, banner_image_local_path, pinned_tweet_id, is_content_downloaded)
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
on conflict do update
set bio=?,
display_name=?,
following_count=?,
followers_count=?,
location=?,
website=?,
is_private=?,
is_verified=?,
is_banned=?,
profile_image_url=?,
profile_image_local_path=?,
banner_image_url=?,
@ -34,8 +36,8 @@ func (p Profile) SaveUser(u scraper.User) error {
pinned_tweet_id=?,
is_content_downloaded=(is_content_downloaded or ?)
`,
u.ID, u.DisplayName, u.Handle, u.Bio, u.FollowingCount, u.FollowersCount, u.Location, u.Website, u.JoinDate.Unix(), u.IsPrivate, u.IsVerified, u.ProfileImageUrl, u.ProfileImageLocalPath, u.BannerImageUrl, u.BannerImageLocalPath, u.PinnedTweetID, u.IsContentDownloaded,
u.Bio, u.FollowingCount, u.FollowersCount, u.Location, u.Website, u.IsPrivate, u.IsVerified, u.ProfileImageUrl, u.ProfileImageLocalPath, u.BannerImageUrl, u.BannerImageLocalPath, u.PinnedTweetID, u.IsContentDownloaded,
u.ID, u.DisplayName, u.Handle, u.Bio, u.FollowingCount, u.FollowersCount, u.Location, u.Website, u.JoinDate.Unix(), u.IsPrivate, u.IsVerified, u.IsBanned, u.ProfileImageUrl, u.ProfileImageLocalPath, u.BannerImageUrl, u.BannerImageLocalPath, u.PinnedTweetID, u.IsContentDownloaded,
u.Bio, u.DisplayName, u.FollowingCount, u.FollowersCount, u.Location, u.Website, u.IsPrivate, u.IsVerified, u.IsBanned, u.ProfileImageUrl, u.ProfileImageLocalPath, u.BannerImageUrl, u.BannerImageLocalPath, u.PinnedTweetID, u.IsContentDownloaded,
)
if err != nil {
return err
@ -76,7 +78,7 @@ func parse_user_from_row(row *sql.Row) (scraper.User, error) {
var u scraper.User
var joinDate int64
err := row.Scan(&u.ID, &u.DisplayName, &u.Handle, &u.Bio, &u.FollowingCount, &u.FollowersCount, &u.Location, &u.Website, &joinDate, &u.IsPrivate, &u.IsVerified, &u.ProfileImageUrl, &u.ProfileImageLocalPath, &u.BannerImageUrl, &u.BannerImageLocalPath, &u.PinnedTweetID, &u.IsContentDownloaded)
err := row.Scan(&u.ID, &u.DisplayName, &u.Handle, &u.Bio, &u.FollowingCount, &u.FollowersCount, &u.Location, &u.Website, &joinDate, &u.IsPrivate, &u.IsVerified, &u.IsBanned, &u.ProfileImageUrl, &u.ProfileImageLocalPath, &u.BannerImageUrl, &u.BannerImageLocalPath, &u.PinnedTweetID, &u.IsContentDownloaded)
if err != nil {
return u, err
}
@ -99,7 +101,7 @@ func (p Profile) GetUserByHandle(handle scraper.UserHandle) (scraper.User, error
db := p.DB
stmt, err := db.Prepare(`
select id, display_name, handle, bio, following_count, followers_count, location, website, join_date, is_private, is_verified, profile_image_url, profile_image_local_path, banner_image_url, banner_image_local_path, pinned_tweet_id, is_content_downloaded
select id, display_name, handle, bio, following_count, followers_count, location, website, join_date, is_private, is_verified, is_banned, profile_image_url, profile_image_local_path, banner_image_url, banner_image_local_path, pinned_tweet_id, is_content_downloaded
from users
where lower(handle) = lower(?)
`)
@ -130,7 +132,7 @@ func (p Profile) GetUserByID(id scraper.UserID) (scraper.User, error) {
db := p.DB
stmt, err := db.Prepare(`
select id, display_name, handle, bio, following_count, followers_count, location, website, join_date, is_private, is_verified, profile_image_url, profile_image_local_path, banner_image_url, banner_image_local_path, pinned_tweet_id, is_content_downloaded
select id, display_name, handle, bio, following_count, followers_count, location, website, join_date, is_private, is_verified, is_banned, profile_image_url, profile_image_local_path, banner_image_url, banner_image_local_path, pinned_tweet_id, is_content_downloaded
from users
where id = ?
`)

View File

@ -2,6 +2,7 @@ package persistence_test
import (
"testing"
"time"
"github.com/go-test/deep"
)
@ -41,6 +42,80 @@ func TestSaveAndLoadUser(t *testing.T) {
}
}
/**
*
*/
func TestModifyUser(t *testing.T) {
profile_path := "test_profiles/TestUserQueries"
profile := create_or_load_profile(profile_path)
fake_user := create_dummy_user()
fake_user.DisplayName = "Display Name 1"
fake_user.Location = "location1"
fake_user.IsPrivate = false
fake_user.IsVerified = false
fake_user.IsBanned = false
fake_user.FollowersCount = 1000
fake_user.JoinDate = time.Unix(1000, 0)
fake_user.ProfileImageUrl = "asdf"
fake_user.IsContentDownloaded = true
// Save the user so it can be modified
err := profile.SaveUser(fake_user)
if err != nil {
panic(err)
}
fake_user.DisplayName = "Display Name 2"
fake_user.Location = "location2"
fake_user.IsPrivate = true
fake_user.IsVerified = true
fake_user.IsBanned = true
fake_user.FollowersCount = 2000
fake_user.JoinDate = time.Unix(2000, 0)
fake_user.ProfileImageUrl = "asdf2"
fake_user.IsContentDownloaded = false // test No Worsening
// Save the modified user
err = profile.SaveUser(fake_user)
if err != nil {
panic(err)
}
// Reload the modified user
new_fake_user, err := profile.GetUserByID(fake_user.ID)
if err != nil {
panic(err)
}
if new_fake_user.DisplayName != "Display Name 2" {
t.Errorf("Expected display name %q, got %q", "Display Name 2", new_fake_user.DisplayName)
}
if new_fake_user.Location != "location2" {
t.Errorf("Expected location %q, got %q", "location2", new_fake_user.Location)
}
if new_fake_user.IsPrivate != true {
t.Errorf("Should be private")
}
if new_fake_user.IsVerified != true {
t.Errorf("Should be verified")
}
if new_fake_user.IsBanned != true {
t.Errorf("Should be banned")
}
if new_fake_user.FollowersCount != 2000 {
t.Errorf("Expected %d followers, got %d", 2000, new_fake_user.FollowersCount)
}
if new_fake_user.JoinDate.Unix() != 1000 {
t.Errorf("Expected unchanged join date (%d), got %d", 1000, new_fake_user.JoinDate.Unix())
}
if new_fake_user.ProfileImageUrl != "asdf2" {
t.Errorf("Expected profile image url to be %q, got %q", "asdf2", new_fake_user.ProfileImageUrl)
}
if new_fake_user.IsContentDownloaded != true {
t.Errorf("Expected content to be downloaded (no-worsening)")
}
}
func TestHandleIsCaseInsensitive(t *testing.T) {
profile_path := "test_profiles/TestUserQueries"

View File

@ -8,7 +8,7 @@ import (
)
const ENGINE_DATABASE_VERSION = 5
const ENGINE_DATABASE_VERSION = 6
type VersionMismatchError struct {
@ -58,6 +58,7 @@ var MIGRATIONS = []string{
alter table videos add column thumbnail_local_filename text not null default "missing"`,
`alter table videos add column duration integer not null default 0;
alter table videos add column view_count integer not null default 0`,
`alter table users add column is_banned boolean default 0`,
}
/**