Toot
This commit is contained in:
@@ -244,20 +244,29 @@ defmodule Mobilizon.Actors.Actor do
|
||||
)
|
||||
end
|
||||
|
||||
def follow(%Actor{} = follower, %Actor{} = followed) do
|
||||
# Check if actor is locked
|
||||
# Check if followed has blocked follower
|
||||
# Check if follower already follows followed
|
||||
cond do
|
||||
following?(follower, followed) ->
|
||||
def follow(%Actor{} = follower, %Actor{} = followed, approved \\ true) do
|
||||
with {:suspended, false} <- {:suspended, followed.suspended},
|
||||
# Check if followed has blocked follower
|
||||
{:already_following, false} <- {:already_following, following?(follower, followed)} do
|
||||
do_follow(follower, followed, approved)
|
||||
else
|
||||
{:already_following, _} ->
|
||||
{:error,
|
||||
"Could not follow actor: you are already following #{followed.preferred_username}"}
|
||||
|
||||
# true -> nil
|
||||
# Follow the person
|
||||
{:suspended, _} ->
|
||||
{:error, "Could not follow actor: #{followed.preferred_username} has been suspended"}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_follow(%Actor{} = follower, %Actor{} = followed, approved \\ true) do
|
||||
Actors.create_follower(%{
|
||||
"actor_id" => follower.id,
|
||||
"target_actor_id" => followed.id,
|
||||
"approved" => approved
|
||||
})
|
||||
end
|
||||
|
||||
def following?(%Actor{} = follower, %Actor{followers: followers}) do
|
||||
Enum.member?(followers, follower)
|
||||
end
|
||||
|
||||
@@ -203,21 +203,24 @@ defmodule Mobilizon.Actors do
|
||||
defp blank?(""), do: nil
|
||||
defp blank?(n), do: n
|
||||
|
||||
def insert_or_update_actor(data) do
|
||||
def insert_or_update_actor(data, preload \\ false) do
|
||||
cs = Actor.remote_actor_creation(data)
|
||||
|
||||
Repo.insert(
|
||||
cs,
|
||||
on_conflict: [
|
||||
set: [
|
||||
keys: data.keys,
|
||||
avatar_url: data.avatar_url,
|
||||
banner_url: data.banner_url,
|
||||
name: data.name
|
||||
]
|
||||
],
|
||||
conflict_target: [:preferred_username, :domain]
|
||||
)
|
||||
actor =
|
||||
Repo.insert(
|
||||
cs,
|
||||
on_conflict: [
|
||||
set: [
|
||||
keys: data.keys,
|
||||
avatar_url: data.avatar_url,
|
||||
banner_url: data.banner_url,
|
||||
name: data.name
|
||||
]
|
||||
],
|
||||
conflict_target: [:preferred_username, :domain]
|
||||
)
|
||||
|
||||
if preload, do: {:ok, Repo.preload(actor, [:followers])}, else: {:ok, actor}
|
||||
end
|
||||
|
||||
# def increase_event_count(%Actor{} = actor) do
|
||||
@@ -267,8 +270,24 @@ defmodule Mobilizon.Actors do
|
||||
end
|
||||
end
|
||||
|
||||
def get_actor_by_url(url) do
|
||||
Repo.get_by(Actor, url: url)
|
||||
@doc """
|
||||
Get an actor by it's URL (ActivityPub ID)
|
||||
"""
|
||||
@spec get_actor_by_url(String.t(), boolean()) :: {:ok, struct()} | {:error, :actor_not_found}
|
||||
def get_actor_by_url(url, preload \\ false) do
|
||||
case Repo.get_by(Actor, url: url) do
|
||||
nil ->
|
||||
{:error, :actor_not_found}
|
||||
|
||||
actor ->
|
||||
if preload, do: {:ok, Repo.preload(actor, [:followers])}, else: {:ok, actor}
|
||||
end
|
||||
end
|
||||
|
||||
@spec get_actor_by_url!(String.t(), boolean()) :: struct()
|
||||
def get_actor_by_url!(url, preload \\ false) do
|
||||
actor = Repo.get_by!(Actor, url: url)
|
||||
if preload, do: Repo.preload(actor, [:followers]), else: actor
|
||||
end
|
||||
|
||||
def get_actor_by_name(name) do
|
||||
@@ -304,11 +323,11 @@ defmodule Mobilizon.Actors do
|
||||
Repo.preload(actor, :organized_events)
|
||||
end
|
||||
|
||||
def get_or_fetch_by_url(url) do
|
||||
if actor = get_actor_by_url(url) do
|
||||
def get_or_fetch_by_url(url, preload \\ false) do
|
||||
if {:ok, actor} = get_actor_by_url(url, preload) do
|
||||
{:ok, actor}
|
||||
else
|
||||
case ActivityPub.make_actor_from_url(url) do
|
||||
case ActivityPub.make_actor_from_url(url, preload) do
|
||||
{:ok, actor} ->
|
||||
{:ok, actor}
|
||||
|
||||
|
||||
@@ -26,7 +26,10 @@ defmodule Mobilizon.Events.Comment do
|
||||
|
||||
@doc false
|
||||
def changeset(comment, attrs) do
|
||||
uuid = Ecto.UUID.generate()
|
||||
uuid =
|
||||
if Map.has_key?(attrs, "uuid"),
|
||||
do: attrs["uuid"],
|
||||
else: Ecto.UUID.generate()
|
||||
|
||||
# TODO : really change me right away
|
||||
url =
|
||||
@@ -35,7 +38,15 @@ defmodule Mobilizon.Events.Comment do
|
||||
else: "#{MobilizonWeb.Endpoint.url()}/comments/#{uuid}"
|
||||
|
||||
comment
|
||||
|> cast(attrs, [:url, :text, :actor_id, :event_id, :in_reply_to_comment_id, :origin_comment_id, :attributed_to_id])
|
||||
|> cast(attrs, [
|
||||
:url,
|
||||
:text,
|
||||
:actor_id,
|
||||
:event_id,
|
||||
:in_reply_to_comment_id,
|
||||
:origin_comment_id,
|
||||
:attributed_to_id
|
||||
])
|
||||
|> put_change(:uuid, uuid)
|
||||
|> put_change(:url, url)
|
||||
|> validate_required([:text, :actor_id, :url])
|
||||
|
||||
@@ -885,7 +885,15 @@ defmodule Mobilizon.Events do
|
||||
"""
|
||||
def get_comment!(id), do: Repo.get!(Comment, id)
|
||||
|
||||
def get_comment_with_uuid!(uuid), do: Repo.get_by!(Comment, uuid: uuid)
|
||||
def get_comment_from_uuid(uuid), do: Repo.get_by(Comment, uuid: uuid)
|
||||
|
||||
def get_comment_from_uuid!(uuid), do: Repo.get_by!(Comment, uuid: uuid)
|
||||
|
||||
def get_comment_full_from_uuid(uuid) do
|
||||
with %Comment{} = comment <- Repo.get_by!(Comment, uuid: uuid) do
|
||||
Repo.preload(comment, [:actor, :attributed_to])
|
||||
end
|
||||
end
|
||||
|
||||
def get_comment_from_url(url), do: Repo.get_by(Comment, url: url)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user