Remove unused ActivityRepresenter module.

This commit is contained in:
lain 2018-03-30 16:50:30 +02:00
parent 7ab94ae22d
commit 802736679c
4 changed files with 25 additions and 290 deletions

View file

@ -1,3 +1,5 @@
# THIS MODULE IS DEPRECATED! DON'T USE IT!
# USE THE Pleroma.Web.TwitterAPI.Views.ActivityView MODULE!
defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter

View file

@ -12,74 +12,6 @@ def create_status(%User{} = user, %{"status" => _} = data) do
CommonAPI.post(user, data)
end
def fetch_friend_statuses(user, opts \\ %{}) do
opts =
opts
|> Map.put("blocking_user", user)
|> Map.put("user", user)
|> Map.put("type", ["Create", "Announce", "Follow", "Like"])
ActivityPub.fetch_activities([user.ap_id | user.following], opts)
|> activities_to_statuses(%{for: user})
end
def fetch_public_statuses(user, opts \\ %{}) do
opts =
opts
|> Map.put("local_only", true)
|> Map.put("blocking_user", user)
|> Map.put("type", ["Create", "Announce", "Follow"])
ActivityPub.fetch_public_activities(opts)
|> activities_to_statuses(%{for: user})
end
def fetch_public_and_external_statuses(user, opts \\ %{}) do
opts =
opts
|> Map.put("blocking_user", user)
|> Map.put("type", ["Create", "Announce", "Follow"])
ActivityPub.fetch_public_activities(opts)
|> activities_to_statuses(%{for: user})
end
def fetch_user_statuses(user, opts \\ %{}) do
opts =
opts
|> Map.put("type", ["Create"])
ActivityPub.fetch_public_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, %{
"blocking_user" => user,
"user" => user
}),
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),
true <- ActivityPub.visible_for_user?(activity, user) 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),
@ -127,25 +59,22 @@ def unblock(%User{} = blocker, params) do
def repeat(%User{} = user, ap_id_or_id) do
with {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(ap_id_or_id, user),
%Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id),
status <- activity_to_status(activity, %{for: user}) do
{:ok, status}
%Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
{:ok, activity}
end
end
def fav(%User{} = user, ap_id_or_id) do
with {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.favorite(ap_id_or_id, user),
%Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id),
status <- activity_to_status(activity, %{for: user}) do
{:ok, status}
%Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
{:ok, activity}
end
end
def unfav(%User{} = user, ap_id_or_id) do
with {:ok, %{data: %{"id" => id}}} = CommonAPI.unfavorite(ap_id_or_id, user),
%Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id),
status <- activity_to_status(activity, %{for: user}) do
{:ok, status}
%Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
{:ok, activity}
end
end
@ -279,63 +208,6 @@ def search(user, %{"q" => query} = params) do
activities = Repo.all(q)
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{data: %{"type" => "Delete"}} = activity, opts) do
actor = get_in(activity.data, ["actor"])
user = User.get_cached_by_ap_id(actor)
ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user}))
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.recipients || [], fn ap_id ->
if ap_id do
User.get_cached_by_ap_id(ap_id)
else
nil
end
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

View file

@ -1,7 +1,6 @@
defmodule Pleroma.Web.TwitterAPI.Controller do
use Pleroma.Web, :controller
alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView, ActivityView}
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
alias Pleroma.Web.CommonAPI
alias Pleroma.{Repo, Activity, User}
alias Pleroma.Web.ActivityPub.ActivityPub
@ -152,10 +151,7 @@ def unblock(%{assigns: %{user: user}} = conn, params) do
def delete_post(%{assigns: %{user: user}} = conn, %{"id" => id}) do
with {:ok, delete} <- CommonAPI.delete(id, user) do
json = ActivityRepresenter.to_json(delete, %{user: user, for: user})
conn
|> json_reply(200, json)
render(conn, ActivityView, "activity.json", %{activity: delete, for: user})
end
end
@ -216,20 +212,20 @@ def get_by_id_or_ap_id(id) do
end
def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
with {:ok, status} <- TwitterAPI.fav(user, id) do
json(conn, status)
with {:ok, activity} <- TwitterAPI.fav(user, id) do
render(conn, ActivityView, "activity.json", %{activity: activity, for: user})
end
end
def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
with {:ok, status} <- TwitterAPI.unfav(user, id) do
json(conn, status)
with {:ok, activity} <- TwitterAPI.unfav(user, id) do
render(conn, ActivityView, "activity.json", %{activity: activity, for: user})
end
end
def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
with {:ok, status} <- TwitterAPI.repeat(user, id) do
json(conn, status)
with {:ok, activity} <- TwitterAPI.repeat(user, id) do
render(conn, ActivityView, "activity.json", %{activity: activity, for: user})
end
end

View file

@ -1,11 +1,10 @@
defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
use Pleroma.DataCase
alias Pleroma.Builders.{UserBuilder, ActivityBuilder}
alias Pleroma.Builders.UserBuilder
alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView}
alias Pleroma.Web.CommonAPI.Utils
alias Pleroma.{Activity, User, Object, Repo}
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.TwitterAPI.ActivityView
import Pleroma.Factory
@ -96,117 +95,6 @@ test "create a status that is a reply" do
assert get_in(reply.data, ["object", "inReplyToStatusId"]) == activity.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.follower_address])
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"})
assert {:error, "You need to specify screen_name or user_id"} == TwitterAPI.get_user(nil, nil)
assert user1_result == TwitterAPI.get_user(nil, %{"user_id" => user1.id})
assert user1_result == TwitterAPI.get_user(nil, %{"user_id" => user1.nickname})
assert user1_result == TwitterAPI.get_user(nil, %{"screen_name" => user1.nickname})
assert user1_result == TwitterAPI.get_user(user1, nil)
assert user1_result == TwitterAPI.get_user(user2, %{"user_id" => user1.id})
assert user1_result == TwitterAPI.get_user(user2, %{"screen_name" => user1.nickname})
assert {:error, "No user with such screen_name"} ==
TwitterAPI.get_user(nil, %{"screen_name" => "Satan"})
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)
@ -293,21 +181,6 @@ test "Unblock another user using screen_name" do
assert user.info["blocks"] == []
end
test "fetch statuses in a context using the conversation id" do
{:ok, user} = UserBuilder.insert()
{:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
{:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
{:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "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, 1)["id"] == activity.id
assert Enum.at(statuses, 0)["id"] == activity_two.id
end
test "upload a file" do
file = %Plug.Upload{
content_type: "image/jpg",
@ -320,46 +193,39 @@ test "upload a file" do
assert is_binary(response)
end
test "it favorites a status, returns the updated status" do
test "it favorites a status, returns the updated activity" do
user = insert(:user)
note_activity = insert(:note_activity)
activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
{:ok, status} = TwitterAPI.fav(user, note_activity.id)
updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
assert status ==
ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})
assert status == updated_activity
end
test "it unfavorites a status, returns the updated status" do
test "it unfavorites a status, returns the updated activity" 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
assert ActivityView.render("activity.json", activity: updated_activity)["fave_num"] == 1
{:ok, status} = TwitterAPI.unfav(user, note_activity.id)
{:ok, activity} = TwitterAPI.unfav(user, note_activity.id)
assert status["fave_num"] == 0
assert ActivityView.render("activity.json", activity: activity)["fave_num"] == 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.repeat(user, note_activity.id)
updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
assert status ==
ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})
assert status == updated_activity
end
test "it registers a new user and returns the user." do
@ -397,8 +263,7 @@ test "it returns the error on registration problems" 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})
status = ActivityView.render("activity.json", activity: note_activity)
assert is_number(status["statusnet_conversation_id"])
end