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:
Thomas Citharel
2019-04-25 19:05:05 +02:00
parent 7cd4df0ce9
commit 12116ba6fa
19 changed files with 392 additions and 234 deletions

View File

@@ -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"] ++

View File

@@ -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()
}