Replace ActivityRepresenter with StatusView and ObjectRepresenter with AttachmentView
This commit is contained in:
parent
3d714f85a8
commit
34d3a90b63
|
@ -1,130 +0,0 @@
|
|||
defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
|
||||
use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
|
||||
alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
|
||||
alias Pleroma.{Activity, User}
|
||||
alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView, Utils}
|
||||
alias Pleroma.Formatter
|
||||
|
||||
defp user_by_ap_id(user_list, ap_id) do
|
||||
Enum.find(user_list, fn (%{ap_id: user_id}) -> ap_id == user_id end)
|
||||
end
|
||||
|
||||
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 |> Utils.date_to_asctime
|
||||
|
||||
text = "#{user.nickname} retweeted a status."
|
||||
|
||||
announced_user = user_by_ap_id(users, announced_activity.data["actor"])
|
||||
retweeted_status = to_map(announced_activity, Map.merge(%{user: announced_user}, opts))
|
||||
%{
|
||||
"id" => activity.id,
|
||||
"user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
|
||||
"statusnet_html" => text,
|
||||
"text" => text,
|
||||
"is_local" => true,
|
||||
"is_post_verb" => false,
|
||||
"uri" => "tag:#{activity.data["id"]}:objectType=note",
|
||||
"created_at" => created_at,
|
||||
"retweeted_status" => retweeted_status,
|
||||
"statusnet_conversation_id" => conversation_id(announced_activity),
|
||||
"external_url" => activity.data["id"]
|
||||
}
|
||||
end
|
||||
|
||||
def to_map(%Activity{data: %{"type" => "Like", "published" => created_at}} = activity,
|
||||
%{user: user, liked_activity: liked_activity} = opts) do
|
||||
created_at = created_at |> Utils.date_to_asctime
|
||||
|
||||
text = "#{user.nickname} favorited a status."
|
||||
|
||||
%{
|
||||
"id" => activity.id,
|
||||
"user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
|
||||
"statusnet_html" => text,
|
||||
"text" => text,
|
||||
"is_local" => true,
|
||||
"is_post_verb" => false,
|
||||
"uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
|
||||
"created_at" => created_at,
|
||||
"in_reply_to_status_id" => liked_activity.id,
|
||||
"external_url" => activity.data["id"]
|
||||
}
|
||||
end
|
||||
|
||||
def to_map(%Activity{data: %{"type" => "Follow", "published" => created_at, "object" => followed_id}} = activity, %{user: user} = opts) do
|
||||
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" => UserView.render("show.json", %{user: user, for: opts[:for]}),
|
||||
"attentions" => [],
|
||||
"statusnet_html" => text,
|
||||
"text" => text,
|
||||
"is_local" => true,
|
||||
"is_post_verb" => false,
|
||||
"created_at" => created_at,
|
||||
"in_reply_to_status_id" => nil,
|
||||
"external_url" => activity.data["id"]
|
||||
}
|
||||
end
|
||||
|
||||
def to_map(%Activity{data: %{"object" => %{"content" => content} = object}} = activity, %{user: user} = opts) do
|
||||
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"] || [])
|
||||
repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
|
||||
|
||||
mentions = opts[:mentioned] || []
|
||||
|
||||
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) -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
|
||||
|
||||
conversation_id = conversation_id(activity)
|
||||
|
||||
%{
|
||||
"id" => activity.id,
|
||||
"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,
|
||||
"is_post_verb" => true,
|
||||
"created_at" => created_at,
|
||||
"in_reply_to_status_id" => object["inReplyToStatusId"],
|
||||
"statusnet_conversation_id" => conversation_id,
|
||||
"attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
|
||||
"attentions" => attentions,
|
||||
"fave_num" => like_count,
|
||||
"repeat_num" => announcement_count,
|
||||
"favorited" => to_boolean(favorited),
|
||||
"repeated" => to_boolean(repeated),
|
||||
"external_url" => activity.data["id"],
|
||||
"tags" => activity.data["object"]["tag"] || []
|
||||
}
|
||||
end
|
||||
|
||||
def conversation_id(activity) do
|
||||
with context when not is_nil(context) <- activity.data["context"] do
|
||||
TwitterAPI.context_to_conversation_id(context)
|
||||
else _e -> nil
|
||||
end
|
||||
end
|
||||
|
||||
defp to_boolean(false) do
|
||||
false
|
||||
end
|
||||
|
||||
defp to_boolean(nil) do
|
||||
false
|
||||
end
|
||||
|
||||
defp to_boolean(_) do
|
||||
true
|
||||
end
|
||||
end
|
|
@ -1,28 +0,0 @@
|
|||
defmodule Pleroma.Web.TwitterAPI.Representers.BaseRepresenter do
|
||||
defmacro __using__(_opts) do
|
||||
quote do
|
||||
def to_json(object) do to_json(object, %{}) end
|
||||
def to_json(object, options) do
|
||||
object
|
||||
|> to_map(options)
|
||||
|> Poison.encode!
|
||||
end
|
||||
|
||||
def enum_to_list(enum, options) do
|
||||
mapping = fn (el) -> to_map(el, options) end
|
||||
Enum.map(enum, mapping)
|
||||
end
|
||||
|
||||
def to_map(object) do
|
||||
to_map(object, %{})
|
||||
end
|
||||
|
||||
def enum_to_json(enum) do enum_to_json(enum, %{}) end
|
||||
def enum_to_json(enum, options) do
|
||||
enum
|
||||
|> enum_to_list(options)
|
||||
|> Poison.encode!
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,20 +0,0 @@
|
|||
defmodule Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter do
|
||||
use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
|
||||
alias Pleroma.Object
|
||||
|
||||
def to_map(%Object{} = object, _opts) do
|
||||
data = object.data
|
||||
url = List.first(data["url"])
|
||||
%{
|
||||
url: url["href"],
|
||||
mimetype: url["mediaType"],
|
||||
id: data["uuid"],
|
||||
oembed: false
|
||||
}
|
||||
end
|
||||
|
||||
# If we only get the naked data, wrap in an object
|
||||
def to_map(%{} = data, opts) do
|
||||
to_map(%Object{data: data}, opts)
|
||||
end
|
||||
end
|
|
@ -1,8 +1,7 @@
|
|||
defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
||||
alias Pleroma.{User, Activity, Repo, Object}
|
||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
|
||||
alias Pleroma.Web.TwitterAPI.UserView
|
||||
alias Pleroma.Web.TwitterAPI.{UserView, Utils}
|
||||
alias Pleroma.Web.OStatus
|
||||
alias Pleroma.Formatter
|
||||
|
||||
|
@ -43,49 +42,6 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
|||
end
|
||||
end
|
||||
|
||||
def fetch_friend_statuses(user, opts \\ %{}) do
|
||||
ActivityPub.fetch_activities([user.ap_id | user.following], opts)
|
||||
|> activities_to_statuses(%{for: user})
|
||||
end
|
||||
|
||||
def fetch_public_statuses(user, opts \\ %{}) do
|
||||
opts = Map.put(opts, "local_only", true)
|
||||
ActivityPub.fetch_public_activities(opts)
|
||||
|> activities_to_statuses(%{for: user})
|
||||
end
|
||||
|
||||
def fetch_public_and_external_statuses(user, opts \\ %{}) do
|
||||
ActivityPub.fetch_public_activities(opts)
|
||||
|> activities_to_statuses(%{for: user})
|
||||
end
|
||||
|
||||
def fetch_user_statuses(user, opts \\ %{}) do
|
||||
ActivityPub.fetch_activities([], opts)
|
||||
|> activities_to_statuses(%{for: user})
|
||||
end
|
||||
|
||||
def fetch_mentions(user, opts \\ %{}) do
|
||||
ActivityPub.fetch_activities([user.ap_id], opts)
|
||||
|> activities_to_statuses(%{for: user})
|
||||
end
|
||||
|
||||
def fetch_conversation(user, id) do
|
||||
with context when is_binary(context) <- conversation_id_to_context(id),
|
||||
activities <- ActivityPub.fetch_activities_for_context(context),
|
||||
statuses <- activities |> activities_to_statuses(%{for: user})
|
||||
do
|
||||
statuses
|
||||
else _e ->
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
def fetch_status(user, id) do
|
||||
with %Activity{} = activity <- Repo.get(Activity, id) do
|
||||
activity_to_status(activity, %{for: user})
|
||||
end
|
||||
end
|
||||
|
||||
def follow(%User{} = follower, params) do
|
||||
with {:ok, %User{} = followed} <- get_user(params),
|
||||
{:ok, follower} <- User.follow(follower, followed),
|
||||
|
@ -104,7 +60,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
|||
"type" => "Undo",
|
||||
"actor" => follower.ap_id,
|
||||
"object" => follow_activity.data["id"], # get latest Follow for these users
|
||||
"published" => make_date()
|
||||
"published" => Utils.make_date()
|
||||
})
|
||||
do
|
||||
{ :ok, follower, unfollowed }
|
||||
|
@ -121,8 +77,6 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
|||
|> Map.put("object", object.data)
|
||||
|
||||
status = %{activity | data: new_data}
|
||||
|> activity_to_status(%{for: user})
|
||||
|
||||
{:ok, status}
|
||||
end
|
||||
|
||||
|
@ -134,8 +88,6 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
|||
|> Map.put("object", object.data)
|
||||
|
||||
status = %{activity | data: new_data}
|
||||
|> activity_to_status(%{for: user})
|
||||
|
||||
{:ok, status}
|
||||
end
|
||||
|
||||
|
@ -147,8 +99,6 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
|||
|> Map.put("object", object.data)
|
||||
|
||||
status = %{activity | data: new_data}
|
||||
|> activity_to_status(%{for: user})
|
||||
|
||||
{:ok, status}
|
||||
end
|
||||
|
||||
|
@ -238,47 +188,6 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
|||
end
|
||||
end
|
||||
|
||||
defp activities_to_statuses(activities, opts) do
|
||||
Enum.map(activities, fn(activity) ->
|
||||
activity_to_status(activity, opts)
|
||||
end)
|
||||
end
|
||||
|
||||
# For likes, fetch the liked activity, too.
|
||||
defp activity_to_status(%Activity{data: %{"type" => "Like"}} = activity, opts) do
|
||||
actor = get_in(activity.data, ["actor"])
|
||||
user = User.get_cached_by_ap_id(actor)
|
||||
[liked_activity] = Activity.all_by_object_ap_id(activity.data["object"])
|
||||
|
||||
ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user, liked_activity: liked_activity}))
|
||||
end
|
||||
|
||||
# For announces, fetch the announced activity and the user.
|
||||
defp activity_to_status(%Activity{data: %{"type" => "Announce"}} = activity, opts) do
|
||||
actor = get_in(activity.data, ["actor"])
|
||||
user = User.get_cached_by_ap_id(actor)
|
||||
[announced_activity] = Activity.all_by_object_ap_id(activity.data["object"])
|
||||
announced_actor = User.get_cached_by_ap_id(announced_activity.data["actor"])
|
||||
|
||||
ActivityRepresenter.to_map(activity, Map.merge(opts, %{users: [user, announced_actor], announced_activity: announced_activity}))
|
||||
end
|
||||
|
||||
defp activity_to_status(activity, opts) do
|
||||
actor = get_in(activity.data, ["actor"])
|
||||
user = User.get_cached_by_ap_id(actor)
|
||||
# mentioned_users = Repo.all(from user in User, where: user.ap_id in ^activity.data["to"])
|
||||
mentioned_users = Enum.map(activity.data["to"] || [], fn (ap_id) ->
|
||||
User.get_cached_by_ap_id(ap_id)
|
||||
end)
|
||||
|> Enum.filter(&(&1))
|
||||
|
||||
ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user, mentioned: mentioned_users}))
|
||||
end
|
||||
|
||||
defp make_date do
|
||||
DateTime.utc_now() |> DateTime.to_iso8601
|
||||
end
|
||||
|
||||
def context_to_conversation_id(context) do
|
||||
with %Object{id: id} <- Object.get_cached_by_ap_id(context) do
|
||||
id
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
defmodule Pleroma.Web.TwitterAPI.Controller do
|
||||
use Pleroma.Web, :controller
|
||||
alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView}
|
||||
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
|
||||
alias Pleroma.Web.TwitterAPI.{StatusView, TwitterAPI, UserView}
|
||||
alias Pleroma.{Web, Repo, Activity}
|
||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||
alias Ecto.Changeset
|
||||
|
@ -15,8 +14,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
|
|||
if l > 0 && l < 5000 do
|
||||
media_ids = extract_media_ids(status_data)
|
||||
{:ok, activity} = TwitterAPI.create_status(user, Map.put(status_data, "media_ids", media_ids))
|
||||
conn
|
||||
|> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user}))
|
||||
render(conn, StatusView, "show.json", %{activity: activity})
|
||||
else
|
||||
empty_status_reply(conn)
|
||||
end
|
||||
|
@ -40,48 +38,36 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
|
|||
end
|
||||
end
|
||||
|
||||
def public_and_external_timeline(%{assigns: %{user: user}} = conn, params) do
|
||||
statuses = TwitterAPI.fetch_public_and_external_statuses(user, params)
|
||||
{:ok, json} = Poison.encode(statuses)
|
||||
|
||||
conn
|
||||
|> json_reply(200, json)
|
||||
def public_and_external_timeline(conn, params) do
|
||||
activities = ActivityPub.fetch_public_activities(params)
|
||||
render(conn, StatusView, "timeline.json", %{activities: activities})
|
||||
end
|
||||
|
||||
def public_timeline(%{assigns: %{user: user}} = conn, params) do
|
||||
statuses = TwitterAPI.fetch_public_statuses(user, params)
|
||||
{:ok, json} = Poison.encode(statuses)
|
||||
|
||||
conn
|
||||
|> json_reply(200, json)
|
||||
def public_timeline(conn, params) do
|
||||
params = Map.put(params, "local_only", true)
|
||||
activities = ActivityPub.fetch_public_activities(params)
|
||||
render(conn, StatusView, "timeline.json", %{activities: activities})
|
||||
end
|
||||
|
||||
def friends_timeline(%{assigns: %{user: user}} = conn, params) do
|
||||
statuses = TwitterAPI.fetch_friend_statuses(user, params)
|
||||
{:ok, json} = Poison.encode(statuses)
|
||||
|
||||
conn
|
||||
|> json_reply(200, json)
|
||||
activities = ActivityPub.fetch_activities([user.ap_id | user.following], params)
|
||||
render(conn, StatusView, "timeline.json", %{activities: activities})
|
||||
end
|
||||
|
||||
def user_timeline(%{assigns: %{user: user}} = conn, params) do
|
||||
case TwitterAPI.get_user(user, params) do
|
||||
{:ok, target_user} ->
|
||||
params = Map.merge(params, %{"actor_id" => target_user.ap_id})
|
||||
statuses = TwitterAPI.fetch_user_statuses(user, params)
|
||||
conn
|
||||
|> json_reply(200, statuses |> Poison.encode!)
|
||||
activities = ActivityPub.fetch_activities([], params)
|
||||
render(conn, StatusView, "timeline.json", %{activities: activities, for: user})
|
||||
{:error, msg} ->
|
||||
bad_request_reply(conn, msg)
|
||||
end
|
||||
end
|
||||
|
||||
def mentions_timeline(%{assigns: %{user: user}} = conn, params) do
|
||||
statuses = TwitterAPI.fetch_mentions(user, params)
|
||||
{:ok, json} = Poison.encode(statuses)
|
||||
|
||||
conn
|
||||
|> json_reply(200, json)
|
||||
activities = ActivityPub.fetch_activities([user.ap_id], params)
|
||||
render(conn, StatusView, "timeline.json", %{activities: activities, for: user})
|
||||
end
|
||||
|
||||
def follow(%{assigns: %{user: user}} = conn, params) do
|
||||
|
@ -101,18 +87,20 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
|
|||
end
|
||||
|
||||
def fetch_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|
||||
response = Poison.encode!(TwitterAPI.fetch_status(user, id))
|
||||
|
||||
conn
|
||||
|> json_reply(200, response)
|
||||
with %Activity{} = activity <- Repo.get(Activity, id) do
|
||||
render(conn, StatusView, "show.json", %{activity: activity, for: user})
|
||||
end
|
||||
end
|
||||
|
||||
def fetch_conversation(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|
||||
id = String.to_integer(id)
|
||||
response = Poison.encode!(TwitterAPI.fetch_conversation(user, id))
|
||||
|
||||
conn
|
||||
|> json_reply(200, response)
|
||||
with context when is_binary(context) <- TwitterAPI.conversation_id_to_context(id),
|
||||
activities <- ActivityPub.fetch_activities_for_context(context)
|
||||
do
|
||||
render(conn, StatusView, "timeline.json", %{activities: activities, for: user})
|
||||
else _e ->
|
||||
json(conn, [])
|
||||
end
|
||||
end
|
||||
|
||||
def upload(conn, %{"media" => media}) do
|
||||
|
@ -123,7 +111,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
|
|||
end
|
||||
|
||||
def help_test(conn, _params) do
|
||||
conn |> json_reply(200, Poison.encode!("ok"))
|
||||
json(conn, "ok")
|
||||
end
|
||||
|
||||
def upload_json(conn, %{"media" => media}) do
|
||||
|
@ -133,35 +121,25 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
|
|||
end
|
||||
|
||||
def config(conn, _params) do
|
||||
response = %{
|
||||
json(conn, %{
|
||||
site: %{
|
||||
name: Web.base_url,
|
||||
server: Web.base_url,
|
||||
textlimit: -1
|
||||
}
|
||||
}
|
||||
|> Poison.encode!
|
||||
|
||||
conn
|
||||
|> json_reply(200, response)
|
||||
})
|
||||
end
|
||||
|
||||
def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|
||||
activity = Repo.get(Activity, id)
|
||||
{:ok, status} = TwitterAPI.favorite(user, activity)
|
||||
response = Poison.encode!(status)
|
||||
|
||||
conn
|
||||
|> json_reply(200, response)
|
||||
{:ok, activity} = TwitterAPI.favorite(user, activity)
|
||||
render(conn, StatusView, "show.json", %{activity: activity, for: user})
|
||||
end
|
||||
|
||||
def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|
||||
activity = Repo.get(Activity, id)
|
||||
{:ok, status} = TwitterAPI.unfavorite(user, activity)
|
||||
response = Poison.encode!(status)
|
||||
|
||||
conn
|
||||
|> json_reply(200, response)
|
||||
{:ok, activity} = TwitterAPI.unfavorite(user, activity)
|
||||
render(conn, StatusView, "show.json", %{activity: activity, for: user})
|
||||
end
|
||||
|
||||
def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|
||||
|
@ -169,18 +147,13 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
|
|||
if activity.data["actor"] == user.ap_id do
|
||||
bad_request_reply(conn, "You cannot repeat your own notice.")
|
||||
else
|
||||
{:ok, status} = TwitterAPI.retweet(user, activity)
|
||||
response = Poison.encode!(status)
|
||||
|
||||
conn
|
||||
|
||||
|> json_reply(200, response)
|
||||
{:ok, activity} = TwitterAPI.retweet(user, activity)
|
||||
render(conn, StatusView, "show.json", %{activity: activity, for: user})
|
||||
end
|
||||
end
|
||||
|
||||
def register(conn, params) do
|
||||
with {:ok, user} <- TwitterAPI.register_user(params) do
|
||||
|
||||
render(conn, UserView, "show.json", %{user: user})
|
||||
else
|
||||
{:error, errors} ->
|
||||
|
|
|
@ -89,4 +89,14 @@ defmodule Pleroma.Web.TwitterAPI.Utils do
|
|||
""
|
||||
end
|
||||
end
|
||||
|
||||
def make_date do
|
||||
DateTime.utc_now() |> DateTime.to_iso8601
|
||||
end
|
||||
|
||||
def to_boolean(false), do: false
|
||||
|
||||
def to_boolean(nil), do: false
|
||||
|
||||
def to_boolean(_), do: true
|
||||
end
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
defmodule Pleroma.Web.TwitterAPI.AttachmentView do
|
||||
use Pleroma.Web, :view
|
||||
alias Pleroma.Object
|
||||
|
||||
def render("show.json", %{attachment: %Object{data: data}}) do
|
||||
url = List.first(data["url"])
|
||||
%{
|
||||
url: url["href"],
|
||||
mimetype: url["mediaType"],
|
||||
id: data["uuid"],
|
||||
oembed: false
|
||||
}
|
||||
end
|
||||
end
|
|
@ -0,0 +1,149 @@
|
|||
defmodule Pleroma.Web.TwitterAPI.StatusView do
|
||||
use Pleroma.Web, :view
|
||||
alias Pleroma.{Activity, User, Utils}
|
||||
alias Pleroma.Web.TwitterAPI.{AttachmentView, TwitterAPI, UserView, Utils}
|
||||
|
||||
def render(
|
||||
"show.json",
|
||||
%{
|
||||
activity: %Activity{
|
||||
data: %{"type" => "Announce", "id" => id, "object" => ap_id}
|
||||
},
|
||||
} = assigns) do
|
||||
{activity, user} = render_activity(assigns)
|
||||
|
||||
[announced_activity = %Activity{}] = Activity.all_by_object_ap_id(ap_id)
|
||||
text = "#{user.nickname} retweeted a status."
|
||||
retweeted_status = render("show.json",
|
||||
Map.merge(assigns, %{activity: announced_activity})
|
||||
)
|
||||
|
||||
Map.merge(activity, %{
|
||||
"retweeted_status" => retweeted_status,
|
||||
"statusnet_html" => text,
|
||||
"text" => text,
|
||||
"uri" => "tag:#{id}:objectType=note"
|
||||
})
|
||||
end
|
||||
|
||||
def render(
|
||||
"show.json",
|
||||
%{activity: %Activity{
|
||||
data: %{"type" => "Like", "id" => id, "object" => liked_id}
|
||||
},
|
||||
} = assigns) do
|
||||
{activity, %User{nickname: nickname}} = render_activity(assigns)
|
||||
text = "#{nickname} favorited a status."
|
||||
[%Activity{id: liked_activity_id}] = Activity.all_by_object_ap_id(liked_id)
|
||||
|
||||
Map.merge(activity, %{
|
||||
"in_reply_to_status_id" => liked_activity_id,
|
||||
"statusnet_html" => text,
|
||||
"text" => text,
|
||||
"uri" => "tag#{id}:objectType=Favorite"
|
||||
})
|
||||
end
|
||||
|
||||
def render(
|
||||
"show.json",
|
||||
%{
|
||||
activity: %Activity{
|
||||
data: %{"type" => "Follow", "object" => followed_id}
|
||||
}
|
||||
} = assigns
|
||||
) do
|
||||
{activity, %User{nickname: follower_name}} = render_activity(assigns)
|
||||
%User{nickname: followed_name} = User.get_cached_by_ap_id(followed_id)
|
||||
text = "#{follower_name} started following #{followed_name}"
|
||||
|
||||
Map.merge(activity, %{
|
||||
"statusnet_html" => text,
|
||||
"text" => text
|
||||
})
|
||||
end
|
||||
|
||||
def render(
|
||||
"show.json",
|
||||
%{
|
||||
activity: %Activity{
|
||||
data: %{
|
||||
"type" => "Create", "to" => to,
|
||||
"object" => %{
|
||||
"content" => content
|
||||
} = object
|
||||
}
|
||||
}
|
||||
} = assigns
|
||||
) do
|
||||
announcement_count = object["announcement_count"] || 0
|
||||
repeated = Utils.to_boolean(assigns[:for] && assigns[:for].ap_id in (object["announcements"] || []))
|
||||
|
||||
like_count = object["like_count"] || 0
|
||||
favorited = Utils.to_boolean(assigns[:for] && assigns[:for].ap_id in (object["likes"] || []))
|
||||
|
||||
mentions = to
|
||||
|> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
|
||||
|> Enum.filter(&Utils.to_boolean/1)
|
||||
|
||||
attentions = to
|
||||
|> Enum.map(fn (ap_id) -> Enum.find(mentions, fn(user) -> ap_id == user.ap_id end) end)
|
||||
|> Enum.filter(&Utils.to_boolean/1)
|
||||
|> Enum.map(fn (user) -> UserView.render("short.json", Map.merge(assigns, %{user: user})) end)
|
||||
|
||||
attachments = (object["attachment"] || [])
|
||||
{activity, _user} = render_activity(assigns)
|
||||
Map.merge(activity, %{
|
||||
"attachments" => render_many(attachments, AttachmentView, "show.json"),
|
||||
"attentions" => attentions,
|
||||
"fave_num" => like_count,
|
||||
"favorited" => favorited,
|
||||
"in_reply_to_status_id" => object["inReplyToStatusId"],
|
||||
"is_post_verb" => true,
|
||||
"repeat_num" => announcement_count,
|
||||
"repeated" => repeated,
|
||||
"statusnet_html" => content,
|
||||
"text" => HtmlSanitizeEx.strip_tags(content)
|
||||
})
|
||||
end
|
||||
|
||||
def render("timeline.json", %{activities: activities} = assigns) do
|
||||
render_many(activities, Pleroma.Web.TwitterAPI.StatusView, "show.json",
|
||||
Map.merge(assigns, %{as: :activity}))
|
||||
end
|
||||
|
||||
def conversation_id(%Activity{data: %{"context" => context}}) do
|
||||
if context do
|
||||
TwitterAPI.context_to_conversation_id(context)
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def conversation_id(%Activity{}), do: nil
|
||||
|
||||
defp render_activity(assigns = %{
|
||||
activity: activity = %Activity{
|
||||
id: activity_id,
|
||||
data: %{"published" => created_at, "id" => external_url, "actor" => actor_id}
|
||||
},
|
||||
}) do
|
||||
user = %User{} = User.get_cached_by_ap_id(actor_id)
|
||||
{%{
|
||||
"attachments" => [],
|
||||
"attentions" => [],
|
||||
"created_at" => created_at |> Utils.date_to_asctime,
|
||||
"external_url" => external_url,
|
||||
"fave_num" => 0,
|
||||
"favorited" => false,
|
||||
"id" => activity_id,
|
||||
"in_reply_to_status_id" => nil,
|
||||
"is_local" => true,
|
||||
"is_post_verb" => false,
|
||||
"repeat_num" => 0,
|
||||
"repeated" => false,
|
||||
"statusnet_conversation_id" => conversation_id(activity),
|
||||
"source" => "api",
|
||||
"user" => UserView.render("show.json", Map.merge(assigns, %{user: user}))
|
||||
}, user}
|
||||
end
|
||||
end
|
|
@ -11,7 +11,8 @@ defmodule Pleroma.Builders.ActivityBuilder do
|
|||
"object" => %{
|
||||
"type" => "Note",
|
||||
"content" => "test"
|
||||
}
|
||||
},
|
||||
"type" => "Create"
|
||||
}
|
||||
Map.merge(activity, data)
|
||||
end
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|
||||
use Pleroma.Web.ConnCase
|
||||
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
|
||||
alias Pleroma.Web.TwitterAPI.{StatusView, UserView}
|
||||
|
||||
import Pleroma.Factory
|
||||
|
||||
|
@ -47,7 +46,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|
|||
assert json_response(conn, 400) == error_response
|
||||
|
||||
conn = conn_with_creds |> post(request_path, %{ status: "Nice meme." })
|
||||
assert json_response(conn, 200) == ActivityRepresenter.to_map(Repo.one(Activity), %{user: user})
|
||||
assert json_response(conn, 200) == StatusView.render("show.json", %{activity: Repo.one(Activity), user: user})
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -69,16 +68,14 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|
|||
|
||||
describe "GET /statuses/show/:id.json" do
|
||||
test "returns one status", %{conn: conn} do
|
||||
{:ok, user} = UserBuilder.insert
|
||||
{:ok, activity} = ActivityBuilder.insert(%{}, %{user: user})
|
||||
actor = Repo.get_by!(User, ap_id: activity.data["actor"])
|
||||
activity = insert(:note_activity)
|
||||
|
||||
conn = conn
|
||||
|> get("/api/statuses/show/#{activity.id}.json")
|
||||
|
||||
response = json_response(conn, 200)
|
||||
|
||||
assert response == ActivityRepresenter.to_map(activity, %{user: actor})
|
||||
assert response == StatusView.render("show.json", %{activity: activity})
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -123,7 +120,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|
|||
response = json_response(conn, 200)
|
||||
|
||||
assert length(response) == 10
|
||||
assert response == Enum.map(returned_activities, fn (activity) -> ActivityRepresenter.to_map(activity, %{user: User.get_cached_by_ap_id(activity.data["actor"]), for: current_user}) end)
|
||||
assert response == Enum.map(returned_activities, fn (activity) -> StatusView.render("show.json", %{activity: activity, for: current_user}) end)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -144,7 +141,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|
|||
response = json_response(conn, 200)
|
||||
|
||||
assert length(response) == 1
|
||||
assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: current_user, mentioned: [current_user]})
|
||||
assert Enum.at(response, 0) == StatusView.render("show.json", %{activity: activity})
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -156,13 +153,13 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|
|||
end
|
||||
|
||||
test "with user_id", %{conn: conn} do
|
||||
user = insert(:user)
|
||||
{:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
|
||||
%Activity{data: %{"actor" => actor_id}} = activity = insert(:note_activity)
|
||||
user = User.get_by_ap_id(actor_id)
|
||||
|
||||
conn = get(conn, "/api/statuses/user_timeline.json", %{"user_id" => user.id})
|
||||
response = json_response(conn, 200)
|
||||
assert length(response) == 1
|
||||
assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
|
||||
assert Enum.at(response, 0) == StatusView.render("show.json", %{activity: activity})
|
||||
end
|
||||
|
||||
test "with screen_name", %{conn: conn} do
|
||||
|
@ -172,7 +169,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|
|||
conn = get(conn, "/api/statuses/user_timeline.json", %{"screen_name" => user.nickname})
|
||||
response = json_response(conn, 200)
|
||||
assert length(response) == 1
|
||||
assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
|
||||
assert Enum.at(response, 0) == StatusView.render("show.json", %{activity: activity})
|
||||
end
|
||||
|
||||
test "with credentials", %{conn: conn, user: current_user} do
|
||||
|
@ -184,7 +181,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|
|||
response = json_response(conn, 200)
|
||||
|
||||
assert length(response) == 1
|
||||
assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: current_user})
|
||||
assert Enum.at(response, 0) == StatusView.render("show.json", %{activity: activity})
|
||||
end
|
||||
|
||||
test "with credentials with user_id", %{conn: conn, user: current_user} do
|
||||
|
@ -197,7 +194,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|
|||
response = json_response(conn, 200)
|
||||
|
||||
assert length(response) == 1
|
||||
assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
|
||||
assert Enum.at(response, 0) == StatusView.render("show.json", %{activity: activity})
|
||||
end
|
||||
|
||||
test "with credentials screen_name", %{conn: conn, user: current_user} do
|
||||
|
@ -210,7 +207,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|
|||
response = json_response(conn, 200)
|
||||
|
||||
assert length(response) == 1
|
||||
assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
|
||||
assert Enum.at(response, 0) == StatusView.render("show.json", %{activity: activity})
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -347,8 +344,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|
|||
|> with_credentials(current_user.nickname, "test")
|
||||
|> post(request_path)
|
||||
activity = Repo.get(Activity, note_activity.id)
|
||||
activity_user = Repo.get_by(User, ap_id: note_activity.data["actor"])
|
||||
assert json_response(response, 200) == ActivityRepresenter.to_map(activity, %{user: activity_user, for: current_user})
|
||||
assert json_response(response, 200) == StatusView.render("show.json", %{activity: activity, for: current_user})
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
|
||||
use Pleroma.DataCase
|
||||
alias Pleroma.Builders.{UserBuilder, ActivityBuilder}
|
||||
alias Pleroma.Web.TwitterAPI.{TwitterAPI,UserView,Utils}
|
||||
alias Pleroma.Builders.{UserBuilder}
|
||||
alias Pleroma.Web.TwitterAPI.{StatusView, TwitterAPI, UserView, Utils}
|
||||
alias Pleroma.{Activity, User, Object, Repo}
|
||||
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
|
||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||
|
||||
import Pleroma.Factory
|
||||
|
@ -76,56 +75,6 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
|
|||
assert Enum.member?(get_in(reply.data, ["to"]), "some_cool_id")
|
||||
end
|
||||
|
||||
test "fetch public statuses, excluding remote ones." do
|
||||
%{ public: activity, user: user } = ActivityBuilder.public_and_non_public
|
||||
insert(:note_activity, %{local: false})
|
||||
|
||||
follower = insert(:user, 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, for: follower})
|
||||
end
|
||||
|
||||
test "fetch whole known network statuses" do
|
||||
%{ public: activity, user: user } = ActivityBuilder.public_and_non_public
|
||||
insert(:note_activity, %{local: false})
|
||||
|
||||
follower = insert(:user, following: [User.ap_followers(user)])
|
||||
|
||||
statuses = TwitterAPI.fetch_public_and_external_statuses(follower)
|
||||
|
||||
assert length(statuses) == 2
|
||||
assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: user, for: follower})
|
||||
end
|
||||
|
||||
test "fetch friends' statuses" do
|
||||
user = insert(:user, %{following: ["someguy/followers"]})
|
||||
{:ok, activity} = ActivityBuilder.insert(%{"to" => ["someguy/followers"]})
|
||||
{:ok, direct_activity} = ActivityBuilder.insert(%{"to" => [user.ap_id]})
|
||||
|
||||
statuses = TwitterAPI.fetch_friend_statuses(user)
|
||||
|
||||
activity_user = Repo.get_by(User, ap_id: activity.data["actor"])
|
||||
direct_activity_user = Repo.get_by(User, ap_id: direct_activity.data["actor"])
|
||||
|
||||
assert length(statuses) == 2
|
||||
assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: activity_user})
|
||||
assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_activity, %{user: direct_activity_user, mentioned: [user]})
|
||||
end
|
||||
|
||||
test "fetch user's mentions" do
|
||||
user = insert(:user)
|
||||
{:ok, activity} = ActivityBuilder.insert(%{"to" => [user.ap_id]})
|
||||
activity_user = Repo.get_by(User, ap_id: activity.data["actor"])
|
||||
|
||||
statuses = TwitterAPI.fetch_mentions(user)
|
||||
|
||||
assert length(statuses) == 1
|
||||
assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: activity_user, mentioned: [user]})
|
||||
end
|
||||
|
||||
test "get a user by params" do
|
||||
user1_result = {:ok, user1} = UserBuilder.insert(%{ap_id: "some id", email: "test@pleroma"})
|
||||
{:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2", email: "test2@pleroma"})
|
||||
|
@ -141,34 +90,6 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
|
|||
assert {:error, "No user with such user_id"} == TwitterAPI.get_user(nil, %{"user_id" => 666})
|
||||
end
|
||||
|
||||
test "fetch user's statuses" do
|
||||
{:ok, user1} = UserBuilder.insert(%{ap_id: "some id", email: "test@pleroma"})
|
||||
{:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2", email: "test2@pleroma"})
|
||||
|
||||
{:ok, status1} = ActivityBuilder.insert(%{"id" => 1}, %{user: user1})
|
||||
{:ok, status2} = ActivityBuilder.insert(%{"id" => 2}, %{user: user2})
|
||||
|
||||
user1_statuses = TwitterAPI.fetch_user_statuses(user1, %{"actor_id" => user1.ap_id})
|
||||
|
||||
assert length(user1_statuses) == 1
|
||||
assert Enum.at(user1_statuses, 0) == ActivityRepresenter.to_map(status1, %{user: user1})
|
||||
|
||||
user2_statuses = TwitterAPI.fetch_user_statuses(user1, %{"actor_id" => user2.ap_id})
|
||||
|
||||
assert length(user2_statuses) == 1
|
||||
assert Enum.at(user2_statuses, 0) == ActivityRepresenter.to_map(status2, %{user: user2})
|
||||
end
|
||||
|
||||
test "fetch a single status" do
|
||||
{:ok, activity} = ActivityBuilder.insert()
|
||||
{:ok, user} = UserBuilder.insert()
|
||||
actor = Repo.get_by!(User, ap_id: activity.data["actor"])
|
||||
|
||||
status = TwitterAPI.fetch_status(user, activity.id)
|
||||
|
||||
assert status == ActivityRepresenter.to_map(activity, %{for: user, user: actor})
|
||||
end
|
||||
|
||||
test "Follow another user using user_id" do
|
||||
user = insert(:user)
|
||||
followed = insert(:user)
|
||||
|
@ -216,21 +137,6 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
|
|||
assert msg == "Not subscribed!"
|
||||
end
|
||||
|
||||
test "fetch statuses in a context using the conversation id" do
|
||||
{:ok, user} = UserBuilder.insert()
|
||||
{:ok, activity} = ActivityBuilder.insert(%{"context" => "2hu"})
|
||||
{:ok, activity_two} = ActivityBuilder.insert(%{"context" => "2hu"})
|
||||
{:ok, _activity_three} = ActivityBuilder.insert(%{"context" => "3hu"})
|
||||
|
||||
{:ok, object} = Object.context_mapping("2hu") |> Repo.insert
|
||||
|
||||
statuses = TwitterAPI.fetch_conversation(user, object.id)
|
||||
|
||||
assert length(statuses) == 2
|
||||
assert Enum.at(statuses, 0)["id"] == activity.id
|
||||
assert Enum.at(statuses, 1)["id"] == activity_two.id
|
||||
end
|
||||
|
||||
test "upload a file" do
|
||||
file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
|
||||
|
||||
|
@ -255,38 +161,34 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
|
|||
test "it favorites a status, returns the updated status" do
|
||||
user = insert(:user)
|
||||
note_activity = insert(:note_activity)
|
||||
activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
|
||||
|
||||
{:ok, status} = TwitterAPI.favorite(user, note_activity)
|
||||
updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
|
||||
|
||||
assert status == ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})
|
||||
assert StatusView.render("show.json", %{activity: status}) == StatusView.render("show.json", %{activity: updated_activity}) # FIXME: was complaining about microseconds
|
||||
end
|
||||
|
||||
test "it unfavorites a status, returns the updated status" do
|
||||
user = insert(:user)
|
||||
note_activity = insert(:note_activity)
|
||||
activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
|
||||
object = Object.get_by_ap_id(note_activity.data["object"]["id"])
|
||||
|
||||
{:ok, _like_activity, _object } = ActivityPub.like(user, object)
|
||||
updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
|
||||
assert ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})["fave_num"] == 1
|
||||
%Activity{data: %{"object" => object}} = Activity.get_by_ap_id(note_activity.data["id"])
|
||||
assert object["like_count"] == 1
|
||||
|
||||
{:ok, status} = TwitterAPI.unfavorite(user, note_activity)
|
||||
{:ok, %Activity{data: %{"object" => object}}} = TwitterAPI.unfavorite(user, note_activity)
|
||||
|
||||
assert status["fave_num"] == 0
|
||||
assert object["like_count"] == 0
|
||||
end
|
||||
|
||||
test "it retweets a status and returns the retweet" do
|
||||
user = insert(:user)
|
||||
note_activity = insert(:note_activity)
|
||||
activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
|
||||
|
||||
{:ok, status} = TwitterAPI.retweet(user, note_activity)
|
||||
updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
|
||||
|
||||
assert status == ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})
|
||||
assert StatusView.render("show.json", %{activity: status}) == StatusView.render("show.json", %{activity: updated_activity}) # FIXME: was complaining about microseconds
|
||||
end
|
||||
|
||||
test "it registers a new user and returns the user." do
|
||||
|
@ -322,10 +224,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
|
|||
|
||||
test "it assigns an integer conversation_id" do
|
||||
note_activity = insert(:note_activity)
|
||||
user = User.get_cached_by_ap_id(note_activity.data["actor"])
|
||||
status = ActivityRepresenter.to_map(note_activity, %{user: user})
|
||||
|
||||
assert is_number(status["statusnet_conversation_id"])
|
||||
assert is_number(StatusView.conversation_id(note_activity))
|
||||
end
|
||||
|
||||
setup do
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
defmodule Pleroma.Web.TwitterAPI.Representers.ObjectReprenterTest do
|
||||
defmodule Pleroma.Web.TwitterAPI.AttachmentViewTest do
|
||||
use Pleroma.DataCase
|
||||
|
||||
alias Pleroma.Object
|
||||
alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
|
||||
alias Pleroma.Web.TwitterAPI.AttachmentView
|
||||
|
||||
test "represent an image attachment" do
|
||||
object = %Object{
|
||||
|
@ -26,6 +26,6 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ObjectReprenterTest do
|
|||
oembed: false
|
||||
}
|
||||
|
||||
assert expected_object == ObjectRepresenter.to_map(object)
|
||||
assert expected_object == AttachmentView.render("show.json", %{attachment: object})
|
||||
end
|
||||
end
|
|
@ -1,27 +1,25 @@
|
|||
defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
|
||||
defmodule Pleroma.Web.TwitterAPI.StatusViewTest do
|
||||
use Pleroma.DataCase
|
||||
alias Pleroma.{User, Activity, Object}
|
||||
alias Pleroma.Web.TwitterAPI.Representers.{ActivityRepresenter, ObjectRepresenter}
|
||||
alias Pleroma.Web.TwitterAPI.{AttachmentView, UserView, StatusView}
|
||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||
alias Pleroma.Builders.UserBuilder
|
||||
alias Pleroma.Web.TwitterAPI.UserView
|
||||
import Pleroma.Factory
|
||||
|
||||
test "an announce activity" do
|
||||
user = insert(:user)
|
||||
note_activity = insert(:note_activity)
|
||||
activity_actor = Repo.get_by(User, ap_id: note_activity.data["actor"])
|
||||
object = Object.get_by_ap_id(note_activity.data["object"]["id"])
|
||||
%Activity{data: %{"id" => note_ap_id, "object" => %{"id" => object_id}}} = insert(:note_activity)
|
||||
object = Object.get_by_ap_id(object_id)
|
||||
|
||||
{:ok, announce_activity, _object} = ActivityPub.announce(user, object)
|
||||
note_activity = Activity.get_by_ap_id(note_activity.data["id"])
|
||||
{:ok,
|
||||
announce_activity = %Activity{id: announce_id, data: %{"type" => "Announce"}},
|
||||
_object} = ActivityPub.announce(user, object)
|
||||
|
||||
status = ActivityRepresenter.to_map(announce_activity, %{users: [user, activity_actor], announced_activity: note_activity, for: user})
|
||||
|
||||
assert status["id"] == announce_activity.id
|
||||
status = StatusView.render("show.json", %{activity: announce_activity, for: user})
|
||||
assert status["id"] == announce_id
|
||||
assert status["user"] == UserView.render("show.json", %{user: user, for: user})
|
||||
|
||||
retweeted_status = ActivityRepresenter.to_map(note_activity, %{user: activity_actor, for: user})
|
||||
note_activity = Activity.get_by_ap_id(note_ap_id)
|
||||
retweeted_status = StatusView.render("show.json", %{activity: note_activity, for: user})
|
||||
assert retweeted_status["repeated"] == true
|
||||
assert retweeted_status["id"] == note_activity.id
|
||||
assert status["statusnet_conversation_id"] == retweeted_status["statusnet_conversation_id"]
|
||||
|
@ -31,23 +29,22 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
|
|||
|
||||
test "a like activity" do
|
||||
user = insert(:user)
|
||||
note_activity = insert(:note_activity)
|
||||
object = Object.get_by_ap_id(note_activity.data["object"]["id"])
|
||||
%Activity{id: note_id, data: %{"id" => note_ap_id, "object" => %{"id" => object_id}}} = insert(:note_activity)
|
||||
object = Object.get_by_ap_id(object_id)
|
||||
|
||||
{:ok, like_activity, _object} = ActivityPub.like(user, object)
|
||||
status = ActivityRepresenter.to_map(like_activity, %{user: user, liked_activity: note_activity})
|
||||
{:ok, like_activity = %Activity{id: like_id, data: %{"type" => "Like"}}, _object} = ActivityPub.like(user, object)
|
||||
status = StatusView.render("show.json", %{activity: like_activity})
|
||||
|
||||
assert status["id"] == like_activity.id
|
||||
assert status["in_reply_to_status_id"] == note_activity.id
|
||||
assert status["id"] == like_id
|
||||
assert status["in_reply_to_status_id"] == note_id
|
||||
|
||||
note_activity = Activity.get_by_ap_id(note_activity.data["id"])
|
||||
activity_actor = Repo.get_by(User, ap_id: note_activity.data["actor"])
|
||||
liked_status = ActivityRepresenter.to_map(note_activity, %{user: activity_actor, for: user})
|
||||
note_activity = Activity.get_by_ap_id(note_ap_id)
|
||||
liked_status = StatusView.render("show.json", %{activity: note_activity, for: user})
|
||||
assert liked_status["favorited"] == true
|
||||
end
|
||||
|
||||
test "an activity" do
|
||||
{:ok, user} = UserBuilder.insert
|
||||
user = insert(:user, %{nickname: "dtluna"})
|
||||
# {:ok, mentioned_user } = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"})
|
||||
mentioned_user = insert(:user, %{nickname: "shp"})
|
||||
|
||||
|
@ -68,7 +65,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
|
|||
}
|
||||
}
|
||||
|
||||
content_html = "<script>alert('YAY')</script>Some content mentioning <a href='#{mentioned_user.ap_id}'>@shp</shp>"
|
||||
content_html = "Some content mentioning <a href='#{mentioned_user.ap_id}'>@shp</shp>"
|
||||
content = HtmlSanitizeEx.strip_tags(content_html)
|
||||
date = DateTime.from_naive!(~N[2016-05-24 13:26:08.003], "Etc/UTC") |> DateTime.to_iso8601
|
||||
|
||||
|
@ -84,7 +81,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
|
|||
"https://www.w3.org/ns/activitystreams#Public",
|
||||
mentioned_user.ap_id
|
||||
],
|
||||
"actor" => User.ap_id(user),
|
||||
"actor" => user.ap_id,
|
||||
"object" => %{
|
||||
"published" => date,
|
||||
"type" => "Note",
|
||||
|
@ -95,8 +92,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
|
|||
],
|
||||
"like_count" => 5,
|
||||
"announcement_count" => 3,
|
||||
"context" => "2hu",
|
||||
"tag" => ["content", "mentioning", "nsfw"]
|
||||
"context" => "2hu"
|
||||
},
|
||||
"published" => date,
|
||||
"context" => "2hu"
|
||||
|
@ -108,26 +104,26 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
|
|||
"id" => activity.id,
|
||||
"user" => UserView.render("show.json", %{user: user, for: follower}),
|
||||
"is_local" => true,
|
||||
"statusnet_html" => HtmlSanitizeEx.basic_html(content_html),
|
||||
"statusnet_html" => content_html,
|
||||
"text" => content,
|
||||
"is_post_verb" => true,
|
||||
"created_at" => "Tue May 24 13:26:08 +0000 2016",
|
||||
"in_reply_to_status_id" => 213123,
|
||||
"statusnet_conversation_id" => convo_object.id,
|
||||
"attachments" => [
|
||||
ObjectRepresenter.to_map(object)
|
||||
AttachmentView.render("show.json", %{attachment: object})
|
||||
],
|
||||
"attentions" => [
|
||||
UserView.render("show.json", %{user: mentioned_user, for: follower})
|
||||
UserView.render("short.json", %{user: mentioned_user})
|
||||
],
|
||||
"fave_num" => 5,
|
||||
"repeat_num" => 3,
|
||||
"favorited" => false,
|
||||
"repeated" => false,
|
||||
"external_url" => activity.data["id"],
|
||||
"tags" => ["content", "mentioning", "nsfw"]
|
||||
"source" => "api"
|
||||
}
|
||||
|
||||
assert ActivityRepresenter.to_map(activity, %{user: user, for: follower, mentioned: [mentioned_user]}) == expected_status
|
||||
assert StatusView.render("show.json", %{activity: activity, for: follower}) == expected_status
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue