From d37c873b04b238baa66803bd16155e41b71ae1e1 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Fri, 22 Feb 2019 16:11:57 +0100 Subject: [PATCH] A few fixes comming from Dialyser Signed-off-by: Thomas Citharel Fixes Signed-off-by: Thomas Citharel --- lib/mobilizon/actors/actor.ex | 2 +- lib/mobilizon/events/events.ex | 1 + lib/mobilizon_web/api/groups.ex | 2 +- lib/mobilizon_web/http_signature.ex | 3 +- lib/service/activity_pub/activity_pub.ex | 38 ++++++++++++++++------ lib/service/activity_pub/transmogrifier.ex | 2 ++ lib/service/activity_pub/utils.ex | 13 +++++++- lib/service/web_finger/web_finger.ex | 1 + 8 files changed, 47 insertions(+), 15 deletions(-) diff --git a/lib/mobilizon/actors/actor.ex b/lib/mobilizon/actors/actor.ex index b264eee4a..0c9804ec2 100644 --- a/lib/mobilizon/actors/actor.ex +++ b/lib/mobilizon/actors/actor.ex @@ -213,7 +213,7 @@ defmodule Mobilizon.Actors.Actor do @doc """ Get a public key for a given ActivityPub actor ID (url) """ - @spec get_public_key_for_url(String.t()) :: {:ok, String.t()} + @spec get_public_key_for_url(String.t()) :: {:ok, String.t()} | {:error, atom()} def get_public_key_for_url(url) do with {:ok, %Actor{keys: keys}} <- Actors.get_or_fetch_by_url(url), {:ok, public_key} <- prepare_public_key(keys) do diff --git a/lib/mobilizon/events/events.ex b/lib/mobilizon/events/events.ex index 0e50ee4ee..6fc784991 100644 --- a/lib/mobilizon/events/events.ex +++ b/lib/mobilizon/events/events.ex @@ -579,6 +579,7 @@ defmodule Mobilizon.Events do @doc """ Create a relation between two tags """ + @spec create_tag_relation(map()) :: {:ok, TagRelation.t()} | {:error, Ecto.Changeset.t()} def create_tag_relation(attrs \\ {}) do %TagRelation{} |> TagRelation.changeset(attrs) diff --git a/lib/mobilizon_web/api/groups.ex b/lib/mobilizon_web/api/groups.ex index 96e891851..ac630b2a8 100644 --- a/lib/mobilizon_web/api/groups.ex +++ b/lib/mobilizon_web/api/groups.ex @@ -51,7 +51,7 @@ defmodule MobilizonWeb.API.Groups do {:existing_group, _} -> {:error, :existing_group_name} - {:bad_actor} -> + {:bad_actor, _} -> {:error, :bad_admin_actor} end end diff --git a/lib/mobilizon_web/http_signature.ex b/lib/mobilizon_web/http_signature.ex index a10b8c123..01aef6018 100644 --- a/lib/mobilizon_web/http_signature.ex +++ b/lib/mobilizon_web/http_signature.ex @@ -32,8 +32,7 @@ defmodule MobilizonWeb.HTTPSignaturePlug do [signature | _] = get_req_header(conn, "signature") cond do - # Dialyzer doesn't like this line - signature && String.contains?(signature, actor) -> + String.contains?(signature, actor) -> conn = conn |> put_req_header( diff --git a/lib/service/activity_pub/activity_pub.ex b/lib/service/activity_pub/activity_pub.ex index 015f11c70..bf6e6da0c 100644 --- a/lib/service/activity_pub/activity_pub.ex +++ b/lib/service/activity_pub/activity_pub.ex @@ -36,9 +36,8 @@ defmodule Mobilizon.Service.ActivityPub do @doc """ Wraps an object into an activity - - TODO: Rename me """ + # TODO: Rename me @spec insert(map(), boolean()) :: {:ok, %Activity{}} | {:error, any()} def insert(map, local \\ true) when is_map(map) do with map <- lazy_put_activity_defaults(map), @@ -113,6 +112,9 @@ defmodule Mobilizon.Service.ActivityPub do end end + @doc """ + Create an activity of type "Create" + """ def create(%{to: to, actor: actor, object: object} = params) do Logger.debug("creating an activity") Logger.debug(inspect(params)) @@ -240,6 +242,9 @@ defmodule Mobilizon.Service.ActivityPub do # end # end + @doc """ + Make an actor follow another + """ def follow(%Actor{} = follower, %Actor{} = followed, activity_id \\ nil, local \\ true) do with {:ok, %Follower{} = follow} <- Actor.follow(followed, follower, true), activity_follow_id <- activity_id || Follower.url(follow), @@ -253,6 +258,9 @@ defmodule Mobilizon.Service.ActivityPub do end end + @doc """ + Make an actor unfollow another + """ @spec unfollow(Actor.t(), Actor.t(), String.t(), boolean()) :: {:ok, map()} | any() def unfollow(%Actor{} = followed, %Actor{} = follower, activity_id \\ nil, local \\ true) do with {:ok, %Follower{id: follow_id}} <- Actor.unfollow(followed, follower), @@ -337,7 +345,7 @@ defmodule Mobilizon.Service.ActivityPub do end @doc """ - Find an actor in our local database or call Webfinger to find what's its AP ID is and then fetch it + Find an actor in our local database or call WebFinger to find what's its AP ID is and then fetch it """ @spec find_or_make_actor_from_nickname(String.t(), atom() | nil) :: tuple() def find_or_make_actor_from_nickname(nickname, type \\ nil) do @@ -355,7 +363,7 @@ defmodule Mobilizon.Service.ActivityPub do def find_or_make_group_from_nickname(nick), do: find_or_make_actor_from_nickname(nick, :Group) @doc """ - Create an actor inside our database from username, using Webfinger to find out it's AP ID and then fetch it + Create an actor inside our database from username, using WebFinger to find out it's AP ID and then fetch it """ @spec make_actor_from_nickname(String.t()) :: {:ok, %Actor{}} | {:error, any()} def make_actor_from_nickname(nickname) do @@ -366,6 +374,9 @@ defmodule Mobilizon.Service.ActivityPub do end end + @doc """ + Publish an activity to all appropriated audiences inboxes + """ def publish(actor, activity) do Logger.debug("Publishing an activity") @@ -395,6 +406,9 @@ defmodule Mobilizon.Service.ActivityPub do end) end + @doc """ + Publish an activity to a specific inbox + """ def publish_one(%{inbox: inbox, json: json, actor: actor, id: id}) do Logger.info("Federating #{id} to #{inbox}") %URI{host: host, path: path} = URI.parse(inbox) @@ -425,7 +439,7 @@ defmodule Mobilizon.Service.ActivityPub do ) end - # Fetching a remote actor's informations through it's AP ID + # Fetching a remote actor's information through it's AP ID @spec fetch_and_prepare_actor_from_url(String.t()) :: {:ok, struct()} | {:error, atom()} | any() defp fetch_and_prepare_actor_from_url(url) do Logger.debug("Fetching and preparing actor from url") @@ -447,6 +461,9 @@ defmodule Mobilizon.Service.ActivityPub do @doc """ Creating proper actor data struct from AP data + + + Convert ActivityPub data to our internal format """ @spec actor_data_from_actor_object(map()) :: {:ok, map()} def actor_data_from_actor_object(data) when is_map(data) do @@ -533,7 +550,7 @@ defmodule Mobilizon.Service.ActivityPub do # Create an activity from a comment @spec comment_to_activity(%Comment{}, boolean()) :: Activity.t() - def comment_to_activity(%Comment{} = comment, local \\ true) do + defp comment_to_activity(%Comment{} = comment, local \\ true) do %Activity{ recipients: ["https://www.w3.org/ns/activitystreams#Public"], actor: comment.actor.url, @@ -590,8 +607,9 @@ defmodule Mobilizon.Service.ActivityPub do nil end - def is_public?(activity) do - "https://www.w3.org/ns/activitystreams#Public" in (activity.data["to"] ++ - (activity.data["cc"] || [])) - end + # # Whether the Public audience is in the activity's audience + # defp is_public?(activity) do + # "https://www.w3.org/ns/activitystreams#Public" in (activity.data["to"] ++ + # (activity.data["cc"] || [])) + # end end diff --git a/lib/service/activity_pub/transmogrifier.ex b/lib/service/activity_pub/transmogrifier.ex index 965446348..d11d60613 100644 --- a/lib/service/activity_pub/transmogrifier.ex +++ b/lib/service/activity_pub/transmogrifier.ex @@ -498,8 +498,10 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do @spec normalize(map()) :: struct() | nil def normalize(obj) when is_map(obj), do: get_anything_by_url(obj["id"]) + @spec normalize(String.t()) :: struct() | nil def normalize(url) when is_binary(url), do: get_anything_by_url(url) + @spec normalize(any()) :: nil def normalize(_), do: nil diff --git a/lib/service/activity_pub/utils.ex b/lib/service/activity_pub/utils.ex index 0c243054f..637b1c132 100644 --- a/lib/service/activity_pub/utils.ex +++ b/lib/service/activity_pub/utils.ex @@ -188,7 +188,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do |> Map.put("in_reply_to_comment_id", id) |> Map.put("origin_comment_id", comment |> Comment.get_thread_id()) - # Anthing else is kind of a MP + # Anything else is kind of a MP {:error, object} -> Logger.debug("Parent object is something we don't handle") Logger.debug(inspect(object)) @@ -239,6 +239,16 @@ defmodule Mobilizon.Service.ActivityPub.Utils do @doc """ Make an AP event object from an set of values """ + @spec make_event_data( + String.t(), + String.t(), + String.t(), + String.t(), + list(), + list(), + map(), + String.t() + ) :: map() def make_event_data( actor, to, @@ -271,6 +281,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do } end + @spec make_event_data(Event.t(), list(String.t())) :: map() def make_event_data( %Event{title: title, organizer_actor: actor, uuid: uuid}, to \\ ["https://www.w3.org/ns/activitystreams#Public"] diff --git a/lib/service/web_finger/web_finger.ex b/lib/service/web_finger/web_finger.ex index 4e453bf59..5e765efd6 100644 --- a/lib/service/web_finger/web_finger.ex +++ b/lib/service/web_finger/web_finger.ex @@ -55,6 +55,7 @@ defmodule Mobilizon.Service.WebFinger do @spec represent_actor(Actor.t()) :: struct() def represent_actor(actor), do: represent_actor(actor, "JSON") + @spec represent_actor(Actor.t(), String.t()) :: struct() def represent_actor(actor, "JSON") do %{ "subject" => "acct:#{actor.preferred_username}@#{MobilizonWeb.Endpoint.host()}",