fix(backend): return fedi info in /users/@me routes

This commit is contained in:
Sam 2023-03-17 14:14:31 +01:00
parent 7a156558b6
commit d6bb2f7743
No known key found for this signature in database
GPG key ID: B4EF20DDE721CAA1
3 changed files with 49 additions and 8 deletions

View file

@ -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").

View file

@ -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
}

View file

@ -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
}