Add unfollowing to TwAPI.
authorRoger Braun <roger@rogerbraun.net>
Thu, 23 Mar 2017 12:13:09 +0000 (13:13 +0100)
committerRoger Braun <roger@rogerbraun.net>
Thu, 23 Mar 2017 12:13:09 +0000 (13:13 +0100)
lib/pleroma/user.ex
lib/pleroma/web/router.ex
lib/pleroma/web/twitter_api/twitter_api.ex
lib/pleroma/web/twitter_api/twitter_api_controller.ex
test/user_test.exs
test/web/twitter_api/twitter_api_controller_test.exs
test/web/twitter_api/twitter_api_test.exs

index 29fd1de8b07125529efb55376fde433361cda398..a10b43b780b93183fa507260397307470e249f6b 100644 (file)
@@ -43,4 +43,14 @@ defmodule Pleroma.User do
     |> follow_changeset(%{following: following})
     |> Repo.update
   end
+
+  def unfollow(%User{} = follower, %User{} = followed) do
+    ap_followers = User.ap_followers(followed)
+    following = follower.following
+    |> List.delete(ap_followers)
+
+    follower
+    |> follow_changeset(%{following: following})
+    |> Repo.update
+  end
 end
index 6d854c538faea8cd8db352cc5110ba8ca031bea1..e7d0b94377210d8955e96c211c5884077743d9f1 100644 (file)
@@ -32,5 +32,6 @@ defmodule Pleroma.Web.Router do
     post "/statuses/update.json", TwitterAPI.Controller, :status_update
     get "/statuses/friends_timeline.json", TwitterAPI.Controller, :friends_timeline
     post "/friendships/create.json", TwitterAPI.Controller, :follow
+    post "/friendships/destroy.json", TwitterAPI.Controller, :unfollow
   end
 end
index fb093b22744c7126d014c82ca703247e176a52ea..2ef679397afffef6a3c852929b51e06469a22df2 100644 (file)
@@ -42,6 +42,14 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
     end
   end
 
+  def unfollow(%User{} = follower, followed_id) do
+    with %User{} = followed <- Repo.get(User, followed_id),
+         { :ok, follower } <- User.unfollow(follower, followed)
+    do
+      { :ok, follower, followed }
+    end
+  end
+
   defp activities_to_statuses(activities) do
     Enum.map(activities, fn(activity) ->
       actor = get_in(activity.data, ["actor"])
index ac319b1095309f7015f51d73323362d5cd1b2d8c..d574f1003ede1db62484525d9b4bcb87daffc4b0 100644 (file)
@@ -41,6 +41,16 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
     |> json_reply(200, response)
   end
 
+  def unfollow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
+    { :ok, _user, follower } = TwitterAPI.unfollow(user, followed_id)
+
+    response = follower |> UserRepresenter.to_json
+
+    conn
+    |> json_reply(200, response)
+  end
+
+
   defp json_reply(conn, status, json) do
     conn
     |> put_resp_content_type("application/json")
index e39c0be3152dd331b5ad428e6fbf469b40b4190b..e11bade9349c962678a17c2fe8ea9eec503c6b07 100644 (file)
@@ -24,7 +24,7 @@ defmodule Pleroma.UserTest do
     assert expected_followers_collection == User.ap_followers(user)
   end
 
-  test "follow takes a user and an id and tries to follow another user" do
+  test "follow takes a user and another user" do
     { :ok, user } = UserBuilder.insert
     { :ok, following } = UserBuilder.insert(%{nickname: "guy"})
 
@@ -34,4 +34,15 @@ defmodule Pleroma.UserTest do
 
     assert user.following == [User.ap_followers(following)]
   end
+
+  test "unfollow takes a user and another user" do
+    { :ok, following } = UserBuilder.insert(%{nickname: "guy"})
+    { :ok, user } = UserBuilder.insert(%{following: [User.ap_followers(following)]})
+
+    {:ok, user } = User.unfollow(user, following)
+
+    user = Repo.get(User, user.id)
+
+    assert user.following == []
+  end
 end
index a504393beea3a4acfa3793101e5dcc56392985ff..b0a0cdb5423b6b0a58353def26753134331ca106 100644 (file)
@@ -99,6 +99,29 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
     end
   end
 
+  describe "POST /friendships/destroy.json" do
+    setup [:valid_user]
+    test "without valid credentials", %{conn: conn} do
+      conn = post conn, "/api/friendships/destroy.json"
+      assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
+    end
+
+    test "with credentials", %{conn: conn, user: current_user} do
+      {:ok, followed } = UserBuilder.insert(%{name: "some guy"})
+
+      {:ok, current_user} = User.follow(current_user, followed)
+      assert current_user.following == [User.ap_followers(followed)]
+
+      conn = conn
+      |> with_credentials(current_user.nickname, "test")
+      |> post("/api/friendships/destroy.json", %{user_id: followed.id})
+
+      current_user = Repo.get(User, current_user.id)
+      assert current_user.following == []
+      assert json_response(conn, 200) == UserRepresenter.to_map(followed)
+    end
+  end
+
   defp valid_user(_context) do
     { :ok, user } = UserBuilder.insert(%{nickname: "lambda", ap_id: "lambda"})
     [user: user]
index c9f79d2ff97f8ea015e5b9a288e6bb9c413898a3..3cb3196d9672cd990425b9f6380c2b4c56459b8c 100644 (file)
@@ -51,4 +51,15 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
 
     assert user.following == [User.ap_followers(following)]
   end
+
+  test "Unfollow another user" do
+    { :ok, following } = UserBuilder.insert(%{nickname: "guy"})
+    { :ok, user } = UserBuilder.insert(%{following: [User.ap_followers(following)]})
+
+    {:ok, user, _following } = TwitterAPI.unfollow(user, following.id)
+
+    user = Repo.get(User, user.id)
+
+    assert user.following == []
+  end
 end