Change IsFollowing method to use id field instead of handle

This commit is contained in:
Alessio 2022-05-07 14:21:59 -07:00
parent 7e33cc1a9f
commit 6498a97816
3 changed files with 40 additions and 7 deletions

View File

@ -193,7 +193,7 @@ func (p Profile) DownloadUserContentWithInjector(u *scraper.User, downloader Med
* If this user should have a big profile picture, defer to the regular `DownloadUserContentFor` method.
*/
func (p Profile) DownloadUserProfileImageTiny(u *scraper.User) error {
if p.IsFollowing(u.Handle) {
if p.IsFollowing(*u) {
return p.DownloadUserContentFor(u)
}

View File

@ -240,11 +240,12 @@ func (p Profile) GetAllFollowedUsers() []scraper.UserHandle {
return ret
}
func (p Profile) IsFollowing(handle scraper.UserHandle) bool {
for _, follow := range p.GetAllFollowedUsers() {
if follow == handle {
return true
}
func (p Profile) IsFollowing(user scraper.User) bool {
row := p.DB.QueryRow("select is_followed from users where id like ?", user.ID)
var ret bool
err := row.Scan(&ret)
if err != nil {
panic(err)
}
return false
return ret
}

View File

@ -214,6 +214,38 @@ func TestFollowUnfollowUser(t *testing.T) {
assert.False(user_reloaded.IsFollowed)
}
/**
* Should correctly report whether a User is followed or not, according to the DB (not the in-memory objects)
*/
func TestIsFollowingUser(t *testing.T) {
assert := assert.New(t)
profile_path := "test_profiles/TestUserQueries"
profile := create_or_load_profile(profile_path)
// Create the user
user := create_dummy_user()
assert.False(user.IsFollowed)
err := profile.SaveUser(&user)
assert.NoError(err)
// Make sure the user isn't "followed"
assert.False(profile.IsFollowing(user))
user.IsFollowed = true
assert.False(profile.IsFollowing(user)) // Should check the DB not the in-memory User
user.IsFollowed = false
profile.SetUserFollowed(&user, true)
assert.True(profile.IsFollowing(user))
user.IsFollowed = false
assert.True(profile.IsFollowing(user)) // Check the DB, not the User
user.IsFollowed = true
profile.SetUserFollowed(&user, false)
assert.False(profile.IsFollowing(user))
}
/**
* Should create a new Unknown User from the given handle.
* The Unknown User should work consistently with other Users.