linstrom/server/apiLinstromAccounts.go
mStar 1bb6cd8a70 bleh
More API stuff. Lots of bleh. Really boring

Also need to figure out a somewhat generic way for "requires ownership"
permission and then a combinator for permissions
2024-11-04 16:25:39 +01:00

109 lines
3.7 KiB
Go

package server
import (
"net/http"
"github.com/google/jsonapi"
"github.com/rs/zerolog/hlog"
"gitlab.com/mstarongitlab/goutils/other"
"gitlab.com/mstarongitlab/goutils/sliceutils"
"gitlab.com/mstarongitlab/linstrom/storage"
)
// No create account. That happens during passkey registration
// and remote accounts are getting created at fetch time
func linstromGetAccount(w http.ResponseWriter, r *http.Request) {
store := StorageFromRequest(r)
log := hlog.FromRequest(r)
accId := AccountIdFromRequest(r)
acc, err := store.FindAccountById(accId)
switch err {
case nil:
// Ok, do nothing
case storage.ErrEntryNotFound:
other.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(
w,
HttpErrIdDbFailure,
"Failed to get account from storage",
http.StatusInternalServerError,
)
return
}
actorId, ok := r.Context().Value(ContextKeyActorId).(string)
if ok {
// Logged in user is accessing account, check if target account has them blocked
roles, err := store.FindRolesByNames(acc.Roles)
if err != nil {
log.Error().
Err(err).
Strs("role-names", acc.Roles).
Msg("Failed to get roles from storage")
other.HttpErr(
w,
HttpErrIdDbFailure,
"Failed to get roles of target account",
http.StatusInternalServerError,
)
return
}
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)
return
}
}
outAccount, err := convertAccountStorageToLinstrom(acc, store)
if err != nil {
log.Error().
Err(err).
Msg("Failed to convert storage account (and attached data) into linstrom API representation")
other.HttpErr(
w,
HttpErrIdConverionFailure,
"Failed to convert storage account and attached data into API representation",
http.StatusInternalServerError,
)
return
}
err = jsonapi.MarshalPayload(w, outAccount)
if err != nil {
log.Error().Err(err).Any("account", outAccount).Msg("Failed to marshal and write account")
}
}
func linstromUpdateAccount(w http.ResponseWriter, r *http.Request) {
store := StorageFromRequest(r)
log := hlog.FromRequest(r)
}
func linstromDeleteAccount(w http.ResponseWriter, r *http.Request) {}
func linstromIsFollowingAccount(w http.ResponseWriter, r *http.Request) {}
func linstromFollowAccount(w http.ResponseWriter, r *http.Request) {}
func linstromUnfollowAccount(w http.ResponseWriter, r *http.Request) {}
func linstromIsBlockingAccount(w http.ResponseWriter, r *http.Request) {}
func linstromBlockAccount(w http.ResponseWriter, r *http.Request) {}
func linstromUnblockAccount(w http.ResponseWriter, r *http.Request) {}
func linstromIsMutedAccount(w http.ResponseWriter, r *http.Request) {}
func linstromMuteAccount(w http.ResponseWriter, r *http.Request) {}
func linstromUnmuteAccount(w http.ResponseWriter, r *http.Request) {}
func linstromReportAccount(w http.ResponseWriter, r *http.Request) {}
func linstromRetractReportAccount(w http.ResponseWriter, r *http.Request) {}
func linstromAdminAddRoleAccount(w http.ResponseWriter, r *http.Request) {}
func linstromAdminRemoveRoleAccount(w http.ResponseWriter, r *http.Request) {}
func linstromAdminWarnAccount(w http.ResponseWriter, r *http.Request) {}
func linstromGetRole(w http.ResponseWriter, r *http.Request) {}
func linstromCreateRole(w http.ResponseWriter, r *http.Request) {}
func linstromUpdateRole(w http.ResponseWriter, r *http.Request) {}
func linstromDeleteRole(w http.ResponseWriter, r *http.Request) {}