Add friends status fetching.

This commit is contained in:
Roger Braun 2017-03-22 16:51:20 +01:00
parent 569d9bac17
commit 37e443ce6c
6 changed files with 47 additions and 14 deletions

View File

@ -1,9 +1,11 @@
defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
alias Pleroma.Web.TwitterAPI.Representers.UserRepresenter 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"]) content = get_in(activity.data, ["object", "content"])
published = get_in(activity.data, ["object", "published"])
%{ %{
"id" => activity.id, "id" => activity.id,
"user" => UserRepresenter.to_map(user), "user" => UserRepresenter.to_map(user),
@ -11,7 +13,8 @@ def to_map(activity, %{user: user}) do
"statusnet_html" => content, "statusnet_html" => content,
"text" => content, "text" => content,
"is_local" => true, "is_local" => true,
"is_post_verb" => true "is_post_verb" => true,
"created_at" => published
} }
end end
end end

View File

@ -5,6 +5,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
def create_status(user = %User{}, data = %{}) do def create_status(user = %User{}, data = %{}) do
date = DateTime.utc_now() |> DateTime.to_iso8601
activity = %{ activity = %{
"type" => "Create", "type" => "Create",
"to" => [ "to" => [
@ -14,16 +15,26 @@ def create_status(user = %User{}, data = %{}) do
"actor" => User.ap_id(user), "actor" => User.ap_id(user),
"object" => %{ "object" => %{
"type" => "Note", "type" => "Note",
"content" => data["status"] "content" => data["status"],
} "published" => date
},
"published" => date
} }
ActivityPub.insert(activity) ActivityPub.insert(activity)
end end
def fetch_public_statuses(opts \\ %{}) do def fetch_friend_statuses(user, opts \\ %{}) do
activities = ActivityPub.fetch_public_activities(opts) ActivityPub.fetch_activities(user.following, opts)
|> activities_to_statuses
end
def fetch_public_statuses(opts \\ %{}) do
ActivityPub.fetch_public_activities(opts)
|> activities_to_statuses
end
defp activities_to_statuses(activities) do
Enum.map(activities, fn(activity) -> Enum.map(activities, fn(activity) ->
actor = get_in(activity.data, ["actor"]) actor = get_in(activity.data, ["actor"])
user = Repo.get_by!(User, ap_id: actor) user = Repo.get_by!(User, ap_id: actor)

View File

@ -1,8 +1,8 @@
defmodule Pleroma.Builders.UserBuilder do defmodule Pleroma.Builders.UserBuilder do
alias Pleroma.{User, Repo} alias Pleroma.{User, Repo}
def build do def build(data \\ %{}) do
%User{ user = %User{
email: "test@example.org", email: "test@example.org",
name: "Test Name", name: "Test Name",
nickname: "testname", nickname: "testname",
@ -10,9 +10,10 @@ def build do
bio: "A tester.", bio: "A tester.",
ap_id: "some id" ap_id: "some id"
} }
Map.merge(user, data)
end end
def insert do def insert(data \\ %{}) do
Repo.insert(build()) Repo.insert(build(data))
end end
end end

View File

@ -20,7 +20,7 @@ test "inserts a given map into the activity database" do
test "retrieve the activities for certain recipients" do test "retrieve the activities for certain recipients" do
{:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]}) {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
{:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]}) {: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"]) activities = ActivityPub.fetch_activities(["someone", "someone_else"])
assert length(activities) == 2 assert length(activities) == 2

View File

@ -7,6 +7,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
test "an activity" do test "an activity" do
{:ok, user} = UserBuilder.insert {:ok, user} = UserBuilder.insert
content = "Some content" content = "Some content"
date = DateTime.utc_now() |> DateTime.to_iso8601
activity = %Activity{ activity = %Activity{
id: 1, id: 1,
data: %{ data: %{
@ -17,9 +19,11 @@ test "an activity" do
], ],
"actor" => User.ap_id(user), "actor" => User.ap_id(user),
"object" => %{ "object" => %{
"published" => date,
"type" => "Note", "type" => "Note",
"content" => content "content" => content
} },
"published" => date
} }
} }
@ -31,7 +35,8 @@ test "an activity" do
"attentions" => [], "attentions" => [],
"statusnet_html" => content, "statusnet_html" => content,
"text" => content, "text" => content,
"is_post_verb" => true "is_post_verb" => true,
"created_at" => date
} }
assert ActivityRepresenter.to_map(activity, %{user: user}) == expected_status assert ActivityRepresenter.to_map(activity, %{user: user}) == expected_status

View File

@ -20,11 +20,24 @@ test "create a status" do
assert Enum.member?(get_in(activity.data, ["to"]), "https://www.w3.org/ns/activitystreams#Public") assert Enum.member?(get_in(activity.data, ["to"]), "https://www.w3.org/ns/activitystreams#Public")
end end
test "fetch public activities" do test "fetch public statuses" do
%{ public: activity, user: user } = ActivityBuilder.public_and_non_public %{ public: activity, user: user } = ActivityBuilder.public_and_non_public
statuses = TwitterAPI.fetch_public_statuses() statuses = TwitterAPI.fetch_public_statuses()
assert length(statuses) == 1 assert length(statuses) == 1
assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: user}) assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: user})
end 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 end