Replace UserRepresenter with UserView

This commit is contained in:
dtluna 2017-06-20 00:12:37 +03:00
parent 45c751985e
commit 069daf644d
10 changed files with 90 additions and 96 deletions

View File

@ -1,9 +1,8 @@
defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ObjectRepresenter} alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
alias Pleroma.{Activity, User} alias Pleroma.{Activity, User}
alias Calendar.Strftime alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView, Utils}
alias Pleroma.Web.TwitterAPI.TwitterAPI
alias Pleroma.Formatter alias Pleroma.Formatter
defp user_by_ap_id(user_list, ap_id) do defp user_by_ap_id(user_list, ap_id) do
@ -13,7 +12,7 @@ defp user_by_ap_id(user_list, ap_id) do
def to_map(%Activity{data: %{"type" => "Announce", "actor" => actor, "published" => created_at}} = activity, def to_map(%Activity{data: %{"type" => "Announce", "actor" => actor, "published" => created_at}} = activity,
%{users: users, announced_activity: announced_activity} = opts) do %{users: users, announced_activity: announced_activity} = opts) do
user = user_by_ap_id(users, actor) user = user_by_ap_id(users, actor)
created_at = created_at |> date_to_asctime created_at = created_at |> Utils.date_to_asctime
text = "#{user.nickname} retweeted a status." text = "#{user.nickname} retweeted a status."
@ -21,7 +20,7 @@ def to_map(%Activity{data: %{"type" => "Announce", "actor" => actor, "published"
retweeted_status = to_map(announced_activity, Map.merge(%{user: announced_user}, opts)) retweeted_status = to_map(announced_activity, Map.merge(%{user: announced_user}, opts))
%{ %{
"id" => activity.id, "id" => activity.id,
"user" => UserRepresenter.to_map(user, opts), "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
"statusnet_html" => text, "statusnet_html" => text,
"text" => text, "text" => text,
"is_local" => true, "is_local" => true,
@ -36,13 +35,13 @@ def to_map(%Activity{data: %{"type" => "Announce", "actor" => actor, "published"
def to_map(%Activity{data: %{"type" => "Like", "published" => created_at}} = activity, def to_map(%Activity{data: %{"type" => "Like", "published" => created_at}} = activity,
%{user: user, liked_activity: liked_activity} = opts) do %{user: user, liked_activity: liked_activity} = opts) do
created_at = created_at |> date_to_asctime created_at = created_at |> Utils.date_to_asctime
text = "#{user.nickname} favorited a status." text = "#{user.nickname} favorited a status."
%{ %{
"id" => activity.id, "id" => activity.id,
"user" => UserRepresenter.to_map(user, opts), "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
"statusnet_html" => text, "statusnet_html" => text,
"text" => text, "text" => text,
"is_local" => true, "is_local" => true,
@ -55,13 +54,13 @@ def to_map(%Activity{data: %{"type" => "Like", "published" => created_at}} = act
end end
def to_map(%Activity{data: %{"type" => "Follow", "published" => created_at, "object" => followed_id}} = activity, %{user: user} = opts) do def to_map(%Activity{data: %{"type" => "Follow", "published" => created_at, "object" => followed_id}} = activity, %{user: user} = opts) do
created_at = created_at |> date_to_asctime created_at = created_at |> Utils.date_to_asctime
followed = User.get_cached_by_ap_id(followed_id) followed = User.get_cached_by_ap_id(followed_id)
text = "#{user.nickname} started following #{followed.nickname}" text = "#{user.nickname} started following #{followed.nickname}"
%{ %{
"id" => activity.id, "id" => activity.id,
"user" => UserRepresenter.to_map(user, opts), "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
"attentions" => [], "attentions" => [],
"statusnet_html" => text, "statusnet_html" => text,
"text" => text, "text" => text,
@ -74,7 +73,7 @@ def to_map(%Activity{data: %{"type" => "Follow", "published" => created_at, "obj
end end
def to_map(%Activity{data: %{"object" => %{"content" => content} = object}} = activity, %{user: user} = opts) do def to_map(%Activity{data: %{"object" => %{"content" => content} = object}} = activity, %{user: user} = opts) do
created_at = object["published"] |> date_to_asctime created_at = object["published"] |> Utils.date_to_asctime
like_count = object["like_count"] || 0 like_count = object["like_count"] || 0
announcement_count = object["announcement_count"] || 0 announcement_count = object["announcement_count"] || 0
favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || []) favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
@ -85,14 +84,13 @@ def to_map(%Activity{data: %{"object" => %{"content" => content} = object}} = ac
attentions = activity.data["to"] attentions = activity.data["to"]
|> Enum.map(fn (ap_id) -> Enum.find(mentions, fn(user) -> ap_id == user.ap_id end) end) |> Enum.map(fn (ap_id) -> Enum.find(mentions, fn(user) -> ap_id == user.ap_id end) end)
|> Enum.filter(&(&1)) |> Enum.filter(&(&1))
|> Enum.map(fn (user) -> UserRepresenter.to_map(user, opts) end) |> Enum.map(fn (user) -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
conversation_id = conversation_id(activity) conversation_id = conversation_id(activity)
%{ %{
"id" => activity.id, "id" => activity.id,
"user" => UserRepresenter.to_map(user, opts), "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
"attentions" => [],
"statusnet_html" => HtmlSanitizeEx.basic_html(content) |> Formatter.finmojifiy, "statusnet_html" => HtmlSanitizeEx.basic_html(content) |> Formatter.finmojifiy,
"text" => HtmlSanitizeEx.strip_tags(content), "text" => HtmlSanitizeEx.strip_tags(content),
"is_local" => true, "is_local" => true,

View File

@ -1,7 +1,6 @@
defmodule Pleroma.Web.TwitterAPI.Representers.BaseRepresenter do defmodule Pleroma.Web.TwitterAPI.Representers.BaseRepresenter do
defmacro __using__(_opts) do defmacro __using__(_opts) do
quote do quote do
alias Calendar.Strftime
def to_json(object) do to_json(object, %{}) end def to_json(object) do to_json(object, %{}) end
def to_json(object, options) do def to_json(object, options) do
object object
@ -24,18 +23,6 @@ def enum_to_json(enum, options) do
|> enum_to_list(options) |> enum_to_list(options)
|> Poison.encode! |> Poison.encode!
end end
def format_asctime(date) do
Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
end
def date_to_asctime(date) do
with {:ok, date, _offset} <- date |> DateTime.from_iso8601 do
format_asctime(date)
else _e ->
""
end
end
end end
end end
end end

View File

@ -1,12 +1,11 @@
defmodule Pleroma.Web.TwitterAPI.TwitterAPI do defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
alias Pleroma.{User, Activity, Repo, Object} alias Pleroma.{User, Activity, Repo, Object}
alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.ActivityPub.Utils alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
alias Pleroma.Web.TwitterAPI.Representers.{ActivityRepresenter, UserRepresenter} alias Pleroma.Web.TwitterAPI.UserView
alias Pleroma.Web.OStatus alias Pleroma.Web.OStatus
alias Pleroma.Formatter alias Pleroma.Formatter
import Ecto.Query
import Pleroma.Web.TwitterAPI.Utils import Pleroma.Web.TwitterAPI.Utils
@httpoison Application.get_env(:pleroma, :httpoison) @httpoison Application.get_env(:pleroma, :httpoison)
@ -197,7 +196,7 @@ def register_user(params) do
changeset = User.register_changeset(%User{}, params) changeset = User.register_changeset(%User{}, params)
with {:ok, user} <- Repo.insert(changeset) do with {:ok, user} <- Repo.insert(changeset) do
{:ok, UserRepresenter.to_map(user)} {:ok, 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)
@ -304,7 +303,7 @@ def get_external_profile(for_user, uri) do
{:ok, %{body: body}} <- @httpoison.get(url, [], follow_redirect: true, timeout: 10000, recv_timeout: 20000) do {:ok, %{body: body}} <- @httpoison.get(url, [], follow_redirect: true, timeout: 10000, recv_timeout: 20000) do
OStatus.handle_incoming(body) OStatus.handle_incoming(body)
end end
{:ok, UserRepresenter.to_map(user, %{for: for_user})} {:ok, UserView.render("show.json", %{user: user, for: for_user})}
else _e -> else _e ->
{:error, "Couldn't find user"} {:error, "Couldn't find user"}
end end

View File

@ -1,16 +1,13 @@
defmodule Pleroma.Web.TwitterAPI.Controller do defmodule Pleroma.Web.TwitterAPI.Controller do
use Pleroma.Web, :controller use Pleroma.Web, :controller
alias Pleroma.Web.TwitterAPI.TwitterAPI alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView}
alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter} alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
alias Pleroma.{Web, Repo, Activity} alias Pleroma.{Web, Repo, Activity}
alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.ActivityPub
alias Ecto.Changeset alias Ecto.Changeset
def verify_credentials(%{assigns: %{user: user}} = conn, _params) do def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
response = user |> UserRepresenter.to_json(%{for: user}) render(conn, UserView, "show.json", %{user: user})
conn
|> json_reply(200, response)
end end
def status_update(%{assigns: %{user: user}} = conn, %{"status" => status_text} = status_data) do def status_update(%{assigns: %{user: user}} = conn, %{"status" => status_text} = status_data) do
@ -90,9 +87,7 @@ def mentions_timeline(%{assigns: %{user: user}} = conn, params) do
def follow(%{assigns: %{user: user}} = conn, params) do def follow(%{assigns: %{user: user}} = conn, params) do
case TwitterAPI.follow(user, params) do case TwitterAPI.follow(user, params) do
{:ok, user, followed, _activity} -> {:ok, user, followed, _activity} ->
response = followed |> UserRepresenter.to_json(%{for: user}) render(conn, UserView, "show.json", %{user: followed, for: user})
conn
|> json_reply(200, response)
{:error, msg} -> forbidden_json_reply(conn, msg) {:error, msg} -> forbidden_json_reply(conn, msg)
end end
end end
@ -100,9 +95,7 @@ def follow(%{assigns: %{user: user}} = conn, params) do
def unfollow(%{assigns: %{user: user}} = conn, params) do def unfollow(%{assigns: %{user: user}} = conn, params) do
case TwitterAPI.unfollow(user, params) do case TwitterAPI.unfollow(user, params) do
{:ok, user, unfollowed} -> {:ok, user, unfollowed} ->
response = unfollowed |> UserRepresenter.to_json(%{for: user}) render(conn, UserView, "show.json", %{user: unfollowed, for: user})
conn
|> json_reply(200, response)
{:error, msg} -> forbidden_json_reply(conn, msg) {:error, msg} -> forbidden_json_reply(conn, msg)
end end
end end
@ -187,8 +180,8 @@ def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
def register(conn, params) do def register(conn, params) do
with {:ok, user} <- TwitterAPI.register_user(params) do with {:ok, user} <- TwitterAPI.register_user(params) do
conn
|> json_reply(200, Poison.encode!(user)) render(conn, UserView, "show.json", %{user: user})
else else
{:error, errors} -> {:error, errors} ->
conn conn
@ -201,10 +194,7 @@ def update_avatar(%{assigns: %{user: user}} = conn, params) do
change = Changeset.change(user, %{avatar: object.data}) change = Changeset.change(user, %{avatar: object.data})
{:ok, user} = Repo.update(change) {:ok, user} = Repo.update(change)
response = Poison.encode!(UserRepresenter.to_map(user, %{for: user})) render(conn, UserView, "show.json", %{user: user, for: user})
conn
|> json_reply(200, response)
end end
def external_profile(%{assigns: %{user: current_user}} = conn, %{"profileurl" => uri}) do def external_profile(%{assigns: %{user: current_user}} = conn, %{"profileurl" => uri}) do

View File

@ -1,6 +1,7 @@
defmodule Pleroma.Web.TwitterAPI.Utils do defmodule Pleroma.Web.TwitterAPI.Utils do
alias Pleroma.{Repo, Object, Formatter, User, Activity} alias Pleroma.{Repo, Object, Formatter, User, Activity}
alias Pleroma.Web.ActivityPub.Utils alias Pleroma.Web.ActivityPub.Utils
alias Calendar.Strftime
def attachments_from_ids(ids) do def attachments_from_ids(ids) do
Enum.map(ids || [], fn (media_id) -> Enum.map(ids || [], fn (media_id) ->
@ -72,4 +73,20 @@ def make_note_data(actor, to, context, content_html, attachments, inReplyTo, tag
object object
end end
end end
def format_naive_asctime(date) do
date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
end
def format_asctime(date) do
Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
end
def date_to_asctime(date) do
with {:ok, date, _offset} <- date |> DateTime.from_iso8601 do
format_asctime(date)
else _e ->
""
end
end
end end

View File

@ -1,39 +1,47 @@
defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenter do defmodule Pleroma.Web.TwitterAPI.UserView do
use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter use Pleroma.Web, :view
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.TwitterAPI.Utils
def to_map(user, opts) do def render("show.json", %{user: user = %User{}} = assigns) do
image = User.avatar_url(user) image = User.avatar_url(user)
following = if opts[:for] do following = if assigns[:for] do
User.following?(opts[:for], user) User.following?(assigns[:for], user)
else else
false false
end end
user_info = User.get_cached_user_info(user) user_info = User.get_cached_user_info(user)
created_at = user.inserted_at |> DateTime.from_naive!("Etc/UTC") |> format_asctime
map = %{ %{
"created_at" => user.inserted_at |> Utils.format_naive_asctime,
"description" => HtmlSanitizeEx.strip_tags(user.bio),
"favourites_count" => 0,
"followers_count" => user_info[:follower_count],
"following" => following,
"friends_count" => user_info[:following_count],
"id" => user.id, "id" => user.id,
"name" => user.name, "name" => user.name,
"screen_name" => user.nickname,
"description" => HtmlSanitizeEx.strip_tags(user.bio),
"following" => following,
"created_at" => created_at,
# Fake fields
"favourites_count" => 0,
"statuses_count" => user_info[:note_count],
"friends_count" => user_info[:following_count],
"followers_count" => user_info[:follower_count],
"profile_image_url" => image, "profile_image_url" => image,
"profile_image_url_https" => image, "profile_image_url_https" => image,
"profile_image_url_profile_size" => image, "profile_image_url_profile_size" => image,
"profile_image_url_original" => image, "profile_image_url_original" => image,
"rights" => %{}, "rights" => %{},
"screen_name" => user.nickname,
"statuses_count" => user_info[:note_count],
"statusnet_profile_url" => user.ap_id "statusnet_profile_url" => user.ap_id
} }
end
map def render("short.json", %{user: %User{
nickname: nickname, id: id, ap_id: ap_id, name: name
}}) do
%{
"fullname" => name,
"id" => id,
"ostatus_uri" => ap_id,
"profile_url" => ap_id,
"screen_name" => nickname
}
end end
end end

View File

@ -1,9 +1,10 @@
defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
use Pleroma.DataCase use Pleroma.DataCase
alias Pleroma.{User, Activity, Object} alias Pleroma.{User, Activity, Object}
alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter, ObjectRepresenter} alias Pleroma.Web.TwitterAPI.Representers.{ActivityRepresenter, ObjectRepresenter}
alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Builders.UserBuilder alias Pleroma.Builders.UserBuilder
alias Pleroma.Web.TwitterAPI.UserView
import Pleroma.Factory import Pleroma.Factory
test "an announce activity" do test "an announce activity" do
@ -18,7 +19,7 @@ test "an announce activity" do
status = ActivityRepresenter.to_map(announce_activity, %{users: [user, activity_actor], announced_activity: note_activity, for: user}) status = ActivityRepresenter.to_map(announce_activity, %{users: [user, activity_actor], announced_activity: note_activity, for: user})
assert status["id"] == announce_activity.id assert status["id"] == announce_activity.id
assert status["user"] == UserRepresenter.to_map(user, %{for: user}) assert status["user"] == UserView.render("show.json", %{user: user, for: user})
retweeted_status = ActivityRepresenter.to_map(note_activity, %{user: activity_actor, for: user}) retweeted_status = ActivityRepresenter.to_map(note_activity, %{user: activity_actor, for: user})
assert retweeted_status["repeated"] == true assert retweeted_status["repeated"] == true
@ -105,9 +106,8 @@ test "an activity" do
expected_status = %{ expected_status = %{
"id" => activity.id, "id" => activity.id,
"user" => UserRepresenter.to_map(user, %{for: follower}), "user" => UserView.render("show.json", %{user: user, for: follower}),
"is_local" => true, "is_local" => true,
"attentions" => [],
"statusnet_html" => HtmlSanitizeEx.basic_html(content_html), "statusnet_html" => HtmlSanitizeEx.basic_html(content_html),
"text" => content, "text" => content,
"is_post_verb" => true, "is_post_verb" => true,
@ -118,7 +118,7 @@ test "an activity" do
ObjectRepresenter.to_map(object) ObjectRepresenter.to_map(object)
], ],
"attentions" => [ "attentions" => [
UserRepresenter.to_map(mentioned_user, %{for: follower}) UserView.render("show.json", %{user: mentioned_user, for: follower})
], ],
"fave_num" => 5, "fave_num" => 5,
"repeat_num" => 3, "repeat_num" => 3,

View File

@ -1,9 +1,10 @@
defmodule Pleroma.Web.TwitterAPI.ControllerTest do defmodule Pleroma.Web.TwitterAPI.ControllerTest do
use Pleroma.Web.ConnCase use Pleroma.Web.ConnCase
alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter} alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
alias Pleroma.Builders.{ActivityBuilder, UserBuilder} alias Pleroma.Builders.{ActivityBuilder, UserBuilder}
alias Pleroma.{Repo, Activity, User, Object} alias Pleroma.{Repo, Activity, User, Object}
alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.TwitterAPI.UserView
import Pleroma.Factory import Pleroma.Factory
@ -19,7 +20,7 @@ test "with credentials", %{conn: conn, user: user} do
|> with_credentials(user.nickname, "test") |> with_credentials(user.nickname, "test")
|> post("/api/account/verify_credentials.json") |> post("/api/account/verify_credentials.json")
assert json_response(conn, 200) == UserRepresenter.to_map(user) assert json_response(conn, 200) == UserView.render("show.json", %{user: user})
end end
end end
@ -229,7 +230,7 @@ test "with credentials", %{conn: conn, user: current_user} do
current_user = Repo.get(User, current_user.id) current_user = Repo.get(User, current_user.id)
assert current_user.following == [User.ap_followers(followed)] assert current_user.following == [User.ap_followers(followed)]
assert json_response(conn, 200) == UserRepresenter.to_map(followed, %{for: current_user}) assert json_response(conn, 200) == UserView.render("show.json", %{user: followed, for: current_user})
end end
end end
@ -253,7 +254,7 @@ test "with credentials", %{conn: conn, user: current_user} do
current_user = Repo.get(User, current_user.id) current_user = Repo.get(User, current_user.id)
assert current_user.following == [] assert current_user.following == []
assert json_response(conn, 200) == UserRepresenter.to_map(followed, %{for: current_user}) assert json_response(conn, 200) == UserView.render("show.json", %{user: followed, for: current_user})
end end
end end
@ -278,7 +279,7 @@ test "with credentials", %{conn: conn, user: current_user} do
current_user = Repo.get(User, current_user.id) current_user = Repo.get(User, current_user.id)
assert is_map(current_user.avatar) assert is_map(current_user.avatar)
assert json_response(conn, 200) == UserRepresenter.to_map(current_user, %{for: current_user}) assert json_response(conn, 200) == UserView.render("show.json", %{user: current_user, for: current_user})
end end
end end
@ -368,7 +369,7 @@ test "it creates a new user", %{conn: conn} do
user = json_response(conn, 200) user = json_response(conn, 200)
fetched_user = Repo.get_by(User, nickname: "lain") fetched_user = Repo.get_by(User, nickname: "lain")
assert user == UserRepresenter.to_map(fetched_user) assert user == UserView.render("show.json", %{user: fetched_user})
end end
test "it returns errors on a problem", %{conn: conn} do test "it returns errors on a problem", %{conn: conn} do
@ -396,7 +397,7 @@ test "it returns the user", %{conn: conn} do
conn = conn conn = conn
|> get("/api/externalprofile/show", %{profileurl: user.ap_id}) |> get("/api/externalprofile/show", %{profileurl: user.ap_id})
assert json_response(conn, 200) == UserRepresenter.to_map(user) assert json_response(conn, 200) == UserView.render("show.json", %{user: user})
end end
end end

View File

@ -1,10 +1,9 @@
defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
use Pleroma.DataCase use Pleroma.DataCase
alias Pleroma.Builders.{UserBuilder, ActivityBuilder} alias Pleroma.Builders.{UserBuilder, ActivityBuilder}
alias Pleroma.Web.TwitterAPI.TwitterAPI alias Pleroma.Web.TwitterAPI.{TwitterAPI,UserView,Utils}
alias Pleroma.Web.TwitterAPI.Utils
alias Pleroma.{Activity, User, Object, Repo} alias Pleroma.{Activity, User, Object, Repo}
alias Pleroma.Web.TwitterAPI.Representers.{ActivityRepresenter, UserRepresenter} alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.ActivityPub
import Pleroma.Factory import Pleroma.Factory
@ -303,7 +302,7 @@ test "it registers a new user and returns the user." do
{:ok, user} = TwitterAPI.register_user(data) {:ok, user} = TwitterAPI.register_user(data)
fetched_user = Repo.get_by(User, nickname: "lain") fetched_user = Repo.get_by(User, nickname: "lain")
assert user == UserRepresenter.to_map(fetched_user) assert UserView.render("show.json", %{user: user}) == UserView.render("show.json", %{user: fetched_user})
end end
test "it returns the error on registration problems" do test "it returns the error on registration problems" do
@ -358,7 +357,7 @@ test "fetches a user by uri" do
{:ok, represented} = TwitterAPI.get_external_profile(user, id) {:ok, represented} = TwitterAPI.get_external_profile(user, id)
remote = User.get_by_ap_id(id) remote = User.get_by_ap_id(id)
assert represented == UserRepresenter.to_map(remote, %{for: user}) assert represented == UserView.render("show.json", %{user: remote, for: user})
# Also fetches the feed. # Also fetches the feed.
assert Activity.get_create_activity_by_object_ap_id("tag:mastodon.social,2017-04-05:objectId=1641750:objectType=Status") assert Activity.get_create_activity_by_object_ap_id("tag:mastodon.social,2017-04-05:objectId=1641750:objectType=Status")

View File

@ -1,8 +1,8 @@
defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenterTest do defmodule Pleroma.Web.TwitterAPI.UserViewTest do
use Pleroma.DataCase use Pleroma.DataCase
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.TwitterAPI.Representers.UserRepresenter alias Pleroma.Web.TwitterAPI.{UserView, Utils}
alias Pleroma.Builders.UserBuilder alias Pleroma.Builders.UserBuilder
import Pleroma.Factory import Pleroma.Factory
@ -15,7 +15,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenterTest do
test "A user with an avatar object", %{user: user} do test "A user with an avatar object", %{user: user} do
image = "image" image = "image"
user = %{ user | avatar: %{ "url" => [%{"href" => image}] }} user = %{ user | avatar: %{ "url" => [%{"href" => image}] }}
represented = UserRepresenter.to_map(user) represented = UserView.render("show.json", %{user: user})
assert represented["profile_image_url"] == image assert represented["profile_image_url"] == image
end end
@ -33,15 +33,12 @@ test "A user" do
image = "https://placehold.it/48x48" image = "https://placehold.it/48x48"
created_at = user.inserted_at |> DateTime.from_naive!("Etc/UTC") |> UserRepresenter.format_asctime
represented = %{ represented = %{
"id" => user.id, "id" => user.id,
"name" => user.name, "name" => user.name,
"screen_name" => user.nickname, "screen_name" => user.nickname,
"description" => HtmlSanitizeEx.strip_tags(user.bio), "description" => HtmlSanitizeEx.strip_tags(user.bio),
"created_at" => created_at, "created_at" => user.inserted_at |> Utils.format_naive_asctime,
# Fake fields
"favourites_count" => 0, "favourites_count" => 0,
"statuses_count" => 1, "statuses_count" => 1,
"friends_count" => 1, "friends_count" => 1,
@ -55,20 +52,18 @@ test "A user" do
"statusnet_profile_url" => user.ap_id "statusnet_profile_url" => user.ap_id
} }
assert represented == UserRepresenter.to_map(user) assert represented == UserView.render("show.json", %{user: user})
end end
test "A user for a given other follower", %{user: user} do test "A user for a given other follower", %{user: user} do
{:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]}) {:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]})
image = "https://placehold.it/48x48" image = "https://placehold.it/48x48"
created_at = user.inserted_at |> DateTime.from_naive!("Etc/UTC") |> UserRepresenter.format_asctime
represented = %{ represented = %{
"id" => user.id, "id" => user.id,
"name" => user.name, "name" => user.name,
"screen_name" => user.nickname, "screen_name" => user.nickname,
"description" => HtmlSanitizeEx.strip_tags(user.bio), "description" => HtmlSanitizeEx.strip_tags(user.bio),
"created_at" => created_at, "created_at" => user.inserted_at |> Utils.format_naive_asctime,
# Fake fields
"favourites_count" => 0, "favourites_count" => 0,
"statuses_count" => 0, "statuses_count" => 0,
"friends_count" => 0, "friends_count" => 0,
@ -82,6 +77,6 @@ test "A user for a given other follower", %{user: user} do
"statusnet_profile_url" => user.ap_id "statusnet_profile_url" => user.ap_id
} }
assert represented == UserRepresenter.to_map(user, %{for: follower}) assert represented == UserView.render("show.json", %{user: user, for: follower})
end end
end end