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),
"statusnet_html" => content,
"text" => content,
"is_local" => true,
- "is_post_verb" => true
+ "is_post_verb" => true,
+ "created_at" => published
}
end
end
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
def create_status(user = %User{}, data = %{}) do
+ date = DateTime.utc_now() |> DateTime.to_iso8601
activity = %{
"type" => "Create",
"to" => [
"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)
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",
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
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
test "an activity" do
{:ok, user} = UserBuilder.insert
content = "Some content"
+ date = DateTime.utc_now() |> DateTime.to_iso8601
+
activity = %Activity{
id: 1,
data: %{
],
"actor" => User.ap_id(user),
"object" => %{
+ "published" => date,
"type" => "Note",
"content" => content
- }
+ },
+ "published" => date
}
}
"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
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