Return user specific json for logged in users.
This commit is contained in:
parent
30650e5bc6
commit
9ad045fd58
|
@ -53,4 +53,8 @@ def unfollow(%User{} = follower, %User{} = followed) do
|
|||
|> follow_changeset(%{following: following})
|
||||
|> Repo.update
|
||||
end
|
||||
|
||||
def following?(%User{} = follower, %User{} = followed) do
|
||||
Enum.member?(follower.following, User.ap_followers(followed))
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,12 +3,12 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
|
|||
alias Pleroma.Web.TwitterAPI.Representers.UserRepresenter
|
||||
alias Pleroma.Activity
|
||||
|
||||
def to_map(%Activity{} = activity, %{user: user}) do
|
||||
def to_map(%Activity{} = activity, %{user: user} = opts) do
|
||||
content = get_in(activity.data, ["object", "content"])
|
||||
published = get_in(activity.data, ["object", "published"])
|
||||
%{
|
||||
"id" => activity.id,
|
||||
"user" => UserRepresenter.to_map(user),
|
||||
"user" => UserRepresenter.to_map(user, opts),
|
||||
"attentions" => [],
|
||||
"statusnet_html" => content,
|
||||
"text" => content,
|
||||
|
|
|
@ -1,14 +1,23 @@
|
|||
defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenter do
|
||||
use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
|
||||
alias Pleroma.User
|
||||
|
||||
def to_map(user, opts \\ %{}) do
|
||||
|
||||
def to_map(user, options) do
|
||||
image = "https://placehold.it/48x48"
|
||||
|
||||
following = if opts[:for] do
|
||||
User.following?(opts[:for], user)
|
||||
else
|
||||
false
|
||||
end
|
||||
|
||||
map = %{
|
||||
"id" => user.id,
|
||||
"name" => user.name,
|
||||
"screen_name" => user.nickname,
|
||||
"description" => user.bio,
|
||||
"following" => false,
|
||||
"following" => following,
|
||||
# Fake fields
|
||||
"favourites_count" => 0,
|
||||
"statuses_count" => 0,
|
||||
|
|
|
@ -26,12 +26,12 @@ def create_status(user = %User{}, data = %{}) do
|
|||
|
||||
def fetch_friend_statuses(user, opts \\ %{}) do
|
||||
ActivityPub.fetch_activities(user.following, opts)
|
||||
|> activities_to_statuses
|
||||
|> activities_to_statuses(%{for: user})
|
||||
end
|
||||
|
||||
def fetch_public_statuses(opts \\ %{}) do
|
||||
def fetch_public_statuses(user, opts \\ %{}) do
|
||||
ActivityPub.fetch_public_activities(opts)
|
||||
|> activities_to_statuses
|
||||
|> activities_to_statuses(%{for: user})
|
||||
end
|
||||
|
||||
def follow(%User{} = follower, followed_id) do
|
||||
|
@ -50,11 +50,11 @@ def unfollow(%User{} = follower, followed_id) do
|
|||
end
|
||||
end
|
||||
|
||||
defp activities_to_statuses(activities) do
|
||||
defp activities_to_statuses(activities, opts) do
|
||||
Enum.map(activities, fn(activity) ->
|
||||
actor = get_in(activity.data, ["actor"])
|
||||
user = Repo.get_by!(User, ap_id: actor)
|
||||
ActivityRepresenter.to_map(activity, %{user: user})
|
||||
ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user}))
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,7 +4,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
|
|||
alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter}
|
||||
|
||||
def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
|
||||
response = user |> UserRepresenter.to_json
|
||||
response = user |> UserRepresenter.to_json(%{for: user})
|
||||
|
||||
conn
|
||||
|> json_reply(200, response)
|
||||
|
@ -16,8 +16,8 @@ def status_update(%{assigns: %{user: user}} = conn, status_data) do
|
|||
|> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user}))
|
||||
end
|
||||
|
||||
def public_timeline(conn, params) do
|
||||
statuses = TwitterAPI.fetch_public_statuses(params)
|
||||
def public_timeline(%{assigns: %{user: user}} = conn, params) do
|
||||
statuses = TwitterAPI.fetch_public_statuses(user, params)
|
||||
{:ok, json} = Poison.encode(statuses)
|
||||
|
||||
conn
|
||||
|
@ -35,22 +35,21 @@ def friends_timeline(%{assigns: %{user: user}} = conn, params) do
|
|||
def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
|
||||
{ :ok, _user, follower } = TwitterAPI.follow(user, followed_id)
|
||||
|
||||
response = follower |> UserRepresenter.to_json
|
||||
response = follower |> UserRepresenter.to_json(%{for: user})
|
||||
|
||||
conn
|
||||
|> json_reply(200, response)
|
||||
end
|
||||
|
||||
def unfollow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
|
||||
{ :ok, _user, follower } = TwitterAPI.unfollow(user, followed_id)
|
||||
{ :ok, user, follower } = TwitterAPI.unfollow(user, followed_id)
|
||||
|
||||
response = follower |> UserRepresenter.to_json
|
||||
response = follower |> UserRepresenter.to_json(%{for: user})
|
||||
|
||||
conn
|
||||
|> json_reply(200, response)
|
||||
end
|
||||
|
||||
|
||||
defp json_reply(conn, status, json) do
|
||||
conn
|
||||
|> put_resp_content_type("application/json")
|
||||
|
|
|
@ -45,4 +45,12 @@ test "unfollow takes a user and another user" do
|
|||
|
||||
assert user.following == []
|
||||
end
|
||||
|
||||
test "test if a user is following another user" do
|
||||
{ :ok, followed } = UserBuilder.insert(%{nickname: "guy"})
|
||||
{ :ok, user } = UserBuilder.insert(%{following: [User.ap_followers(followed)]})
|
||||
|
||||
assert User.following?(user, followed)
|
||||
refute User.following?(followed, user)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,6 +6,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
|
|||
|
||||
test "an activity" do
|
||||
{:ok, user} = UserBuilder.insert
|
||||
{:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]})
|
||||
|
||||
content = "Some content"
|
||||
date = DateTime.utc_now() |> DateTime.to_iso8601
|
||||
|
||||
|
@ -30,7 +32,7 @@ test "an activity" do
|
|||
|
||||
expected_status = %{
|
||||
"id" => activity.id,
|
||||
"user" => UserRepresenter.to_map(user),
|
||||
"user" => UserRepresenter.to_map(user, %{for: follower}),
|
||||
"is_local" => true,
|
||||
"attentions" => [],
|
||||
"statusnet_html" => content,
|
||||
|
@ -39,6 +41,6 @@ test "an activity" do
|
|||
"created_at" => date
|
||||
}
|
||||
|
||||
assert ActivityRepresenter.to_map(activity, %{user: user}) == expected_status
|
||||
assert ActivityRepresenter.to_map(activity, %{user: user, for: follower}) == expected_status
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,16 +3,10 @@ defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenterTest do
|
|||
|
||||
alias Pleroma.User
|
||||
alias Pleroma.Web.TwitterAPI.Representers.UserRepresenter
|
||||
alias Pleroma.Builders.UserBuilder
|
||||
|
||||
setup do
|
||||
user = %User{
|
||||
email: "test@example.org",
|
||||
name: "Test Name",
|
||||
nickname: "testname",
|
||||
password_hash: Comeonin.Pbkdf2.hashpwsalt("test"),
|
||||
bio: "A tester."
|
||||
}
|
||||
user = Repo.insert!(user)
|
||||
{:ok, user} = UserBuilder.insert
|
||||
[user: user]
|
||||
end
|
||||
|
||||
|
@ -38,4 +32,28 @@ test "A user", %{user: user} do
|
|||
|
||||
assert represented == UserRepresenter.to_map(user)
|
||||
end
|
||||
|
||||
test "A user for a given other follower", %{user: user} do
|
||||
{:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]})
|
||||
image = "https://placehold.it/48x48"
|
||||
represented = %{
|
||||
"id" => user.id,
|
||||
"name" => user.name,
|
||||
"screen_name" => user.nickname,
|
||||
"description" => user.bio,
|
||||
# Fake fields
|
||||
"favourites_count" => 0,
|
||||
"statuses_count" => 0,
|
||||
"friends_count" => 0,
|
||||
"followers_count" => 0,
|
||||
"profile_image_url" => image,
|
||||
"profile_image_url_https" => image,
|
||||
"profile_image_url_profile_size" => image,
|
||||
"profile_image_url_original" => image,
|
||||
"following" => true,
|
||||
"rights" => %{}
|
||||
}
|
||||
|
||||
assert represented == UserRepresenter.to_map(user, %{for: follower})
|
||||
end
|
||||
end
|
||||
|
|
|
@ -62,7 +62,7 @@ test "without valid credentials", %{conn: conn} do
|
|||
test "with credentials", %{conn: conn, user: current_user} do
|
||||
{:ok, user} = UserBuilder.insert
|
||||
activities = ActivityBuilder.insert_list(30, %{"to" => [User.ap_followers(user)]}, %{user: user})
|
||||
ActivityBuilder.insert_list(10, %{"to" => [User.ap_followers(user)]}, %{user: user})
|
||||
returned_activities = ActivityBuilder.insert_list(10, %{"to" => [User.ap_followers(user)]}, %{user: user})
|
||||
{:ok, other_user} = UserBuilder.insert(%{ap_id: "glimmung", nickname: "nockame"})
|
||||
ActivityBuilder.insert_list(10, %{}, %{user: other_user})
|
||||
since_id = List.last(activities).id
|
||||
|
@ -76,6 +76,7 @@ test "with credentials", %{conn: conn, user: current_user} do
|
|||
response = json_response(conn, 200)
|
||||
|
||||
assert length(response) == 10
|
||||
assert response == Enum.map(returned_activities, fn (activity) -> ActivityRepresenter.to_map(activity, %{user: user, for: current_user}) end)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -22,10 +22,12 @@ test "create a status" do
|
|||
|
||||
test "fetch public statuses" do
|
||||
%{ public: activity, user: user } = ActivityBuilder.public_and_non_public
|
||||
statuses = TwitterAPI.fetch_public_statuses()
|
||||
{:ok, follower } = UserBuilder.insert(%{name: "dude", ap_id: "idididid", following: [User.ap_followers(user)]})
|
||||
|
||||
statuses = TwitterAPI.fetch_public_statuses(follower)
|
||||
|
||||
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, for: follower})
|
||||
end
|
||||
|
||||
test "fetch friends' statuses" do
|
||||
|
|
Loading…
Reference in New Issue