Add reblogging to MastodonAPI.
This commit is contained in:
parent
4dc517a0bb
commit
66e4c710d4
@ -10,4 +10,25 @@ defmodule Pleroma.Web.CommonAPI do
|
||||
{:ok, delete}
|
||||
end
|
||||
end
|
||||
|
||||
def repeat(id_or_ap_id, user) do
|
||||
with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
|
||||
false <- activity.data["actor"] == user.ap_id,
|
||||
object <- Object.get_by_ap_id(activity.data["object"]["id"]) do
|
||||
ActivityPub.announce(user, object)
|
||||
else
|
||||
_ ->
|
||||
{:error, "Could not repeat"}
|
||||
end
|
||||
end
|
||||
|
||||
# This is a hack for twidere.
|
||||
def get_by_id_or_ap_id(id) do
|
||||
activity = Repo.get(Activity, id) || Activity.get_create_activity_by_object_ap_id(id)
|
||||
if activity.data["type"] == "Create" do
|
||||
activity
|
||||
else
|
||||
Activity.get_create_activity_by_object_ap_id(activity.data["object"])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -80,4 +80,11 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
|
||||
|> json(%{error: "Can't delete this post"})
|
||||
end
|
||||
end
|
||||
|
||||
def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => 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) do
|
||||
render conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -7,7 +7,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
|
||||
render_many(opts.activities, StatusView, "status.json", opts)
|
||||
end
|
||||
|
||||
def render("status.json", %{activity: %{data: %{"object" => object}} = activity}) do
|
||||
def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
|
||||
user = User.get_cached_by_ap_id(activity.data["actor"])
|
||||
|
||||
like_count = object["like_count"] || 0
|
||||
@ -21,6 +21,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
|
||||
|> Enum.filter(&(&1))
|
||||
|> Enum.map(fn (user) -> AccountView.render("mention.json", %{user: user}) end)
|
||||
|
||||
repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
|
||||
|
||||
%{
|
||||
id: activity.id,
|
||||
uri: object["id"],
|
||||
@ -33,7 +35,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
|
||||
created_at: object["published"],
|
||||
reblogs_count: announcement_count,
|
||||
favourites_count: like_count,
|
||||
reblogged: false,
|
||||
reblogged: !!repeated,
|
||||
favourited: false, # fix
|
||||
muted: false,
|
||||
sensitive: sensitive,
|
||||
|
@ -57,6 +57,8 @@ defmodule Pleroma.Web.Router do
|
||||
|
||||
post "/statuses", MastodonAPIController, :post_status
|
||||
delete "/statuses/:id", MastodonAPIController, :delete_status
|
||||
|
||||
post "/statuses/:id/reblog", MastodonAPIController, :reblog_status
|
||||
end
|
||||
|
||||
scope "/api", Pleroma.Web do
|
||||
|
@ -3,7 +3,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
|
||||
alias Pleroma.Web.TwitterAPI.UserView
|
||||
alias Pleroma.Web.OStatus
|
||||
alias Pleroma.Web.{OStatus, CommonAPI}
|
||||
alias Pleroma.Formatter
|
||||
|
||||
import Pleroma.Web.TwitterAPI.Utils
|
||||
@ -141,17 +141,12 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
||||
{:ok, status}
|
||||
end
|
||||
|
||||
def retweet(%User{} = user, %Activity{data: %{"object" => object}} = activity) do
|
||||
object = Object.get_by_ap_id(object["id"])
|
||||
|
||||
{:ok, _announce_activity, object} = ActivityPub.announce(user, object)
|
||||
new_data = activity.data
|
||||
|> Map.put("object", object.data)
|
||||
|
||||
status = %{activity | data: new_data}
|
||||
|> activity_to_status(%{for: user})
|
||||
|
||||
{:ok, status}
|
||||
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}
|
||||
end
|
||||
end
|
||||
|
||||
def upload(%Plug.Upload{} = file, format \\ "xml") do
|
||||
|
@ -167,22 +167,13 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
|
||||
end
|
||||
|
||||
def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|
||||
activity = get_by_id_or_ap_id(id)
|
||||
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)
|
||||
with {:ok, status} <- TwitterAPI.repeat(user, id) do
|
||||
json(conn, status)
|
||||
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} ->
|
||||
|
@ -121,4 +121,18 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
|
||||
assert Repo.get(Activity, activity.id) == activity
|
||||
end
|
||||
end
|
||||
|
||||
describe "reblogging" do
|
||||
test "reblogs and returns the reblogged status", %{conn: conn} do
|
||||
activity = insert(:note_activity)
|
||||
user = insert(:user)
|
||||
|
||||
conn = conn
|
||||
|> assign(:user, user)
|
||||
|> post("/api/v1/statuses/#{activity.id}/reblog")
|
||||
|
||||
assert %{"id" => id, "reblogged" => true, "reblogs_count" => 1} = json_response(conn, 200)
|
||||
assert activity.id == id
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -354,13 +354,6 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|
||||
|
||||
request_path = "/api/statuses/retweet/#{note_activity.id}.json"
|
||||
|
||||
user = Repo.get_by(User, ap_id: note_activity.data["actor"])
|
||||
response = conn
|
||||
|> with_credentials(user.nickname, "test")
|
||||
|> post(request_path)
|
||||
assert json_response(response, 400) == %{"error" => "You cannot repeat your own notice.",
|
||||
"request" => request_path}
|
||||
|
||||
response = conn
|
||||
|> with_credentials(current_user.nickname, "test")
|
||||
|> post(request_path)
|
||||
|
@ -290,7 +290,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
|
||||
note_activity = insert(:note_activity)
|
||||
activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
|
||||
|
||||
{:ok, status} = TwitterAPI.retweet(user, note_activity)
|
||||
{: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})
|
||||
|
Loading…
Reference in New Issue
Block a user