From 48b6ae2314ffc087d1db33b0caaee34b877120c3 Mon Sep 17 00:00:00 2001 From: dtluna Date: Mon, 10 Apr 2017 15:41:21 +0300 Subject: [PATCH 1/9] Add GET route for verify_credentials --- lib/pleroma/web/router.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 52030d684..0d2432b9e 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -30,6 +30,7 @@ def user_fetcher(username) do scope "/api", Pleroma.Web do pipe_through :authenticated_api + get "/account/verify_credentials", TwitterAPI.Controller, :verify_credentials post "/account/verify_credentials", TwitterAPI.Controller, :verify_credentials post "/statuses/update", TwitterAPI.Controller, :status_update get "/statuses/friends_timeline", TwitterAPI.Controller, :friends_timeline From 6f99765fbace8081455e57cf2077ba42fd06d338 Mon Sep 17 00:00:00 2001 From: dtluna Date: Mon, 10 Apr 2017 18:26:14 +0300 Subject: [PATCH 2/9] Add home timeline --- lib/pleroma/web/router.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 52030d684..66dcbb118 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -32,6 +32,7 @@ def user_fetcher(username) do post "/account/verify_credentials", TwitterAPI.Controller, :verify_credentials post "/statuses/update", TwitterAPI.Controller, :status_update + get "/statuses/home_timeline", TwitterAPI.Controller, :friends_timeline get "/statuses/friends_timeline", TwitterAPI.Controller, :friends_timeline post "/friendships/create", TwitterAPI.Controller, :follow post "/friendships/destroy", TwitterAPI.Controller, :unfollow From 15655d992e8ba2aa8d4680eb38a8d3039b58703a Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Wed, 12 Apr 2017 16:38:55 +0200 Subject: [PATCH 3/9] Fix user representation after following. --- lib/pleroma/web/twitter_api/twitter_api_controller.ex | 2 +- test/web/twitter_api/twitter_api_controller_test.exs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index f2c893e96..c3a58b63a 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, _activity } = 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_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 5aad12593..7c75ff757 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -127,7 +127,7 @@ test "with credentials", %{conn: conn, user: current_user} do current_user = Repo.get(User, current_user.id) assert current_user.following == [User.ap_followers(followed)] - assert json_response(conn, 200) == UserRepresenter.to_map(followed) + assert json_response(conn, 200) == UserRepresenter.to_map(followed, %{for: current_user}) end end @@ -150,7 +150,7 @@ test "with credentials", %{conn: conn, user: current_user} do current_user = Repo.get(User, current_user.id) assert current_user.following == [] - assert json_response(conn, 200) == UserRepresenter.to_map(followed) + assert json_response(conn, 200) == UserRepresenter.to_map(followed, %{for: current_user}) end end From 8eea09889b114a27eaf6ca1998b4d3ca3505ad51 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Wed, 12 Apr 2017 16:45:23 +0200 Subject: [PATCH 4/9] Return directly addressed activities in friends timeline. --- lib/pleroma/web/twitter_api/twitter_api.ex | 2 +- test/web/twitter_api/twitter_api_test.exs | 4 +++- 2 files 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 0a942e880..0217b28d6 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -69,7 +69,7 @@ def create_status(user = %User{}, data = %{}) do end def fetch_friend_statuses(user, opts \\ %{}) do - ActivityPub.fetch_activities(user.following, opts) + ActivityPub.fetch_activities([user.ap_id | user.following], opts) |> activities_to_statuses(%{for: user}) end diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index ad932131a..dcb38b8b1 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -82,14 +82,16 @@ test "fetch public statuses" do test "fetch friends' statuses" do ActivityBuilder.public_and_non_public + {:ok, activity} = ActivityBuilder.insert(%{"to" => ["someguy/followers"]}) + {:ok, direct_activity} = ActivityBuilder.insert(%{"to" => ["some other id"]}) {:ok, user} = UserBuilder.insert(%{ap_id: "some other id", following: ["someguy/followers"]}) statuses = TwitterAPI.fetch_friend_statuses(user) activity_user = Repo.get_by(User, ap_id: activity.data["actor"]) - assert length(statuses) == 1 + assert length(statuses) == 2 assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: activity_user}) end From 6a179a44bec113b8fb9a8092f791111a318e7a53 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Wed, 12 Apr 2017 16:53:46 +0200 Subject: [PATCH 5/9] Test friends timeline more thoroughly. --- test/web/twitter_api/twitter_api_test.exs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index dcb38b8b1..e8853a910 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -93,6 +93,7 @@ test "fetch friends' statuses" do assert length(statuses) == 2 assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: activity_user}) + assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_activity, %{user: activity_user, mentioned: [user]}) end test "fetch a single status" do From b666f11d2fc32b10dce0d36e72cc285377c28f18 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Wed, 12 Apr 2017 17:11:01 +0200 Subject: [PATCH 6/9] Add max_id fetching to activity fetching. --- lib/pleroma/web/activity_pub/activity_pub.ex | 6 ++++++ test/web/activity_pub/activity_pub_test.exs | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index ec9c5e970..75e4101f2 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -54,6 +54,12 @@ def fetch_activities(recipients, opts \\ %{}) do query = from activity in query, where: activity.id > ^since_id + query = if opts["max_id"] do + from activity in query, where: activity.id < ^opts["max_id"] + else + query + end + Repo.all(query) |> Enum.reverse end diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 2c6f67621..5cfd46238 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -94,6 +94,20 @@ test "retrieves ids starting from a since_id" do assert length(activities) == 10 assert last == last_expected end + + test "retrieves ids up to max_id" do + _first_activities = ActivityBuilder.insert_list(10) + activities = ActivityBuilder.insert_list(20) + later_activities = ActivityBuilder.insert_list(10) + max_id = List.first(later_activities).id + last_expected = List.last(activities) + + activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id}) + last = List.last(activities) + + assert length(activities) == 20 + assert last == last_expected + end end describe "uploading files" do From 1b086834bd83a490e55d44ffa5167c8856db92d9 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Wed, 12 Apr 2017 17:22:29 +0200 Subject: [PATCH 7/9] Don't return html in the text field. --- .../web/twitter_api/representers/activity_representer.ex | 2 +- .../twitter_api/representers/activity_representer_test.exs | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/pleroma/web/twitter_api/representers/activity_representer.ex b/lib/pleroma/web/twitter_api/representers/activity_representer.ex index 5fe0df359..b0dd85bbb 100644 --- a/lib/pleroma/web/twitter_api/representers/activity_representer.ex +++ b/lib/pleroma/web/twitter_api/representers/activity_representer.ex @@ -33,7 +33,7 @@ def to_map(%Activity{} = activity, %{user: user} = opts) do "user" => UserRepresenter.to_map(user, opts), "attentions" => [], "statusnet_html" => content, - "text" => content, + "text" => HtmlSanitizeEx.strip_tags(content), "is_local" => true, "is_post_verb" => true, "created_at" => published, diff --git a/test/web/twitter_api/representers/activity_representer_test.exs b/test/web/twitter_api/representers/activity_representer_test.exs index 256d920c0..6b0da810f 100644 --- a/test/web/twitter_api/representers/activity_representer_test.exs +++ b/test/web/twitter_api/representers/activity_representer_test.exs @@ -23,7 +23,8 @@ test "an activity" do } } - content = "Some content mentioning @shp" + content_html = "Some content mentioning @shp" + content = HtmlSanitizeEx.strip_tags(content_html) date = DateTime.utc_now() |> DateTime.to_iso8601 activity = %Activity{ @@ -39,7 +40,7 @@ test "an activity" do "object" => %{ "published" => date, "type" => "Note", - "content" => content, + "content" => content_html, "inReplyToStatusId" => 213123, "statusnetConversationId" => 4711, "attachment" => [ @@ -56,7 +57,7 @@ test "an activity" do "user" => UserRepresenter.to_map(user, %{for: follower}), "is_local" => true, "attentions" => [], - "statusnet_html" => content, + "statusnet_html" => content_html, "text" => content, "is_post_verb" => true, "created_at" => date, From 129e30f4658312e8bb1737fd5577497107fbdeb1 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Wed, 12 Apr 2017 17:47:05 +0200 Subject: [PATCH 8/9] Add basic config endpoint. --- lib/pleroma/web/router.ex | 1 + .../web/twitter_api/twitter_api_controller.ex | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 52030d684..17eab293a 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -25,6 +25,7 @@ def user_fetcher(username) do get "/statuses/public_and_external_timeline", TwitterAPI.Controller, :public_timeline get "/statuses/show/:id", TwitterAPI.Controller, :fetch_status get "/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation + get "/statusnet/config", TwitterAPI.Controller, :config end scope "/api", Pleroma.Web do diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index c3a58b63a..3f299a941 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -83,6 +83,20 @@ def upload(conn, %{"media" => media}) do |> send_resp(200, response) end + def config(conn, _params) do + response = %{ + site: %{ + name: Pleroma.Web.base_url, + server: Pleroma.Web.base_url, + textlimit: -1 + } + } + |> Poison.encode! + + conn + |> json_reply(200, response) + end + defp json_reply(conn, status, json) do conn |> put_resp_content_type("application/json") From d2bf099ae66b7332128c854f322bb8a00eb62212 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Wed, 12 Apr 2017 19:34:58 +0200 Subject: [PATCH 9/9] Fix twitter api date. --- .../representers/activity_representer.ex | 19 ++++++++++++++++--- mix.exs | 1 + mix.lock | 12 ++++++++++-- .../activity_representer_test.exs | 4 ++-- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/lib/pleroma/web/twitter_api/representers/activity_representer.ex b/lib/pleroma/web/twitter_api/representers/activity_representer.ex index b0dd85bbb..9e4ffaefe 100644 --- a/lib/pleroma/web/twitter_api/representers/activity_representer.ex +++ b/lib/pleroma/web/twitter_api/representers/activity_representer.ex @@ -3,7 +3,11 @@ 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 + created_at = get_in(activity.data, ["published"]) + |> date_to_asctime + %{ "id" => activity.id, "user" => UserRepresenter.to_map(user, opts), @@ -12,14 +16,15 @@ def to_map(%Activity{data: %{"type" => "Follow"}} = activity, %{user: user} = op "text" => "", "is_local" => true, "is_post_verb" => false, - "created_at" => get_in(activity.data, ["published"]), + "created_at" => created_at, "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"]) + created_at = get_in(activity.data, ["object", "published"]) + |> date_to_asctime mentions = opts[:mentioned] || [] @@ -36,11 +41,19 @@ def to_map(%Activity{} = activity, %{user: user} = opts) do "text" => HtmlSanitizeEx.strip_tags(content), "is_local" => true, "is_post_verb" => true, - "created_at" => published, + "created_at" => created_at, "in_reply_to_status_id" => activity.data["object"]["inReplyToStatusId"], "statusnet_conversation_id" => activity.data["object"]["statusnetConversationId"], "attachments" => (activity.data["object"]["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts), "attentions" => attentions } end + + defp date_to_asctime(date) do + with {:ok, date, _offset} <- date |> DateTime.from_iso8601 do + Calendar.Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y") + else e -> + "" + end + end end diff --git a/mix.exs b/mix.exs index 90ecba514..43a93cc2c 100644 --- a/mix.exs +++ b/mix.exs @@ -37,6 +37,7 @@ defp deps do {:comeonin, "~> 3.0"}, {:trailing_format_plug, "~> 0.0.5" }, {:html_sanitize_ex, "~> 1.0.0"}, + {:calendar, "~> 0.16.1"}, {:mix_test_watch, "~> 0.2", only: :dev}] end diff --git a/mix.lock b/mix.lock index 072f5bce9..780a63221 100644 --- a/mix.lock +++ b/mix.lock @@ -1,4 +1,6 @@ -%{"comeonin": {:hex, :comeonin, "3.0.2", "8b213268a6634bd2e31a8035a963e974681d13ccc1f73f2ae664b6ac4e993c96", [:make, :mix], [{:elixir_make, "~> 0.4", [hex: :elixir_make, optional: false]}]}, +%{"calendar": {:hex, :calendar, "0.16.1", "782327ad8bae7c797b887840dc4ddb933f05ce6e333e5b04964d7a5d5f79bde3", [:mix], [{:tzdata, "~> 0.5.8 or ~> 0.1.201603", [hex: :tzdata, optional: false]}]}, + "certifi": {:hex, :certifi, "1.0.0", "1c787a85b1855ba354f0b8920392c19aa1d06b0ee1362f9141279620a5be2039", [:rebar3], []}, + "comeonin": {:hex, :comeonin, "3.0.2", "8b213268a6634bd2e31a8035a963e974681d13ccc1f73f2ae664b6ac4e993c96", [:make, :mix], [{:elixir_make, "~> 0.4", [hex: :elixir_make, optional: false]}]}, "connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], []}, "cowboy": {:hex, :cowboy, "1.1.2", "61ac29ea970389a88eca5a65601460162d370a70018afe6f949a29dca91f3bb0", [:rebar3], [{:cowlib, "~> 1.0.2", [hex: :cowlib, optional: false]}, {:ranch, "~> 1.3.2", [hex: :ranch, optional: false]}]}, "cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [:make], []}, @@ -8,8 +10,12 @@ "elixir_make": {:hex, :elixir_make, "0.4.0", "992f38fabe705bb45821a728f20914c554b276838433349d4f2341f7a687cddf", [:mix], []}, "fs": {:hex, :fs, "2.12.0", "ad631efacc9a5683c8eaa1b274e24fa64a1b8eb30747e9595b93bec7e492e25e", [:rebar3], []}, "gettext": {:hex, :gettext, "0.13.1", "5e0daf4e7636d771c4c71ad5f3f53ba09a9ae5c250e1ab9c42ba9edccc476263", [:mix], []}, + "hackney": {:hex, :hackney, "1.7.1", "e238c52c5df3c3b16ce613d3a51c7220a784d734879b1e231c9babd433ac1cb4", [:rebar3], [{:certifi, "1.0.0", [hex: :certifi, optional: false]}, {:idna, "4.0.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]}, "html_sanitize_ex": {:hex, :html_sanitize_ex, "1.0.1", "2572e7122c78ab7e57b613e7c7f5e42bf9b3c25e430e32f23f1413d86db8a0af", [:mix], [{:mochiweb, "~> 2.12.2", [hex: :mochiweb, optional: false]}]}, + "idna": {:hex, :idna, "4.0.0", "10aaa9f79d0b12cf0def53038547855b91144f1bfcc0ec73494f38bb7b9c4961", [:rebar3], []}, + "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []}, "mime": {:hex, :mime, "1.1.0", "01c1d6f4083d8aa5c7b8c246ade95139620ef8effb009edde934e0ec3b28090a", [:mix], []}, + "mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []}, "mix_test_watch": {:hex, :mix_test_watch, "0.3.3", "70859889a8d1d43d1b75d69d87258a301f43209a17787cdb2bd9cab42adf271d", [:mix], [{:fs, "~> 2.12", [hex: :fs, optional: false]}]}, "mochiweb": {:hex, :mochiweb, "2.12.2", "80804ad342afa3d7f3524040d4eed66ce74b17a555de454ac85b07c479928e46", [:make, :rebar], []}, "phoenix": {:hex, :phoenix, "1.3.0-rc.1", "0d04948a4bd24823f101024c07b6a4d35e58f1fd92a465c1bc75dd37acd1041a", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}, {:phoenix_pubsub, "~> 1.0", [hex: :phoenix_pubsub, optional: false]}, {:plug, "~> 1.3.2 or ~> 1.4", [hex: :plug, optional: false]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, optional: false]}]}, @@ -20,4 +26,6 @@ "poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], []}, "postgrex": {:hex, :postgrex, "0.13.2", "2b88168fc6a5456a27bfb54ccf0ba4025d274841a7a3af5e5deb1b755d95154e", [:mix], [{:connection, "~> 1.0", [hex: :connection, optional: false]}, {:db_connection, "~> 1.1", [hex: :db_connection, optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}]}, "ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [:rebar3], []}, - "trailing_format_plug": {:hex, :trailing_format_plug, "0.0.6", "777fd71bbc30e54cb36133bacd38fac88d44be410bc60353fe48e91e14c0b990", [:mix], [{:cowboy, "~> 1.0.0", [hex: :cowboy, optional: false]}, {:plug, "> 0.12.0", [hex: :plug, optional: false]}]}} + "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], []}, + "trailing_format_plug": {:hex, :trailing_format_plug, "0.0.6", "777fd71bbc30e54cb36133bacd38fac88d44be410bc60353fe48e91e14c0b990", [:mix], [{:cowboy, "~> 1.0.0", [hex: :cowboy, optional: false]}, {:plug, "> 0.12.0", [hex: :plug, optional: false]}]}, + "tzdata": {:hex, :tzdata, "0.5.11", "3d5469a9f46bdf4a8760333dbdabdcc4751325035c454b10521f71e7c611ae50", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, optional: false]}]}} diff --git a/test/web/twitter_api/representers/activity_representer_test.exs b/test/web/twitter_api/representers/activity_representer_test.exs index 6b0da810f..f1f2b4c9c 100644 --- a/test/web/twitter_api/representers/activity_representer_test.exs +++ b/test/web/twitter_api/representers/activity_representer_test.exs @@ -25,7 +25,7 @@ test "an activity" do content_html = "Some content mentioning @shp" content = HtmlSanitizeEx.strip_tags(content_html) - date = DateTime.utc_now() |> DateTime.to_iso8601 + date = DateTime.from_naive!(~N[2016-05-24 13:26:08.003], "Etc/UTC") |> DateTime.to_iso8601 activity = %Activity{ id: 1, @@ -60,7 +60,7 @@ test "an activity" do "statusnet_html" => content_html, "text" => content, "is_post_verb" => true, - "created_at" => date, + "created_at" => "Tue May 24 13:26:08 +0000 2016", "in_reply_to_status_id" => 213123, "statusnet_conversation_id" => 4711, "attachments" => [