Fixed a lot of bad queries

This commit is contained in:
Marco Realacci 2022-11-22 23:41:52 +01:00
parent a3cf4f17f8
commit 233217beb9
15 changed files with 48 additions and 30 deletions

View file

@ -42,6 +42,7 @@ import (
type AppDatabase interface {
CreateUser(name string) (string, error)
UserExists(uid string) (bool, error)
UserExistsNotBanned(uid string, requesting_uid string) (bool, error)
GetUserID(name string) (string, error)
SearchByName(name string, requesting_uid string, start_index int, limit int) (*[]structures.UIDName, error)
@ -67,7 +68,7 @@ type AppDatabase interface {
UnlikePhoto(uid string, photo int64, liker_uid string) (QueryResult, error)
GetUserProfile(uid string, requesting_uid string) (QueryResult, *structures.UserProfile, error)
GetUserPhotos(uid string, start_index int, limit int) (*[]structures.UserPhoto, error)
GetUserPhotos(uid string, requesting_uid string, start_index int, limit int) (*[]structures.UserPhoto, error)
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

@ -23,8 +23,9 @@ func (db *appdbimpl) PostComment(uid string, photo_id int64, comment_user string
_, err = db.c.Exec(`PRAGMA foreign_keys = ON;
INSERT INTO "comments" ("user", "photo", "comment", "date") VALUES (?, ?, ?, ?)`, comment_user, photo_id, comment, time.Now().Format(time.RFC3339))
// todo: we don't actually need it, it's already done before
if db_errors.ForeignKeyViolation(err) {
// trying to post a comment on a photo that does not exist
// (actually this should never happen, as we checked if the photo exists before)
return ERR_NOT_FOUND, nil
}

View file

@ -64,7 +64,7 @@ func (db *appdbimpl) GetUserProfile(uid string, requesting_uid string) (QueryRes
}, nil
}
func (db *appdbimpl) GetUserPhotos(uid string, start_index int, limit int) (*[]structures.UserPhoto, error) {
func (db *appdbimpl) GetUserPhotos(uid string, requesting_uid string, start_index int, limit int) (*[]structures.UserPhoto, error) {
// Get photos
rows, err := db.c.Query(`SELECT "p"."id", "p"."date",
@ -83,8 +83,9 @@ func (db *appdbimpl) GetUserPhotos(uid string, start_index int, limit int) (*[]s
)
FROM "photos" AS "p"
WHERE "p"."user" = ?
OFFSET ?
LIMIT ?`, uid, uid, start_index, limit)
LIMIT ?
OFFSET ?`, requesting_uid, uid, limit, start_index)
if err != nil {
// Return the error
return nil, err

View file

@ -20,6 +20,24 @@ func (db *appdbimpl) UserExists(uid string) (bool, error) {
return cnt > 0, nil
}
// User exists and is not banned
func (db *appdbimpl) UserExistsNotBanned(uid string, requesting_uid string) (bool, error) {
var cnt int
err := db.c.QueryRow(`SELECT COUNT(*) FROM "users"
WHERE "uid" = ?
AND NOT EXISTS (
SELECT "bans"."user" FROM "bans"
WHERE "bans"."user" = "users"."uid"
AND "bans"."ban" = ?
)`, uid, requesting_uid).Scan(&cnt)
if err != nil {
return false, err
}
return cnt > 0, nil
}
// Get user id by username
func (db *appdbimpl) GetUserID(name string) (string, error) {
var uid string
@ -47,7 +65,7 @@ func (db *appdbimpl) UpdateUsername(uid string, name string) 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)
exists, err := db.UserExistsNotBanned(uid, requesting_uid)
if err != nil {
return ERR_INTERNAL, nil, err
@ -57,7 +75,7 @@ func (db *appdbimpl) GetUserFollowers(uid string, requesting_uid string, start_i
return ERR_NOT_FOUND, nil, nil
}
rows, err := db.c.Query(`SELECT "follower", "user"."name" FROM "follows", "users"
rows, err := db.c.Query(`SELECT "follower", "users"."name" FROM "follows", "users"
WHERE "follows"."follower" = "users"."uid"
AND "follows"."follower" NOT IN (
@ -67,8 +85,8 @@ func (db *appdbimpl) GetUserFollowers(uid string, requesting_uid string, start_i
)
AND "followed" = ?
OFFSET ?
LIMIT ?`, uid, requesting_uid, start_index, limit)
LIMIT ?
OFFSET ?`, uid, requesting_uid, limit, start_index)
followers, err := db.uidNameQuery(rows, err)
@ -83,7 +101,7 @@ func (db *appdbimpl) GetUserFollowers(uid string, requesting_uid string, start_i
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)
exists, err := db.UserExistsNotBanned(uid, requesting_uid)
if err != nil {
return ERR_INTERNAL, nil, err