|> follow_changeset(%{following: following})
|> Repo.update
end
+
+ def following?(%User{} = follower, %User{} = followed) do
+ Enum.member?(follower.following, User.ap_followers(followed))
+ end
end
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,
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,
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
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
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)
|> 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
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")
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
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
expected_status = %{
"id" => activity.id,
- "user" => UserRepresenter.to_map(user),
+ "user" => UserRepresenter.to_map(user, %{for: follower}),
"is_local" => true,
"attentions" => [],
"statusnet_html" => content,
"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
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
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
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
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
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