Add visibility to actors
Also use url helpers to generate urls properly Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -14,6 +14,14 @@ defenum(Mobilizon.Actors.ActorOpennessEnum, :actor_openness, [
|
||||
:open
|
||||
])
|
||||
|
||||
defenum(Mobilizon.Actors.ActorVisibilityEnum, :actor_visibility_type, [
|
||||
:public,
|
||||
:unlisted,
|
||||
# Probably unused
|
||||
:restricted,
|
||||
:private
|
||||
])
|
||||
|
||||
defmodule Mobilizon.Actors.Actor do
|
||||
@moduledoc """
|
||||
Represents an actor (local and remote actors)
|
||||
@@ -26,6 +34,9 @@ defmodule Mobilizon.Actors.Actor do
|
||||
alias Mobilizon.Actors.{Actor, Follower, Member}
|
||||
alias Mobilizon.Events.{Event, FeedToken}
|
||||
|
||||
alias MobilizonWeb.Router.Helpers, as: Routes
|
||||
alias MobilizonWeb.Endpoint
|
||||
|
||||
import Ecto.Query
|
||||
import Mobilizon.Ecto
|
||||
alias Mobilizon.Repo
|
||||
@@ -49,6 +60,7 @@ defmodule Mobilizon.Actors.Actor do
|
||||
field(:keys, :string)
|
||||
field(:manually_approves_followers, :boolean, default: false)
|
||||
field(:openness, Mobilizon.Actors.ActorOpennessEnum, default: :moderated)
|
||||
field(:visibility, Mobilizon.Actors.ActorVisibilityEnum, default: :private)
|
||||
field(:suspended, :boolean, default: false)
|
||||
field(:avatar_url, :string)
|
||||
field(:banner_url, :string)
|
||||
@@ -217,24 +229,43 @@ defmodule Mobilizon.Actors.Actor do
|
||||
@spec build_urls(Ecto.Changeset.t(), atom()) :: Ecto.Changeset.t()
|
||||
defp build_urls(changeset, type \\ :Person)
|
||||
|
||||
defp build_urls(%Ecto.Changeset{changes: %{preferred_username: username}} = changeset, type) do
|
||||
symbol = if type == :Group, do: "~", else: "@"
|
||||
|
||||
defp build_urls(%Ecto.Changeset{changes: %{preferred_username: username}} = changeset, _type) do
|
||||
changeset
|
||||
|> put_change(
|
||||
:outbox_url,
|
||||
"#{MobilizonWeb.Endpoint.url()}/#{symbol}#{username}/outbox"
|
||||
build_url(username, :outbox)
|
||||
)
|
||||
|> put_change(
|
||||
:inbox_url,
|
||||
"#{MobilizonWeb.Endpoint.url()}/#{symbol}#{username}/inbox"
|
||||
build_url(username, :inbox)
|
||||
)
|
||||
|> put_change(:shared_inbox_url, "#{MobilizonWeb.Endpoint.url()}/inbox")
|
||||
|> put_change(:url, "#{MobilizonWeb.Endpoint.url()}/#{symbol}#{username}")
|
||||
|> put_change(:url, build_url(username, :page))
|
||||
end
|
||||
|
||||
defp build_urls(%Ecto.Changeset{} = changeset, _type), do: changeset
|
||||
|
||||
@doc """
|
||||
Build an AP URL for an actor
|
||||
"""
|
||||
@spec build_url(String.t(), atom()) :: String.t()
|
||||
def build_url(preferred_username, endpoint, args \\ [])
|
||||
|
||||
def build_url(preferred_username, :page, args) do
|
||||
Endpoint
|
||||
|> Routes.page_url(:actor, preferred_username, args)
|
||||
|> URI.decode()
|
||||
end
|
||||
|
||||
def build_url(username, :inbox, _args), do: "#{build_url(username, :page)}/inbox"
|
||||
|
||||
def build_url(preferred_username, endpoint, args)
|
||||
when endpoint in [:outbox, :following, :followers] do
|
||||
Endpoint
|
||||
|> Routes.activity_pub_url(endpoint, preferred_username, args)
|
||||
|> URI.decode()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Get a public key for a given ActivityPub actor ID (url)
|
||||
"""
|
||||
@@ -272,8 +303,24 @@ defmodule Mobilizon.Actors.Actor do
|
||||
|
||||
If actor A and C both follow actor B, actor B's followers are A and C
|
||||
"""
|
||||
@spec get_followers(struct(), number(), number()) :: list()
|
||||
@spec get_followers(struct(), number(), number()) :: map()
|
||||
def get_followers(%Actor{id: actor_id} = _actor, page \\ nil, limit \\ nil) do
|
||||
query =
|
||||
from(
|
||||
a in Actor,
|
||||
join: f in Follower,
|
||||
on: a.id == f.actor_id,
|
||||
where: f.target_actor_id == ^actor_id
|
||||
)
|
||||
|
||||
total = Task.async(fn -> Repo.aggregate(query, :count, :id) end)
|
||||
elements = Task.async(fn -> Repo.all(paginate(query, page, limit)) end)
|
||||
|
||||
%{total: Task.await(total), elements: Task.await(elements)}
|
||||
end
|
||||
|
||||
@spec get_full_followers(struct()) :: list()
|
||||
def get_full_followers(%Actor{id: actor_id} = _actor) do
|
||||
Repo.all(
|
||||
from(
|
||||
a in Actor,
|
||||
@@ -281,7 +328,6 @@ defmodule Mobilizon.Actors.Actor do
|
||||
on: a.id == f.actor_id,
|
||||
where: f.target_actor_id == ^actor_id
|
||||
)
|
||||
|> paginate(page, limit)
|
||||
)
|
||||
end
|
||||
|
||||
@@ -292,6 +338,22 @@ defmodule Mobilizon.Actors.Actor do
|
||||
"""
|
||||
@spec get_followings(struct(), number(), number()) :: list()
|
||||
def get_followings(%Actor{id: actor_id} = _actor, page \\ nil, limit \\ nil) do
|
||||
query =
|
||||
from(
|
||||
a in Actor,
|
||||
join: f in Follower,
|
||||
on: a.id == f.target_actor_id,
|
||||
where: f.actor_id == ^actor_id
|
||||
)
|
||||
|
||||
total = Task.async(fn -> Repo.aggregate(query, :count, :id) end)
|
||||
elements = Task.async(fn -> Repo.all(paginate(query, page, limit)) end)
|
||||
|
||||
%{total: Task.await(total), elements: Task.await(elements)}
|
||||
end
|
||||
|
||||
@spec get_full_followings(struct()) :: list()
|
||||
def get_full_followings(%Actor{id: actor_id} = _actor) do
|
||||
Repo.all(
|
||||
from(
|
||||
a in Actor,
|
||||
@@ -299,7 +361,6 @@ defmodule Mobilizon.Actors.Actor do
|
||||
on: a.id == f.target_actor_id,
|
||||
where: f.actor_id == ^actor_id
|
||||
)
|
||||
|> paginate(page, limit)
|
||||
)
|
||||
end
|
||||
|
||||
@@ -390,6 +451,9 @@ defmodule Mobilizon.Actors.Actor do
|
||||
end
|
||||
end
|
||||
|
||||
@spec public_visibility?(struct()) :: boolean()
|
||||
def public_visibility?(%Actor{visibility: visibility}), do: visibility in [:public, :unlisted]
|
||||
|
||||
@doc """
|
||||
Return the preferred_username with the eventual @domain suffix if it's a distant actor
|
||||
"""
|
||||
|
||||
@@ -158,7 +158,8 @@ defmodule Mobilizon.Actors do
|
||||
Repo.all(
|
||||
from(
|
||||
a in Actor,
|
||||
where: a.type == ^:Group
|
||||
where: a.type == ^:Group,
|
||||
where: a.visibility in [^:public, ^:unlisted]
|
||||
)
|
||||
|> paginate(page, limit)
|
||||
)
|
||||
|
||||
@@ -19,6 +19,8 @@ defmodule Mobilizon.Events.Comment do
|
||||
alias Mobilizon.Events.Event
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Events.Comment
|
||||
alias MobilizonWeb.Router.Helpers, as: Routes
|
||||
alias MobilizonWeb.Endpoint
|
||||
|
||||
schema "comments" do
|
||||
field(:text, :string)
|
||||
@@ -46,7 +48,7 @@ defmodule Mobilizon.Events.Comment do
|
||||
url =
|
||||
if Map.has_key?(attrs, "url"),
|
||||
do: attrs["url"],
|
||||
else: "#{MobilizonWeb.Endpoint.url()}/comments/#{uuid}"
|
||||
else: Routes.page_url(Endpoint, :comment, uuid)
|
||||
|
||||
comment
|
||||
|> Ecto.Changeset.cast(attrs, [
|
||||
|
||||
@@ -111,8 +111,12 @@ defmodule MobilizonWeb.ActivityPubController do
|
||||
end
|
||||
end
|
||||
|
||||
def outbox(conn, %{"name" => username}) do
|
||||
outbox(conn, %{"name" => username, "page" => "0"})
|
||||
def outbox(conn, %{"name" => name}) do
|
||||
with %Actor{} = actor <- Actors.get_local_actor_by_name(name) do
|
||||
conn
|
||||
|> put_resp_header("content-type", "application/activity+json")
|
||||
|> json(ActorView.render("outbox.json", %{actor: actor}))
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: Ensure that this inbox is a recipient of the message
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
defmodule MobilizonWeb.ActivityPub.ActorView do
|
||||
use MobilizonWeb, :view
|
||||
|
||||
alias MobilizonWeb.ActivityPub.ActorView
|
||||
alias MobilizonWeb.ActivityPub.ObjectView
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Service.ActivityPub
|
||||
alias Mobilizon.Service.ActivityPub.Utils
|
||||
alias Mobilizon.Activity
|
||||
|
||||
@private_visibility_empty_collection %{elements: [], total: 0}
|
||||
|
||||
def render("actor.json", %{actor: actor}) do
|
||||
public_key = Mobilizon.Service.ActivityPub.Utils.pem_to_public_key_pem(actor.keys)
|
||||
|
||||
%{
|
||||
"id" => actor.url,
|
||||
"id" => Actor.build_url(actor.preferred_username, :page),
|
||||
"type" => "Person",
|
||||
"following" => actor.following_url,
|
||||
"followers" => actor.followers_url,
|
||||
"inbox" => actor.inbox_url,
|
||||
"outbox" => actor.outbox_url,
|
||||
"following" => Actor.build_url(actor.preferred_username, :following),
|
||||
"followers" => Actor.build_url(actor.preferred_username, :followers),
|
||||
"inbox" => Actor.build_url(actor.preferred_username, :inbox),
|
||||
"outbox" => Actor.build_url(actor.preferred_username, :outbox),
|
||||
"preferredUsername" => actor.preferred_username,
|
||||
"name" => actor.name,
|
||||
"summary" => actor.summary,
|
||||
@@ -46,125 +46,102 @@ defmodule MobilizonWeb.ActivityPub.ActorView do
|
||||
end
|
||||
|
||||
def render("following.json", %{actor: actor, page: page}) do
|
||||
actor
|
||||
|> Actor.get_followings(page)
|
||||
|> collection(actor.following_url, page)
|
||||
%{total: total, elements: following} =
|
||||
if Actor.public_visibility?(actor),
|
||||
do: Actor.get_followings(actor, page),
|
||||
else: @private_visibility_empty_collection
|
||||
|
||||
following
|
||||
|> collection(actor.preferred_username, :following, page, total)
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
end
|
||||
|
||||
def render("following.json", %{actor: actor}) do
|
||||
following = Actor.get_followings(actor)
|
||||
%{total: total, elements: following} =
|
||||
if Actor.public_visibility?(actor),
|
||||
do: Actor.get_followings(actor),
|
||||
else: @private_visibility_empty_collection
|
||||
|
||||
%{
|
||||
"id" => actor.following_url,
|
||||
"id" => Actor.build_url(actor.preferred_username, :following),
|
||||
"type" => "OrderedCollection",
|
||||
"totalItems" => length(following),
|
||||
"first" => collection(following, actor.following_url, 1)
|
||||
"totalItems" => total,
|
||||
"first" => collection(following, actor.preferred_username, :following, 1, total)
|
||||
}
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
end
|
||||
|
||||
def render("followers.json", %{actor: actor, page: page}) do
|
||||
actor
|
||||
|> Actor.get_followers(page)
|
||||
|> collection(actor.followers_url, page)
|
||||
%{total: total, elements: followers} =
|
||||
if Actor.public_visibility?(actor),
|
||||
do: Actor.get_followers(actor, page),
|
||||
else: @private_visibility_empty_collection
|
||||
|
||||
followers
|
||||
|> collection(actor.preferred_username, :followers, page, total)
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
end
|
||||
|
||||
def render("followers.json", %{actor: actor}) do
|
||||
followers = Actor.get_followers(actor)
|
||||
%{total: total, elements: followers} =
|
||||
if Actor.public_visibility?(actor),
|
||||
do: Actor.get_followers(actor),
|
||||
else: @private_visibility_empty_collection
|
||||
|
||||
%{
|
||||
"id" => actor.followers_url,
|
||||
"id" => Actor.build_url(actor.preferred_username, :followers),
|
||||
"type" => "OrderedCollection",
|
||||
# TODO put me back
|
||||
# "totalItems" => length(followers),
|
||||
"first" => collection(followers, actor.followers_url, 1)
|
||||
"totalItems" => total,
|
||||
"first" => collection(followers, actor.preferred_username, :followers, 1, total)
|
||||
}
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
end
|
||||
|
||||
def render("outbox.json", %{actor: actor, page: page}) do
|
||||
{page, no_page} =
|
||||
if page == 0 do
|
||||
{1, true}
|
||||
else
|
||||
{page, false}
|
||||
end
|
||||
%{total: total, elements: followers} =
|
||||
if Actor.public_visibility?(actor),
|
||||
do: ActivityPub.fetch_public_activities_for_actor(actor, page),
|
||||
else: @private_visibility_empty_collection
|
||||
|
||||
{activities, total} = ActivityPub.fetch_public_activities_for_actor(actor, page)
|
||||
|
||||
# collection =
|
||||
# Enum.map(activities, fn act ->
|
||||
# {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
|
||||
# data
|
||||
# end)
|
||||
|
||||
iri = "#{actor.url}/outbox"
|
||||
|
||||
page = %{
|
||||
"id" => "#{iri}?page=#{page}",
|
||||
"type" => "OrderedCollectionPage",
|
||||
"partOf" => iri,
|
||||
"totalItems" => total,
|
||||
"orderedItems" => render_many(activities, ActorView, "activity.json", as: :activity),
|
||||
"next" => "#{iri}?page=#{page + 1}"
|
||||
}
|
||||
|
||||
if no_page do
|
||||
%{
|
||||
"id" => iri,
|
||||
"type" => "OrderedCollection",
|
||||
"totalItems" => total,
|
||||
"first" => page
|
||||
}
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
else
|
||||
page |> Map.merge(Utils.make_json_ld_header())
|
||||
end
|
||||
followers
|
||||
|> collection(actor.preferred_username, :outbox, page, total)
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
end
|
||||
|
||||
def render("activity.json", %{activity: %Activity{local: local, data: data} = activity}) do
|
||||
%{
|
||||
"id" => data["id"],
|
||||
"type" =>
|
||||
if local do
|
||||
"Create"
|
||||
else
|
||||
"Announce"
|
||||
end,
|
||||
"actor" => activity.actor,
|
||||
# Not sure if needed since this is used into outbox
|
||||
"published" => Timex.now(),
|
||||
"to" => activity.recipients,
|
||||
"object" =>
|
||||
case data["type"] do
|
||||
"Event" ->
|
||||
render_one(data, ObjectView, "event.json", as: :event)
|
||||
def render("outbox.json", %{actor: actor}) do
|
||||
%{total: total, elements: followers} =
|
||||
if Actor.public_visibility?(actor),
|
||||
do: ActivityPub.fetch_public_activities_for_actor(actor),
|
||||
else: @private_visibility_empty_collection
|
||||
|
||||
"Note" ->
|
||||
render_one(data, ObjectView, "comment.json", as: :comment)
|
||||
end
|
||||
%{
|
||||
"id" => Actor.build_url(actor.preferred_username, :outbox),
|
||||
"type" => "OrderedCollection",
|
||||
"totalItems" => total,
|
||||
"first" => collection(followers, actor.preferred_username, :outbox, 1, total)
|
||||
}
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
end
|
||||
|
||||
def collection(collection, iri, page, _total \\ nil) do
|
||||
items = Enum.map(collection, fn account -> account.url end)
|
||||
@spec collection(list(), String.t(), atom(), integer(), integer()) :: map()
|
||||
defp collection(collection, preferred_username, endpoint, page, total)
|
||||
when endpoint in [:followers, :following, :outbox] do
|
||||
offset = (page - 1) * 10
|
||||
|
||||
# TODO : Add me back
|
||||
# total = total || length(collection)
|
||||
|
||||
%{
|
||||
"id" => "#{iri}?page=#{page}",
|
||||
map = %{
|
||||
"id" => Actor.build_url(preferred_username, endpoint, page: page),
|
||||
"type" => "OrderedCollectionPage",
|
||||
"partOf" => iri,
|
||||
# "totalItems" => total,
|
||||
"orderedItems" => items
|
||||
"partOf" => Actor.build_url(preferred_username, endpoint),
|
||||
"orderedItems" => Enum.map(collection, &item/1)
|
||||
}
|
||||
|
||||
# if offset < total do
|
||||
# Map.put(map, "next", "#{iri}?page=#{page + 1}")
|
||||
# end
|
||||
if offset < total do
|
||||
Map.put(map, "next", Actor.build_url(preferred_username, endpoint, page: page + 1))
|
||||
end
|
||||
|
||||
map
|
||||
end
|
||||
|
||||
def item(%Activity{data: %{"id" => id}}), do: id
|
||||
def item(%Actor{url: url}), do: url
|
||||
end
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
defmodule MobilizonWeb.ActivityPub.ObjectView do
|
||||
use MobilizonWeb, :view
|
||||
alias Mobilizon.Service.ActivityPub.Utils
|
||||
alias Mobilizon.Activity
|
||||
|
||||
def render("event.json", %{event: event}) do
|
||||
{:ok, html, []} = Earmark.as_html(event["summary"])
|
||||
@@ -40,4 +41,29 @@ defmodule MobilizonWeb.ActivityPub.ObjectView do
|
||||
|
||||
Map.merge(comment, Utils.make_json_ld_header())
|
||||
end
|
||||
|
||||
def render("activity.json", %{activity: %Activity{local: local, data: data} = activity}) do
|
||||
%{
|
||||
"id" => data["id"],
|
||||
"type" =>
|
||||
if local do
|
||||
"Create"
|
||||
else
|
||||
"Announce"
|
||||
end,
|
||||
"actor" => activity.actor,
|
||||
# Not sure if needed since this is used into outbox
|
||||
"published" => Timex.now(),
|
||||
"to" => activity.recipients,
|
||||
"object" =>
|
||||
case data["type"] do
|
||||
"Event" ->
|
||||
render_one(data, ObjectView, "event.json", as: :event)
|
||||
|
||||
"Note" ->
|
||||
render_one(data, ObjectView, "comment.json", as: :comment)
|
||||
end
|
||||
}
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
end
|
||||
end
|
||||
|
||||
@@ -383,7 +383,7 @@ defmodule Mobilizon.Service.ActivityPub do
|
||||
|
||||
followers =
|
||||
if actor.followers_url in activity.recipients do
|
||||
Actor.get_followers(actor) |> Enum.filter(fn follower -> is_nil(follower.domain) end)
|
||||
Actor.get_full_followers(actor) |> Enum.filter(fn follower -> is_nil(follower.domain) end)
|
||||
else
|
||||
[]
|
||||
end
|
||||
@@ -492,50 +492,18 @@ defmodule Mobilizon.Service.ActivityPub do
|
||||
@doc """
|
||||
Return all public activities (events & comments) for an actor
|
||||
"""
|
||||
@spec fetch_public_activities_for_actor(Actor.t(), integer(), integer()) :: {list(), integer()}
|
||||
def fetch_public_activities_for_actor(actor, page \\ nil, limit \\ nil)
|
||||
@spec fetch_public_activities_for_actor(Actor.t(), integer(), integer()) :: map()
|
||||
def fetch_public_activities_for_actor(%Actor{} = actor, page \\ 1, limit \\ 10) do
|
||||
{:ok, events, total_events} = Events.get_public_events_for_actor(actor, page, limit)
|
||||
{:ok, comments, total_comments} = Events.get_public_comments_for_actor(actor, page, limit)
|
||||
|
||||
def fetch_public_activities_for_actor(%Actor{} = actor, page, limit) do
|
||||
case actor.type do
|
||||
:Person ->
|
||||
{:ok, events, total_events} = Events.get_public_events_for_actor(actor, page, limit)
|
||||
{:ok, comments, total_comments} = Events.get_public_comments_for_actor(actor, page, limit)
|
||||
event_activities = Enum.map(events, &event_to_activity/1)
|
||||
|
||||
event_activities = Enum.map(events, &event_to_activity/1)
|
||||
comment_activities = Enum.map(comments, &comment_to_activity/1)
|
||||
|
||||
comment_activities = Enum.map(comments, &comment_to_activity/1)
|
||||
activities = event_activities ++ comment_activities
|
||||
|
||||
activities = event_activities ++ comment_activities
|
||||
|
||||
{activities, total_events + total_comments}
|
||||
|
||||
:Service ->
|
||||
bot = Actors.get_bot_by_actor(actor)
|
||||
|
||||
case bot.type do
|
||||
"ics" ->
|
||||
{:ok, %HTTPoison.Response{body: body} = _resp} = HTTPoison.get(bot.source)
|
||||
|
||||
ical_events =
|
||||
body
|
||||
|> ExIcal.parse()
|
||||
|> ExIcal.by_range(
|
||||
DateTime.utc_now(),
|
||||
DateTime.utc_now() |> DateTime.truncate(:second) |> Timex.shift(years: 1)
|
||||
)
|
||||
|
||||
activities =
|
||||
ical_events
|
||||
|> Enum.chunk_every(limit)
|
||||
|> Enum.at(page - 1)
|
||||
|> Enum.map(fn event ->
|
||||
{:ok, activity} = ical_event_to_activity(event, actor, bot.source)
|
||||
activity
|
||||
end)
|
||||
|
||||
{activities, length(ical_events)}
|
||||
end
|
||||
end
|
||||
%{elements: activities, total: total_events + total_comments}
|
||||
end
|
||||
|
||||
# Create an activity from an event
|
||||
@@ -560,38 +528,6 @@ defmodule Mobilizon.Service.ActivityPub do
|
||||
}
|
||||
end
|
||||
|
||||
defp ical_event_to_activity(%ExIcal.Event{} = ical_event, %Actor{} = actor, _source) do
|
||||
# Logger.debug(inspect ical_event)
|
||||
# TODO : Use MobilizonWeb.API instead
|
||||
# TODO : refactor me and move me somewhere else!
|
||||
# TODO : also, there should be a form of cache that allows this to be more efficient
|
||||
|
||||
# ical_event.categories should be tags
|
||||
|
||||
{:ok, event} =
|
||||
Events.create_event(%{
|
||||
begins_on: ical_event.start,
|
||||
ends_on: ical_event.end,
|
||||
inserted_at: ical_event.stamp,
|
||||
updated_at: ical_event.stamp,
|
||||
description: ical_event.description |> sanitize_ical_event_strings,
|
||||
title: ical_event.summary |> sanitize_ical_event_strings,
|
||||
organizer_actor: actor
|
||||
})
|
||||
|
||||
event_to_activity(event, false)
|
||||
end
|
||||
|
||||
defp sanitize_ical_event_strings(string) when is_binary(string) do
|
||||
string
|
||||
|> String.replace(~s"\r\n", "")
|
||||
|> String.replace(~s"\\,", ",")
|
||||
end
|
||||
|
||||
defp sanitize_ical_event_strings(nil) do
|
||||
nil
|
||||
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"] ++
|
||||
|
||||
@@ -20,6 +20,8 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
|
||||
alias Mobilizon.Service.ActivityPub
|
||||
alias Ecto.Changeset
|
||||
require Logger
|
||||
alias MobilizonWeb.Router.Helpers, as: Routes
|
||||
alias MobilizonWeb.Endpoint
|
||||
|
||||
# Some implementations send the actor URI as the actor field, others send the entire actor object,
|
||||
# so figure out what the actor's URI is based on what we have.
|
||||
@@ -275,7 +277,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
|
||||
"begins_on" => metadata.begins_on,
|
||||
"category" => category,
|
||||
"actor" => actor,
|
||||
"id" => "#{MobilizonWeb.Endpoint.url()}/events/#{uuid}",
|
||||
"id" => Routes.page_url(Endpoint, :event, uuid),
|
||||
"uuid" => uuid,
|
||||
"tag" => tags |> Enum.map(fn {_, tag} -> tag end) |> Enum.uniq()
|
||||
}
|
||||
@@ -296,7 +298,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
|
||||
"summary" => event.description,
|
||||
"publish_at" => (event.publish_at || event.inserted_at) |> DateTime.to_iso8601(),
|
||||
"updated_at" => event.updated_at |> DateTime.to_iso8601(),
|
||||
"id" => "#{MobilizonWeb.Endpoint.url()}/events/#{event.uuid}"
|
||||
"id" => Routes.page_url(Endpoint, :event, event.uuid)
|
||||
}
|
||||
end
|
||||
|
||||
@@ -320,7 +322,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
|
||||
"actor" => actor.url,
|
||||
"attributedTo" => actor.url,
|
||||
"uuid" => uuid,
|
||||
"id" => "#{MobilizonWeb.Endpoint.url()}/comments/#{uuid}"
|
||||
"id" => Routes.page_url(Endpoint, :comment, uuid)
|
||||
}
|
||||
|
||||
if reply_to do
|
||||
@@ -354,7 +356,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
|
||||
# "summary" => cw,
|
||||
# "attachment" => attachments,
|
||||
"actor" => actor,
|
||||
"id" => "#{MobilizonWeb.Endpoint.url()}/comments/#{uuid}",
|
||||
"id" => Routes.page_url(Endpoint, :comment, uuid),
|
||||
"uuid" => uuid,
|
||||
"tag" => tags |> Enum.map(fn {_, tag} -> tag end) |> Enum.uniq()
|
||||
}
|
||||
@@ -386,7 +388,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
|
||||
"summary" => content_html,
|
||||
"attributedTo" => actor,
|
||||
"preferredUsername" => preferred_username,
|
||||
"id" => "#{MobilizonWeb.Endpoint.url()}/~#{preferred_username}",
|
||||
"id" => Actor.build_url(preferred_username, :page),
|
||||
"uuid" => uuid,
|
||||
"tag" => tags |> Enum.map(fn {_, tag} -> tag end) |> Enum.uniq()
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ defmodule Mobilizon.Service.Export.Feed do
|
||||
@spec fetch_actor_event_feed(String.t()) :: String.t()
|
||||
defp fetch_actor_event_feed(name) do
|
||||
with %Actor{} = actor <- Actors.get_local_actor_by_name(name),
|
||||
{:visibility, true} <- {:visibility, Actor.public_visibility?(actor)},
|
||||
{:ok, events, _count} <- Events.get_public_events_for_actor(actor) do
|
||||
{:ok, build_actor_feed(actor, events)}
|
||||
else
|
||||
|
||||
@@ -40,12 +40,12 @@ defmodule Mobilizon.Service.Export.ICalendar do
|
||||
@doc """
|
||||
Export a public actor's events to iCalendar format.
|
||||
|
||||
The events must have a visibility of `:public` or `:unlisted`
|
||||
The actor must have a visibility of `:public` or `:unlisted`, as well as the events
|
||||
"""
|
||||
# TODO: The actor should also have visibility options
|
||||
@spec export_public_actor(Actor.t()) :: String.t()
|
||||
def export_public_actor(%Actor{} = actor) do
|
||||
with {:ok, events, _} <- Events.get_public_events_for_actor(actor) do
|
||||
with true <- Actor.public_visibility?(actor),
|
||||
{:ok, events, _} <- Events.get_public_events_for_actor(actor) do
|
||||
{:ok, %ICalendar{events: events |> Enum.map(&do_export_event/1)} |> ICalendar.to_ics()}
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user