Add start_index & limit to GetFollowersFollowing

This commit is contained in:
Marco Realacci 2022-11-22 15:27:05 +01:00
parent c134becb61
commit 77e9f405c6
3 changed files with 21 additions and 9 deletions

View file

@ -25,13 +25,21 @@ func (rt *_router) GetFollowersFollowing(w http.ResponseWriter, r *http.Request,
var err error
var status database.QueryResult
// Get limits, or use default values
start_index, limit, err := helpers.GetLimits(r.URL.Query())
if err != nil {
helpers.SendBadRequest(w, "Invalid start_index or limit", rt.baseLogger)
return
}
// Check if client is asking for followers or following
if strings.HasSuffix(r.URL.Path, "/followers") {
// Get the followers from the database
status, users, err = rt.db.GetUserFollowers(uid, ctx.Auth.GetUserID())
status, users, err = rt.db.GetUserFollowers(uid, ctx.Auth.GetUserID(), start_index, limit)
} else {
// Get the following users from the database
status, users, err = rt.db.GetUserFollowing(uid, ctx.Auth.GetUserID())
status, users, err = rt.db.GetUserFollowing(uid, ctx.Auth.GetUserID(), start_index, limit)
}
// Send a 500 response if there was an error

View file

@ -48,8 +48,8 @@ type AppDatabase interface {
UpdateUsername(uid, name string) error
GetUserFollowers(uid string, requesting_uid string) (QueryResult, *[]structures.UIDName, error) // todo: maybe use a pointer to a slice?
GetUserFollowing(uid string, requesting_uid string) (QueryResult, *[]structures.UIDName, error)
GetUserFollowers(uid string, requesting_uid string, start_index int, limit int) (QueryResult, *[]structures.UIDName, error)
GetUserFollowing(uid string, requesting_uid string, start_index int, offset int) (QueryResult, *[]structures.UIDName, error)
FollowUser(uid string, follow string) (QueryResult, error)
UnfollowUser(uid string, unfollow string) (QueryResult, error)
@ -64,7 +64,7 @@ type AppDatabase interface {
LikePhoto(uid string, photo int64, liker_uid string) (QueryResult, error)
UnlikePhoto(uid string, photo int64, liker_uid string) (QueryResult, error)
GetUserProfile(uid string) (QueryResult, *structures.UserProfile, error)
GetUserProfile(uid string) (QueryResult, *structures.UserProfile, error) // should support limits
GetUserStream(uid string, start_index int, limit int) (*[]structures.Photo, error)
GetComments(uid string, photo_id int64, requesting_uid string, start_index int, offset int) (QueryResult, *[]structures.Comment, error)

View file

@ -48,7 +48,7 @@ func (db *appdbimpl) UpdateUsername(uid string, name string) error {
}
// Get user followers
func (db *appdbimpl) GetUserFollowers(uid string, requesting_uid string) (QueryResult, *[]structures.UIDName, error) {
func (db *appdbimpl) GetUserFollowers(uid string, requesting_uid string, start_index int, limit int) (QueryResult, *[]structures.UIDName, error) {
// user may exist but have no followers
exists, err := db.UserExists(uid)
@ -70,7 +70,9 @@ func (db *appdbimpl) GetUserFollowers(uid string, requesting_uid string) (QueryR
AND "bans"."ban" = "follows"."follower"
)
AND "followed" = ?`, uid, requesting_uid)
AND "followed" = ?
OFFSET ?
LIMIT ?`, uid, requesting_uid, start_index, limit)
followers, err := db.uidNameQuery(rows, err)
@ -82,7 +84,7 @@ func (db *appdbimpl) GetUserFollowers(uid string, requesting_uid string) (QueryR
}
// Get user following
func (db *appdbimpl) GetUserFollowing(uid string, requesting_uid string) (QueryResult, *[]structures.UIDName, error) {
func (db *appdbimpl) GetUserFollowing(uid string, requesting_uid string, start_index int, offset int) (QueryResult, *[]structures.UIDName, error) {
// user may exist but have no followers
exists, err := db.UserExists(uid)
@ -104,7 +106,9 @@ func (db *appdbimpl) GetUserFollowing(uid string, requesting_uid string) (QueryR
AND "bans"."ban" = "follows"."followed"
)
AND "follower" = ?`, uid, requesting_uid)
AND "follower" = ?
OFFSET ?
LIMIT ?`, uid, requesting_uid, start_index, offset)
following, err := db.uidNameQuery(rows, err)