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 @@ def fetch_activities(recipients, opts \\ %{}) do
query
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)
|> Enum.reverse
end

View File

@ -78,6 +78,12 @@ def fetch_public_statuses(user, opts \\ %{}) do
|> activities_to_statuses(%{for: user})
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
query = from activity in Activity,
where: fragment("? @> ?", activity.data, ^%{ statusnetConversationId: id}),
@ -188,4 +194,15 @@ defp activity_to_status(activity, opts) do
defp make_date do
DateTime.utc_now() |> DateTime.to_iso8601
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

View File

@ -43,24 +43,10 @@ def friends_timeline(%{assigns: %{user: user}} = conn, params) do
|> json_reply(200, json)
end
def user_timeline(conn, %{ "user_id" => user_id } = params) do
user = Repo.get(User, user_id)
conn = Map.merge(conn, %{assigns: %{user: user}})
params = Map.delete(params, "user_id")
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)
def user_timeline(%{assigns: %{user: user}} = conn, params) do
statuses = TwitterAPI.fetch_user_statuses(user, params)
conn
|> json_reply(200, statuses |> Poison.encode!)
end
def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do

View File

@ -96,6 +96,24 @@ test "fetch friends' statuses" do
assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_activity, %{user: activity_user, mentioned: [user]})
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
{:ok, activity} = ActivityBuilder.insert()
{:ok, user} = UserBuilder.insert()