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("storage/include/storage.hrl").
-export([details/1, new/3, update/4, delete/1]).
-export([details/2, new/4, update/5, delete/1]).
-doc """
Return user details.
@ -26,22 +26,30 @@ Return user details.
Spec:
```
-spec details(User) -> Value | {error, Reason} when
-spec details(User, Host) -> {ok, Value} | {error, Reason} when
User :: binary(),
Host :: binary(),
Value :: term(),
Reason :: term().
```
""".
-spec details(User) -> Value | {error, Reason} when
-spec details(User, Host) -> {ok, Value} | {error, Reason} when
User :: binary(),
Host :: binary(),
Value :: term(),
Reason :: term().
details(User) ->
case storage:read(?USERSBUCK, User) of
{ok, [R]} -> R#object.value;
{error, Reason} -> {error, Reason};
{ok, []} -> {error, not_found}
details(User, Host) ->
ComplUser = <<User/binary, "@", Host/binary>>,
case storage:read(?USERSBUCK, ComplUser) of
{ok, [R]} ->
{details, Details} = proplists:lookup(details, R#object.value),
{ok, Details};
{error, Reason} ->
{error, Reason};
{ok, []} ->
{error, not_found}
end.
-doc """
@ -50,8 +58,9 @@ Create a new user.
Spec:
```
-spec new(User, Password, Email) -> ok | {error, Reason} when
-spec new(User, Host, Password, Email) -> ok | {error, Reason} when
User :: binary(),
Host :: binary(),
Password :: binary(),
Email :: binary(),
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`,
""".
-spec new(User, Password, Email) -> ok | {error, Reason} when
-spec new(User, Host, Password, Email) -> ok | {error, Reason} when
User :: binary(),
Host :: binary(),
Password :: binary(),
Email :: binary(),
Reason :: term().
new(User, Password, Email) ->
new(User, Host, Password, Email) ->
ComplUser = <<User/binary, "@", Host/binary>>,
Salt = rand:bytes(?RANDBYTES),
Hash = crypto:hash(sha256, <<Password/binary, Salt/binary>>),
Data = #{<<"email">> => Email},
Metadata = [{salt, Salt}, {hash, Hash}, {approved, false}],
Data = [{details, #{<<"email">> => Email}}, {salt, Salt}, {hash, Hash}],
Metadata = [{approved, false}],
storage:write(?USERSBUCK, User, Data, Metadata).
storage:write(?USERSBUCK, ComplUser, Data, Metadata).
-doc """
Update user's details
@ -83,8 +95,9 @@ Update user's details
Spec:
```
-spec update(User, Name, Email, Desc) -> ok | {error, Reason} when
-spec update(User, Host, Name, Email, Desc) -> ok | {error, Reason} when
User :: binary(),
Host :: binary(),
Name :: binary(),
Email :: 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
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(),
Host :: binary(),
Name :: binary(),
Email :: binary(),
Desc :: binary(),
Reason :: term().
update(User, Name, Email, Desc) ->
{ok, CurData, Metadata} = case storage:read(?USERSBUCK, User) of
update(User, Host, Name, Email, Desc) ->
ComplUser = <<User/binary, "@", Host/binary>>,
{ok, Value, Metadata} = case storage:read(?USERSBUCK, ComplUser) of
{ok, [R]} ->
{ok, R#object.value, R#object.metadata};
{error, Reason} -> {error, Reason}
end,
Data = CurData#{<<"email">> => Email, <<"name">> => Name,
{details, CurDets} = proplists:lookup(details, Value),
NewDets = CurDets#{<<"email">> => Email, <<"name">> => Name,
<<"description">> => Desc},
NewData = lists:keyreplace(details, 1, Value, {details, NewDets}),
storage:write(?USERSBUCK, User, Data, Metadata).
storage:write(?USERSBUCK, ComplUser, NewData, Metadata).
-doc """
Delete an existing user from the database.