Skip to content

Commit

Permalink
add: user followers endpoint (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
lenforiee authored Sep 12, 2024
1 parent 727a97a commit 8237bfe
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func Start(dbO *sqlx.DB) *fhr.Router {

// ReadConfidential privilege required
r.Method("/api/v1/friends", v1.FriendsGET, common.PrivilegeReadConfidential)
r.Method("/api/v1/followers", v1.FollowersGET, common.PrivilegeReadConfidential)
r.Method("/api/v1/friends/with", v1.FriendsWithGET, common.PrivilegeReadConfidential)
r.Method("/api/v1/users/self/donor_info", v1.UsersSelfDonorInfoGET, common.PrivilegeReadConfidential)
r.Method("/api/v1/users/self/favourite_mode", v1.UsersSelfFavouriteModeGET, common.PrivilegeReadConfidential)
Expand Down
67 changes: 67 additions & 0 deletions app/v1/friend.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ type friendsGETResponse struct {
Friends []friendData `json:"friends"`
}

type followersGETResponse struct {
common.ResponseBase
Followers []userData `json:"followers"`
}

// FriendsGET is the API request handler for GET /friends.
// It retrieves an user's friends, and whether the friendship is mutual or not.
func FriendsGET(md common.MethodData) common.CodeMessager {
Expand Down Expand Up @@ -89,6 +94,68 @@ AND privileges & 1
return r
}

func FollowersGET(md common.MethodData) common.CodeMessager {
r := followersGETResponse{}

if md.User.UserPrivileges&common.UserPrivilegePremium == 0 {
return common.SimpleResponse(403, "You don't have privileges to access that route.")
}

myFollowersQuery := `
SELECT
users.id, users.username, users.register_datetime, users.privileges, users.latest_activity,
users.username_aka, users.country
FROM
users_relationships
INNER JOIN
users
ON
users_relationships.user1 = users.id
WHERE
users_relationships.user2 = ? AND users_relationships.user1 NOT IN
(SELECT user2 FROM users_relationships WHERE user1 = ?) AND users.privileges & 1 `

myFollowersQuery += common.Sort(md, common.SortConfiguration{
Allowed: []string{
"id",
"username",
"latest_activity",
},
Default: "users.id asc",
Table: "users",
}) + "\n"

results, err := md.DB.Query(myFollowersQuery+common.Paginate(md.Query("p"), md.Query("l"), 100), md.ID(), md.ID())
if err != nil {
md.Err(err)
return Err500
}

var followers []userData
defer results.Close()
for results.Next() {
u := userData{}
err = results.Scan(
&u.ID, &u.Username, &u.RegisteredOn, &u.Privileges, &u.LatestActivity,
&u.UsernameAKA, &u.Country,
)

if err != nil {
md.Err(err)
return Err500
}

followers = append(followers, u)
}
if err := results.Err(); err != nil {
md.Err(err)
}

r.Code = 200
r.Followers = followers
return r
}

func friendPuts(md common.MethodData, row *sql.Rows) (user friendData) {
err := row.Scan(&user.ID, &user.Username, &user.RegisteredOn, &user.Privileges, &user.LatestActivity, &user.UsernameAKA, &user.Country)
if err != nil {
Expand Down

0 comments on commit 8237bfe

Please sign in to comment.