Commit 069daf64 authored by dtluna's avatar dtluna

Replace UserRepresenter with UserView

parent 45c75198
defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ObjectRepresenter}
alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
alias Pleroma.{Activity, User}
alias Calendar.Strftime
alias Pleroma.Web.TwitterAPI.TwitterAPI
alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView, Utils}
alias Pleroma.Formatter
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,
%{users: users, announced_activity: announced_activity} = opts) do
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."
......@@ -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))
%{
"id" => activity.id,
"user" => UserRepresenter.to_map(user, opts),
"user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
"statusnet_html" => text,
"text" => text,
"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,
%{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."
%{
"id" => activity.id,
"user" => UserRepresenter.to_map(user, opts),
"user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
"statusnet_html" => text,
"text" => text,
"is_local" => true,
......@@ -55,13 +54,13 @@ def to_map(%Activity{data: %{"type" => "Like", "published" => created_at}} = act
end
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)
text = "#{user.nickname} started following #{followed.nickname}"
%{
"id" => activity.id,
"user" => UserRepresenter.to_map(user, opts),
"user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
"attentions" => [],
"statusnet_html" => text,
"text" => text,
......@@ -74,7 +73,7 @@ def to_map(%Activity{data: %{"type" => "Follow", "published" => created_at, "obj
end
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
announcement_count = object["announcement_count"] || 0
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"]
|> Enum.map(fn (ap_id) -> Enum.find(mentions, fn(user) -> ap_id == user.ap_id end) end)
|> 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)
%{
"id" => activity.id,
"user" => UserRepresenter.to_map(user, opts),
"attentions" => [],
"user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
"statusnet_html" => HtmlSanitizeEx.basic_html(content) |> Formatter.finmojifiy,
"text" => HtmlSanitizeEx.strip_tags(content),
"is_local" => true,
......
defmodule Pleroma.Web.TwitterAPI.Representers.BaseRepresenter do
defmacro __using__(_opts) do
quote do
alias Calendar.Strftime
def to_json(object) do to_json(object, %{}) end
def to_json(object, options) do
object
......@@ -24,18 +23,6 @@ def enum_to_json(enum, options) do
|> enum_to_list(options)
|> Poison.encode!
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
defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
alias Pleroma.{User, Activity, Repo, Object}
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.ActivityPub.Utils
alias Pleroma.Web.TwitterAPI.Representers.{ActivityRepresenter, UserRepresenter}
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
alias Pleroma.Web.TwitterAPI.UserView
alias Pleroma.Web.OStatus
alias Pleroma.Formatter
import Ecto.Query
import Pleroma.Web.TwitterAPI.Utils
@httpoison Application.get_env(:pleroma, :httpoison)
......@@ -197,7 +196,7 @@ def register_user(params) do
changeset = User.register_changeset(%User{}, params)
with {:ok, user} <- Repo.insert(changeset) do
{:ok, UserRepresenter.to_map(user)}
{:ok, user}
else
{:error, changeset} ->
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
OStatus.handle_incoming(body)
end
{:ok, UserRepresenter.to_map(user, %{for: for_user})}
{:ok, UserView.render("show.json", %{user: user, for: for_user})}
else _e ->
{:error, "Couldn't find user"}
end
......
defmodule Pleroma.Web.TwitterAPI.Controller do
use Pleroma.Web, :controller
alias Pleroma.Web.TwitterAPI.TwitterAPI
alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter}
alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView}
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
alias Pleroma.{Web, Repo, Activity}
alias Pleroma.Web.ActivityPub.ActivityPub
alias Ecto.Changeset
def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
response = user |> UserRepresenter.to_json(%{for: user})
conn
|> json_reply(200, response)
render(conn, UserView, "show.json", %{user: user})
end
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
case TwitterAPI.follow(user, params) do
{:ok, user, followed, _activity} ->
response = followed |> UserRepresenter.to_json(%{for: user})
conn
|> json_reply(200, response)
render(conn, UserView, "show.json", %{user: followed, for: user})
{:error, msg} -> forbidden_json_reply(conn, msg)
end
end
......@@ -100,9 +95,7 @@ def follow(%{assigns: %{user: user}} = conn, params) do
def unfollow(%{assigns: %{user: user}} = conn, params) do
case TwitterAPI.unfollow(user, params) do
{:ok, user, unfollowed} ->
response = unfollowed |> UserRepresenter.to_json(%{for: user})
conn
|> json_reply(200, response)
render(conn, UserView, "show.json", %{user: unfollowed, for: user})
{:error, msg} -> forbidden_json_reply(conn, msg)
end
end
......@@ -187,8 +180,8 @@ def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
def register(conn, 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
{:error, errors} ->
conn
......@@ -201,10 +194,7 @@ def update_avatar(%{assigns: %{user: user}} = conn, params) do
change = Changeset.change(user, %{avatar: object.data})
{:ok, user} = Repo.update(change)
response = Poison.encode!(UserRepresenter.to_map(user, %{for: user}))
conn
|> json_reply(200, response)
render(conn, UserView, "show.json", %{user: user, for: user})
end
def external_profile(%{assigns: %{user: current_user}} = conn, %{"profileurl" => uri}) do
......
defmodule Pleroma.Web.TwitterAPI.Utils do
alias Pleroma.{Repo, Object, Formatter, User, Activity}
alias Pleroma.Web.ActivityPub.Utils
alias Calendar.Strftime
def attachments_from_ids(ids) do
Enum.map(ids || [], fn (media_id) ->
......@@ -72,4 +73,20 @@ def make_note_data(actor, to, context, content_html, attachments, inReplyTo, tag
object
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
defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenter do
use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
defmodule Pleroma.Web.TwitterAPI.UserView do
use Pleroma.Web, :view
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)
following = if opts[:for] do
User.following?(opts[:for], user)
following = if assigns[:for] do
User.following?(assigns[:for], user)
else
false
end
user_info = User.get_cached_user_info(user)
created_at = user.inserted_at |> DateTime.from_naive!("Etc/UTC") |> format_asctime
map = %{
"id" => user.id,
"name" => user.name,
"screen_name" => user.nickname,
%{
"created_at" => user.inserted_at |> Utils.format_naive_asctime,
"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],
"following" => following,
"friends_count" => user_info[:following_count],
"id" => user.id,
"name" => user.name,
"profile_image_url" => image,
"profile_image_url_https" => image,
"profile_image_url_profile_size" => image,
"profile_image_url_original" => image,
"rights" => %{},
"screen_name" => user.nickname,
"statuses_count" => user_info[:note_count],
"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
defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
use Pleroma.DataCase
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.Builders.UserBuilder
alias Pleroma.Web.TwitterAPI.UserView
import Pleroma.Factory
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})
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})
assert retweeted_status["repeated"] == true
......@@ -105,9 +106,8 @@ test "an activity" do
expected_status = %{
"id" => activity.id,
"user" => UserRepresenter.to_map(user, %{for: follower}),
"user" => UserView.render("show.json", %{user: user, for: follower}),
"is_local" => true,
"attentions" => [],
"statusnet_html" => HtmlSanitizeEx.basic_html(content_html),
"text" => content,
"is_post_verb" => true,
......@@ -118,7 +118,7 @@ test "an activity" do
ObjectRepresenter.to_map(object)
],
"attentions" => [
UserRepresenter.to_map(mentioned_user, %{for: follower})
UserView.render("show.json", %{user: mentioned_user, for: follower})
],
"fave_num" => 5,
"repeat_num" => 3,
......
defmodule Pleroma.Web.TwitterAPI.ControllerTest do
use Pleroma.Web.ConnCase
alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter}
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
alias Pleroma.Builders.{ActivityBuilder, UserBuilder}
alias Pleroma.{Repo, Activity, User, Object}
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.TwitterAPI.UserView
import Pleroma.Factory
......@@ -19,7 +20,7 @@ test "with credentials", %{conn: conn, user: user} do
|> with_credentials(user.nickname, "test")
|> 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
......@@ -229,7 +230,7 @@ test "with credentials", %{conn: conn, user: current_user} do
current_user = Repo.get(User, current_user.id)
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
......@@ -253,7 +254,7 @@ test "with credentials", %{conn: conn, user: current_user} do
current_user = Repo.get(User, current_user.id)
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
......@@ -278,7 +279,7 @@ test "with credentials", %{conn: conn, user: current_user} do
current_user = Repo.get(User, current_user.id)
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
......@@ -368,7 +369,7 @@ test "it creates a new user", %{conn: conn} do
user = json_response(conn, 200)
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
test "it returns errors on a problem", %{conn: conn} do
......@@ -396,7 +397,7 @@ test "it returns the user", %{conn: conn} do
conn = conn
|> 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
......
defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
use Pleroma.DataCase
alias Pleroma.Builders.{UserBuilder, ActivityBuilder}
alias Pleroma.Web.TwitterAPI.TwitterAPI
alias Pleroma.Web.TwitterAPI.Utils
alias Pleroma.Web.TwitterAPI.{TwitterAPI,UserView,Utils}
alias Pleroma.{Activity, User, Object, Repo}
alias Pleroma.Web.TwitterAPI.Representers.{ActivityRepresenter, UserRepresenter}
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
alias Pleroma.Web.ActivityPub.ActivityPub
import Pleroma.Factory
......@@ -303,7 +302,7 @@ test "it registers a new user and returns the user." do
{:ok, user} = TwitterAPI.register_user(data)
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
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)
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.
assert Activity.get_create_activity_by_object_ap_id("tag:mastodon.social,2017-04-05:objectId=1641750:objectType=Status")
......
defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenterTest do
defmodule Pleroma.Web.TwitterAPI.UserViewTest do
use Pleroma.DataCase
alias Pleroma.User
alias Pleroma.Web.TwitterAPI.Representers.UserRepresenter
alias Pleroma.Web.TwitterAPI.{UserView, Utils}
alias Pleroma.Builders.UserBuilder
import Pleroma.Factory
......@@ -15,7 +15,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenterTest do
test "A user with an avatar object", %{user: user} do
image = "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
end
......@@ -33,15 +33,12 @@ test "A user" do
image = "https://placehold.it/48x48"
created_at = user.inserted_at |> DateTime.from_naive!("Etc/UTC") |> UserRepresenter.format_asctime
represented = %{
"id" => user.id,
"name" => user.name,
"screen_name" => user.nickname,
"description" => HtmlSanitizeEx.strip_tags(user.bio),
"created_at" => created_at,
# Fake fields
"created_at" => user.inserted_at |> Utils.format_naive_asctime,
"favourites_count" => 0,
"statuses_count" => 1,
"friends_count" => 1,
......@@ -55,20 +52,18 @@ test "A user" do
"statusnet_profile_url" => user.ap_id
}
assert represented == UserRepresenter.to_map(user)
assert represented == UserView.render("show.json", %{user: 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"
created_at = user.inserted_at |> DateTime.from_naive!("Etc/UTC") |> UserRepresenter.format_asctime
represented = %{
"id" => user.id,
"name" => user.name,
"screen_name" => user.nickname,
"description" => HtmlSanitizeEx.strip_tags(user.bio),
"created_at" => created_at,
# Fake fields
"created_at" => user.inserted_at |> Utils.format_naive_asctime,
"favourites_count" => 0,
"statuses_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
}
assert represented == UserRepresenter.to_map(user, %{for: follower})
assert represented == UserView.render("show.json", %{user: user, for: follower})
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment