From fbb70d25fe28177b8d053d1055942abc7e7a3db9 Mon Sep 17 00:00:00 2001 From: lain Date: Sun, 18 Nov 2018 22:36:47 +0100 Subject: [PATCH] Several twitter api fixes. --- lib/pleroma/user/info.ex | 1 + lib/pleroma/web/twitter_api/twitter_api.ex | 10 +--------- lib/pleroma/web/twitter_api/views/user_view.ex | 14 +++++++------- test/web/twitter_api/twitter_api_test.exs | 10 +++++----- 4 files changed, 14 insertions(+), 21 deletions(-) diff --git a/lib/pleroma/user/info.ex b/lib/pleroma/user/info.ex index b664af81c..48f3a4d4a 100644 --- a/lib/pleroma/user/info.ex +++ b/lib/pleroma/user/info.ex @@ -4,6 +4,7 @@ defmodule Pleroma.User.Info do embedded_schema do field(:banner, :map, default: %{}) + field(:background, :string, default: nil) field(:source_data, :map, default: %{}) field(:note_count, :integer, default: 0) field(:follower_count, :integer, default: 0) diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 5bfb83b1e..d98deeb03 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -148,7 +148,7 @@ def register_user(params) do cond do registrations_open || (!is_nil(token) && !token.used) -> - changeset = User.register_changeset(%User{}, params) + changeset = User.register_changeset(%User{info: %{}}, params) with {:ok, user} <- Repo.insert(changeset) do !registrations_open && UserInviteToken.mark_as_used(token.token) @@ -279,14 +279,6 @@ def conversation_id_to_context(id) do def get_external_profile(for_user, uri) do with %User{} = user <- User.get_or_fetch(uri) do - spawn(fn -> - with url <- user.info["topic"], - {:ok, %{body: body}} <- - @httpoison.get(url, [], follow_redirect: true, timeout: 10000, recv_timeout: 20000) do - OStatus.handle_incoming(body) - end - end) - {:ok, UserView.render("show.json", %{user: user, for: for_user})} else _e -> diff --git a/lib/pleroma/web/twitter_api/views/user_view.ex b/lib/pleroma/web/twitter_api/views/user_view.ex index a100a1127..b78024ed7 100644 --- a/lib/pleroma/web/twitter_api/views/user_view.ex +++ b/lib/pleroma/web/twitter_api/views/user_view.ex @@ -31,7 +31,7 @@ def render("user.json", %{user: user = %User{}} = assigns) do user_info = User.get_cached_user_info(user) emoji = - (user.info["source_data"]["tag"] || []) + (user.info.source_data["tag"] || []) |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end) |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} -> {String.trim(name, ":"), url} @@ -40,7 +40,7 @@ def render("user.json", %{user: user = %User{}} = assigns) do # ``fields`` is an array of mastodon profile field, containing ``{"name": "…", "value": "…"}``. # For example: [{"name": "Pronoun", "value": "she/her"}, …] fields = - (user.info["source_data"]["attachment"] || []) + (user.info.source_data["attachment"] || []) |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end) |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end) @@ -66,17 +66,17 @@ def render("user.json", %{user: user = %User{}} = assigns) do "profile_image_url_profile_size" => image, "profile_image_url_original" => image, "rights" => %{ - "delete_others_notice" => !!user.info["is_moderator"] + "delete_others_notice" => !!user.info.is_moderator }, "screen_name" => user.nickname, "statuses_count" => user_info[:note_count], "statusnet_profile_url" => user.ap_id, "cover_photo" => User.banner_url(user) |> MediaProxy.url(), - "background_image" => image_url(user.info["background"]) |> MediaProxy.url(), + "background_image" => image_url(user.info.background) |> MediaProxy.url(), "is_local" => user.local, - "locked" => !!user.info["locked"], - "default_scope" => user.info["default_scope"] || "public", - "no_rich_text" => user.info["no_rich_text"] || false, + "locked" => user.info.locked, + "default_scope" => user.info.default_scope, + "no_rich_text" => user.info.no_rich_text, "fields" => fields } diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index 8b9920bd9..ec13b89d4 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -10,7 +10,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do test "create a status" do user = insert(:user) - _mentioned_user = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"}) + _mentioned_user = insert(:user, %{nickname: "shp", ap_id: "shp"}) object_data = %{ "type" => "Image", @@ -67,7 +67,7 @@ test "create a status" do user = User.get_by_ap_id(user.ap_id) - assert user.info["note_count"] == 1 + assert user.info.note_count == 1 end test "create a status that is a reply" do @@ -116,7 +116,7 @@ test "Follow another user using screen_name" do assert User.ap_followers(followed) in user.following followed = User.get_by_ap_id(followed.ap_id) - assert followed.info["follower_count"] == 1 + assert followed.info.follower_count == 1 {:error, msg} = TwitterAPI.follow(user, %{"screen_name" => followed.nickname}) assert msg == "Could not follow user: #{followed.nickname} is already on your list." @@ -169,7 +169,7 @@ test "Unblock another user using user_id" do {:ok, user, _unblocked} = TwitterAPI.block(user, %{"user_id" => unblocked.id}) {:ok, user, _unblocked} = TwitterAPI.unblock(user, %{"user_id" => unblocked.id}) - assert user.info["blocks"] == [] + assert user.info.blocks == [] end test "Unblock another user using screen_name" do @@ -178,7 +178,7 @@ test "Unblock another user using screen_name" do {:ok, user, _unblocked} = TwitterAPI.block(user, %{"screen_name" => unblocked.nickname}) {:ok, user, _unblocked} = TwitterAPI.unblock(user, %{"screen_name" => unblocked.nickname}) - assert user.info["blocks"] == [] + assert user.info.blocks == [] end test "upload a file" do