From 8075badafe60d396c236d8d6d911cbb60ae9c5b6 Mon Sep 17 00:00:00 2001 From: dtluna Date: Tue, 4 Apr 2017 03:30:07 +0300 Subject: [PATCH 1/4] Add Follow activity insertion --- lib/pleroma/web/activity_pub/activity_pub.ex | 2 +- lib/pleroma/web/twitter_api/twitter_api.ex | 9 +++++++-- lib/pleroma/web/twitter_api/twitter_api_controller.ex | 2 +- test/web/twitter_api/twitter_api_test.exs | 4 +++- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index b01def693..ec9c5e970 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -6,7 +6,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do def insert(map) when is_map(map) do map = Map.put_new_lazy(map, "id", &generate_activity_id/0) - map = if map["object"] do + map = if is_map(map["object"]) do object = Map.put_new_lazy(map["object"], "id", &generate_object_id/0) Repo.insert!(%Object{data: object}) Map.put(map, "object", object) diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 2aaa73b78..e1b6f2487 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -103,9 +103,14 @@ def fetch_status(user, id) do def follow(%User{} = follower, followed_id) do with %User{} = followed <- Repo.get(User, followed_id), - { :ok, follower } <- User.follow(follower, followed) + { :ok, follower } <- User.follow(follower, followed), + { :ok, activity } <- ActivityPub.insert(%{ + "type" => "Follow", + "actor" => follower.ap_id, + "object" => followed.ap_id + }) do - { :ok, follower, followed } + { :ok, follower, followed, activity } end end diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 2154f34f7..a13a205fc 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -44,7 +44,7 @@ def friends_timeline(%{assigns: %{user: user}} = conn, params) do end def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do - { :ok, _user, follower } = TwitterAPI.follow(user, followed_id) + { :ok, _user, follower, _activity } = TwitterAPI.follow(user, followed_id) response = follower |> UserRepresenter.to_json(%{for: user}) diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index 99b6a6cb2..ad932131a 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -107,11 +107,13 @@ test "Follow another user" do { :ok, user } = UserBuilder.insert { :ok, following } = UserBuilder.insert(%{nickname: "guy"}) - {:ok, user, following } = TwitterAPI.follow(user, following.id) + {:ok, user, following, activity } = TwitterAPI.follow(user, following.id) user = Repo.get(User, user.id) + follow = Repo.get(Activity, activity.id) assert user.following == [User.ap_followers(following)] + assert follow == activity end test "Unfollow another user" do From b502d7981c5dec762f12829a8763e18b03ae445d Mon Sep 17 00:00:00 2001 From: dtluna Date: Wed, 5 Apr 2017 02:04:54 +0300 Subject: [PATCH 2/4] Add Follow Activity representer --- .../representers/activity_representer.ex | 14 ++++++++++++++ lib/pleroma/web/twitter_api/twitter_api.ex | 13 ++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/pleroma/web/twitter_api/representers/activity_representer.ex b/lib/pleroma/web/twitter_api/representers/activity_representer.ex index 32c6a48e9..5fe0df359 100644 --- a/lib/pleroma/web/twitter_api/representers/activity_representer.ex +++ b/lib/pleroma/web/twitter_api/representers/activity_representer.ex @@ -3,6 +3,20 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ObjectRepresenter} alias Pleroma.Activity + def to_map(%Activity{data: %{"type" => "Follow"}} = activity, %{user: user} = opts) do + %{ + "id" => activity.id, + "user" => UserRepresenter.to_map(user, opts), + "attentions" => [], + "statusnet_html" => "", # TODO: add summary + "text" => "", + "is_local" => true, + "is_post_verb" => false, + "created_at" => get_in(activity.data, ["published"]), + "in_reply_to_status_id" => nil, + } + end + def to_map(%Activity{} = activity, %{user: user} = opts) do content = get_in(activity.data, ["object", "content"]) published = get_in(activity.data, ["object", "published"]) diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index e1b6f2487..932bef5ef 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -6,8 +6,6 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do import Ecto.Query def create_status(user = %User{}, data = %{}) do - date = DateTime.utc_now() |> DateTime.to_iso8601 - attachments = Enum.map(data["media_ids"] || [], fn (media_id) -> Repo.get(Object, media_id).data end) @@ -35,11 +33,11 @@ def create_status(user = %User{}, data = %{}) do "type" => "Note", "to" => to, "content" => content_html, - "published" => date, + "published" => make_date, "context" => context, "attachment" => attachments }, - "published" => date, + "published" => make_date, "context" => context } @@ -107,7 +105,8 @@ def follow(%User{} = follower, followed_id) do { :ok, activity } <- ActivityPub.insert(%{ "type" => "Follow", "actor" => follower.ap_id, - "object" => followed.ap_id + "object" => followed.ap_id, + "published" => make_date }) do { :ok, follower, followed, activity } @@ -183,4 +182,8 @@ defp activity_to_status(activity, opts) do mentioned_users = Repo.all(from user in User, where: user.ap_id in ^activity.data["to"]) ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user, mentioned: mentioned_users})) end + + defp make_date do + DateTime.utc_now() |> DateTime.to_iso8601 + end end From 57795f73323820d9c67184b9c383b77b8d6c6fbd Mon Sep 17 00:00:00 2001 From: dtluna Date: Sat, 8 Apr 2017 17:19:57 +0300 Subject: [PATCH 3/4] Refactor lib/pleroma/web/twitter_api/twitter_api.ex --- lib/pleroma/web/twitter_api/twitter_api.ex | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 932bef5ef..0d9a491da 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -25,6 +25,8 @@ def create_status(user = %User{}, data = %{}) do content_html = add_user_links(content, mentions) + date = make_date + activity = %{ "type" => "Create", "to" => to, @@ -33,11 +35,11 @@ def create_status(user = %User{}, data = %{}) do "type" => "Note", "to" => to, "content" => content_html, - "published" => make_date, + "published" => date, "context" => context, "attachment" => attachments }, - "published" => make_date, + "published" => date, "context" => context } From 0016589aea391fd6b8d41257b7f39f76b5b93775 Mon Sep 17 00:00:00 2001 From: dtluna Date: Mon, 10 Apr 2017 15:22:49 +0300 Subject: [PATCH 4/4] Remove ambiguity in call to make_date/0 --- lib/pleroma/web/twitter_api/twitter_api.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 0d9a491da..0a942e880 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -25,7 +25,7 @@ def create_status(user = %User{}, data = %{}) do content_html = add_user_links(content, mentions) - date = make_date + date = make_date() activity = %{ "type" => "Create", @@ -108,7 +108,7 @@ def follow(%User{} = follower, followed_id) do "type" => "Follow", "actor" => follower.ap_id, "object" => followed.ap_id, - "published" => make_date + "published" => make_date() }) do { :ok, follower, followed, activity }