Commit e2c1d464 authored by Karen Konou's avatar Karen Konou
Browse files

User profiles:add endpoint/BE support for websites

parent 900d5769
......@@ -71,17 +71,20 @@ defmodule MoodleNet.Accounts do
def update_user(actor, changes) do
{icon_url, changes} = Map.pop(changes, :icon)
{location_content, changes} = Map.pop(changes, :location)
{website, changes} = Map.pop(changes, :website)
icon = Query.new() |> Query.belongs_to(:icon, actor) |> Query.one()
location = Query.new() |> Query.belongs_to(:location, actor) |> Query.one()
attachment = Query.new() |> Query.belongs_to(:attachment, actor) |> Query.one()
# FIXME this should be a transaction
with {:ok, _icon} <- ActivityPub.update(icon, url: icon_url),
{:ok, _location} <- update_location(location, location_content, actor),
{:ok, _attachment} <- update_attachment(attachment, website, actor),
{:ok, actor} <- ActivityPub.update(actor, changes) do
# FIXME
actor =
ActivityPub.reload(actor)
|> Query.preload_assoc([:icon, :location])
|> Query.preload_assoc([:icon, :location, :attachment])
{:ok, actor}
end
......@@ -104,6 +107,28 @@ defmodule MoodleNet.Accounts do
defp update_location(location, content, _) do
ActivityPub.update(location, content: content)
end
defp update_attachment(nil, nil, _), do: {:ok, nil}
defp update_attachment(attachment, nil, _) do
ActivityPub.delete(attachment)
{:ok, nil}
end
defp update_attachment(nil, changes, actor) do
with {:ok, attachment} <- ActivityPub.new(%{
name: "Website",
type: "PropertyValue",
value: changes
}),
{:ok, attachment} <- ActivityPub.insert(attachment),
{:ok, _} <- Alter.add(actor, :attachment, attachment),
do: {:ok, attachment}
end
defp update_attachment(attachment, changes, _) do
ActivityPub.update(attachment, value: changes)
end
def delete_user(actor) do
# FIXME this should be a transaction
......
......@@ -20,7 +20,7 @@ defmodule MoodleNetWeb.GraphQL.UserResolver do
end
def create_user(%{user: attrs}, info) do
attrs = attrs |> set_icon() |> set_location()
attrs = attrs |> set_icon() |> set_location() |> set_website()
with {:ok, %{actor: actor, user: user}} <- MoodleNet.Accounts.register_user(attrs),
{:ok, token} <- MoodleNet.OAuth.create_token(user.id) do
......@@ -88,14 +88,14 @@ defmodule MoodleNetWeb.GraphQL.UserResolver do
def prepare_user([e | _] = list, fields) when APG.has_type(e, "Person") do
list
|> preload_assoc_cond([:icon, :location], fields)
|> preload_assoc_cond([:icon, :location, :attachment], fields)
|> preload_aspect_cond([:actor_aspect], fields)
|> Enum.map(&prepare(&1, fields))
end
def prepare_user(e, fields) when APG.has_type(e, "Person") do
e
|> preload_assoc_cond([:icon, :location], fields)
|> preload_assoc_cond([:icon, :location, :attachment], fields)
|> preload_aspect_cond([:actor_aspect], fields)
|> prepare_common_fields()
end
......
......@@ -57,6 +57,12 @@ defmodule MoodleNetWeb.GraphQL.MoodleNetSchema do
def set_location(attrs), do: attrs
def set_website(%{website: website} = attrs) when is_binary(website) do
Map.put(attrs, :attachment, %{type: "PropertyValue", name: "Website", value: website})
end
def set_website(attrs), do: attrs
def current_user(%{context: %{current_user: nil}}), do: Errors.unauthorized_error()
def current_user(%{context: %{current_user: user}}), do: {:ok, user}
......@@ -151,6 +157,8 @@ defmodule MoodleNetWeb.GraphQL.MoodleNetSchema do
end
def prepare_common_fields(entity) do
website = to_website(entity.attachment)
entity
|> Map.put(:local_id, Entity.local_id(entity))
|> Map.put(:local, Entity.local?(entity))
......@@ -161,6 +169,7 @@ defmodule MoodleNetWeb.GraphQL.MoodleNetSchema do
|> Map.update(:preferred_username, nil, &from_language_value/1)
|> Map.update(:icon, nil, &to_icon/1)
|> Map.update(:location, nil, &to_location/1)
|> Map.put(:website, website)
|> Map.put(:followers_count, count_items(entity, :followers))
|> Map.put(:following_count, 15)
|> Map.put(:likes_count, entity[:likers_count])
......@@ -200,6 +209,16 @@ defmodule MoodleNetWeb.GraphQL.MoodleNetSchema do
defp to_location(_), do: nil
defp to_website([entity | _]) when APG.is_entity(entity) do
with website <- entity["value"] do
website
else
_ -> nil
end
end
defp to_website(_), do: nil
defp count_items(entity, collection) do
case entity[collection] do
%ActivityPub.SQL.AssociationNotLoaded{} -> nil
......@@ -357,8 +376,10 @@ defmodule MoodleNetWeb.GraphQL.MoodleNetSchema do
cond do
Keyword.get(opts, :collection, false) ->
{__MODULE__, :preload_collection, preload_args}
Keyword.get(opts, :preload_assoc_individually, false) ->
{__MODULE__, :preload_assoc_individually, preload_args}
true ->
{__MODULE__, :preload_assoc, preload_args}
end
......
......@@ -87,6 +87,7 @@ defmodule MoodleNetWeb.GraphQL.UserSchema do
field(:name, :string)
field(:summary, :string)
field(:location, :string)
field(:website, :string)
field(:icon, :string)
field(:primary_language, :string)
......@@ -184,6 +185,7 @@ defmodule MoodleNetWeb.GraphQL.UserSchema do
field(:name, :string)
field(:summary, :string)
field(:location, :string)
field(:website, :string)
field(:icon, :string)
field(:primary_language, :string)
end
......@@ -194,6 +196,7 @@ defmodule MoodleNetWeb.GraphQL.UserSchema do
field(:summary, :string)
field(:primary_language, :string)
field(:location, :string)
field(:website, :string)
field(:icon, :string)
end
......
......@@ -75,14 +75,15 @@ defmodule MoodleNet.AccountsTest do
describe "update_user/2" do
test "works" do
actor = Factory.actor(location: nil)
actor = Factory.actor(location: nil, attachment: nil)
attrs = %{
name: "name",
preferred_username: "username",
locale: "fr",
primary_language: "cz",
summary: "summary",
location: nil
location: nil,
website: nil
}
assert {:ok, actor} = MoodleNet.Accounts.update_user(actor, attrs)
assert actor.name == %{"und" => attrs.name}
......@@ -97,6 +98,23 @@ defmodule MoodleNet.AccountsTest do
assert {:ok, actor} = MoodleNet.Accounts.update_user(actor, %{location: nil})
assert [] == actor.location
assert {:ok, actor} = MoodleNet.Accounts.update_user(actor, %{website: "kawen.space"})
assert [%{
:name => %{"und" => "Website"},
:type => ["Object", "PropertyValue"],
"value" => "kawen.space"
}] = actor.attachment
assert {:ok, actor} = MoodleNet.Accounts.update_user(actor, %{website: "testing.kawen.dance"})
assert [%{
:name => %{"und" => "Website"},
:type => ["Object", "PropertyValue"],
"value" => "testing.kawen.dance"
}] = actor.attachment
assert {:ok, actor} = MoodleNet.Accounts.update_user(actor, %{website: nil})
assert [] == actor.attachment
end
end
......
......@@ -18,6 +18,7 @@ defmodule MoodleNetWeb.GraphQL.UserSchemaTest do
email: "alexcastano@newworld.com"
password: "password"
primaryLanguage: "Elixir"
website: "test.tld"
}
) {
token
......@@ -34,6 +35,7 @@ defmodule MoodleNetWeb.GraphQL.UserSchemaTest do
location
icon
primaryLanguage
website
}
}
}
......@@ -59,6 +61,7 @@ defmodule MoodleNetWeb.GraphQL.UserSchemaTest do
assert user["location"] == "MoodleNet"
assert user["icon"] == "https://imag.es/alexcastano"
assert user["primaryLanguage"] == "Elixir"
assert user["website"] == "test.tld"
end
test "email should be whitelist", %{conn: conn} do
......@@ -527,6 +530,7 @@ defmodule MoodleNetWeb.GraphQL.UserSchemaTest do
location: "MoodleNet"
icon: "https://imag.es/alexcastano"
primaryLanguage: "Elixir"
website: "test.tld"
}
) {
email
......@@ -539,6 +543,7 @@ defmodule MoodleNetWeb.GraphQL.UserSchemaTest do
name
summary
location
website
icon
primaryLanguage
}
......@@ -560,6 +565,7 @@ defmodule MoodleNetWeb.GraphQL.UserSchemaTest do
assert user["summary"] == "Summary"
assert user["primaryLanguage"] == "Elixir"
assert user["location"] == "MoodleNet"
assert user["website"] == "test.tld"
assert user["icon"] == "https://imag.es/alexcastano"
end
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment