Handle unrepeats via the TwitterAPI

This commit is contained in:
Francis Dinh 2018-04-18 06:00:40 -04:00
parent 687db1bc3a
commit 7b4f55238e
5 changed files with 25 additions and 11 deletions

View File

@ -146,7 +146,7 @@ def unannounce(%User{} = actor, %Object{} = object, local \\ true) do
{:ok, unannounce_activity} <- insert(unannounce_data, local), {:ok, unannounce_activity} <- insert(unannounce_data, local),
{:ok, _activity} <- Repo.delete(activity), {:ok, _activity} <- Repo.delete(activity),
{:ok, object} <- remove_announce_from_object(activity, object) do {:ok, object} <- remove_announce_from_object(activity, object) do
{:ok, unannounce_activity, object} {:ok, unannounce_activity, activity, object}
else else
_e -> {:ok, object} _e -> {:ok, object}
end end

View File

@ -297,7 +297,7 @@ def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
end end
def unreblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do def unreblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
with {:ok, _, %{data: %{"id" => id}}} = CommonAPI.unrepeat(ap_id_or_id, user), with {:ok, _, _, %{data: %{"id" => id}}} = CommonAPI.unrepeat(ap_id_or_id, user),
%Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity}) render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
end end

View File

@ -12,6 +12,18 @@ def create_status(%User{} = user, %{"status" => _} = data) do
CommonAPI.post(user, data) CommonAPI.post(user, data)
end end
def delete(%User{} = user, id) do
# TwitterAPI does not have an "unretweet" endpoint; instead this is done
# via the "destroy" endpoint. Therefore, there is a need to handle
# when the status to "delete" is actually an Announce (repeat) object.
with %Activity{data: %{"type" => type}} <- Repo.get(Activity, id) do
case type do
"Announce" -> unrepeat(user, id)
_ -> CommonAPI.delete(id, user)
end
end
end
def follow(%User{} = follower, params) do def follow(%User{} = follower, params) do
with {:ok, %User{} = followed} <- get_user(params), with {:ok, %User{} = followed} <- get_user(params),
{:ok, follower} <- User.follow(follower, followed), {:ok, follower} <- User.follow(follower, followed),
@ -64,6 +76,12 @@ def repeat(%User{} = user, ap_id_or_id) do
end end
end end
defp unrepeat(%User{} = user, ap_id_or_id) do
with {:ok, _unannounce, activity, _object} <- CommonAPI.unrepeat(ap_id_or_id, user) do
{:ok, activity}
end
end
def fav(%User{} = user, ap_id_or_id) do def fav(%User{} = user, ap_id_or_id) do
with {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.favorite(ap_id_or_id, user), 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 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do

View File

@ -150,8 +150,8 @@ def unblock(%{assigns: %{user: user}} = conn, params) do
end end
def delete_post(%{assigns: %{user: user}} = conn, %{"id" => id}) do def delete_post(%{assigns: %{user: user}} = conn, %{"id" => id}) do
with {:ok, delete} <- CommonAPI.delete(id, user) do with {:ok, activity} <- TwitterAPI.delete(id, user) do
render(conn, ActivityView, "activity.json", %{activity: delete, for: user}) render(conn, ActivityView, "activity.json", %{activity: activity, for: user})
end end
end end
@ -229,12 +229,6 @@ def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
end end
end end
def unretweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
with {:ok, activity} <- TwitterAPI.unrepeat(user, id) do
render(conn, ActivityView, "activity.json", %{activity: activity, for: user})
end
end
def register(conn, params) do def register(conn, params) do
with {:ok, user} <- TwitterAPI.register_user(params) do with {:ok, user} <- TwitterAPI.register_user(params) do
render(conn, UserView, "show.json", %{user: user}) render(conn, UserView, "show.json", %{user: user})

View File

@ -284,9 +284,11 @@ test "unannouncing a previously announced object" do
{:ok, announce_activity, object} = ActivityPub.announce(user, object) {:ok, announce_activity, object} = ActivityPub.announce(user, object)
assert object.data["announcement_count"] == 1 assert object.data["announcement_count"] == 1
{:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object) {:ok, unannounce_activity, activity, object} = ActivityPub.unannounce(user, object)
assert object.data["announcement_count"] == 0 assert object.data["announcement_count"] == 0
assert activity == announce_activity
assert unannounce_activity.data["to"] == [ assert unannounce_activity.data["to"] == [
User.ap_followers(user), User.ap_followers(user),
note_activity.data["actor"] note_activity.data["actor"]