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
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

View file

@ -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_public_statuses(opts \\ %{}) do
activities = ActivityPub.fetch_public_activities(opts)
def fetch_friend_statuses(user, opts \\ %{}) do
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) ->
actor = get_in(activity.data, ["actor"])
user = Repo.get_by!(User, ap_id: actor)

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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