Add unliking to TwAPI controller.

This commit is contained in:
Roger Braun 2017-04-14 18:27:17 +02:00
parent f4eea0847b
commit c83f279a7d
4 changed files with 34 additions and 3 deletions

View File

@ -42,5 +42,6 @@ def user_fetcher(username) do
post "/media/upload", TwitterAPI.Controller, :upload_json post "/media/upload", TwitterAPI.Controller, :upload_json
post "/favorites/create/:id", TwitterAPI.Controller, :favorite post "/favorites/create/:id", TwitterAPI.Controller, :favorite
post "/favorites/create", TwitterAPI.Controller, :favorite post "/favorites/create", TwitterAPI.Controller, :favorite
post "/favorites/destroy/:id", TwitterAPI.Controller, :unfavorite
end end
end end

View File

@ -113,6 +113,15 @@ def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|> json_reply(200, response) |> json_reply(200, response)
end 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 defp json_reply(conn, status, json) do
conn conn
|> put_resp_content_type("application/json") |> put_resp_content_type("application/json")

View File

@ -2,7 +2,8 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
use Pleroma.Web.ConnCase use Pleroma.Web.ConnCase
alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter} alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter}
alias Pleroma.Builders.{ActivityBuilder, UserBuilder} alias Pleroma.Builders.{ActivityBuilder, UserBuilder}
alias Pleroma.{Repo, Activity, User} alias Pleroma.{Repo, Activity, User, Object}
alias Pleroma.Web.ActivityPub.ActivityPub
import Pleroma.Factory import Pleroma.Factory
@ -175,6 +176,27 @@ test "with credentials", %{conn: conn, user: current_user} do
end end
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 defp valid_user(_context) do
{ :ok, user } = UserBuilder.insert(%{nickname: "lambda", ap_id: "lambda"}) { :ok, user } = UserBuilder.insert(%{nickname: "lambda", ap_id: "lambda"})
[user: user] [user: user]

View File

@ -198,12 +198,11 @@ test "it unfavorites a status, returns the updated status" do
activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"]) activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
object = Object.get_by_ap_id(note_activity.data["object"]["id"]) 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"]) 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 ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})["fave_num"] == 1
{:ok, status} = TwitterAPI.unfavorite(user, note_activity) {:ok, status} = TwitterAPI.unfavorite(user, note_activity)
updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
assert status["fave_num"] == 0 assert status["fave_num"] == 0
end end