Extract list of User SQL fields to a constant
This commit is contained in:
parent
723b7e4fa1
commit
8079d44781
@ -101,9 +101,7 @@ func (p Profile) fill_content(trove *TweetTrove, current_user_id UserID) {
|
|||||||
if len(user_ids) > 0 { // It could be a search with no results, end of feed, etc-- strings.Repeat will fail!
|
if len(user_ids) > 0 { // It could be a search with no results, end of feed, etc-- strings.Repeat will fail!
|
||||||
var users []User
|
var users []User
|
||||||
userquery := `
|
userquery := `
|
||||||
select id, display_name, handle, bio, following_count, followers_count, location, website, join_date, is_private, is_verified,
|
select ` + USERS_ALL_SQL_FIELDS + `
|
||||||
is_banned, profile_image_url, profile_image_local_path, banner_image_url, banner_image_local_path, pinned_tweet_id,
|
|
||||||
is_content_downloaded, is_followed
|
|
||||||
from users
|
from users
|
||||||
where id in (` + strings.Repeat("?,", len(user_ids)-1) + `?)`
|
where id in (` + strings.Repeat("?,", len(user_ids)-1) + `?)`
|
||||||
// fmt.Printf("%s\n", userquery)
|
// fmt.Printf("%s\n", userquery)
|
||||||
|
@ -6,9 +6,16 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
|
"github.com/jmoiron/sqlx"
|
||||||
|
|
||||||
"gitlab.com/offline-twitter/twitter_offline_engine/pkg/scraper"
|
"gitlab.com/offline-twitter/twitter_offline_engine/pkg/scraper"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const USERS_ALL_SQL_FIELDS = `
|
||||||
|
id, display_name, handle, bio, following_count, followers_count, location, website, join_date, is_private, is_verified,
|
||||||
|
is_banned, is_deleted, profile_image_url, profile_image_local_path, banner_image_url, banner_image_local_path,
|
||||||
|
pinned_tweet_id, is_content_downloaded, is_followed`
|
||||||
|
|
||||||
// Save the given User to the database.
|
// Save the given User to the database.
|
||||||
// If the User is already in the database, it will update most of its attributes (follower count, etc)
|
// If the User is already in the database, it will update most of its attributes (follower count, etc)
|
||||||
//
|
//
|
||||||
@ -100,9 +107,7 @@ func (p Profile) GetUserByHandle(handle scraper.UserHandle) (scraper.User, error
|
|||||||
|
|
||||||
var ret scraper.User
|
var ret scraper.User
|
||||||
err := db.Get(&ret, `
|
err := db.Get(&ret, `
|
||||||
select id, display_name, handle, bio, following_count, followers_count, location, website, join_date, is_private, is_verified,
|
select `+USERS_ALL_SQL_FIELDS+`
|
||||||
is_banned, is_deleted, profile_image_url, profile_image_local_path, banner_image_url, banner_image_local_path,
|
|
||||||
pinned_tweet_id, is_content_downloaded, is_followed
|
|
||||||
from users
|
from users
|
||||||
where lower(handle) = lower(?)
|
where lower(handle) = lower(?)
|
||||||
`, handle)
|
`, handle)
|
||||||
@ -126,9 +131,7 @@ func (p Profile) GetUserByID(id scraper.UserID) (scraper.User, error) {
|
|||||||
var ret scraper.User
|
var ret scraper.User
|
||||||
|
|
||||||
err := db.Get(&ret, `
|
err := db.Get(&ret, `
|
||||||
select id, display_name, handle, bio, following_count, followers_count, location, website, join_date, is_private, is_verified,
|
select `+USERS_ALL_SQL_FIELDS+`
|
||||||
is_banned, is_deleted, profile_image_url, profile_image_local_path, banner_image_url, banner_image_local_path,
|
|
||||||
pinned_tweet_id, is_content_downloaded, is_followed
|
|
||||||
from users
|
from users
|
||||||
where id = ?
|
where id = ?
|
||||||
`, id)
|
`, id)
|
||||||
@ -260,16 +263,22 @@ func (p Profile) get_profile_image_output_path(u scraper.User) string {
|
|||||||
// Do a text search for users
|
// Do a text search for users
|
||||||
func (p Profile) SearchUsers(s string) []scraper.User {
|
func (p Profile) SearchUsers(s string) []scraper.User {
|
||||||
var ret []scraper.User
|
var ret []scraper.User
|
||||||
val := fmt.Sprintf("%%%s%%", s)
|
q, args, err := sqlx.Named(`
|
||||||
err := p.DB.Select(&ret, `
|
select `+USERS_ALL_SQL_FIELDS+`
|
||||||
select id, display_name, handle, bio, following_count, followers_count, location, website, join_date, is_private, is_verified,
|
|
||||||
is_banned, is_deleted, profile_image_url, profile_image_local_path, banner_image_url, banner_image_local_path,
|
|
||||||
pinned_tweet_id, is_content_downloaded, is_followed
|
|
||||||
from users
|
from users
|
||||||
where handle like ?
|
where handle like :val
|
||||||
or display_name like ?
|
or display_name like :val
|
||||||
order by followers_count desc
|
order by handle like :val or display_name like :val desc,
|
||||||
`, val, val)
|
followers_count desc
|
||||||
|
`,
|
||||||
|
struct {
|
||||||
|
Val string `db:"val"`
|
||||||
|
}{fmt.Sprintf("%%%s%%", s)},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
err = p.DB.Select(&ret, q, args...)
|
||||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user