end
end
+ def unannounce(%User{} = actor, %Object{} = object) do
+ with %Activity{} = activity <- get_existing_announce(actor.ap_id, object),
+ {:ok, _activity} <- Repo.delete(activity),
+ {:ok, object} <- remove_announce_from_object(activity, object) do
+ {:ok, object}
+ else
+ _e -> {:ok, object}
+ end
+ end
+
def follow(follower, followed, activity_id \\ nil, local \\ true) do
with data <- make_follow_data(follower, followed, activity_id),
{:ok, activity} <- insert(data, local),
@doc """
Make announce activity data for the given actor and object
"""
+ def get_existing_announce(actor, %{data: %{"id" => id}}) do
+ query =
+ from(
+ activity in Activity,
+ where: fragment("(?)->>'actor' = ?", activity.data, ^actor),
+ # this is to use the index
+ where:
+ fragment(
+ "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
+ activity.data,
+ activity.data,
+ ^id
+ ),
+ where: fragment("(?)->>'type' = 'Announce'", activity.data)
+ )
+
+ Repo.one(query)
+ end
+
def make_announce_data(
%User{ap_id: ap_id} = user,
%Object{data: %{"id" => id}} = object,
end
end
+ def remove_announce_from_object(%Activity{data: %{"actor" => actor}}, object) do
+ with announcements <- (object.data["announcements"] || []) |> List.delete(actor) do
+ update_element_in_object("announcement", announcements, object)
+ end
+ end
+
#### Unfollow-related helpers
def make_unfollow_data(follower, followed, follow_activity) do
end
end
+ def unrepeat(id_or_ap_id, user) do
+ with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
+ object <- Object.get_by_ap_id(activity.data["object"]["id"]) do
+ ActivityPub.unannounce(user, object)
+ else
+ _ ->
+ {:error, "Could not unrepeat"}
+ 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,
end
end
+ def unreblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
+ with {:ok, announce, _activity} = CommonAPI.unrepeat(ap_id_or_id, user) do
+ render(conn, StatusView, "status.json", %{activity: announce, for: user, as: :activity})
+ end
+ end
+
def fav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
with {:ok, _fav, %{data: %{"id" => id}}} = CommonAPI.favorite(ap_id_or_id, user),
%Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
delete("/statuses/:id", MastodonAPIController, :delete_status)
post("/statuses/:id/reblog", MastodonAPIController, :reblog_status)
+ post("/statuses/:id/unreblog", MastodonAPIController, :unreblog_status)
post("/statuses/:id/favourite", MastodonAPIController, :fav_status)
post("/statuses/:id/unfavourite", MastodonAPIController, :unfav_status)
end
end
+ def unrepeat(%User{} = user, ap_id_or_id) do
+ with {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.unrepeat(ap_id_or_id, user),
+ %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
+ {:ok, activity}
+ 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) do
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
with {:ok, user} <- TwitterAPI.register_user(params) do
render(conn, UserView, "show.json", %{user: user})