Add unliking to activitypub.

This commit is contained in:
Roger Braun 2017-04-14 18:08:47 +02:00
parent 8c2aad1cab
commit a926038cd0
4 changed files with 67 additions and 9 deletions

View File

@ -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.

View File

@ -24,7 +24,7 @@ def like(%User{ap_id: ap_id} = user, %Object{data: %{ "id" => id}} = object) 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,14 +47,46 @@ def like(%User{ap_id: ap_id} = user, %Object{data: %{ "id" => id}} = object) 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_object_in_activities(object)
{:ok, activity, object}
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. # Update activities that already had this. Could be done in a seperate process.
relevant_activities = Activity.all_by_object_ap_id(id) relevant_activities = Activity.all_by_object_ap_id(id)
Enum.map(relevant_activities, fn (activity) -> Enum.map(relevant_activities, fn (activity) ->
new_activity_data = activity.data |> Map.put("object", new_data) new_activity_data = activity.data |> Map.put("object", object.data)
changeset = Ecto.Changeset.change(activity, data: new_activity_data) changeset = Ecto.Changeset.change(activity, data: new_activity_data)
Repo.update(changeset) Repo.update(changeset)
end) end)
{:ok, activity, object} 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
end end

View File

@ -22,7 +22,9 @@ def note_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{

View File

@ -142,6 +142,26 @@ test "adds a like activity to the db" 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"}