end
end
+ def favorite(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.like(user, object)
+ else
+ _ ->
+ {:error, "Could not favorite"}
+ 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)
render conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity}
end
end
+
+ def fav_status(%{assigns: %{user: user}} = conn, %{"id" => 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) do
+ render conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity}
+ end
+ end
end
|> Enum.map(fn (user) -> AccountView.render("mention.json", %{user: user}) end)
repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
+ favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
%{
id: activity.id,
reblogs_count: announcement_count,
favourites_count: like_count,
reblogged: !!repeated,
- favourited: false, # fix
+ favourited: !!favorited,
muted: false,
sensitive: sensitive,
spoiler_text: "",
delete "/statuses/:id", MastodonAPIController, :delete_status
post "/statuses/:id/reblog", MastodonAPIController, :reblog_status
+ post "/statuses/:id/favourite", MastodonAPIController, :fav_status
end
scope "/api", Pleroma.Web do
end
end
- def favorite(%User{} = user, %Activity{data: %{"object" => object}} = activity) do
- object = Object.get_by_ap_id(object["id"])
-
- {:ok, _like_activity, object} = ActivityPub.like(user, object)
- new_data = activity.data
- |> Map.put("object", object.data)
-
- status = %{activity | data: new_data}
- |> activity_to_status(%{for: user})
-
- {:ok, status}
- end
-
def unfavorite(%User{} = user, %Activity{data: %{"object" => object}} = activity) do
object = Object.get_by_ap_id(object["id"])
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}
+ end
+ end
+
def upload(%Plug.Upload{} = file, format \\ "xml") do
{:ok, object} = ActivityPub.upload(file)
end
def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
- activity = get_by_id_or_ap_id(id)
- {:ok, status} = TwitterAPI.favorite(user, activity)
- response = Poison.encode!(status)
-
- conn
- |> json_reply(200, response)
+ with {:ok, status} <- TwitterAPI.fav(user, id) do
+ json(conn, status)
+ end
end
def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
assert activity.id == id
end
end
+
+ describe "favoriting" do
+ test "favs a status and returns it", %{conn: conn} do
+ activity = insert(:note_activity)
+ user = insert(:user)
+
+ conn = conn
+ |> assign(:user, user)
+ |> post("/api/v1/statuses/#{activity.id}/favourite")
+
+ assert %{"id" => id, "favourites_count" => 1, "favourited" => true} = json_response(conn, 200)
+ assert activity.id == id
+ end
+ end
end
note_activity = insert(:note_activity)
activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
- {:ok, status} = TwitterAPI.favorite(user, note_activity)
+ {: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})