This commit is contained in:
parent
8ee314b0bb
commit
daf401a2f7
9 changed files with 73 additions and 61 deletions
|
@ -3,10 +3,11 @@ package server
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
httputil "git.mstar.dev/mstar/goutils/http"
|
||||
"git.mstar.dev/mstar/goutils/sliceutils"
|
||||
"github.com/google/jsonapi"
|
||||
"github.com/rs/zerolog/hlog"
|
||||
"git.mstar.dev/mstar/goutils/other"
|
||||
"git.mstar.dev/mstar/goutils/sliceutils"
|
||||
|
||||
"git.mstar.dev/mstar/linstrom/storage"
|
||||
)
|
||||
|
||||
|
@ -22,11 +23,11 @@ func linstromGetAccount(w http.ResponseWriter, r *http.Request) {
|
|||
case nil:
|
||||
// Ok, do nothing
|
||||
case storage.ErrEntryNotFound:
|
||||
other.HttpErr(w, HttpErrIdNotFound, "account not found", http.StatusNotFound)
|
||||
httputil.HttpErr(w, HttpErrIdNotFound, "account not found", http.StatusNotFound)
|
||||
return
|
||||
default:
|
||||
log.Error().Err(err).Str("account-id", accId).Msg("Failed to get account from storage")
|
||||
other.HttpErr(
|
||||
httputil.HttpErr(
|
||||
w,
|
||||
HttpErrIdDbFailure,
|
||||
"Failed to get account from storage",
|
||||
|
@ -43,7 +44,7 @@ func linstromGetAccount(w http.ResponseWriter, r *http.Request) {
|
|||
Err(err).
|
||||
Strs("role-names", acc.Roles).
|
||||
Msg("Failed to get roles from storage")
|
||||
other.HttpErr(
|
||||
httputil.HttpErr(
|
||||
w,
|
||||
HttpErrIdDbFailure,
|
||||
"Failed to get roles of target account",
|
||||
|
@ -54,7 +55,7 @@ func linstromGetAccount(w http.ResponseWriter, r *http.Request) {
|
|||
collapsedRole := storage.CollapseRolesIntoOne(roles...)
|
||||
if sliceutils.Contains(collapsedRole.BlockedUsers, actorId) {
|
||||
// Actor account is in list of blocked accounts, deny access
|
||||
other.HttpErr(w, HttpErrIdNotAuthenticated, "Access forbidden", http.StatusForbidden)
|
||||
httputil.HttpErr(w, HttpErrIdNotAuthenticated, "Access forbidden", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -64,7 +65,7 @@ func linstromGetAccount(w http.ResponseWriter, r *http.Request) {
|
|||
log.Error().
|
||||
Err(err).
|
||||
Msg("Failed to convert storage account (and attached data) into linstrom API representation")
|
||||
other.HttpErr(
|
||||
httputil.HttpErr(
|
||||
w,
|
||||
HttpErrIdConversionFailure,
|
||||
"Failed to convert storage account and attached data into API representation",
|
||||
|
@ -86,12 +87,12 @@ func linstromUpdateAccount(w http.ResponseWriter, r *http.Request) {
|
|||
apiTarget := linstromAccount{}
|
||||
err := jsonapi.UnmarshalPayload(r.Body, &apiTarget)
|
||||
if err != nil {
|
||||
other.HttpErr(w, HttpErrIdBadRequest, "bad body", http.StatusBadRequest)
|
||||
httputil.HttpErr(w, HttpErrIdBadRequest, "bad body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
targetAccId := AccountIdFromRequest(r)
|
||||
if apiTarget.Id != targetAccId {
|
||||
other.HttpErr(
|
||||
httputil.HttpErr(
|
||||
w,
|
||||
HttpErrIdBadRequest,
|
||||
"Provided entity's id doesn't match path id",
|
||||
|
@ -100,7 +101,7 @@ func linstromUpdateAccount(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
if !(actorId == apiTarget.Id) {
|
||||
other.HttpErr(w, HttpErrIdNotAuthenticated, "Invalid permissions", http.StatusForbidden)
|
||||
httputil.HttpErr(w, HttpErrIdNotAuthenticated, "Invalid permissions", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
dbTarget, err := store.FindAccountById(apiTarget.Id)
|
||||
|
@ -112,7 +113,7 @@ func linstromUpdateAccount(w http.ResponseWriter, r *http.Request) {
|
|||
Err(err).
|
||||
Str("account-id", actorId).
|
||||
Msg("Failed to get account from db despite valid session")
|
||||
other.HttpErr(
|
||||
httputil.HttpErr(
|
||||
w,
|
||||
HttpErrIdDbFailure,
|
||||
"Failed to get account despite valid session",
|
||||
|
@ -140,7 +141,7 @@ func linstromUpdateAccount(w http.ResponseWriter, r *http.Request) {
|
|||
err = store.UpdateAccount(dbTarget)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to update account in db")
|
||||
other.HttpErr(
|
||||
httputil.HttpErr(
|
||||
w,
|
||||
HttpErrIdDbFailure,
|
||||
"Failed to update db entries",
|
||||
|
@ -152,7 +153,7 @@ func linstromUpdateAccount(w http.ResponseWriter, r *http.Request) {
|
|||
newAccData, err := convertAccountStorageToLinstrom(dbTarget, store)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to convert updated account back into api form")
|
||||
other.HttpErr(
|
||||
httputil.HttpErr(
|
||||
w,
|
||||
HttpErrIdConversionFailure,
|
||||
"Failed to convert updated account back into api form",
|
||||
|
@ -176,14 +177,14 @@ func linstromDeleteAccount(w http.ResponseWriter, r *http.Request) {
|
|||
Str("actor-id", actorId).
|
||||
Str("target-id", targetAccountId).
|
||||
Msg("Invalid attempt to delete account")
|
||||
other.HttpErr(w, HttpErrIdNotAuthenticated, "Action forbidden", http.StatusForbidden)
|
||||
httputil.HttpErr(w, HttpErrIdNotAuthenticated, "Action forbidden", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
log.Info().Str("account-id", actorId).Msg("Deleting account")
|
||||
acc, err := store.FindAccountById(targetAccountId)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("account-id", actorId).Msg("Failed to get account for deletion")
|
||||
other.HttpErr(
|
||||
httputil.HttpErr(
|
||||
w,
|
||||
HttpErrIdDbFailure,
|
||||
"Failed to get account from db",
|
||||
|
@ -204,7 +205,7 @@ func linstromDeleteAccount(w http.ResponseWriter, r *http.Request) {
|
|||
Err(err).
|
||||
Str("role-name", acc.ID).
|
||||
Msg("Failed to delete user role for account deletion request")
|
||||
other.HttpErr(
|
||||
httputil.HttpErr(
|
||||
w,
|
||||
HttpErrIdDbFailure,
|
||||
"Failed to delete user role",
|
||||
|
@ -218,7 +219,7 @@ func linstromDeleteAccount(w http.ResponseWriter, r *http.Request) {
|
|||
Err(err).
|
||||
Str("account-id", acc.ID).
|
||||
Msg("Failed to delete custom info fields for account deletion")
|
||||
other.HttpErr(
|
||||
httputil.HttpErr(
|
||||
w,
|
||||
HttpErrIdDbFailure,
|
||||
"Failed to delete custom info fields",
|
||||
|
@ -229,7 +230,7 @@ func linstromDeleteAccount(w http.ResponseWriter, r *http.Request) {
|
|||
err = store.DeleteAccount(actorId)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("account-id", acc.ID).Msg("Failed to delete account")
|
||||
other.HttpErr(
|
||||
httputil.HttpErr(
|
||||
w,
|
||||
HttpErrIdDbFailure,
|
||||
"Failed to delete account from db",
|
||||
|
@ -275,7 +276,7 @@ func linstromIsFollowingToAccount(w http.ResponseWriter, r *http.Request) {
|
|||
Str("from-id", actorId).
|
||||
Str("to-id", targetId).
|
||||
Msg("Failed to get follow relation")
|
||||
other.HttpErr(
|
||||
httputil.HttpErr(
|
||||
w,
|
||||
HttpErrIdDbFailure,
|
||||
"Failed to get relation",
|
||||
|
@ -286,7 +287,7 @@ func linstromIsFollowingToAccount(w http.ResponseWriter, r *http.Request) {
|
|||
err = jsonapi.MarshalPayload(w, outData)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("Failed to marshal response")
|
||||
other.HttpErr(
|
||||
httputil.HttpErr(
|
||||
w,
|
||||
HttpErrIdJsonMarshalFail,
|
||||
"Failed to marshal response",
|
||||
|
@ -330,7 +331,7 @@ func linstromIsFollowingFromAccount(w http.ResponseWriter, r *http.Request) {
|
|||
Str("from-id", targetId).
|
||||
Str("to-id", actorId).
|
||||
Msg("Failed to get follow relation")
|
||||
other.HttpErr(
|
||||
httputil.HttpErr(
|
||||
w,
|
||||
HttpErrIdDbFailure,
|
||||
"Failed to get relation",
|
||||
|
@ -341,7 +342,7 @@ func linstromIsFollowingFromAccount(w http.ResponseWriter, r *http.Request) {
|
|||
err = jsonapi.MarshalPayload(w, outData)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("Failed to marshal response")
|
||||
other.HttpErr(
|
||||
httputil.HttpErr(
|
||||
w,
|
||||
HttpErrIdJsonMarshalFail,
|
||||
"Failed to marshal response",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue