forked from AkkomaGang/akkoma
AP refactoring.
This commit is contained in:
parent
423194520e
commit
70024632ba
12 changed files with 334 additions and 247 deletions
|
@ -9,6 +9,11 @@ defmodule Pleroma.Object do
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def create(data) do
|
||||||
|
Object.change(%Object{}, %{data: data})
|
||||||
|
|> Repo.insert
|
||||||
|
end
|
||||||
|
|
||||||
def change(struct, params \\ %{}) do
|
def change(struct, params \\ %{}) do
|
||||||
changeset = struct
|
changeset = struct
|
||||||
|> cast(params, [:data])
|
|> cast(params, [:data])
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
defmodule Pleroma.Plugs.AuthenticationPlug do
|
defmodule Pleroma.Plugs.AuthenticationPlug do
|
||||||
alias Comeonin.Pbkdf2
|
alias Comeonin.Pbkdf2
|
||||||
import Plug.Conn
|
import Plug.Conn
|
||||||
|
alias Pleroma.User
|
||||||
|
|
||||||
def init(options) do
|
def init(options) do
|
||||||
options
|
options
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def call(%{assigns: %{user: %User{}}} = conn, _), do: conn
|
||||||
|
|
||||||
def call(conn, opts) do
|
def call(conn, opts) do
|
||||||
with {:ok, username, password} <- decode_header(conn),
|
with {:ok, username, password} <- decode_header(conn),
|
||||||
{:ok, user} <- opts[:fetcher].(username),
|
{:ok, user} <- opts[:fetcher].(username),
|
||||||
|
|
|
@ -6,6 +6,7 @@ defmodule Pleroma.User do
|
||||||
alias Comeonin.Pbkdf2
|
alias Comeonin.Pbkdf2
|
||||||
alias Pleroma.Web.{OStatus, Websub}
|
alias Pleroma.Web.{OStatus, Websub}
|
||||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||||
|
alias Pleroma.Web.ActivityPub.Utils
|
||||||
|
|
||||||
schema "users" do
|
schema "users" do
|
||||||
field :bio, :string
|
field :bio, :string
|
||||||
|
@ -126,7 +127,7 @@ def unfollow(%User{} = follower, %User{} = followed) do
|
||||||
{ :ok, follower } = follower
|
{ :ok, follower } = follower
|
||||||
|> follow_changeset(%{following: following})
|
|> follow_changeset(%{following: following})
|
||||||
|> Repo.update
|
|> Repo.update
|
||||||
{ :ok, follower, ActivityPub.fetch_latest_follow(follower, followed)}
|
{ :ok, follower, Utils.fetch_latest_follow(follower, followed)}
|
||||||
else
|
else
|
||||||
{:error, "Not subscribed!"}
|
{:error, "Not subscribed!"}
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,261 +2,78 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
||||||
alias Pleroma.{Activity, Repo, Object, Upload, User, Web}
|
alias Pleroma.{Activity, Repo, Object, Upload, User, Web}
|
||||||
alias Ecto.{Changeset, UUID}
|
alias Ecto.{Changeset, UUID}
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
import Pleroma.Web.ActivityPub.Utils
|
||||||
require Logger
|
require Logger
|
||||||
|
|
||||||
def insert(map, local \\ true) when is_map(map) do
|
def insert(map, local \\ true) when is_map(map) do
|
||||||
map = map
|
with nil <- Activity.get_by_ap_id(map["id"]),
|
||||||
|> Map.put_new_lazy("id", &generate_activity_id/0)
|
map <- lazy_put_activity_defaults(map),
|
||||||
|> Map.put_new_lazy("published", &make_date/0)
|
:ok <- insert_full_object(map) do
|
||||||
|
|
||||||
with %Activity{} = activity <- Activity.get_by_ap_id(map["id"]) do
|
|
||||||
Logger.debug(fn -> "Already have activity, #{activity.id}, not inserting." end)
|
|
||||||
{:ok, activity}
|
|
||||||
else _e ->
|
|
||||||
map = if is_map(map["object"]) do
|
|
||||||
object = Map.put_new_lazy(map["object"], "id", &generate_object_id/0)
|
|
||||||
Repo.insert!(%Object{data: object})
|
|
||||||
Map.put(map, "object", object)
|
|
||||||
else
|
|
||||||
map
|
|
||||||
end
|
|
||||||
|
|
||||||
Repo.insert(%Activity{data: map, local: local})
|
Repo.insert(%Activity{data: map, local: local})
|
||||||
|
else
|
||||||
|
%Activity{} = activity -> {:ok, activity}
|
||||||
|
error -> {:error, error}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def create(to, actor, context, object, additional \\ %{}, published \\ nil, local \\ true) do
|
def create(to, actor, context, object, additional \\ %{}, published \\ nil, local \\ true) do
|
||||||
published = published || make_date()
|
with create_data <- make_create_data(%{to: to, actor: actor, published: published, context: context, object: object}, additional),
|
||||||
|
{:ok, activity} <- insert(create_data, local),
|
||||||
activity = %{
|
:ok <- maybe_federate(activity) do
|
||||||
"type" => "Create",
|
|
||||||
"to" => to |> Enum.uniq,
|
|
||||||
"actor" => actor.ap_id,
|
|
||||||
"object" => object,
|
|
||||||
"published" => published,
|
|
||||||
"context" => context
|
|
||||||
}
|
|
||||||
|> Map.merge(additional)
|
|
||||||
|
|
||||||
with {:ok, activity} <- insert(activity, local) do
|
|
||||||
if actor.local do
|
|
||||||
Pleroma.Web.Federator.enqueue(:publish, activity)
|
|
||||||
end
|
|
||||||
|
|
||||||
{:ok, activity}
|
{:ok, activity}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# TODO: This is weird, maybe we shouldn't check here if we can make the activity.
|
||||||
def like(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, activity_id \\ nil, local \\ true) do
|
def like(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, activity_id \\ nil, local \\ true) do
|
||||||
cond do
|
with nil <- get_existing_like(ap_id, object),
|
||||||
# There's already a like here, so return the original activity.
|
like_data <- make_like_data(user, object, activity_id),
|
||||||
ap_id in (object.data["likes"] || []) ->
|
{:ok, activity} <- insert(like_data, local),
|
||||||
query = from activity in Activity,
|
{:ok, object} <- add_like_to_object(activity, object),
|
||||||
where: fragment("? @> ?", activity.data, ^%{actor: ap_id, object: id, type: "Like"})
|
:ok <- maybe_federate(activity) do
|
||||||
|
|
||||||
activity = Repo.one(query)
|
|
||||||
{:ok, activity, object}
|
|
||||||
true ->
|
|
||||||
data = %{
|
|
||||||
"type" => "Like",
|
|
||||||
"actor" => ap_id,
|
|
||||||
"object" => id,
|
|
||||||
"to" => [User.ap_followers(user), object.data["actor"]],
|
|
||||||
"context" => object.data["context"]
|
|
||||||
}
|
|
||||||
|
|
||||||
data = if activity_id, do: Map.put(data, "id", activity_id), else: data
|
|
||||||
|
|
||||||
{:ok, activity} = insert(data, local)
|
|
||||||
|
|
||||||
likes = [ap_id | (object.data["likes"] || [])] |> Enum.uniq
|
|
||||||
|
|
||||||
new_data = object.data
|
|
||||||
|> Map.put("like_count", length(likes))
|
|
||||||
|> Map.put("likes", likes)
|
|
||||||
|
|
||||||
changeset = Changeset.change(object, data: new_data)
|
|
||||||
{:ok, object} = Repo.update(changeset)
|
|
||||||
|
|
||||||
update_object_in_activities(object)
|
|
||||||
|
|
||||||
if user.local do
|
|
||||||
Pleroma.Web.Federator.enqueue(:publish, activity)
|
|
||||||
end
|
|
||||||
|
|
||||||
{:ok, activity, object}
|
{:ok, activity, object}
|
||||||
|
else
|
||||||
|
%Activity{} = activity -> {:ok, activity, object}
|
||||||
|
error -> {:error, error}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp update_object_in_activities(%{data: %{"id" => id}} = object) do
|
def unlike(%User{} = actor, %Object{} = object) do
|
||||||
# TODO
|
with %Activity{} = activity <- get_existing_like(actor.ap_id, object),
|
||||||
# Update activities that already had this. Could be done in a seperate process.
|
{:ok, _activity} <- Repo.delete(activity),
|
||||||
# Alternatively, just don't do this and fetch the current object each time. Most
|
{:ok, object} <- remove_like_from_object(activity, object) do
|
||||||
# could probably be taken from cache.
|
|
||||||
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 = Changeset.change(activity, data: new_activity_data)
|
|
||||||
Repo.update(changeset)
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
def unlike(%User{ap_id: ap_id}, %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 = Changeset.change(object, data: new_data)
|
|
||||||
{:ok, object} = Repo.update(changeset)
|
|
||||||
|
|
||||||
update_object_in_activities(object)
|
|
||||||
|
|
||||||
{:ok, object}
|
|
||||||
else
|
|
||||||
{:ok, object}
|
{:ok, object}
|
||||||
|
else _e -> {:ok, object}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate_activity_id do
|
|
||||||
generate_id("activities")
|
|
||||||
end
|
|
||||||
|
|
||||||
def generate_context_id do
|
|
||||||
generate_id("contexts")
|
|
||||||
end
|
|
||||||
|
|
||||||
def generate_object_id do
|
|
||||||
Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :object, Ecto.UUID.generate)
|
|
||||||
end
|
|
||||||
|
|
||||||
def generate_id(type) do
|
|
||||||
"#{Web.base_url()}/#{type}/#{UUID.generate}"
|
|
||||||
end
|
|
||||||
|
|
||||||
def fetch_public_activities(opts \\ %{}) do
|
|
||||||
public = ["https://www.w3.org/ns/activitystreams#Public"]
|
|
||||||
fetch_activities(public, opts)
|
|
||||||
end
|
|
||||||
|
|
||||||
def fetch_activities(recipients, opts \\ %{}) do
|
|
||||||
since_id = opts["since_id"] || 0
|
|
||||||
|
|
||||||
query = from activity in Activity,
|
|
||||||
limit: 20,
|
|
||||||
order_by: [desc: :inserted_at]
|
|
||||||
|
|
||||||
query = Enum.reduce(recipients, query, fn (recipient, q) ->
|
|
||||||
map = %{ to: [recipient] }
|
|
||||||
from activity in q,
|
|
||||||
or_where: fragment(~s(? @> ?), activity.data, ^map)
|
|
||||||
end)
|
|
||||||
|
|
||||||
query = from activity in query,
|
|
||||||
where: activity.id > ^since_id
|
|
||||||
|
|
||||||
query = if opts["local_only"] do
|
|
||||||
from activity in query, where: activity.local == true
|
|
||||||
else
|
|
||||||
query
|
|
||||||
end
|
|
||||||
|
|
||||||
query = if opts["max_id"] do
|
|
||||||
from activity in query, where: activity.id < ^opts["max_id"]
|
|
||||||
else
|
|
||||||
query
|
|
||||||
end
|
|
||||||
|
|
||||||
query = if opts["actor_id"] do
|
|
||||||
from activity in query,
|
|
||||||
where: fragment("? @> ?", activity.data, ^%{actor: opts["actor_id"]})
|
|
||||||
else
|
|
||||||
query
|
|
||||||
end
|
|
||||||
|
|
||||||
Enum.reverse(Repo.all(query))
|
|
||||||
end
|
|
||||||
|
|
||||||
def announce(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, activity_id \\ nil, local \\ true) do
|
def announce(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, activity_id \\ nil, local \\ true) do
|
||||||
data = %{
|
with announce_data <- make_announce_data(user, object, activity_id),
|
||||||
"type" => "Announce",
|
{:ok, activity} <- insert(announce_data, local),
|
||||||
"actor" => ap_id,
|
{:ok, object} <- add_announce_to_object(activity, object),
|
||||||
"object" => id,
|
:ok <- maybe_federate(activity) do
|
||||||
"to" => [User.ap_followers(user), object.data["actor"]],
|
|
||||||
"context" => object.data["context"]
|
|
||||||
}
|
|
||||||
|
|
||||||
data = if activity_id, do: Map.put(data, "id", activity_id), else: data
|
|
||||||
|
|
||||||
{:ok, activity} = insert(data, local)
|
|
||||||
|
|
||||||
announcements = [ap_id | (object.data["announcements"] || [])] |> Enum.uniq
|
|
||||||
|
|
||||||
new_data = object.data
|
|
||||||
|> Map.put("announcement_count", length(announcements))
|
|
||||||
|> Map.put("announcements", announcements)
|
|
||||||
|
|
||||||
changeset = Changeset.change(object, data: new_data)
|
|
||||||
{:ok, object} = Repo.update(changeset)
|
|
||||||
|
|
||||||
update_object_in_activities(object)
|
|
||||||
|
|
||||||
if user.local do
|
|
||||||
Pleroma.Web.Federator.enqueue(:publish, activity)
|
|
||||||
end
|
|
||||||
|
|
||||||
{:ok, activity, object}
|
{:ok, activity, object}
|
||||||
|
else
|
||||||
|
error -> {:error, error}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def follow(%User{ap_id: follower_id, local: actor_local}, %User{ap_id: followed_id}, activity_id \\ nil, local \\ true) do
|
def follow(follower, followed, activity_id \\ nil, local \\ true) do
|
||||||
data = %{
|
with data <- make_follow_data(follower, followed, activity_id),
|
||||||
"type" => "Follow",
|
{:ok, activity} <- insert(data, local),
|
||||||
"actor" => follower_id,
|
:ok <- maybe_federate(activity) do
|
||||||
"to" => [followed_id],
|
|
||||||
"object" => followed_id,
|
|
||||||
"published" => make_date()
|
|
||||||
}
|
|
||||||
|
|
||||||
data = if activity_id, do: Map.put(data, "id", activity_id), else: data
|
|
||||||
|
|
||||||
with {:ok, activity} <- insert(data, local) do
|
|
||||||
if actor_local do
|
|
||||||
Pleroma.Web.Federator.enqueue(:publish, activity)
|
|
||||||
end
|
|
||||||
|
|
||||||
{:ok, activity}
|
{:ok, activity}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def unfollow(follower, followed, local \\ true) do
|
def unfollow(follower, followed, local \\ true) do
|
||||||
with follow_activity when not is_nil(follow_activity) <- fetch_latest_follow(follower, followed) do
|
with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
|
||||||
data = %{
|
unfollow_data <- make_unfollow_data(follower, followed, follow_activity),
|
||||||
"type" => "Undo",
|
{:ok, activity} <- insert(unfollow_data, local),
|
||||||
"actor" => follower.ap_id,
|
:ok, maybe_federate(activity) do
|
||||||
"to" => [followed.ap_id],
|
|
||||||
"object" => follow_activity.data["id"],
|
|
||||||
"published" => make_date()
|
|
||||||
}
|
|
||||||
|
|
||||||
with {:ok, activity} <- insert(data, local) do
|
|
||||||
if follower.local do
|
|
||||||
Pleroma.Web.Federator.enqueue(:publish, activity)
|
|
||||||
end
|
|
||||||
|
|
||||||
{:ok, activity}
|
{:ok, activity}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def fetch_activities_for_context(context) do
|
def fetch_activities_for_context(context) do
|
||||||
query = from activity in Activity,
|
query = from activity in Activity,
|
||||||
|
@ -264,22 +81,57 @@ def fetch_activities_for_context(context) do
|
||||||
Repo.all(query)
|
Repo.all(query)
|
||||||
end
|
end
|
||||||
|
|
||||||
def fetch_latest_follow(%User{ap_id: follower_id},
|
def fetch_public_activities(opts \\ %{}) do
|
||||||
%User{ap_id: followed_id}) do
|
public = ["https://www.w3.org/ns/activitystreams#Public"]
|
||||||
query = from activity in Activity,
|
fetch_activities(public, opts)
|
||||||
where: fragment("? @> ?", activity.data, ^%{type: "Follow", actor: follower_id,
|
end
|
||||||
object: followed_id}),
|
|
||||||
order_by: [desc: :inserted_at],
|
defp restrict_since(query, %{"since_id" => since_id}) do
|
||||||
limit: 1
|
from activity in query, where: activity.id > ^since_id
|
||||||
Repo.one(query)
|
end
|
||||||
|
defp restrict_since(query, _), do: query
|
||||||
|
|
||||||
|
defp restrict_recipients(query, recipients) do
|
||||||
|
Enum.reduce(recipients, query, fn (recipient, q) ->
|
||||||
|
map = %{ to: [recipient] }
|
||||||
|
from activity in q,
|
||||||
|
or_where: fragment(~s(? @> ?), activity.data, ^map)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp restrict_local(query, %{"local_only" => true}) do
|
||||||
|
from activity in query, where: activity.local == true
|
||||||
|
end
|
||||||
|
defp restrict_local(query, _), do: query
|
||||||
|
|
||||||
|
defp restrict_max(query, %{"max_id" => max_id}) do
|
||||||
|
from activity in query, where: activity.id < ^max_id
|
||||||
|
end
|
||||||
|
defp restrict_max(query, _), do: query
|
||||||
|
|
||||||
|
defp restrict_actor(query, %{"actor_id" => actor_id}) do
|
||||||
|
from activity in query,
|
||||||
|
where: fragment("? @> ?", activity.data, ^%{actor: actor_id})
|
||||||
|
end
|
||||||
|
defp restrict_actor(query, _), do: query
|
||||||
|
|
||||||
|
def fetch_activities(recipients, opts \\ %{}) do
|
||||||
|
base_query = from activity in Activity,
|
||||||
|
limit: 20,
|
||||||
|
order_by: [desc: :inserted_at]
|
||||||
|
|
||||||
|
base_query
|
||||||
|
|> restrict_recipients(recipients)
|
||||||
|
|> restrict_since(opts)
|
||||||
|
|> restrict_local(opts)
|
||||||
|
|> restrict_max(opts)
|
||||||
|
|> restrict_actor(opts)
|
||||||
|
|> Repo.all
|
||||||
|
|> Enum.reverse
|
||||||
end
|
end
|
||||||
|
|
||||||
def upload(file) do
|
def upload(file) 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
|
||||||
|
|
210
lib/pleroma/web/activity_pub/utils.ex
Normal file
210
lib/pleroma/web/activity_pub/utils.ex
Normal file
|
@ -0,0 +1,210 @@
|
||||||
|
defmodule Pleroma.Web.ActivityPub.Utils do
|
||||||
|
alias Pleroma.{Repo, Web, Object, Activity, User}
|
||||||
|
alias Pleroma.Web.Router.Helpers
|
||||||
|
alias Pleroma.Web.Endpoint
|
||||||
|
alias Ecto.{Changeset, UUID}
|
||||||
|
import Ecto.Query
|
||||||
|
|
||||||
|
def make_date do
|
||||||
|
DateTime.utc_now() |> DateTime.to_iso8601
|
||||||
|
end
|
||||||
|
|
||||||
|
def generate_activity_id do
|
||||||
|
generate_id("activities")
|
||||||
|
end
|
||||||
|
|
||||||
|
def generate_context_id do
|
||||||
|
generate_id("contexts")
|
||||||
|
end
|
||||||
|
|
||||||
|
def generate_object_id do
|
||||||
|
Helpers.o_status_url(Endpoint, :object, UUID.generate)
|
||||||
|
end
|
||||||
|
|
||||||
|
def generate_id(type) do
|
||||||
|
"#{Web.base_url()}/#{type}/#{UUID.generate}"
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Enqueues an activity for federation if it's local
|
||||||
|
"""
|
||||||
|
def maybe_federate(%Activity{local: true} = activity) do
|
||||||
|
Pleroma.Web.Federator.enqueue(:publish, activity)
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
def maybe_federate(_), do: :ok
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Adds an id and a published data if they aren't there,
|
||||||
|
also adds it to an included object
|
||||||
|
"""
|
||||||
|
def lazy_put_activity_defaults(map) do
|
||||||
|
map = map
|
||||||
|
|> Map.put_new_lazy("id", &generate_activity_id/0)
|
||||||
|
|> Map.put_new_lazy("published", &make_date/0)
|
||||||
|
|
||||||
|
if is_map(map["object"]) do
|
||||||
|
object = lazy_put_object_defaults(map["object"])
|
||||||
|
%{map | "object" => object}
|
||||||
|
else
|
||||||
|
map
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Adds an id and published date if they aren't there.
|
||||||
|
"""
|
||||||
|
def lazy_put_object_defaults(map) do
|
||||||
|
map
|
||||||
|
|> Map.put_new_lazy("id", &generate_object_id/0)
|
||||||
|
|> Map.put_new_lazy("published", &make_date/0)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Inserts a full object if it is contained in an activity.
|
||||||
|
"""
|
||||||
|
def insert_full_object(%{"object" => object_data}) when is_map(object_data) do
|
||||||
|
with {:ok, object} <- Object.create(object_data) do
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
end
|
||||||
|
def insert_full_object(_), do: :ok
|
||||||
|
|
||||||
|
def update_object_in_activities(%{data: %{"id" => id}} = object) do
|
||||||
|
# TODO
|
||||||
|
# Update activities that already had this. Could be done in a seperate process.
|
||||||
|
# Alternatively, just don't do this and fetch the current object each time. Most
|
||||||
|
# could probably be taken from cache.
|
||||||
|
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 = Changeset.change(activity, data: new_activity_data)
|
||||||
|
Repo.update(changeset)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
#### Like-related helpers
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Returns an existing like if a user already liked an object
|
||||||
|
"""
|
||||||
|
def get_existing_like(actor, %{data: %{"id" => id}} = object) do
|
||||||
|
query = from activity in Activity,
|
||||||
|
where: fragment("? @> ?", activity.data, ^%{actor: actor, object: id, type: "Like"})
|
||||||
|
Repo.one(query)
|
||||||
|
end
|
||||||
|
|
||||||
|
def make_like_data(%User{ap_id: ap_id} = actor, %{data: %{"id" => id}} = object, activity_id) do
|
||||||
|
data = %{
|
||||||
|
"type" => "Like",
|
||||||
|
"actor" => ap_id,
|
||||||
|
"object" => id,
|
||||||
|
"to" => [User.ap_followers(actor), object.data["actor"]],
|
||||||
|
"context" => object.data["context"]
|
||||||
|
}
|
||||||
|
|
||||||
|
if activity_id, do: Map.put(data, "id", activity_id), else: data
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_element_in_object(property, element, object) do
|
||||||
|
with new_data <- object.data |> Map.put("#{property}_count", length(element)) |> Map.put("#{property}s", element),
|
||||||
|
changeset <- Changeset.change(object, data: new_data),
|
||||||
|
{:ok, object} <- Repo.update(changeset),
|
||||||
|
_ <- update_object_in_activities(object) do
|
||||||
|
{:ok, object}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_likes_in_object(likes, object) do
|
||||||
|
update_element_in_object("like", likes, object)
|
||||||
|
end
|
||||||
|
|
||||||
|
def add_like_to_object(%Activity{data: %{"actor" => actor}}, object) do
|
||||||
|
with likes <- [actor | (object.data["likes"] || [])] |> Enum.uniq do
|
||||||
|
update_likes_in_object(likes, object)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove_like_from_object(%Activity{data: %{"actor" => actor}}, object) do
|
||||||
|
with likes <- (object.data["likes"] || []) |> List.delete(actor) do
|
||||||
|
update_likes_in_object(likes, object)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
#### Follow-related helpers
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Makes a follow activity data for the given follower and followed
|
||||||
|
"""
|
||||||
|
def make_follow_data(%User{ap_id: follower_id}, %User{ap_id: followed_id}, activity_id) do
|
||||||
|
data = %{
|
||||||
|
"type" => "Follow",
|
||||||
|
"actor" => follower_id,
|
||||||
|
"to" => [followed_id],
|
||||||
|
"object" => followed_id
|
||||||
|
}
|
||||||
|
|
||||||
|
if activity_id, do: Map.put(data, "id", activity_id), else: data
|
||||||
|
end
|
||||||
|
|
||||||
|
def fetch_latest_follow(%User{ap_id: follower_id},
|
||||||
|
%User{ap_id: followed_id}) do
|
||||||
|
query = from activity in Activity,
|
||||||
|
where: fragment("? @> ?", activity.data, ^%{type: "Follow", actor: follower_id,
|
||||||
|
object: followed_id}),
|
||||||
|
order_by: [desc: :inserted_at],
|
||||||
|
limit: 1
|
||||||
|
Repo.one(query)
|
||||||
|
end
|
||||||
|
|
||||||
|
#### Announce-related helpers
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Make announce activity data for the given actor and object
|
||||||
|
"""
|
||||||
|
def make_announce_data(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, activity_id) do
|
||||||
|
data = %{
|
||||||
|
"type" => "Announce",
|
||||||
|
"actor" => ap_id,
|
||||||
|
"object" => id,
|
||||||
|
"to" => [User.ap_followers(user), object.data["actor"]],
|
||||||
|
"context" => object.data["context"]
|
||||||
|
}
|
||||||
|
|
||||||
|
if activity_id, do: Map.put(data, "id", activity_id), else: data
|
||||||
|
end
|
||||||
|
|
||||||
|
def add_announce_to_object(%Activity{data: %{"actor" => actor}}, object) do
|
||||||
|
with announcements <- [actor | (object.data["announcements"] || [])] |> Enum.uniq do
|
||||||
|
update_element_in_object("announcement", announcements, object)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
#### Unfollow-related helpers
|
||||||
|
|
||||||
|
def make_unfollow_data(follower, followed, follow_activity) do
|
||||||
|
%{
|
||||||
|
"type" => "Undo",
|
||||||
|
"actor" => follower.ap_id,
|
||||||
|
"to" => [followed.ap_id],
|
||||||
|
"object" => follow_activity.data["id"]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
#### Create-related helpers
|
||||||
|
|
||||||
|
def make_create_data(params, additional) do
|
||||||
|
published = params.published || make_date()
|
||||||
|
|
||||||
|
activity = %{
|
||||||
|
"type" => "Create",
|
||||||
|
"to" => params.to |> Enum.uniq,
|
||||||
|
"actor" => params.actor.ap_id,
|
||||||
|
"object" => params.object,
|
||||||
|
"published" => published,
|
||||||
|
"context" => params.context
|
||||||
|
}
|
||||||
|
|> Map.merge(additional)
|
||||||
|
end
|
||||||
|
end
|
|
@ -7,6 +7,7 @@ defmodule Pleroma.Web.OStatus do
|
||||||
|
|
||||||
alias Pleroma.{Repo, User, Web, Object, Activity}
|
alias Pleroma.{Repo, User, Web, Object, Activity}
|
||||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||||
|
alias Pleroma.Web.ActivityPub.Utils
|
||||||
alias Pleroma.Web.{WebFinger, Websub}
|
alias Pleroma.Web.{WebFinger, Websub}
|
||||||
alias Pleroma.Web.OStatus.FollowHandler
|
alias Pleroma.Web.OStatus.FollowHandler
|
||||||
|
|
||||||
|
@ -158,7 +159,7 @@ def handle_note(entry, doc \\ nil) do
|
||||||
if String.length(context) > 0 do
|
if String.length(context) > 0 do
|
||||||
context
|
context
|
||||||
else
|
else
|
||||||
ActivityPub.generate_context_id
|
Utils.generate_context_id
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
||||||
alias Pleroma.{User, Activity, Repo, Object}
|
alias Pleroma.{User, Activity, Repo, Object}
|
||||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||||
|
alias Pleroma.Web.ActivityPub.Utils
|
||||||
alias Pleroma.Web.TwitterAPI.Representers.{ActivityRepresenter, UserRepresenter}
|
alias Pleroma.Web.TwitterAPI.Representers.{ActivityRepresenter, UserRepresenter}
|
||||||
alias Pleroma.Web.OStatus
|
alias Pleroma.Web.OStatus
|
||||||
|
|
||||||
|
@ -44,7 +45,7 @@ def add_attachments(text, attachments) do
|
||||||
|
|
||||||
def create_status(%User{} = user, %{"status" => status} = data) do
|
def create_status(%User{} = user, %{"status" => status} = data) do
|
||||||
attachments = attachments_from_ids(data["media_ids"])
|
attachments = attachments_from_ids(data["media_ids"])
|
||||||
context = ActivityPub.generate_context_id
|
context = Utils.generate_context_id
|
||||||
mentions = parse_mentions(status)
|
mentions = parse_mentions(status)
|
||||||
content_html = status
|
content_html = status
|
||||||
|> format_input(mentions)
|
|> format_input(mentions)
|
||||||
|
|
|
@ -2,12 +2,13 @@ defmodule Pleroma.Plugs.AuthenticationPlugTest do
|
||||||
use Pleroma.Web.ConnCase, async: true
|
use Pleroma.Web.ConnCase, async: true
|
||||||
|
|
||||||
alias Pleroma.Plugs.AuthenticationPlug
|
alias Pleroma.Plugs.AuthenticationPlug
|
||||||
|
alias Pleroma.User
|
||||||
|
|
||||||
defp fetch_nil(_name) do
|
defp fetch_nil(_name) do
|
||||||
{:ok, nil}
|
{:ok, nil}
|
||||||
end
|
end
|
||||||
|
|
||||||
@user %{
|
@user %User{
|
||||||
id: 1,
|
id: 1,
|
||||||
name: "dude",
|
name: "dude",
|
||||||
password_hash: Comeonin.Pbkdf2.hashpwsalt("guy")
|
password_hash: Comeonin.Pbkdf2.hashpwsalt("guy")
|
||||||
|
@ -129,6 +130,7 @@ test "it assigns the user", %{conn: conn} do
|
||||||
assert conn.halted == false
|
assert conn.halted == false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "with a user_id in the session for an existing user" do
|
describe "with a user_id in the session for an existing user" do
|
||||||
test "it assigns the user", %{conn: conn} do
|
test "it assigns the user", %{conn: conn} do
|
||||||
opts = %{
|
opts = %{
|
||||||
|
@ -150,4 +152,15 @@ test "it assigns the user", %{conn: conn} do
|
||||||
assert conn.halted == false
|
assert conn.halted == false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "with an assigned user" do
|
||||||
|
test "it does nothing, returning the incoming conn", %{conn: conn} do
|
||||||
|
conn = conn
|
||||||
|
|> assign(:user, @user)
|
||||||
|
|
||||||
|
conn_result = AuthenticationPlug.call(conn, %{})
|
||||||
|
|
||||||
|
assert conn == conn_result
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,7 +5,7 @@ defmodule Pleroma.Builders.ActivityBuilder do
|
||||||
def build(data \\ %{}, opts \\ %{}) do
|
def build(data \\ %{}, opts \\ %{}) do
|
||||||
user = opts[:user] || Pleroma.Factory.insert(:user)
|
user = opts[:user] || Pleroma.Factory.insert(:user)
|
||||||
activity = %{
|
activity = %{
|
||||||
"id" => Pleroma.Web.ActivityPub.ActivityPub.generate_object_id,
|
"id" => Pleroma.Web.ActivityPub.Utils.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"],
|
||||||
"object" => %{
|
"object" => %{
|
||||||
|
|
|
@ -19,7 +19,7 @@ def note_factory do
|
||||||
data = %{
|
data = %{
|
||||||
"type" => "Note",
|
"type" => "Note",
|
||||||
"content" => text,
|
"content" => text,
|
||||||
"id" => Pleroma.Web.ActivityPub.ActivityPub.generate_object_id,
|
"id" => Pleroma.Web.ActivityPub.Utils.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,
|
||||||
|
@ -36,7 +36,7 @@ def note_factory do
|
||||||
def note_activity_factory do
|
def note_activity_factory do
|
||||||
note = insert(:note)
|
note = insert(:note)
|
||||||
data = %{
|
data = %{
|
||||||
"id" => Pleroma.Web.ActivityPub.ActivityPub.generate_activity_id,
|
"id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id,
|
||||||
"type" => "Create",
|
"type" => "Create",
|
||||||
"actor" => note.data["actor"],
|
"actor" => note.data["actor"],
|
||||||
"to" => note.data["to"],
|
"to" => note.data["to"],
|
||||||
|
@ -55,7 +55,7 @@ def like_activity_factory do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
|
|
||||||
data = %{
|
data = %{
|
||||||
"id" => Pleroma.Web.ActivityPub.ActivityPub.generate_activity_id,
|
"id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id,
|
||||||
"actor" => user.ap_id,
|
"actor" => user.ap_id,
|
||||||
"type" => "Like",
|
"type" => "Like",
|
||||||
"object" => note_activity.data["object"]["id"],
|
"object" => note_activity.data["object"]["id"],
|
||||||
|
@ -72,7 +72,7 @@ def follow_activity_factory do
|
||||||
followed = insert(:user)
|
followed = insert(:user)
|
||||||
|
|
||||||
data = %{
|
data = %{
|
||||||
"id" => Pleroma.Web.ActivityPub.ActivityPub.generate_activity_id,
|
"id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id,
|
||||||
"actor" => follower.ap_id,
|
"actor" => follower.ap_id,
|
||||||
"type" => "Follow",
|
"type" => "Follow",
|
||||||
"object" => followed.ap_id,
|
"object" => followed.ap_id,
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
|
defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
|
||||||
use Pleroma.DataCase
|
use Pleroma.DataCase
|
||||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||||
|
alias Pleroma.Web.ActivityPub.Utils
|
||||||
alias Pleroma.{Activity, Object, User}
|
alias Pleroma.{Activity, Object, User}
|
||||||
alias Pleroma.Builders.ActivityBuilder
|
alias Pleroma.Builders.ActivityBuilder
|
||||||
|
|
||||||
|
@ -216,7 +217,7 @@ test "fetches the latest Follow activity" do
|
||||||
follower = Repo.get_by(User, ap_id: activity.data["actor"])
|
follower = Repo.get_by(User, ap_id: activity.data["actor"])
|
||||||
followed = Repo.get_by(User, ap_id: activity.data["object"])
|
followed = Repo.get_by(User, ap_id: activity.data["object"])
|
||||||
|
|
||||||
assert activity == ActivityPub.fetch_latest_follow(follower, followed)
|
assert activity == Utils.fetch_latest_follow(follower, followed)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -71,7 +71,7 @@ test "it pushes an activity to remote accounts it's addressed to" do
|
||||||
mentioned_user = insert(:user, user_data)
|
mentioned_user = insert(:user, user_data)
|
||||||
note = insert(:note)
|
note = insert(:note)
|
||||||
activity_data = %{
|
activity_data = %{
|
||||||
"id" => Pleroma.Web.ActivityPub.ActivityPub.generate_activity_id,
|
"id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id,
|
||||||
"type" => "Create",
|
"type" => "Create",
|
||||||
"actor" => note.data["actor"],
|
"actor" => note.data["actor"],
|
||||||
"to" => note.data["to"] ++ [mentioned_user.ap_id],
|
"to" => note.data["to"] ++ [mentioned_user.ap_id],
|
||||||
|
|
Loading…
Reference in a new issue