Add user timeline

This commit is contained in:
dtluna 2017-04-14 16:09:13 +03:00
parent 443381d0a0
commit 18cd04ade5
4 changed files with 46 additions and 18 deletions

View File

@ -60,6 +60,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
query query
end end
query = if opts["actor_id"] do
from activity in query,
where: fragment("? @> ?", activity.data, ^%{actor: opts["actor_id"]})
else
query
end
Repo.all(query) Repo.all(query)
|> Enum.reverse |> Enum.reverse
end end

View File

@ -78,6 +78,12 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|> activities_to_statuses(%{for: user}) |> activities_to_statuses(%{for: user})
end end
def fetch_user_statuses(user, opts \\ %{}) do
target = get_user(user, opts)
ActivityPub.fetch_activities([], Map.merge(opts, %{"actor_id" => target.ap_id}))
|> activities_to_statuses(%{for: user})
end
def fetch_conversation(user, id) do def fetch_conversation(user, id) do
query = from activity in Activity, query = from activity in Activity,
where: fragment("? @> ?", activity.data, ^%{ statusnetConversationId: id}), where: fragment("? @> ?", activity.data, ^%{ statusnetConversationId: id}),
@ -188,4 +194,15 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
defp make_date do defp make_date do
DateTime.utc_now() |> DateTime.to_iso8601 DateTime.utc_now() |> DateTime.to_iso8601
end end
defp get_user(user, params) do
case params do
%{ "user_id" => user_id } ->
Repo.get(User, user_id)
%{ "screen_name" => nickname } ->
Repo.get_by!(User, nickname: nickname)
_ ->
user
end
end
end end

View File

@ -43,24 +43,10 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
|> json_reply(200, json) |> json_reply(200, json)
end end
def user_timeline(conn, %{ "user_id" => user_id } = params) do def user_timeline(%{assigns: %{user: user}} = conn, params) do
user = Repo.get(User, user_id) statuses = TwitterAPI.fetch_user_statuses(user, params)
conn = Map.merge(conn, %{assigns: %{user: user}}) conn
params = Map.delete(params, "user_id") |> json_reply(200, statuses |> Poison.encode!)
friends_timeline(conn, params)
end
def user_timeline(conn, %{ "screen_name" => nickname} = params) do
user = Repo.get_by!(User, nickname: nickname)
conn = Map.merge(conn, %{assigns: %{user: user}})
params = Map.delete(params, "screen_name")
friends_timeline(conn, params)
end
def user_timeline(conn, params) do
friends_timeline(conn, params)
end end
def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do

View File

@ -96,6 +96,24 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_activity, %{user: activity_user, mentioned: [user]}) assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_activity, %{user: activity_user, mentioned: [user]})
end end
test "fetch user's statuses" do
{:ok, user1} = UserBuilder.insert(%{ap_id: "some id"})
{:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2"})
{:ok, status1} = ActivityBuilder.insert(%{"id" => 1}, %{user: user1})
{:ok, status2} = ActivityBuilder.insert(%{"id" => 2}, %{user: user2})
user1_statuses = TwitterAPI.fetch_user_statuses(user1, %{})
assert length(user1_statuses) == 1
assert Enum.at(user1_statuses, 0) == ActivityRepresenter.to_map(status1, %{user: user1})
user2_statuses = TwitterAPI.fetch_user_statuses(user1, %{"screen_name" => user2.nickname })
assert length(user2_statuses) == 1
assert Enum.at(user2_statuses, 0) == ActivityRepresenter.to_map(status2, %{user: user2})
end
test "fetch a single status" do test "fetch a single status" do
{:ok, activity} = ActivityBuilder.insert() {:ok, activity} = ActivityBuilder.insert()
{:ok, user} = UserBuilder.insert() {:ok, user} = UserBuilder.insert()