117
lib/eventos_web/views/activity_pub/account_view.ex
Normal file
117
lib/eventos_web/views/activity_pub/account_view.ex
Normal file
@@ -0,0 +1,117 @@
|
||||
defmodule EventosWeb.ActivityPub.AccountView do
|
||||
use EventosWeb, :view
|
||||
|
||||
alias EventosWeb.ActivityPub.AccountView
|
||||
alias EventosWeb.ActivityPub.ObjectView
|
||||
alias EventosWeb.WebFinger
|
||||
alias Eventos.Accounts.Account
|
||||
alias Eventos.Repo
|
||||
alias Eventos.Service.ActivityPub
|
||||
alias Eventos.Service.ActivityPub.Transmogrifier
|
||||
alias Eventos.Service.ActivityPub.Utils
|
||||
import Ecto.Query
|
||||
|
||||
def render("account.json", %{account: account}) do
|
||||
{:ok, public_key} = Account.get_public_key_for_account(account)
|
||||
|
||||
%{
|
||||
"id" => account.url,
|
||||
"type" => "Person",
|
||||
#"following" => "#{account.url}/following",
|
||||
#"followers" => "#{account.url}/followers",
|
||||
"inbox" => "#{account.url}/inbox",
|
||||
"outbox" => "#{account.url}/outbox",
|
||||
"preferredUsername" => account.username,
|
||||
"name" => account.display_name,
|
||||
"summary" => account.description,
|
||||
"url" => account.url,
|
||||
#"manuallyApprovesFollowers" => false,
|
||||
"publicKey" => %{
|
||||
"id" => "#{account.url}#main-key",
|
||||
"owner" => account.url,
|
||||
"publicKeyPem" => public_key
|
||||
},
|
||||
"endpoints" => %{
|
||||
"sharedInbox" => "#{EventosWeb.Endpoint.url()}/inbox"
|
||||
},
|
||||
# "icon" => %{
|
||||
# "type" => "Image",
|
||||
# "url" => User.avatar_url(account)
|
||||
# },
|
||||
# "image" => %{
|
||||
# "type" => "Image",
|
||||
# "url" => User.banner_url(account)
|
||||
# }
|
||||
}
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
end
|
||||
|
||||
def render("outbox.json", %{account: account, page: page}) do
|
||||
{page, no_page} = if page == 0 do
|
||||
{1, true}
|
||||
else
|
||||
{page, false}
|
||||
end
|
||||
|
||||
{activities, total} = ActivityPub.fetch_public_activities_for_account(account, page)
|
||||
|
||||
collection =
|
||||
Enum.map(activities, fn act ->
|
||||
{:ok, data} = Transmogrifier.prepare_outgoing(act.data)
|
||||
data
|
||||
end)
|
||||
|
||||
iri = "#{account.url}/outbox"
|
||||
|
||||
page = %{
|
||||
"id" => "#{iri}?page=#{page}",
|
||||
"type" => "OrderedCollectionPage",
|
||||
"partOf" => iri,
|
||||
"totalItems" => total,
|
||||
"orderedItems" => render_many(activities, AccountView, "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
|
||||
end
|
||||
|
||||
def render("activity.json", %{activity: activity}) do
|
||||
%{
|
||||
"id" => activity.data.url <> "/activity",
|
||||
"type" => "Create",
|
||||
"actor" => activity.data.organizer_account.url,
|
||||
"published" => Timex.now(),
|
||||
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
|
||||
"object" => render_one(activity.data, ObjectView, "event.json", as: :event)
|
||||
}
|
||||
end
|
||||
|
||||
def collection(collection, iri, page, total \\ nil) do
|
||||
offset = (page - 1) * 10
|
||||
items = Enum.slice(collection, offset, 10)
|
||||
items = Enum.map(items, fn user -> user.ap_id end)
|
||||
total = total || length(collection)
|
||||
|
||||
map = %{
|
||||
"id" => "#{iri}?page=#{page}",
|
||||
"type" => "OrderedCollectionPage",
|
||||
"partOf" => iri,
|
||||
"totalItems" => total,
|
||||
"orderedItems" => items
|
||||
}
|
||||
|
||||
if offset < total do
|
||||
Map.put(map, "next", "#{iri}?page=#{page + 1}")
|
||||
end
|
||||
end
|
||||
end
|
||||
37
lib/eventos_web/views/activity_pub/object_view.ex
Normal file
37
lib/eventos_web/views/activity_pub/object_view.ex
Normal file
@@ -0,0 +1,37 @@
|
||||
defmodule EventosWeb.ActivityPub.ObjectView do
|
||||
use EventosWeb, :view
|
||||
alias Eventos.Service.ActivityPub.Transmogrifier
|
||||
@base %{
|
||||
"@context" => [
|
||||
"https://www.w3.org/ns/activitystreams",
|
||||
"https://w3id.org/security/v1",
|
||||
%{
|
||||
"manuallyApprovesFollowers" => "as:manuallyApprovesFollowers",
|
||||
"sensitive" => "as:sensitive",
|
||||
"Hashtag" => "as:Hashtag",
|
||||
"toot" => "http://joinmastodon.org/ns#",
|
||||
"Emoji" => "toot:Emoji"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
def render("event.json", %{event: event}) do
|
||||
|
||||
|
||||
event = %{
|
||||
"type" => "Event",
|
||||
"id" => event.url,
|
||||
"name" => event.title,
|
||||
"category" => %{"title" => event.category.title},
|
||||
"content" => event.description,
|
||||
"mediaType" => "text/markdown",
|
||||
"published" => Timex.format!(event.inserted_at, "{ISO:Extended}"),
|
||||
"updated" => Timex.format!(event.updated_at, "{ISO:Extended}"),
|
||||
}
|
||||
Map.merge(event, @base)
|
||||
end
|
||||
|
||||
def render("category.json", %{category: category}) do
|
||||
category
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user