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/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 8e6a2b012..1ced7f3ea 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -25,13 +25,16 @@ 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 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/home_timeline", TwitterAPI.Controller, :friends_timeline get "/statuses/friends_timeline", TwitterAPI.Controller, :friends_timeline get "/statuses/user_timeline", TwitterAPI.Controller, :user_timeline post "/friendships/create", TwitterAPI.Controller, :follow diff --git a/lib/pleroma/web/twitter_api/representers/activity_representer.ex b/lib/pleroma/web/twitter_api/representers/activity_representer.ex index 5fe0df359..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] || [] @@ -33,14 +38,22 @@ 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, + "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/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/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 741689ebf..11e7b3bdf 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -64,7 +64,7 @@ def user_timeline(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}) @@ -103,6 +103,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") 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/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 diff --git a/test/web/twitter_api/representers/activity_representer_test.exs b/test/web/twitter_api/representers/activity_representer_test.exs index 256d920c0..f1f2b4c9c 100644 --- a/test/web/twitter_api/representers/activity_representer_test.exs +++ b/test/web/twitter_api/representers/activity_representer_test.exs @@ -23,8 +23,9 @@ test "an activity" do } } - content = "Some content mentioning @shp" - date = DateTime.utc_now() |> DateTime.to_iso8601 + content_html = "Some content mentioning @shp" + content = HtmlSanitizeEx.strip_tags(content_html) + date = DateTime.from_naive!(~N[2016-05-24 13:26:08.003], "Etc/UTC") |> DateTime.to_iso8601 activity = %Activity{ id: 1, @@ -39,7 +40,7 @@ test "an activity" do "object" => %{ "published" => date, "type" => "Note", - "content" => content, + "content" => content_html, "inReplyToStatusId" => 213123, "statusnetConversationId" => 4711, "attachment" => [ @@ -56,10 +57,10 @@ 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, + "created_at" => "Tue May 24 13:26:08 +0000 2016", "in_reply_to_status_id" => 213123, "statusnet_conversation_id" => 4711, "attachments" => [ 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 diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index ad932131a..e8853a910 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -82,15 +82,18 @@ 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}) + assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_activity, %{user: activity_user, mentioned: [user]}) end test "fetch a single status" do