mirror of
https://akkoma.dev/AkkomaGang/akkoma.git
synced 2024-12-17 23:38:19 +00:00
Add unliking to activitypub.
This commit is contained in:
parent
8c2aad1cab
commit
a926038cd0
4
TODO.txt
4
TODO.txt
|
@ -1 +1,5 @@
|
||||||
- Add cache for user fetching / representing. (mostly in TwitterAPI.activity_to_status)
|
- Add cache for user fetching / representing. (mostly in TwitterAPI.activity_to_status)
|
||||||
|
|
||||||
|
Unliking:
|
||||||
|
|
||||||
|
- Add a proper undo activity, find out how to ignore those in twitter api.
|
||||||
|
|
|
@ -24,7 +24,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
||||||
# There's already a like here, so return the original activity.
|
# There's already a like here, so return the original activity.
|
||||||
ap_id in (object.data["likes"] || []) ->
|
ap_id in (object.data["likes"] || []) ->
|
||||||
query = from activity in Activity,
|
query = from activity in Activity,
|
||||||
where: fragment("? @> ?", activity.data, ^%{actor: ap_id, object: id})
|
where: fragment("? @> ?", activity.data, ^%{actor: ap_id, object: id, type: "Like"})
|
||||||
|
|
||||||
activity = Repo.one(query)
|
activity = Repo.one(query)
|
||||||
{:ok, activity, object}
|
{:ok, activity, object}
|
||||||
|
@ -47,17 +47,49 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
||||||
changeset = Ecto.Changeset.change(object, data: new_data)
|
changeset = Ecto.Changeset.change(object, data: new_data)
|
||||||
{:ok, object} = Repo.update(changeset)
|
{:ok, object} = Repo.update(changeset)
|
||||||
|
|
||||||
# Update activities that already had this. Could be done in a seperate process.
|
update_object_in_activities(object)
|
||||||
relevant_activities = Activity.all_by_object_ap_id(id)
|
|
||||||
Enum.map(relevant_activities, fn (activity) ->
|
|
||||||
new_activity_data = activity.data |> Map.put("object", new_data)
|
|
||||||
changeset = Ecto.Changeset.change(activity, data: new_activity_data)
|
|
||||||
Repo.update(changeset)
|
|
||||||
end)
|
|
||||||
{:ok, activity, object}
|
{:ok, activity, object}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp update_object_in_activities(%{data: %{"id" => id}} = object) do
|
||||||
|
# Update activities that already had this. Could be done in a seperate process.
|
||||||
|
relevant_activities = Activity.all_by_object_ap_id(id)
|
||||||
|
Enum.map(relevant_activities, fn (activity) ->
|
||||||
|
new_activity_data = activity.data |> Map.put("object", object.data)
|
||||||
|
changeset = Ecto.Changeset.change(activity, data: new_activity_data)
|
||||||
|
Repo.update(changeset)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
def unlike(%User{ap_id: ap_id} = user, %Object{data: %{ "id" => id}} = object) do
|
||||||
|
query = from activity in Activity,
|
||||||
|
where: fragment("? @> ?", activity.data, ^%{actor: ap_id, object: id, type: "Like"})
|
||||||
|
|
||||||
|
activity = Repo.one(query)
|
||||||
|
|
||||||
|
if activity do
|
||||||
|
# just delete for now...
|
||||||
|
{:ok, _activity} = Repo.delete(activity)
|
||||||
|
|
||||||
|
likes = (object.data["likes"] || []) |> List.delete(ap_id)
|
||||||
|
|
||||||
|
new_data = object.data
|
||||||
|
|> Map.put("like_count", length(likes))
|
||||||
|
|> Map.put("likes", likes)
|
||||||
|
|
||||||
|
changeset = Ecto.Changeset.change(object, data: new_data)
|
||||||
|
{:ok, object} = Repo.update(changeset)
|
||||||
|
|
||||||
|
update_object_in_activities(object)
|
||||||
|
|
||||||
|
{:ok, object}
|
||||||
|
else
|
||||||
|
{:ok, object}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def generate_activity_id do
|
def generate_activity_id do
|
||||||
generate_id("activities")
|
generate_id("activities")
|
||||||
end
|
end
|
||||||
|
|
|
@ -22,7 +22,9 @@ defmodule Pleroma.Factory do
|
||||||
"id" => Pleroma.Web.ActivityPub.ActivityPub.generate_object_id,
|
"id" => Pleroma.Web.ActivityPub.ActivityPub.generate_object_id,
|
||||||
"actor" => user.ap_id,
|
"actor" => user.ap_id,
|
||||||
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
|
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
|
||||||
"published_at" => DateTime.utc_now() |> DateTime.to_iso8601
|
"published_at" => DateTime.utc_now() |> DateTime.to_iso8601,
|
||||||
|
"likes" => [],
|
||||||
|
"like_count" => 0
|
||||||
}
|
}
|
||||||
|
|
||||||
%Pleroma.Object{
|
%Pleroma.Object{
|
||||||
|
|
|
@ -142,6 +142,26 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "unliking" do
|
||||||
|
test "unliking a previously liked object" do
|
||||||
|
note_activity = insert(:note_activity)
|
||||||
|
object = Object.get_by_ap_id(note_activity.data["object"]["id"])
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
# Unliking something that hasn't been liked does nothing
|
||||||
|
{:ok, object} = ActivityPub.unlike(user, object)
|
||||||
|
assert object.data["like_count"] == 0
|
||||||
|
|
||||||
|
{:ok, like_activity, object} = ActivityPub.like(user, object)
|
||||||
|
assert object.data["like_count"] == 1
|
||||||
|
|
||||||
|
{:ok, object} = ActivityPub.unlike(user, object)
|
||||||
|
assert object.data["like_count"] == 0
|
||||||
|
|
||||||
|
assert Repo.get(Activity, like_activity.id) == nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "uploading files" do
|
describe "uploading files" do
|
||||||
test "copies the file to the configured folder" do
|
test "copies the file to the configured folder" do
|
||||||
file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
|
file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
|
||||||
|
|
Loading…
Reference in a new issue