{: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
|> 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
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
|> 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"],
created_at: object["published"],
reblogs_count: announcement_count,
favourites_count: like_count,
- reblogged: false,
+ reblogged: !!repeated,
favourited: false, # fix
muted: false,
sensitive: sensitive,
post "/statuses", MastodonAPIController, :post_status
delete "/statuses/:id", MastodonAPIController, :delete_status
+
+ post "/statuses/:id/reblog", MastodonAPIController, :reblog_status
end
scope "/api", Pleroma.Web 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
{: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
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} ->
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
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)
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})