From 37e443ce6c4d263f7febd7e0723be9a3a2c9ed1d Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Wed, 22 Mar 2017 16:51:20 +0100 Subject: [PATCH] Add friends status fetching. --- .../representers/activity_representer.ex | 7 +++++-- lib/pleroma/web/twitter_api/twitter_api.ex | 17 ++++++++++++++--- test/support/builders/user_builder.ex | 9 +++++---- test/web/activity_pub/activity_pub_test.exs | 2 +- .../representers/activity_representer_test.exs | 9 +++++++-- test/web/twitter_api/twitter_api_test.exs | 15 ++++++++++++++- 6 files changed, 46 insertions(+), 13 deletions(-) diff --git a/lib/pleroma/web/twitter_api/representers/activity_representer.ex b/lib/pleroma/web/twitter_api/representers/activity_representer.ex index 32871c0de..171878162 100644 --- a/lib/pleroma/web/twitter_api/representers/activity_representer.ex +++ b/lib/pleroma/web/twitter_api/representers/activity_representer.ex @@ -1,9 +1,11 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter alias Pleroma.Web.TwitterAPI.Representers.UserRepresenter + alias Pleroma.Activity - def to_map(activity, %{user: user}) do + def to_map(%Activity{} = activity, %{user: user}) do content = get_in(activity.data, ["object", "content"]) + published = get_in(activity.data, ["object", "published"]) %{ "id" => activity.id, "user" => UserRepresenter.to_map(user), @@ -11,7 +13,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do "statusnet_html" => content, "text" => content, "is_local" => true, - "is_post_verb" => true + "is_post_verb" => true, + "created_at" => published } end end diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index da707dd6e..7e0ca4233 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -5,6 +5,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter def create_status(user = %User{}, data = %{}) do + date = DateTime.utc_now() |> DateTime.to_iso8601 activity = %{ "type" => "Create", "to" => [ @@ -14,16 +15,26 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do "actor" => User.ap_id(user), "object" => %{ "type" => "Note", - "content" => data["status"] - } + "content" => data["status"], + "published" => date + }, + "published" => date } ActivityPub.insert(activity) end + def fetch_friend_statuses(user, opts \\ %{}) do + ActivityPub.fetch_activities(user.following, opts) + |> activities_to_statuses + end + def fetch_public_statuses(opts \\ %{}) do - activities = ActivityPub.fetch_public_activities(opts) + ActivityPub.fetch_public_activities(opts) + |> activities_to_statuses + end + defp activities_to_statuses(activities) do Enum.map(activities, fn(activity) -> actor = get_in(activity.data, ["actor"]) user = Repo.get_by!(User, ap_id: actor) diff --git a/test/support/builders/user_builder.ex b/test/support/builders/user_builder.ex index 0028d42e0..710a1b87c 100644 --- a/test/support/builders/user_builder.ex +++ b/test/support/builders/user_builder.ex @@ -1,8 +1,8 @@ defmodule Pleroma.Builders.UserBuilder do alias Pleroma.{User, Repo} - def build do - %User{ + def build(data \\ %{}) do + user = %User{ email: "test@example.org", name: "Test Name", nickname: "testname", @@ -10,9 +10,10 @@ defmodule Pleroma.Builders.UserBuilder do bio: "A tester.", ap_id: "some id" } + Map.merge(user, data) end - def insert do - Repo.insert(build()) + def insert(data \\ %{}) do + Repo.insert(build(data)) end end diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 754bd72f3..d220f91c3 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -20,7 +20,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do test "retrieve the activities for certain recipients" do {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]}) {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]}) - {:ok, activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]}) + {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]}) activities = ActivityPub.fetch_activities(["someone", "someone_else"]) assert length(activities) == 2 diff --git a/test/web/twitter_api/representers/activity_representer_test.exs b/test/web/twitter_api/representers/activity_representer_test.exs index b34d3b787..ecfd5a73f 100644 --- a/test/web/twitter_api/representers/activity_representer_test.exs +++ b/test/web/twitter_api/representers/activity_representer_test.exs @@ -7,6 +7,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do test "an activity" do {:ok, user} = UserBuilder.insert content = "Some content" + date = DateTime.utc_now() |> DateTime.to_iso8601 + activity = %Activity{ id: 1, data: %{ @@ -17,9 +19,11 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do ], "actor" => User.ap_id(user), "object" => %{ + "published" => date, "type" => "Note", "content" => content - } + }, + "published" => date } } @@ -31,7 +35,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do "attentions" => [], "statusnet_html" => content, "text" => content, - "is_post_verb" => true + "is_post_verb" => true, + "created_at" => date } assert ActivityRepresenter.to_map(activity, %{user: user}) == expected_status diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index 8016f4537..5c78790af 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -20,11 +20,24 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do assert Enum.member?(get_in(activity.data, ["to"]), "https://www.w3.org/ns/activitystreams#Public") end - test "fetch public activities" do + test "fetch public statuses" do %{ public: activity, user: user } = ActivityBuilder.public_and_non_public statuses = TwitterAPI.fetch_public_statuses() assert length(statuses) == 1 assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: user}) end + + test "fetch friends' statuses" do + ActivityBuilder.public_and_non_public + {:ok, activity} = ActivityBuilder.insert(%{"to" => ["someguy/followers"]}) + {: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 Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: activity_user}) + end end -- 2.45.2