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