Merge branch 'feature/user-timeline' into 'develop'
Feature/user timeline See merge request !8
This commit is contained in:
commit
eda742c78b
6 changed files with 155 additions and 2 deletions
|
@ -133,6 +133,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
|
||||||
|
|
|
@ -25,6 +25,7 @@ defmodule Pleroma.Web.Router do
|
||||||
get "/help/test", TwitterAPI.Controller, :help_test
|
get "/help/test", TwitterAPI.Controller, :help_test
|
||||||
get "/statuses/public_timeline", TwitterAPI.Controller, :public_timeline
|
get "/statuses/public_timeline", TwitterAPI.Controller, :public_timeline
|
||||||
get "/statuses/public_and_external_timeline", TwitterAPI.Controller, :public_timeline
|
get "/statuses/public_and_external_timeline", TwitterAPI.Controller, :public_timeline
|
||||||
|
get "/statuses/user_timeline", TwitterAPI.Controller, :user_timeline
|
||||||
get "/statuses/show/:id", TwitterAPI.Controller, :fetch_status
|
get "/statuses/show/:id", TwitterAPI.Controller, :fetch_status
|
||||||
get "/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation
|
get "/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation
|
||||||
get "/statusnet/config", TwitterAPI.Controller, :config
|
get "/statusnet/config", TwitterAPI.Controller, :config
|
||||||
|
|
|
@ -80,6 +80,11 @@ 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
|
||||||
|
ActivityPub.fetch_activities([], opts)
|
||||||
|
|> 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}),
|
||||||
|
@ -248,12 +253,37 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
||||||
{:ok, UserRepresenter.to_map(user)}
|
{:ok, UserRepresenter.to_map(user)}
|
||||||
else
|
else
|
||||||
{:error, changeset} ->
|
{:error, changeset} ->
|
||||||
errors = Ecto.Changeset.traverse_errors(changeset, fn {msg, opts} -> msg end)
|
errors = Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> msg end)
|
||||||
|> Poison.encode!
|
|> Poison.encode!
|
||||||
{:error, %{error: errors}}
|
{:error, %{error: errors}}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get_user(user, params) do
|
||||||
|
case params do
|
||||||
|
%{ "user_id" => user_id } ->
|
||||||
|
case target = Repo.get(User, user_id) do
|
||||||
|
nil ->
|
||||||
|
{:error, "No user with such user_id"}
|
||||||
|
_ ->
|
||||||
|
{:ok, target}
|
||||||
|
end
|
||||||
|
%{ "screen_name" => nickname } ->
|
||||||
|
case target = Repo.get_by(User, nickname: nickname) do
|
||||||
|
nil ->
|
||||||
|
{:error, "No user with such screen_name"}
|
||||||
|
_ ->
|
||||||
|
{:ok, target}
|
||||||
|
end
|
||||||
|
_ ->
|
||||||
|
if user do
|
||||||
|
{:ok, user}
|
||||||
|
else
|
||||||
|
{:error, "You need to specify screen_name or user_id"}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defp activities_to_statuses(activities, opts) do
|
defp activities_to_statuses(activities, opts) do
|
||||||
Enum.map(activities, fn(activity) ->
|
Enum.map(activities, fn(activity) ->
|
||||||
activity_to_status(activity, opts)
|
activity_to_status(activity, opts)
|
||||||
|
|
|
@ -45,6 +45,18 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
|
||||||
|> json_reply(200, json)
|
|> json_reply(200, json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def user_timeline(%{assigns: %{user: user}} = conn, params) do
|
||||||
|
case TwitterAPI.get_user(user, params) do
|
||||||
|
{:ok, target_user} ->
|
||||||
|
params = Map.merge(params, %{"actor_id" => target_user.ap_id})
|
||||||
|
statuses = TwitterAPI.fetch_user_statuses(user, params)
|
||||||
|
conn
|
||||||
|
|> json_reply(200, statuses |> Poison.encode!)
|
||||||
|
{:error, msg} ->
|
||||||
|
bad_request_reply(conn, msg)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
|
def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
|
||||||
case TwitterAPI.follow(user, followed_id) do
|
case TwitterAPI.follow(user, followed_id) do
|
||||||
{ :ok, user, followed, _activity } ->
|
{ :ok, user, followed, _activity } ->
|
||||||
|
@ -162,6 +174,11 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
|
||||||
|> json_reply(200, response)
|
|> json_reply(200, response)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp bad_request_reply(conn, error_message) do
|
||||||
|
json = Poison.encode!(%{"error" => error_message})
|
||||||
|
json_reply(conn, 400, json)
|
||||||
|
end
|
||||||
|
|
||||||
defp json_reply(conn, status, json) do
|
defp json_reply(conn, status, json) do
|
||||||
conn
|
conn
|
||||||
|> put_resp_content_type("application/json")
|
|> put_resp_content_type("application/json")
|
||||||
|
|
|
@ -114,6 +114,72 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "GET /statuses/user_timeline.json" do
|
||||||
|
setup [:valid_user]
|
||||||
|
test "without any params", %{conn: conn} do
|
||||||
|
conn = get(conn, "/api/statuses/user_timeline.json")
|
||||||
|
assert json_response(conn, 400) == %{"error" => "You need to specify screen_name or user_id"}
|
||||||
|
end
|
||||||
|
|
||||||
|
test "with user_id", %{conn: conn} do
|
||||||
|
user = insert(:user)
|
||||||
|
{:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
|
||||||
|
|
||||||
|
conn = get(conn, "/api/statuses/user_timeline.json", %{"user_id" => user.id})
|
||||||
|
response = json_response(conn, 200)
|
||||||
|
assert length(response) == 1
|
||||||
|
assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
|
||||||
|
end
|
||||||
|
|
||||||
|
test "with screen_name", %{conn: conn} do
|
||||||
|
user = insert(:user)
|
||||||
|
{:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
|
||||||
|
|
||||||
|
conn = get(conn, "/api/statuses/user_timeline.json", %{"screen_name" => user.nickname})
|
||||||
|
response = json_response(conn, 200)
|
||||||
|
assert length(response) == 1
|
||||||
|
assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
|
||||||
|
end
|
||||||
|
|
||||||
|
test "with credentials", %{conn: conn, user: current_user} do
|
||||||
|
{:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: current_user})
|
||||||
|
conn = conn
|
||||||
|
|> with_credentials(current_user.nickname, "test")
|
||||||
|
|> get("/api/statuses/user_timeline.json")
|
||||||
|
|
||||||
|
response = json_response(conn, 200)
|
||||||
|
|
||||||
|
assert length(response) == 1
|
||||||
|
assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: current_user})
|
||||||
|
end
|
||||||
|
|
||||||
|
test "with credentials with user_id", %{conn: conn, user: current_user} do
|
||||||
|
user = insert(:user)
|
||||||
|
{:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
|
||||||
|
conn = conn
|
||||||
|
|> with_credentials(current_user.nickname, "test")
|
||||||
|
|> get("/api/statuses/user_timeline.json", %{"user_id" => user.id})
|
||||||
|
|
||||||
|
response = json_response(conn, 200)
|
||||||
|
|
||||||
|
assert length(response) == 1
|
||||||
|
assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
|
||||||
|
end
|
||||||
|
|
||||||
|
test "with credentials screen_name", %{conn: conn, user: current_user} do
|
||||||
|
user = insert(:user)
|
||||||
|
{:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
|
||||||
|
conn = conn
|
||||||
|
|> with_credentials(current_user.nickname, "test")
|
||||||
|
|> get("/api/statuses/user_timeline.json", %{"screen_name" => user.nickname})
|
||||||
|
|
||||||
|
response = json_response(conn, 200)
|
||||||
|
|
||||||
|
assert length(response) == 1
|
||||||
|
assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "POST /friendships/create.json" do
|
describe "POST /friendships/create.json" do
|
||||||
setup [:valid_user]
|
setup [:valid_user]
|
||||||
test "without valid credentials", %{conn: conn} do
|
test "without valid credentials", %{conn: conn} do
|
||||||
|
|
|
@ -102,6 +102,38 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
|
||||||
assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_activity, %{user: direct_activity_user, mentioned: [user]})
|
assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_activity, %{user: direct_activity_user, mentioned: [user]})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "get a user by params" do
|
||||||
|
user1_result = {:ok, user1} = UserBuilder.insert(%{ap_id: "some id", email: "test@pleroma"})
|
||||||
|
{:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2", email: "test2@pleroma"})
|
||||||
|
|
||||||
|
assert {:error, "You need to specify screen_name or user_id"} == TwitterAPI.get_user(nil, nil)
|
||||||
|
assert user1_result == TwitterAPI.get_user(nil, %{"user_id" => user1.id})
|
||||||
|
assert user1_result == TwitterAPI.get_user(nil, %{"screen_name" => user1.nickname})
|
||||||
|
assert user1_result == TwitterAPI.get_user(user1, nil)
|
||||||
|
assert user1_result == TwitterAPI.get_user(user2, %{"user_id" => user1.id})
|
||||||
|
assert user1_result == TwitterAPI.get_user(user2, %{"screen_name" => user1.nickname})
|
||||||
|
assert {:error, "No user with such screen_name"} == TwitterAPI.get_user(nil, %{"screen_name" => "Satan"})
|
||||||
|
assert {:error, "No user with such user_id"} == TwitterAPI.get_user(nil, %{"user_id" => 666})
|
||||||
|
end
|
||||||
|
|
||||||
|
test "fetch user's statuses" do
|
||||||
|
{:ok, user1} = UserBuilder.insert(%{ap_id: "some id", email: "test@pleroma"})
|
||||||
|
{:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2", email: "test2@pleroma"})
|
||||||
|
|
||||||
|
{:ok, status1} = ActivityBuilder.insert(%{"id" => 1}, %{user: user1})
|
||||||
|
{:ok, status2} = ActivityBuilder.insert(%{"id" => 2}, %{user: user2})
|
||||||
|
|
||||||
|
user1_statuses = TwitterAPI.fetch_user_statuses(user1, %{"actor_id" => user1.ap_id})
|
||||||
|
|
||||||
|
assert length(user1_statuses) == 1
|
||||||
|
assert Enum.at(user1_statuses, 0) == ActivityRepresenter.to_map(status1, %{user: user1})
|
||||||
|
|
||||||
|
user2_statuses = TwitterAPI.fetch_user_statuses(user1, %{"actor_id" => user2.ap_id})
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
Loading…
Reference in a new issue