mirror of
https://git.pleroma.social/pleroma/pleroma.git
synced 2024-12-23 00:26:30 +00:00
Add liking to ActivityPub.
This commit is contained in:
parent
f97c8e4379
commit
1388054796
2 changed files with 56 additions and 2 deletions
|
@ -1,10 +1,12 @@
|
||||||
defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
||||||
alias Pleroma.Repo
|
alias Pleroma.Repo
|
||||||
alias Pleroma.{Activity, Object, Upload}
|
alias Pleroma.{Activity, Object, Upload, User}
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
|
||||||
def insert(map) when is_map(map) do
|
def insert(map) when is_map(map) do
|
||||||
map = Map.put_new_lazy(map, "id", &generate_activity_id/0)
|
map = map
|
||||||
|
|> Map.put_new_lazy("id", &generate_activity_id/0)
|
||||||
|
|> Map.put_new_lazy("published", &make_date/0)
|
||||||
|
|
||||||
map = if is_map(map["object"]) do
|
map = if is_map(map["object"]) do
|
||||||
object = Map.put_new_lazy(map["object"], "id", &generate_object_id/0)
|
object = Map.put_new_lazy(map["object"], "id", &generate_object_id/0)
|
||||||
|
@ -17,6 +19,30 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
||||||
Repo.insert(%Activity{data: map})
|
Repo.insert(%Activity{data: map})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def like(%User{ap_id: ap_id}, object = %Object{data: %{ "id" => id}}) do
|
||||||
|
data = %{
|
||||||
|
"type" => "Like",
|
||||||
|
"actor" => ap_id,
|
||||||
|
"object" => id
|
||||||
|
}
|
||||||
|
|
||||||
|
{:ok, activity} = insert(data)
|
||||||
|
like_count = (object.data["like_count"] || 0) + 1
|
||||||
|
new_data = object.data |> Map.put("like_count", like_count)
|
||||||
|
changeset = Ecto.Changeset.change(object, data: new_data)
|
||||||
|
{:ok, object} = Repo.update(changeset)
|
||||||
|
|
||||||
|
# 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", new_data)
|
||||||
|
changeset = Ecto.Changeset.change(activity, data: new_activity_data)
|
||||||
|
Repo.update(changeset)
|
||||||
|
end)
|
||||||
|
|
||||||
|
{:ok, activity, object}
|
||||||
|
end
|
||||||
|
|
||||||
def generate_activity_id do
|
def generate_activity_id do
|
||||||
generate_id("activities")
|
generate_id("activities")
|
||||||
end
|
end
|
||||||
|
@ -74,4 +100,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
||||||
data = Upload.store(file)
|
data = Upload.store(file)
|
||||||
Repo.insert(%Object{data: data})
|
Repo.insert(%Object{data: data})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp make_date do
|
||||||
|
DateTime.utc_now() |> DateTime.to_iso8601
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,6 +4,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
|
||||||
alias Pleroma.{Activity, Object}
|
alias Pleroma.{Activity, Object}
|
||||||
alias Pleroma.Builders.ActivityBuilder
|
alias Pleroma.Builders.ActivityBuilder
|
||||||
|
|
||||||
|
import Pleroma.Factory
|
||||||
|
|
||||||
describe "insertion" do
|
describe "insertion" do
|
||||||
test "inserts a given map into the activity database, giving it an id if it has none." do
|
test "inserts a given map into the activity database, giving it an id if it has none." do
|
||||||
data = %{
|
data = %{
|
||||||
|
@ -110,6 +112,28 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "like an object" do
|
||||||
|
test "adds a like activity to the db" do
|
||||||
|
note_activity = insert(:note_activity)
|
||||||
|
object = Object.get_by_ap_id(note_activity.data["object"]["id"])
|
||||||
|
user = insert(:user)
|
||||||
|
user_two = insert(:user)
|
||||||
|
|
||||||
|
{:ok, like_activity, object} = ActivityPub.like(user, object)
|
||||||
|
|
||||||
|
assert like_activity.data["actor"] == user.ap_id
|
||||||
|
assert like_activity.data["type"] == "Like"
|
||||||
|
assert like_activity.data["object"] == object.data["id"]
|
||||||
|
assert object.data["like_count"] == 1
|
||||||
|
|
||||||
|
[note_activity] = Activity.all_by_object_ap_id(object.data["id"])
|
||||||
|
assert note_activity.data["object"]["like_count"] == 1
|
||||||
|
|
||||||
|
{:ok, _like_activity, object} = ActivityPub.like(user_two, object)
|
||||||
|
assert object.data["like_count"] == 2
|
||||||
|
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