From d6bb2f77438dcc41eca030cd8e7591207b612ddc Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 17 Mar 2023 14:14:31 +0100 Subject: [PATCH] fix(backend): return fedi info in /users/@me routes --- backend/db/fediverse.go | 13 +++++++++++++ backend/routes/user/get_user.go | 24 ++++++++++++++++++++---- backend/routes/user/patch_user.go | 20 ++++++++++++++++---- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/backend/db/fediverse.go b/backend/db/fediverse.go index 3d0e16c..8ebfa50 100644 --- a/backend/db/fediverse.go +++ b/backend/db/fediverse.go @@ -59,6 +59,19 @@ func (db *DB) FediverseApp(ctx context.Context, instance string) (fa FediverseAp return fa, nil } +func (db *DB) FediverseAppByID(ctx context.Context, id int64) (fa FediverseApp, err error) { + sql, args, err := sq.Select("*").From("fediverse_apps").Where("id = ?", id).ToSql() + if err != nil { + return fa, errors.Wrap(err, "building sql") + } + + err = pgxscan.Get(ctx, db, &fa, sql, args...) + if err != nil { + return fa, errors.Wrap(err, "executing query") + } + return fa, nil +} + func (db *DB) CreateFediverseApp(ctx context.Context, instance, instanceType, clientID, clientSecret string) (fa FediverseApp, err error) { sql, args, err := sq.Insert("fediverse_apps"). Columns("instance", "instance_type", "client_id", "client_secret"). diff --git a/backend/routes/user/get_user.go b/backend/routes/user/get_user.go index a99ff43..dff8f77 100644 --- a/backend/routes/user/get_user.go +++ b/backend/routes/user/get_user.go @@ -30,6 +30,10 @@ type GetMeResponse struct { MaxInvites int `json:"max_invites"` Discord *string `json:"discord"` DiscordUsername *string `json:"discord_username"` + + Fediverse *string `json:"fediverse"` + FediverseUsername *string `json:"fediverse_username"` + FediverseInstance *string `json:"fediverse_instance"` } type PartialMember struct { @@ -155,11 +159,23 @@ func (s *Server) getMeUser(w http.ResponseWriter, r *http.Request) error { return err } + // get fedi instance name if the user has a linked fedi account + var fediInstance *string + if u.FediverseAppID != nil { + app, err := s.DB.FediverseAppByID(ctx, *u.FediverseAppID) + if err == nil { + fediInstance = &app.Instance + } + } + render.JSON(w, r, GetMeResponse{ - GetUserResponse: dbUserToResponse(u, fields, members), - MaxInvites: u.MaxInvites, - Discord: u.Discord, - DiscordUsername: u.DiscordUsername, + GetUserResponse: dbUserToResponse(u, fields, members), + MaxInvites: u.MaxInvites, + Discord: u.Discord, + DiscordUsername: u.DiscordUsername, + Fediverse: u.Fediverse, + FediverseUsername: u.FediverseUsername, + FediverseInstance: fediInstance, }) return nil } diff --git a/backend/routes/user/patch_user.go b/backend/routes/user/patch_user.go index 486fc7f..0a5a228 100644 --- a/backend/routes/user/patch_user.go +++ b/backend/routes/user/patch_user.go @@ -211,12 +211,24 @@ func (s *Server) patchUser(w http.ResponseWriter, r *http.Request) error { return err } + // get fedi instance name if the user has a linked fedi account + var fediInstance *string + if u.FediverseAppID != nil { + app, err := s.DB.FediverseAppByID(ctx, *u.FediverseAppID) + if err == nil { + fediInstance = &app.Instance + } + } + // echo the updated user back on success render.JSON(w, r, GetMeResponse{ - GetUserResponse: dbUserToResponse(u, fields, nil), - MaxInvites: u.MaxInvites, - Discord: u.Discord, - DiscordUsername: u.DiscordUsername, + GetUserResponse: dbUserToResponse(u, fields, nil), + MaxInvites: u.MaxInvites, + Discord: u.Discord, + DiscordUsername: u.DiscordUsername, + Fediverse: u.Fediverse, + FediverseUsername: u.FediverseUsername, + FediverseInstance: fediInstance, }) return nil }