mirror of
https://akkoma.dev/AkkomaGang/akkoma.git
synced 2024-11-17 14:44:52 +00:00
Add unliking to TwAPI controller.
This commit is contained in:
parent
f4eea0847b
commit
c83f279a7d
4 changed files with 34 additions and 3 deletions
|
@ -42,5 +42,6 @@ defmodule Pleroma.Web.Router do
|
|||
post "/media/upload", TwitterAPI.Controller, :upload_json
|
||||
post "/favorites/create/:id", TwitterAPI.Controller, :favorite
|
||||
post "/favorites/create", TwitterAPI.Controller, :favorite
|
||||
post "/favorites/destroy/:id", TwitterAPI.Controller, :unfavorite
|
||||
end
|
||||
end
|
||||
|
|
|
@ -113,6 +113,15 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
|
|||
|> json_reply(200, response)
|
||||
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)
|
||||
end
|
||||
|
||||
defp json_reply(conn, status, json) do
|
||||
conn
|
||||
|> put_resp_content_type("application/json")
|
||||
|
|
|
@ -2,7 +2,8 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|
|||
use Pleroma.Web.ConnCase
|
||||
alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter}
|
||||
alias Pleroma.Builders.{ActivityBuilder, UserBuilder}
|
||||
alias Pleroma.{Repo, Activity, User}
|
||||
alias Pleroma.{Repo, Activity, User, Object}
|
||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||
|
||||
import Pleroma.Factory
|
||||
|
||||
|
@ -175,6 +176,27 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|
|||
end
|
||||
end
|
||||
|
||||
describe "POST /api/favorites/destroy/:id" do
|
||||
setup [:valid_user]
|
||||
test "without valid credentials", %{conn: conn} do
|
||||
note_activity = insert(:note_activity)
|
||||
conn = post conn, "/api/favorites/destroy/#{note_activity.id}.json"
|
||||
assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
|
||||
end
|
||||
|
||||
test "with credentials", %{conn: conn, user: current_user} do
|
||||
note_activity = insert(:note_activity)
|
||||
object = Object.get_by_ap_id(note_activity.data["object"]["id"])
|
||||
ActivityPub.like(current_user, object)
|
||||
|
||||
conn = conn
|
||||
|> with_credentials(current_user.nickname, "test")
|
||||
|> post("/api/favorites/destroy/#{note_activity.id}.json")
|
||||
|
||||
assert json_response(conn, 200)
|
||||
end
|
||||
end
|
||||
|
||||
defp valid_user(_context) do
|
||||
{ :ok, user } = UserBuilder.insert(%{nickname: "lambda", ap_id: "lambda"})
|
||||
[user: user]
|
||||
|
|
|
@ -198,12 +198,11 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
|
|||
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)
|
||||
{: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
|
||||
|
||||
{:ok, status} = TwitterAPI.unfavorite(user, note_activity)
|
||||
updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
|
||||
|
||||
assert status["fave_num"] == 0
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue