forked from potsda.mn/mobilizon
Fix credo style reports following it's update
Mainly transform `with` into `case` Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
parent
1cd511f440
commit
c3cca5d613
|
@ -10,11 +10,15 @@ defmodule Mix.Tasks.Mobilizon.Toot do
|
|||
def run([from, content]) do
|
||||
Mix.Task.run("app.start")
|
||||
|
||||
with {:ok, _} <- MobilizonWeb.API.Comments.create_comment(from, content) do
|
||||
Mix.shell().info("Tooted")
|
||||
else
|
||||
{:local_actor, _} -> Mix.shell().error("Failed to toot.\nActor #{from} doesn't exist")
|
||||
_ -> Mix.shell().error("Failed to toot.")
|
||||
case MobilizonWeb.API.Comments.create_comment(from, content) do
|
||||
{:ok, _} ->
|
||||
Mix.shell().info("Tooted")
|
||||
|
||||
{:local_actor, _} ->
|
||||
Mix.shell().error("Failed to toot.\nActor #{from} doesn't exist")
|
||||
|
||||
_ ->
|
||||
Mix.shell().error("Failed to toot.")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -295,9 +295,10 @@ defmodule Mobilizon.Actors.Actor do
|
|||
"""
|
||||
@spec prepare_public_key(String.t()) :: {:ok, tuple()} | {:error, :pem_decode_error}
|
||||
def prepare_public_key(public_key_code) do
|
||||
with [public_key_entry] <- :public_key.pem_decode(public_key_code) do
|
||||
{:ok, :public_key.pem_entry_decode(public_key_entry)}
|
||||
else
|
||||
case :public_key.pem_decode(public_key_code) do
|
||||
[public_key_entry] ->
|
||||
{:ok, :public_key.pem_entry_decode(public_key_entry)}
|
||||
|
||||
_err ->
|
||||
{:error, :pem_decode_error}
|
||||
end
|
||||
|
@ -423,10 +424,10 @@ defmodule Mobilizon.Actors.Actor do
|
|||
"""
|
||||
@spec unfollow(struct(), struct()) :: {:ok, Follower.t()} | {:error, Ecto.Changeset.t()}
|
||||
def unfollow(%Actor{} = followed, %Actor{} = follower) do
|
||||
with {:already_following, %Follower{} = follow} <-
|
||||
{:already_following, following?(follower, followed)} do
|
||||
Actors.delete_follower(follow)
|
||||
else
|
||||
case {:already_following, following?(follower, followed)} do
|
||||
{:already_following, %Follower{} = follow} ->
|
||||
Actors.delete_follower(follow)
|
||||
|
||||
{:already_following, false} ->
|
||||
{:error, "Could not unfollow actor: you are not following #{followed.preferred_username}"}
|
||||
end
|
||||
|
|
|
@ -281,21 +281,21 @@ defmodule Mobilizon.Actors do
|
|||
data
|
||||
|> Actor.remote_actor_creation()
|
||||
|
||||
with {:ok, actor} <-
|
||||
Repo.insert(
|
||||
cs,
|
||||
on_conflict: [
|
||||
set: [
|
||||
keys: data.keys,
|
||||
name: data.name,
|
||||
summary: data.summary
|
||||
]
|
||||
],
|
||||
conflict_target: [:url]
|
||||
) do
|
||||
actor = if preload, do: Repo.preload(actor, [:followers]), else: actor
|
||||
{:ok, actor}
|
||||
else
|
||||
case Repo.insert(
|
||||
cs,
|
||||
on_conflict: [
|
||||
set: [
|
||||
keys: data.keys,
|
||||
name: data.name,
|
||||
summary: data.summary
|
||||
]
|
||||
],
|
||||
conflict_target: [:url]
|
||||
) do
|
||||
{:ok, actor} ->
|
||||
actor = if preload, do: Repo.preload(actor, [:followers]), else: actor
|
||||
{:ok, actor}
|
||||
|
||||
err ->
|
||||
Logger.error(inspect(err))
|
||||
{:error, err}
|
||||
|
@ -474,9 +474,10 @@ defmodule Mobilizon.Actors do
|
|||
# TODO: Move this to Mobilizon.Service.ActivityPub
|
||||
@spec get_or_fetch_by_url(String.t(), bool()) :: {:ok, Actor.t()} | {:error, String.t()}
|
||||
def get_or_fetch_by_url(url, preload \\ false) do
|
||||
with {:ok, actor} <- get_actor_by_url(url, preload) do
|
||||
{:ok, actor}
|
||||
else
|
||||
case get_actor_by_url(url, preload) do
|
||||
{:ok, actor} ->
|
||||
{:ok, actor}
|
||||
|
||||
_ ->
|
||||
case ActivityPub.make_actor_from_url(url, preload) do
|
||||
{:ok, actor} ->
|
||||
|
@ -497,9 +498,10 @@ defmodule Mobilizon.Actors do
|
|||
# TODO: Move this to Mobilizon.Service.ActivityPub
|
||||
@spec get_or_fetch_by_url!(String.t(), bool()) :: Actor.t()
|
||||
def get_or_fetch_by_url!(url, preload \\ false) do
|
||||
with {:ok, actor} <- get_actor_by_url(url, preload) do
|
||||
actor
|
||||
else
|
||||
case get_actor_by_url(url, preload) do
|
||||
{:ok, actor} ->
|
||||
actor
|
||||
|
||||
_ ->
|
||||
case ActivityPub.make_actor_from_url(url, preload) do
|
||||
{:ok, actor} ->
|
||||
|
@ -1001,9 +1003,10 @@ defmodule Mobilizon.Actors do
|
|||
end
|
||||
|
||||
defp safe_remove_file(url, %Actor{} = actor) do
|
||||
with {:ok, _value} <- MobilizonWeb.Upload.remove(url) do
|
||||
{:ok, actor}
|
||||
else
|
||||
case MobilizonWeb.Upload.remove(url) do
|
||||
{:ok, _value} ->
|
||||
{:ok, actor}
|
||||
|
||||
{:error, error} ->
|
||||
Logger.error("Error while removing an upload file")
|
||||
Logger.error(inspect(error))
|
||||
|
|
|
@ -187,10 +187,12 @@ defmodule Mobilizon.Events do
|
|||
|
||||
def get_cached_event_full_by_uuid(uuid) do
|
||||
Cachex.fetch(:activity_pub, "event_" <> uuid, fn "event_" <> uuid ->
|
||||
with %Event{} = event <- get_event_full_by_uuid(uuid) do
|
||||
{:commit, event}
|
||||
else
|
||||
_ -> {:ignore, nil}
|
||||
case get_event_full_by_uuid(uuid) do
|
||||
%Event{} = event ->
|
||||
{:commit, event}
|
||||
|
||||
_ ->
|
||||
{:ignore, nil}
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
@ -1133,10 +1135,12 @@ defmodule Mobilizon.Events do
|
|||
|
||||
def get_cached_comment_full_by_uuid(uuid) do
|
||||
Cachex.fetch(:activity_pub, "comment_" <> uuid, fn "comment_" <> uuid ->
|
||||
with %Comment{} = comment <- get_comment_full_from_uuid(uuid) do
|
||||
{:commit, comment}
|
||||
else
|
||||
_ -> {:ignore, nil}
|
||||
case get_comment_full_from_uuid(uuid) do
|
||||
%Comment{} = comment ->
|
||||
{:commit, comment}
|
||||
|
||||
_ ->
|
||||
{:ignore, nil}
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
|
|
@ -235,8 +235,9 @@ defmodule Mobilizon.Users do
|
|||
end
|
||||
|
||||
def update_user_default_actor(user_id, actor_id) do
|
||||
with from(u in User, where: u.id == ^user_id, update: [set: [default_actor_id: ^actor_id]])
|
||||
|> Repo.update_all([]) do
|
||||
with _ <-
|
||||
from(u in User, where: u.id == ^user_id, update: [set: [default_actor_id: ^actor_id]])
|
||||
|> Repo.update_all([]) do
|
||||
Repo.get!(User, user_id) |> Repo.preload([:default_actor])
|
||||
end
|
||||
end
|
||||
|
|
|
@ -25,9 +25,10 @@ defmodule MobilizonWeb.API.Search do
|
|||
# Some URLs could be domain.tld/@username, so keep this condition above handle_search? function
|
||||
url_search?(search) ->
|
||||
# If this is not an actor, skip
|
||||
with %{:total => total, :elements => [%Actor{}] = elements} <- process_from_url(search) do
|
||||
{:ok, %{total: total, elements: elements}}
|
||||
else
|
||||
case process_from_url(search) do
|
||||
%{:total => total, :elements => [%Actor{}] = elements} ->
|
||||
{:ok, %{total: total, elements: elements}}
|
||||
|
||||
_ ->
|
||||
{:ok, %{total: 0, elements: []}}
|
||||
end
|
||||
|
@ -55,9 +56,10 @@ defmodule MobilizonWeb.API.Search do
|
|||
|
||||
url_search?(search) ->
|
||||
# If this is not an event, skip
|
||||
with {total = total, [%Event{} = elements]} <- process_from_url(search) do
|
||||
{:ok, %{total: total, elements: elements}}
|
||||
else
|
||||
case process_from_url(search) do
|
||||
{total = total, [%Event{} = elements]} ->
|
||||
{:ok, %{total: total, elements: elements}}
|
||||
|
||||
_ ->
|
||||
{:ok, %{total: 0, elements: []}}
|
||||
end
|
||||
|
@ -70,9 +72,10 @@ defmodule MobilizonWeb.API.Search do
|
|||
# If the search string is an username
|
||||
@spec process_from_username(String.t()) :: %{total: integer(), elements: [Actor.t()]}
|
||||
defp process_from_username(search) do
|
||||
with {:ok, actor} <- ActivityPub.find_or_make_actor_from_nickname(search) do
|
||||
%{total: 1, elements: [actor]}
|
||||
else
|
||||
case ActivityPub.find_or_make_actor_from_nickname(search) do
|
||||
{:ok, actor} ->
|
||||
%{total: 1, elements: [actor]}
|
||||
|
||||
{:error, _err} ->
|
||||
Logger.debug(fn -> "Unable to find or make actor '#{search}'" end)
|
||||
%{total: 0, elements: []}
|
||||
|
@ -85,9 +88,10 @@ defmodule MobilizonWeb.API.Search do
|
|||
elements: [Actor.t() | Event.t() | Comment.t()]
|
||||
}
|
||||
defp process_from_url(search) do
|
||||
with {:ok, object} <- ActivityPub.fetch_object_from_url(search) do
|
||||
%{total: 1, elements: [object]}
|
||||
else
|
||||
case ActivityPub.fetch_object_from_url(search) do
|
||||
{:ok, object} ->
|
||||
%{total: 1, elements: [object]}
|
||||
|
||||
{:error, _err} ->
|
||||
Logger.debug(fn -> "Unable to find or make object from URL '#{search}'" end)
|
||||
%{total: 0, elements: []}
|
||||
|
|
|
@ -15,7 +15,7 @@ defmodule MobilizonWeb.ActivityPubController do
|
|||
action_fallback(:errors)
|
||||
|
||||
def following(conn, %{"name" => name, "page" => page}) do
|
||||
with {page, ""} = Integer.parse(page),
|
||||
with {page, ""} <- Integer.parse(page),
|
||||
%Actor{} = actor <- Actors.get_local_actor_by_name_with_everything(name) do
|
||||
conn
|
||||
|> put_resp_header("content-type", "application/activity+json")
|
||||
|
@ -32,7 +32,7 @@ defmodule MobilizonWeb.ActivityPubController do
|
|||
end
|
||||
|
||||
def followers(conn, %{"name" => name, "page" => page}) do
|
||||
with {page, ""} = Integer.parse(page),
|
||||
with {page, ""} <- Integer.parse(page),
|
||||
%Actor{} = actor <- Actors.get_local_actor_by_name_with_everything(name) do
|
||||
conn
|
||||
|> put_resp_header("content-type", "application/activity+json")
|
||||
|
@ -49,7 +49,7 @@ defmodule MobilizonWeb.ActivityPubController do
|
|||
end
|
||||
|
||||
def outbox(conn, %{"name" => name, "page" => page}) do
|
||||
with {page, ""} = Integer.parse(page),
|
||||
with {page, ""} <- Integer.parse(page),
|
||||
%Actor{} = actor <- Actors.get_local_actor_by_name(name) do
|
||||
conn
|
||||
|> put_resp_header("content-type", "application/activity+json")
|
||||
|
|
|
@ -7,60 +7,60 @@ defmodule MobilizonWeb.FeedController do
|
|||
action_fallback(MobilizonWeb.FallbackController)
|
||||
|
||||
def actor(conn, %{"name" => name, "format" => "atom"}) do
|
||||
with {status, data} when status in [:ok, :commit] <-
|
||||
Cachex.fetch(:feed, "actor_" <> name) do
|
||||
conn
|
||||
|> put_resp_content_type("application/atom+xml")
|
||||
|> send_resp(200, data)
|
||||
else
|
||||
case Cachex.fetch(:feed, "actor_" <> name) do
|
||||
{status, data} when status in [:ok, :commit] ->
|
||||
conn
|
||||
|> put_resp_content_type("application/atom+xml")
|
||||
|> send_resp(200, data)
|
||||
|
||||
_err ->
|
||||
{:error, :not_found}
|
||||
end
|
||||
end
|
||||
|
||||
def actor(conn, %{"name" => name, "format" => "ics"}) do
|
||||
with {status, data} when status in [:ok, :commit] <-
|
||||
Cachex.fetch(:ics, "actor_" <> name) do
|
||||
conn
|
||||
|> put_resp_content_type("text/calendar")
|
||||
|> send_resp(200, data)
|
||||
else
|
||||
case Cachex.fetch(:ics, "actor_" <> name) do
|
||||
{status, data} when status in [:ok, :commit] ->
|
||||
conn
|
||||
|> put_resp_content_type("text/calendar")
|
||||
|> send_resp(200, data)
|
||||
|
||||
_err ->
|
||||
{:error, :not_found}
|
||||
end
|
||||
end
|
||||
|
||||
def event(conn, %{"uuid" => uuid, "format" => "ics"}) do
|
||||
with {status, data} when status in [:ok, :commit] <-
|
||||
Cachex.fetch(:ics, "event_" <> uuid) do
|
||||
conn
|
||||
|> put_resp_content_type("text/calendar")
|
||||
|> send_resp(200, data)
|
||||
else
|
||||
case Cachex.fetch(:ics, "event_" <> uuid) do
|
||||
{status, data} when status in [:ok, :commit] ->
|
||||
conn
|
||||
|> put_resp_content_type("text/calendar")
|
||||
|> send_resp(200, data)
|
||||
|
||||
_err ->
|
||||
{:error, :not_found}
|
||||
end
|
||||
end
|
||||
|
||||
def going(conn, %{"token" => token, "format" => "ics"}) do
|
||||
with {status, data} when status in [:ok, :commit] <-
|
||||
Cachex.fetch(:ics, "token_" <> token) do
|
||||
conn
|
||||
|> put_resp_content_type("text/calendar")
|
||||
|> send_resp(200, data)
|
||||
else
|
||||
case Cachex.fetch(:ics, "token_" <> token) do
|
||||
{status, data} when status in [:ok, :commit] ->
|
||||
conn
|
||||
|> put_resp_content_type("text/calendar")
|
||||
|> send_resp(200, data)
|
||||
|
||||
_err ->
|
||||
{:error, :not_found}
|
||||
end
|
||||
end
|
||||
|
||||
def going(conn, %{"token" => token, "format" => "atom"}) do
|
||||
with {status, data} when status in [:ok, :commit] <-
|
||||
Cachex.fetch(:feed, "token_" <> token) do
|
||||
conn
|
||||
|> put_resp_content_type("application/atom+xml")
|
||||
|> send_resp(200, data)
|
||||
else
|
||||
case Cachex.fetch(:feed, "token_" <> token) do
|
||||
{status, data} when status in [:ok, :commit] ->
|
||||
conn
|
||||
|> put_resp_content_type("application/atom+xml")
|
||||
|> send_resp(200, data)
|
||||
|
||||
_err ->
|
||||
{:error, :not_found}
|
||||
end
|
||||
|
|
|
@ -176,11 +176,12 @@ defmodule MobilizonWeb.Resolvers.Event do
|
|||
# and that it's the actor requesting leaving the event we return true
|
||||
@spec check_that_participant_is_not_only_organizer(integer(), integer()) :: boolean()
|
||||
defp check_that_participant_is_not_only_organizer(event_id, actor_id) do
|
||||
with [%Participant{actor: %Actor{id: participant_actor_id}}] <-
|
||||
Mobilizon.Events.list_organizers_participants_for_event(event_id) do
|
||||
participant_actor_id == actor_id
|
||||
else
|
||||
_ -> false
|
||||
case Mobilizon.Events.list_organizers_participants_for_event(event_id) do
|
||||
[%Participant{actor: %Actor{id: participant_actor_id}}] ->
|
||||
participant_actor_id == actor_id
|
||||
|
||||
_ ->
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -218,17 +218,18 @@ defmodule MobilizonWeb.Resolvers.Group do
|
|||
# and that it's the actor requesting leaving the group we return true
|
||||
@spec check_that_member_is_not_only_administrator(integer(), integer()) :: boolean()
|
||||
defp check_that_member_is_not_only_administrator(group_id, actor_id) do
|
||||
with [
|
||||
%Member{
|
||||
actor: %Actor{
|
||||
id: member_actor_id
|
||||
}
|
||||
}
|
||||
] <-
|
||||
Member.list_administrator_members_for_group(group_id) do
|
||||
actor_id == member_actor_id
|
||||
else
|
||||
_ -> false
|
||||
case Member.list_administrator_members_for_group(group_id) do
|
||||
[
|
||||
%Member{
|
||||
actor: %Actor{
|
||||
id: member_actor_id
|
||||
}
|
||||
}
|
||||
] ->
|
||||
actor_id == member_actor_id
|
||||
|
||||
_ ->
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -35,10 +35,17 @@ defmodule MobilizonWeb.Resolvers.Picture do
|
|||
|
||||
@spec do_fetch_picture(String.t()) :: {:ok, Picture.t()} | {:error, :not_found}
|
||||
defp do_fetch_picture(picture_id) do
|
||||
with %Picture{id: id, file: file} = _pic <- Media.get_picture(picture_id) do
|
||||
{:ok,
|
||||
%{name: file.name, url: file.url, id: id, content_type: file.content_type, size: file.size}}
|
||||
else
|
||||
case Media.get_picture(picture_id) do
|
||||
%Picture{id: id, file: file} = _pic ->
|
||||
{:ok,
|
||||
%{
|
||||
name: file.name,
|
||||
url: file.url,
|
||||
id: id,
|
||||
content_type: file.content_type,
|
||||
size: file.size
|
||||
}}
|
||||
|
||||
_err ->
|
||||
{:error, "Picture with ID #{picture_id} was not found"}
|
||||
end
|
||||
|
|
|
@ -94,7 +94,9 @@ defmodule MobilizonWeb.Upload do
|
|||
{:same_host, true} <- {:same_host, host == MobilizonWeb.Endpoint.host()} do
|
||||
MobilizonWeb.Uploaders.Uploader.remove_file(opts.uploader, path)
|
||||
else
|
||||
%URI{} = _uri -> {:error, "URL doesn't match pattern"}
|
||||
%URI{} = _uri ->
|
||||
{:error, "URL doesn't match pattern"}
|
||||
|
||||
{:same_host, _} ->
|
||||
Logger.error("Media can't be deleted because its URL doesn't match current host")
|
||||
end
|
||||
|
|
|
@ -289,7 +289,7 @@ defmodule Mobilizon.Service.ActivityPub do
|
|||
"to" => [actor.url <> "/followers", "https://www.w3.org/ns/activitystreams#Public"]
|
||||
}
|
||||
|
||||
with Events.delete_event(event),
|
||||
with {:ok, _} <- Events.delete_event(event),
|
||||
{:ok, activity, _object} <- insert(data, local),
|
||||
:ok <- maybe_federate(activity) do
|
||||
{:ok, activity}
|
||||
|
@ -304,7 +304,7 @@ defmodule Mobilizon.Service.ActivityPub do
|
|||
"to" => [actor.url <> "/followers", "https://www.w3.org/ns/activitystreams#Public"]
|
||||
}
|
||||
|
||||
with Events.delete_comment(comment),
|
||||
with {:ok, _} <- Events.delete_comment(comment),
|
||||
{:ok, activity, _object} <- insert(data, local),
|
||||
:ok <- maybe_federate(activity) do
|
||||
{:ok, activity}
|
||||
|
@ -319,7 +319,7 @@ defmodule Mobilizon.Service.ActivityPub do
|
|||
"to" => [url <> "/followers", "https://www.w3.org/ns/activitystreams#Public"]
|
||||
}
|
||||
|
||||
with Actors.delete_actor(actor),
|
||||
with {:ok, _} <- Actors.delete_actor(actor),
|
||||
{:ok, activity, _object} <- insert(data, local),
|
||||
:ok <- maybe_federate(activity) do
|
||||
{:ok, activity}
|
||||
|
@ -352,9 +352,10 @@ defmodule Mobilizon.Service.ActivityPub do
|
|||
"""
|
||||
@spec make_actor_from_url(String.t(), boolean()) :: {:ok, %Actor{}} | {:error, any()}
|
||||
def make_actor_from_url(url, preload \\ false) do
|
||||
with {:ok, data} <- fetch_and_prepare_actor_from_url(url) do
|
||||
Actors.insert_or_update_actor(data, preload)
|
||||
else
|
||||
case fetch_and_prepare_actor_from_url(url) do
|
||||
{:ok, data} ->
|
||||
Actors.insert_or_update_actor(data, preload)
|
||||
|
||||
# Request returned 410
|
||||
{:error, :actor_deleted} ->
|
||||
{:error, :actor_deleted}
|
||||
|
@ -371,10 +372,12 @@ defmodule Mobilizon.Service.ActivityPub do
|
|||
"""
|
||||
@spec find_or_make_actor_from_nickname(String.t(), atom() | nil) :: tuple()
|
||||
def find_or_make_actor_from_nickname(nickname, type \\ nil) do
|
||||
with %Actor{} = actor <- Actors.get_actor_by_name(nickname, type) do
|
||||
{:ok, actor}
|
||||
else
|
||||
nil -> make_actor_from_nickname(nickname)
|
||||
case Actors.get_actor_by_name(nickname, type) do
|
||||
%Actor{} = actor ->
|
||||
{:ok, actor}
|
||||
|
||||
nil ->
|
||||
make_actor_from_nickname(nickname)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -389,10 +392,12 @@ defmodule Mobilizon.Service.ActivityPub do
|
|||
"""
|
||||
@spec make_actor_from_nickname(String.t()) :: {:ok, %Actor{}} | {:error, any()}
|
||||
def make_actor_from_nickname(nickname) do
|
||||
with {:ok, %{"url" => url}} when not is_nil(url) <- WebFinger.finger(nickname) do
|
||||
make_actor_from_url(url)
|
||||
else
|
||||
_e -> {:error, "No ActivityPub URL found in WebFinger"}
|
||||
case WebFinger.finger(nickname) do
|
||||
{:ok, %{"url" => url}} when not is_nil(url) ->
|
||||
make_actor_from_url(url)
|
||||
|
||||
_e ->
|
||||
{:error, "No ActivityPub URL found in WebFinger"}
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -37,18 +37,22 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Flag do
|
|||
with {:ok, %Actor{} = reporter} <- Actors.get_actor_by_url(object["actor"]),
|
||||
%Actor{} = reported <-
|
||||
Enum.reduce_while(objects, nil, fn url, _ ->
|
||||
with {:ok, %Actor{} = actor} <- Actors.get_actor_by_url(url) do
|
||||
{:halt, actor}
|
||||
else
|
||||
_ -> {:cont, nil}
|
||||
case Actors.get_actor_by_url(url) do
|
||||
{:ok, %Actor{} = actor} ->
|
||||
{:halt, actor}
|
||||
|
||||
_ ->
|
||||
{:cont, nil}
|
||||
end
|
||||
end),
|
||||
event <-
|
||||
Enum.reduce_while(objects, nil, fn url, _ ->
|
||||
with %Event{} = event <- Events.get_event_by_url(url) do
|
||||
{:halt, event}
|
||||
else
|
||||
_ -> {:cont, nil}
|
||||
case Events.get_event_by_url(url) do
|
||||
%Event{} = event ->
|
||||
{:halt, event}
|
||||
|
||||
_ ->
|
||||
{:cont, nil}
|
||||
end
|
||||
end),
|
||||
|
||||
|
|
|
@ -209,19 +209,20 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do
|
|||
data
|
||||
)
|
||||
when object_type in ["Person", "Application", "Service", "Organization"] do
|
||||
with {:ok, %Actor{url: url}} <- Actors.get_actor_by_url(object["id"]) do
|
||||
{:ok, new_actor_data} = ActivityPub.actor_data_from_actor_object(object)
|
||||
case Actors.get_actor_by_url(object["id"]) do
|
||||
{:ok, %Actor{url: url}} ->
|
||||
{:ok, new_actor_data} = ActivityPub.actor_data_from_actor_object(object)
|
||||
|
||||
Actors.insert_or_update_actor(new_actor_data)
|
||||
Actors.insert_or_update_actor(new_actor_data)
|
||||
|
||||
ActivityPub.update(%{
|
||||
local: false,
|
||||
to: data["to"] || [],
|
||||
cc: data["cc"] || [],
|
||||
object: object,
|
||||
actor: url
|
||||
})
|
||||
|
||||
ActivityPub.update(%{
|
||||
local: false,
|
||||
to: data["to"] || [],
|
||||
cc: data["cc"] || [],
|
||||
object: object,
|
||||
actor: url
|
||||
})
|
||||
else
|
||||
e ->
|
||||
Logger.error(inspect(e))
|
||||
:error
|
||||
|
|
|
@ -206,10 +206,12 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
|
|||
Save picture data from %Plug.Upload{} and return AS Link data.
|
||||
"""
|
||||
def make_picture_data(%Plug.Upload{} = picture) do
|
||||
with {:ok, picture} <- MobilizonWeb.Upload.store(picture) do
|
||||
picture
|
||||
else
|
||||
_ -> nil
|
||||
case MobilizonWeb.Upload.store(picture) do
|
||||
{:ok, picture} ->
|
||||
picture
|
||||
|
||||
_ ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -21,9 +21,10 @@ defmodule Mobilizon.Service.Export.Feed do
|
|||
|
||||
@spec create_cache(String.t()) :: {:commit, String.t()} | {:ignore, any()}
|
||||
def create_cache("actor_" <> name) do
|
||||
with {:ok, res} <- fetch_actor_event_feed(name) do
|
||||
{:commit, res}
|
||||
else
|
||||
case fetch_actor_event_feed(name) do
|
||||
{:ok, res} ->
|
||||
{:commit, res}
|
||||
|
||||
err ->
|
||||
{:ignore, err}
|
||||
end
|
||||
|
@ -31,9 +32,10 @@ defmodule Mobilizon.Service.Export.Feed do
|
|||
|
||||
@spec create_cache(String.t()) :: {:commit, String.t()} | {:ignore, any()}
|
||||
def create_cache("token_" <> token) do
|
||||
with {:ok, res} <- fetch_events_from_token(token) do
|
||||
{:commit, res}
|
||||
else
|
||||
case fetch_events_from_token(token) do
|
||||
{:ok, res} ->
|
||||
{:commit, res}
|
||||
|
||||
err ->
|
||||
{:ignore, err}
|
||||
end
|
||||
|
@ -99,21 +101,22 @@ defmodule Mobilizon.Service.Export.Feed do
|
|||
defp get_entry(%Event{} = event) do
|
||||
description = event.description || ""
|
||||
|
||||
with {:ok, html, []} <- Earmark.as_html(description) do
|
||||
entry =
|
||||
Entry.new(event.url, event.publish_at || event.inserted_at, event.title)
|
||||
|> Entry.link(event.url, rel: "alternate", type: "text/html")
|
||||
|> Entry.content({:cdata, html}, type: "html")
|
||||
|> Entry.published(event.publish_at || event.inserted_at)
|
||||
case Earmark.as_html(description) do
|
||||
{:ok, html, []} ->
|
||||
entry =
|
||||
Entry.new(event.url, event.publish_at || event.inserted_at, event.title)
|
||||
|> Entry.link(event.url, rel: "alternate", type: "text/html")
|
||||
|> Entry.content({:cdata, html}, type: "html")
|
||||
|> Entry.published(event.publish_at || event.inserted_at)
|
||||
|
||||
# Add tags
|
||||
entry =
|
||||
event.tags
|
||||
|> Enum.uniq()
|
||||
|> Enum.reduce(entry, fn tag, acc -> Entry.category(acc, tag.slug, label: tag.title) end)
|
||||
# Add tags
|
||||
entry =
|
||||
event.tags
|
||||
|> Enum.uniq()
|
||||
|> Enum.reduce(entry, fn tag, acc -> Entry.category(acc, tag.slug, label: tag.title) end)
|
||||
|
||||
Entry.build(entry)
|
||||
|
||||
Entry.build(entry)
|
||||
else
|
||||
{:error, _html, error_messages} ->
|
||||
Logger.error("Unable to produce HTML for Markdown", details: inspect(error_messages))
|
||||
end
|
||||
|
|
|
@ -87,9 +87,10 @@ defmodule Mobilizon.Service.Export.ICalendar do
|
|||
Create cache for an actor
|
||||
"""
|
||||
def create_cache("token_" <> token) do
|
||||
with {:ok, res} <- fetch_events_from_token(token) do
|
||||
{:commit, res}
|
||||
else
|
||||
case fetch_events_from_token(token) do
|
||||
{:ok, res} ->
|
||||
{:commit, res}
|
||||
|
||||
err ->
|
||||
{:ignore, err}
|
||||
end
|
||||
|
|
|
@ -51,8 +51,10 @@ defmodule Mobilizon.Service.Federator do
|
|||
Logger.info("Handling incoming AP activity")
|
||||
Logger.debug(inspect(params))
|
||||
|
||||
with {:ok, _activity} <- Transmogrifier.handle_incoming(params) do
|
||||
else
|
||||
case Transmogrifier.handle_incoming(params) do
|
||||
{:ok, activity} ->
|
||||
{:ok, activity}
|
||||
|
||||
%Activity{} ->
|
||||
Logger.info("Already had #{params["id"]}")
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ defmodule Mobilizon.Service.Geospatial.Addok do
|
|||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url),
|
||||
{:ok, %{"features" => features}} <- Poison.decode(body) do
|
||||
processData(features)
|
||||
process_data(features)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -39,7 +39,7 @@ defmodule Mobilizon.Service.Geospatial.Addok do
|
|||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url),
|
||||
{:ok, %{"features" => features}} <- Poison.decode(body) do
|
||||
processData(features)
|
||||
process_data(features)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -59,23 +59,23 @@ defmodule Mobilizon.Service.Geospatial.Addok do
|
|||
end
|
||||
end
|
||||
|
||||
defp processData(features) do
|
||||
defp process_data(features) do
|
||||
features
|
||||
|> Enum.map(fn %{"geometry" => geometry, "properties" => properties} ->
|
||||
%Address{
|
||||
country: Map.get(properties, "country"),
|
||||
locality: Map.get(properties, "city"),
|
||||
region: Map.get(properties, "state"),
|
||||
description: Map.get(properties, "name") || streetAddress(properties),
|
||||
description: Map.get(properties, "name") || street_address(properties),
|
||||
floor: Map.get(properties, "floor"),
|
||||
geom: Map.get(geometry, "coordinates") |> Provider.coordinates(),
|
||||
postal_code: Map.get(properties, "postcode"),
|
||||
street: properties |> streetAddress()
|
||||
street: properties |> street_address()
|
||||
}
|
||||
end)
|
||||
end
|
||||
|
||||
defp streetAddress(properties) do
|
||||
defp street_address(properties) do
|
||||
if Map.has_key?(properties, "housenumber") do
|
||||
Map.get(properties, "housenumber") <> " " <> Map.get(properties, "street")
|
||||
else
|
||||
|
|
|
@ -39,7 +39,7 @@ defmodule Mobilizon.Service.Geospatial.MapQuest do
|
|||
},#{lon}&maxResults=#{limit}"
|
||||
),
|
||||
{:ok, %{"results" => results, "info" => %{"statuscode" => 0}}} <- Poison.decode(body) do
|
||||
results |> Enum.map(&processData/1)
|
||||
results |> Enum.map(&process_data/1)
|
||||
else
|
||||
{:ok, %HTTPoison.Response{status_code: 403, body: err}} ->
|
||||
raise(ArgumentError, message: err)
|
||||
|
@ -71,14 +71,14 @@ defmodule Mobilizon.Service.Geospatial.MapQuest do
|
|||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url),
|
||||
{:ok, %{"results" => results, "info" => %{"statuscode" => 0}}} <- Poison.decode(body) do
|
||||
results |> Enum.map(&processData/1)
|
||||
results |> Enum.map(&process_data/1)
|
||||
else
|
||||
{:ok, %HTTPoison.Response{status_code: 403, body: err}} ->
|
||||
raise(ArgumentError, message: err)
|
||||
end
|
||||
end
|
||||
|
||||
defp processData(
|
||||
defp process_data(
|
||||
%{
|
||||
"locations" => addresses,
|
||||
"providedLocation" => %{"latLng" => %{"lat" => lat, "lng" => lng}}
|
||||
|
@ -86,22 +86,22 @@ defmodule Mobilizon.Service.Geospatial.MapQuest do
|
|||
) do
|
||||
case addresses do
|
||||
[] -> nil
|
||||
addresses -> addresses |> hd |> produceAddress(lat, lng)
|
||||
addresses -> addresses |> hd |> produce_address(lat, lng)
|
||||
end
|
||||
end
|
||||
|
||||
defp processData(%{"locations" => addresses}) do
|
||||
defp process_data(%{"locations" => addresses}) do
|
||||
case addresses do
|
||||
[] -> nil
|
||||
addresses -> addresses |> hd |> produceAddress()
|
||||
addresses -> addresses |> hd |> produce_address()
|
||||
end
|
||||
end
|
||||
|
||||
defp produceAddress(%{"latLng" => %{"lat" => lat, "lng" => lng}} = address) do
|
||||
produceAddress(address, lat, lng)
|
||||
defp produce_address(%{"latLng" => %{"lat" => lat, "lng" => lng}} = address) do
|
||||
produce_address(address, lat, lng)
|
||||
end
|
||||
|
||||
defp produceAddress(address, lat, lng) do
|
||||
defp produce_address(address, lat, lng) do
|
||||
%Address{
|
||||
country: Map.get(address, "adminArea1"),
|
||||
locality: Map.get(address, "adminArea5"),
|
||||
|
|
|
@ -24,7 +24,7 @@ defmodule Mobilizon.Service.Geospatial.Photon do
|
|||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url),
|
||||
{:ok, %{"features" => features}} <- Poison.decode(body) do
|
||||
processData(features)
|
||||
process_data(features)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -40,7 +40,7 @@ defmodule Mobilizon.Service.Geospatial.Photon do
|
|||
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
|
||||
HTTPoison.get(url),
|
||||
{:ok, %{"features" => features}} <- Poison.decode(body) do
|
||||
processData(features)
|
||||
process_data(features)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -61,23 +61,23 @@ defmodule Mobilizon.Service.Geospatial.Photon do
|
|||
end
|
||||
end
|
||||
|
||||
defp processData(features) do
|
||||
defp process_data(features) do
|
||||
features
|
||||
|> Enum.map(fn %{"geometry" => geometry, "properties" => properties} ->
|
||||
%Address{
|
||||
country: Map.get(properties, "country"),
|
||||
locality: Map.get(properties, "city"),
|
||||
region: Map.get(properties, "state"),
|
||||
description: Map.get(properties, "name") || streetAddress(properties),
|
||||
description: Map.get(properties, "name") || street_address(properties),
|
||||
floor: Map.get(properties, "floor"),
|
||||
geom: Map.get(geometry, "coordinates") |> Provider.coordinates(),
|
||||
postal_code: Map.get(properties, "postcode"),
|
||||
street: properties |> streetAddress()
|
||||
street: properties |> street_address()
|
||||
}
|
||||
end)
|
||||
end
|
||||
|
||||
defp streetAddress(properties) do
|
||||
defp street_address(properties) do
|
||||
if Map.has_key?(properties, "housenumber") do
|
||||
Map.get(properties, "housenumber") <> " " <> Map.get(properties, "street")
|
||||
else
|
||||
|
|
|
@ -49,19 +49,19 @@ defmodule Mobilizon.Service.HTTPSignatures do
|
|||
def validate_conn(conn) do
|
||||
# TODO: How to get the right key and see if it is actually valid for that request.
|
||||
# For now, fetch the key for the actor.
|
||||
with {:ok, public_key} <- conn.params["actor"] |> Actor.get_public_key_for_url() do
|
||||
if validate_conn(conn, public_key) do
|
||||
true
|
||||
else
|
||||
Logger.info("Could not validate request, re-fetching user and trying one more time")
|
||||
# Fetch user anew and try one more time
|
||||
with actor_id <- conn.params["actor"],
|
||||
{:ok, _actor} <- ActivityPub.make_actor_from_url(actor_id),
|
||||
{:ok, public_key} <- actor_id |> Actor.get_public_key_for_url() do
|
||||
validate_conn(conn, public_key)
|
||||
case conn.params["actor"] |> Actor.get_public_key_for_url() do
|
||||
{:ok, public_key} ->
|
||||
if validate_conn(conn, public_key) do
|
||||
true
|
||||
Logger.info("Could not validate request, re-fetching user and trying one more time")
|
||||
# Fetch user anew and try one more time
|
||||
with actor_id <- conn.params["actor"],
|
||||
{:ok, _actor} <- ActivityPub.make_actor_from_url(actor_id),
|
||||
{:ok, public_key} <- actor_id |> Actor.get_public_key_for_url() do
|
||||
validate_conn(conn, public_key)
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
|
||||
e ->
|
||||
Logger.debug("Could not found url for actor!")
|
||||
Logger.debug(inspect(e))
|
||||
|
@ -104,9 +104,10 @@ defmodule Mobilizon.Service.HTTPSignatures do
|
|||
end
|
||||
|
||||
def generate_date_header(date \\ Timex.now("GMT")) do
|
||||
with {:ok, date} <- Timex.format(date, "%a, %d %b %Y %H:%M:%S %Z", :strftime) do
|
||||
date
|
||||
else
|
||||
case Timex.format(date, "%a, %d %b %Y %H:%M:%S %Z", :strftime) do
|
||||
{:ok, date} ->
|
||||
date
|
||||
|
||||
{:error, err} ->
|
||||
Logger.error("Unable to generate date header")
|
||||
Logger.error(inspect(err))
|
||||
|
|
|
@ -43,9 +43,10 @@ defmodule Mobilizon.Service.WebFinger do
|
|||
{:ok, represent_actor(actor, "JSON")}
|
||||
else
|
||||
_e ->
|
||||
with {:ok, %Actor{} = actor} when not is_nil(actor) <- Actors.get_actor_by_url(resource) do
|
||||
{:ok, represent_actor(actor, "JSON")}
|
||||
else
|
||||
case Actors.get_actor_by_url(resource) do
|
||||
{:ok, %Actor{} = actor} when not is_nil(actor) ->
|
||||
{:ok, represent_actor(actor, "JSON")}
|
||||
|
||||
_e ->
|
||||
{:error, "Couldn't find actor"}
|
||||
end
|
||||
|
@ -94,9 +95,10 @@ defmodule Mobilizon.Service.WebFinger do
|
|||
actor = String.trim_leading(actor, "@")
|
||||
|
||||
domain =
|
||||
with [_name, domain] <- String.split(actor, "@") do
|
||||
domain
|
||||
else
|
||||
case String.split(actor, "@") do
|
||||
[_name, domain] ->
|
||||
domain
|
||||
|
||||
_e ->
|
||||
URI.parse(actor).host
|
||||
end
|
||||
|
|
|
@ -440,9 +440,10 @@ defmodule Mobilizon.ActorsTest do
|
|||
end
|
||||
|
||||
test "create_bot/1 with invalid data returns error changeset" do
|
||||
with {:error, %Ecto.Changeset{}} <- Actors.create_bot(@invalid_attrs) do
|
||||
assert true
|
||||
else
|
||||
case Actors.create_bot(@invalid_attrs) do
|
||||
{:error, %Ecto.Changeset{}} ->
|
||||
assert true
|
||||
|
||||
_ ->
|
||||
assert false
|
||||
end
|
||||
|
|
|
@ -92,12 +92,13 @@ defmodule Mobilizon.EventsTest do
|
|||
|> Map.put(:organizer_actor_id, actor.id)
|
||||
|> Map.put(:address_id, address.id)
|
||||
|
||||
with {:ok, %Event{} = event} <- Events.create_event(valid_attrs) do
|
||||
assert event.begins_on == DateTime.from_naive!(~N[2010-04-17 14:00:00Z], "Etc/UTC")
|
||||
assert event.description == "some description"
|
||||
assert event.ends_on == DateTime.from_naive!(~N[2010-04-17 14:00:00Z], "Etc/UTC")
|
||||
assert event.title == "some title"
|
||||
else
|
||||
case Events.create_event(valid_attrs) do
|
||||
{:ok, %Event{} = event} ->
|
||||
assert event.begins_on == DateTime.from_naive!(~N[2010-04-17 14:00:00Z], "Etc/UTC")
|
||||
assert event.description == "some description"
|
||||
assert event.ends_on == DateTime.from_naive!(~N[2010-04-17 14:00:00Z], "Etc/UTC")
|
||||
assert event.title == "some title"
|
||||
|
||||
err ->
|
||||
flunk("Failed to create an event #{inspect(err)}")
|
||||
end
|
||||
|
@ -138,10 +139,11 @@ defmodule Mobilizon.EventsTest do
|
|||
test "get_public_events_for_actor/3", %{actor: actor, event: event} do
|
||||
event1 = insert(:event, organizer_actor: actor)
|
||||
|
||||
with {:ok, events_found, 2} <- Events.get_public_events_for_actor(actor, 1, 10) do
|
||||
event_ids = MapSet.new(events_found |> Enum.map(& &1.id))
|
||||
assert event_ids == MapSet.new([event.id, event1.id])
|
||||
else
|
||||
case Events.get_public_events_for_actor(actor, 1, 10) do
|
||||
{:ok, events_found, 2} ->
|
||||
event_ids = MapSet.new(events_found |> Enum.map(& &1.id))
|
||||
assert event_ids == MapSet.new([event.id, event1.id])
|
||||
|
||||
err ->
|
||||
flunk("Failed to get events for an actor #{inspect(err)}")
|
||||
end
|
||||
|
@ -150,10 +152,10 @@ defmodule Mobilizon.EventsTest do
|
|||
test "get_public_events_for_actor/3 with limited results", %{actor: actor, event: event} do
|
||||
event1 = insert(:event, organizer_actor: actor)
|
||||
|
||||
with {:ok, [%Event{id: event_found_id}], 2} <-
|
||||
Events.get_public_events_for_actor(actor, 1, 1) do
|
||||
assert event_found_id in [event.id, event1.id]
|
||||
else
|
||||
case Events.get_public_events_for_actor(actor, 1, 1) do
|
||||
{:ok, [%Event{id: event_found_id}], 2} ->
|
||||
assert event_found_id in [event.id, event1.id]
|
||||
|
||||
err ->
|
||||
flunk("Failed to get limited events for an actor #{inspect(err)}")
|
||||
end
|
||||
|
@ -345,11 +347,12 @@ defmodule Mobilizon.EventsTest do
|
|||
valid_attrs = Map.put(@valid_attrs, :event_id, event.id)
|
||||
valid_attrs = Map.put(valid_attrs, :actor_id, actor.id)
|
||||
|
||||
with {:ok, %Participant{} = participant} <- Events.create_participant(valid_attrs) do
|
||||
assert participant.event_id == event.id
|
||||
assert participant.actor_id == actor.id
|
||||
assert participant.role == :creator
|
||||
else
|
||||
case Events.create_participant(valid_attrs) do
|
||||
{:ok, %Participant{} = participant} ->
|
||||
assert participant.event_id == event.id
|
||||
assert participant.actor_id == actor.id
|
||||
assert participant.role == :creator
|
||||
|
||||
err ->
|
||||
flunk("Failed to create a participant #{inspect(err)}")
|
||||
end
|
||||
|
@ -362,10 +365,10 @@ defmodule Mobilizon.EventsTest do
|
|||
test "update_participant/2 with valid data updates the participant", %{
|
||||
participant: participant
|
||||
} do
|
||||
with {:ok, %Participant{} = participant} <-
|
||||
Events.update_participant(participant, @update_attrs) do
|
||||
assert participant.role == :moderator
|
||||
else
|
||||
case Events.update_participant(participant, @update_attrs) do
|
||||
{:ok, %Participant{} = participant} ->
|
||||
assert participant.role == :moderator
|
||||
|
||||
err ->
|
||||
flunk("Failed to update a participant #{inspect(err)}")
|
||||
end
|
||||
|
@ -575,10 +578,11 @@ defmodule Mobilizon.EventsTest do
|
|||
actor = insert(:actor)
|
||||
comment_data = Map.merge(@valid_attrs, %{actor_id: actor.id})
|
||||
|
||||
with {:ok, %Comment{} = comment} <- Events.create_comment(comment_data) do
|
||||
assert comment.text == "some text"
|
||||
assert comment.actor_id == actor.id
|
||||
else
|
||||
case Events.create_comment(comment_data) do
|
||||
{:ok, %Comment{} = comment} ->
|
||||
assert comment.text == "some text"
|
||||
assert comment.actor_id == actor.id
|
||||
|
||||
err ->
|
||||
flunk("Failed to create a comment #{inspect(err)}")
|
||||
end
|
||||
|
@ -591,9 +595,10 @@ defmodule Mobilizon.EventsTest do
|
|||
test "update_comment/2 with valid data updates the comment" do
|
||||
comment = insert(:comment)
|
||||
|
||||
with {:ok, %Comment{} = comment} <- Events.update_comment(comment, @update_attrs) do
|
||||
assert comment.text == "some updated text"
|
||||
else
|
||||
case Events.update_comment(comment, @update_attrs) do
|
||||
{:ok, %Comment{} = comment} ->
|
||||
assert comment.text == "some updated text"
|
||||
|
||||
err ->
|
||||
flunk("Failed to update a comment #{inspect(err)}")
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue