Commit 37e406ae authored by lain's avatar lain

Get avatar and banner from AP users.

parent 1555b7fa
Pipeline #795 failed with stage
in 2 minutes and 36 seconds
...@@ -105,7 +105,7 @@ def update_changeset(struct, params \\ %{}) do ...@@ -105,7 +105,7 @@ def update_changeset(struct, params \\ %{}) do
def upgrade_changeset(struct, params \\ %{}) do def upgrade_changeset(struct, params \\ %{}) do
struct struct
|> cast(params, [:bio, :name, :info, :follower_address]) |> cast(params, [:bio, :name, :info, :follower_address, :avatar])
|> unique_constraint(:nickname) |> unique_constraint(:nickname)
|> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/) |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
|> validate_length(:bio, min: 1, max: 1000) |> validate_length(:bio, min: 1, max: 1000)
......
...@@ -264,15 +264,27 @@ def fetch_and_prepare_user_from_ap_id(ap_id) do ...@@ -264,15 +264,27 @@ def fetch_and_prepare_user_from_ap_id(ap_id) do
with {:ok, %{status_code: 200, body: body}} <- @httpoison.get(ap_id, ["Accept": "application/activity+json"]), with {:ok, %{status_code: 200, body: body}} <- @httpoison.get(ap_id, ["Accept": "application/activity+json"]),
{:ok, data} <- Poison.decode(body) {:ok, data} <- Poison.decode(body)
do do
avatar = %{
"type" => "Image",
"url" => [%{"href" => data["icon"]["url"]}]
}
banner = %{
"type" => "Image",
"url" => [%{"href" => data["image"]["url"]}]
}
user_data = %{ user_data = %{
ap_id: data["id"], ap_id: data["id"],
info: %{ info: %{
"ap_enabled" => true, "ap_enabled" => true,
"source_data" => data "source_data" => data,
"banner" => banner
}, },
avatar: avatar,
nickname: "#{data["preferredUsername"]}@#{URI.parse(ap_id).host}", nickname: "#{data["preferredUsername"]}@#{URI.parse(ap_id).host}",
name: data["name"], name: data["name"],
follower_address: data["followers"] follower_address: data["followers"],
} }
{:ok, user_data} {:ok, user_data}
......
...@@ -204,6 +204,8 @@ test "it upgrades a user to activitypub" do ...@@ -204,6 +204,8 @@ test "it upgrades a user to activitypub" do
activity = Repo.get(Activity, activity.id) activity = Repo.get(Activity, activity.id)
assert user.follower_address in activity.recipients assert user.follower_address in activity.recipients
assert %{"url" => [%{"href" => "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"}]} = user.avatar
assert %{"url" => [%{"href" => "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"}]} = user.info["banner"]
refute "..." in activity.recipients refute "..." in activity.recipients
unrelated_activity = Repo.get(Activity, unrelated_activity.id) unrelated_activity = Repo.get(Activity, unrelated_activity.id)
......
Markdown is supported
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