Handle multi-host users.

Also, put the details in the value field. We reserve the metadata field
only for searches where relevant.
main
absc 2024-09-15 13:17:19 +00:00
parent 9e100b0317
commit f51847fe97
1 changed files with 38 additions and 20 deletions

View File

@ -18,7 +18,7 @@
-include_lib("dudeswave_backend/include/defines.hrl"). -include_lib("dudeswave_backend/include/defines.hrl").
-include_lib("storage/include/storage.hrl"). -include_lib("storage/include/storage.hrl").
-export([details/1, new/3, update/4, delete/1]). -export([details/2, new/4, update/5, delete/1]).
-doc """ -doc """
Return user details. Return user details.
@ -26,22 +26,30 @@ Return user details.
Spec: Spec:
``` ```
-spec details(User) -> Value | {error, Reason} when -spec details(User, Host) -> {ok, Value} | {error, Reason} when
User :: binary(), User :: binary(),
Host :: binary(),
Value :: term(), Value :: term(),
Reason :: term(). Reason :: term().
``` ```
""". """.
-spec details(User) -> Value | {error, Reason} when -spec details(User, Host) -> {ok, Value} | {error, Reason} when
User :: binary(), User :: binary(),
Host :: binary(),
Value :: term(), Value :: term(),
Reason :: term(). Reason :: term().
details(User) -> details(User, Host) ->
case storage:read(?USERSBUCK, User) of ComplUser = <<User/binary, "@", Host/binary>>,
{ok, [R]} -> R#object.value;
{error, Reason} -> {error, Reason}; case storage:read(?USERSBUCK, ComplUser) of
{ok, []} -> {error, not_found} {ok, [R]} ->
{details, Details} = proplists:lookup(details, R#object.value),
{ok, Details};
{error, Reason} ->
{error, Reason};
{ok, []} ->
{error, not_found}
end. end.
-doc """ -doc """
@ -50,8 +58,9 @@ Create a new user.
Spec: Spec:
``` ```
-spec new(User, Password, Email) -> ok | {error, Reason} when -spec new(User, Host, Password, Email) -> ok | {error, Reason} when
User :: binary(), User :: binary(),
Host :: binary(),
Password :: binary(), Password :: binary(),
Email :: binary(), Email :: binary(),
Reason :: term(). Reason :: term().
@ -62,20 +71,23 @@ The `User` is created, and stored in the application's users bucket
The new user is saved with a metadata `approved` of `false`, The new user is saved with a metadata `approved` of `false`,
""". """.
-spec new(User, Password, Email) -> ok | {error, Reason} when -spec new(User, Host, Password, Email) -> ok | {error, Reason} when
User :: binary(), User :: binary(),
Host :: binary(),
Password :: binary(), Password :: binary(),
Email :: binary(), Email :: binary(),
Reason :: term(). Reason :: term().
new(User, Password, Email) -> new(User, Host, Password, Email) ->
ComplUser = <<User/binary, "@", Host/binary>>,
Salt = rand:bytes(?RANDBYTES), Salt = rand:bytes(?RANDBYTES),
Hash = crypto:hash(sha256, <<Password/binary, Salt/binary>>), Hash = crypto:hash(sha256, <<Password/binary, Salt/binary>>),
Data = #{<<"email">> => Email}, Data = [{details, #{<<"email">> => Email}}, {salt, Salt}, {hash, Hash}],
Metadata = [{salt, Salt}, {hash, Hash}, {approved, false}], Metadata = [{approved, false}],
storage:write(?USERSBUCK, User, Data, Metadata). storage:write(?USERSBUCK, ComplUser, Data, Metadata).
-doc """ -doc """
Update user's details Update user's details
@ -83,8 +95,9 @@ Update user's details
Spec: Spec:
``` ```
-spec update(User, Name, Email, Desc) -> ok | {error, Reason} when -spec update(User, Host, Name, Email, Desc) -> ok | {error, Reason} when
User :: binary(), User :: binary(),
Host :: binary(),
Name :: binary(), Name :: binary(),
Email :: binary(), Email :: binary(),
Desc :: binary(), Desc :: binary(),
@ -95,24 +108,29 @@ The details apart from `User` are updated. The username itself is immutable
and cannot be modified. All the other fields, excluding the e-mail, are the and cannot be modified. All the other fields, excluding the e-mail, are the
ones that can be seen in the public page. ones that can be seen in the public page.
""". """.
-spec update(User, Name, Email, Desc) -> ok | {error, Reason} when -spec update(User, Host, Name, Email, Desc) -> ok | {error, Reason} when
User :: binary(), User :: binary(),
Host :: binary(),
Name :: binary(), Name :: binary(),
Email :: binary(), Email :: binary(),
Desc :: binary(), Desc :: binary(),
Reason :: term(). Reason :: term().
update(User, Name, Email, Desc) -> update(User, Host, Name, Email, Desc) ->
{ok, CurData, Metadata} = case storage:read(?USERSBUCK, User) of ComplUser = <<User/binary, "@", Host/binary>>,
{ok, Value, Metadata} = case storage:read(?USERSBUCK, ComplUser) of
{ok, [R]} -> {ok, [R]} ->
{ok, R#object.value, R#object.metadata}; {ok, R#object.value, R#object.metadata};
{error, Reason} -> {error, Reason} {error, Reason} -> {error, Reason}
end, end,
Data = CurData#{<<"email">> => Email, <<"name">> => Name, {details, CurDets} = proplists:lookup(details, Value),
NewDets = CurDets#{<<"email">> => Email, <<"name">> => Name,
<<"description">> => Desc}, <<"description">> => Desc},
NewData = lists:keyreplace(details, 1, Value, {details, NewDets}),
storage:write(?USERSBUCK, User, Data, Metadata). storage:write(?USERSBUCK, ComplUser, NewData, Metadata).
-doc """ -doc """
Delete an existing user from the database. Delete an existing user from the database.