@@ -1,48 +1,44 @@
|
||||
defmodule EventosWeb.AccountView do
|
||||
defmodule EventosWeb.ActorView do
|
||||
@moduledoc """
|
||||
View for Accounts
|
||||
View for Actors
|
||||
"""
|
||||
use EventosWeb, :view
|
||||
alias EventosWeb.{AccountView, EventView}
|
||||
alias EventosWeb.{ActorView, EventView}
|
||||
|
||||
def render("index.json", %{accounts: accounts}) do
|
||||
%{data: render_many(accounts, AccountView, "acccount_basic.json")}
|
||||
def render("index.json", %{actors: actors}) do
|
||||
%{data: render_many(actors, ActorView, "acccount_basic.json")}
|
||||
end
|
||||
|
||||
def render("show.json", %{account: account}) do
|
||||
%{data: render_one(account, AccountView, "account.json")}
|
||||
def render("show.json", %{actor: actor}) do
|
||||
%{data: render_one(actor, ActorView, "actor.json")}
|
||||
end
|
||||
|
||||
def render("show_basic.json", %{account: account}) do
|
||||
%{data: render_one(account, AccountView, "account_basic.json")}
|
||||
def render("show_basic.json", %{actor: actor}) do
|
||||
%{data: render_one(actor, ActorView, "actor_basic.json")}
|
||||
end
|
||||
|
||||
def render("acccount_basic.json", %{account: account}) do
|
||||
%{id: account.id,
|
||||
username: account.username,
|
||||
domain: account.domain,
|
||||
display_name: account.display_name,
|
||||
description: account.description,
|
||||
# public_key: account.public_key,
|
||||
suspended: account.suspended,
|
||||
url: account.url,
|
||||
avatar_url: account.avatar_url,
|
||||
banner_url: account.banner_url,
|
||||
def render("acccount_basic.json", %{actor: actor}) do
|
||||
%{id: actor.id,
|
||||
username: actor.username,
|
||||
domain: actor.domain,
|
||||
display_name: actor.display_name,
|
||||
description: actor.description,
|
||||
# public_key: actor.public_key,
|
||||
suspended: actor.suspended,
|
||||
url: actor.url,
|
||||
}
|
||||
end
|
||||
|
||||
def render("account.json", %{account: account}) do
|
||||
%{id: account.id,
|
||||
username: account.username,
|
||||
domain: account.domain,
|
||||
display_name: account.display_name,
|
||||
description: account.description,
|
||||
# public_key: account.public_key,
|
||||
suspended: account.suspended,
|
||||
url: account.url,
|
||||
avatar_url: account.avatar_url,
|
||||
banner_url: account.banner_url,
|
||||
organized_events: render_many(account.organized_events, EventView, "event_simple.json")
|
||||
def render("actor.json", %{actor: actor}) do
|
||||
%{id: actor.id,
|
||||
username: actor.username,
|
||||
domain: actor.domain,
|
||||
display_name: actor.display_name,
|
||||
description: actor.description,
|
||||
# public_key: actor.public_key,
|
||||
suspended: actor.suspended,
|
||||
url: actor.url,
|
||||
organized_events: render_many(actor.organized_events, EventView, "event_simple.json")
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,117 +0,0 @@
|
||||
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
|
||||
155
lib/eventos_web/views/activity_pub/actor_view.ex
Normal file
155
lib/eventos_web/views/activity_pub/actor_view.ex
Normal file
@@ -0,0 +1,155 @@
|
||||
defmodule EventosWeb.ActivityPub.ActorView do
|
||||
use EventosWeb, :view
|
||||
|
||||
alias EventosWeb.ActivityPub.ActorView
|
||||
alias EventosWeb.ActivityPub.ObjectView
|
||||
alias EventosWeb.WebFinger
|
||||
alias Eventos.Actors.Actor
|
||||
alias Eventos.Repo
|
||||
alias Eventos.Service.ActivityPub
|
||||
alias Eventos.Service.ActivityPub.Transmogrifier
|
||||
alias Eventos.Service.ActivityPub.Utils
|
||||
import Ecto.Query
|
||||
|
||||
def render("actor.json", %{actor: actor}) do
|
||||
{:ok, public_key} = Actor.get_public_key_for_actor(actor)
|
||||
|
||||
%{
|
||||
"id" => actor.url,
|
||||
"type" => "Person",
|
||||
"following" => actor.following_url,
|
||||
"followers" => actor.followers_url,
|
||||
"inbox" => actor.inbox_url,
|
||||
"outbox" => actor.outbox_url,
|
||||
"preferredUsername" => actor.preferred_username,
|
||||
"name" => actor.name,
|
||||
"summary" => actor.summary,
|
||||
"url" => actor.url,
|
||||
"manuallyApprovesFollowers" => actor.manually_approves_followers,
|
||||
"publicKey" => %{
|
||||
"id" => "#{actor.url}#main-key",
|
||||
"owner" => actor.url,
|
||||
"publicKeyPem" => public_key
|
||||
},
|
||||
"endpoints" => %{
|
||||
"sharedInbox" => actor.shared_inbox_url,
|
||||
},
|
||||
# "icon" => %{
|
||||
# "type" => "Image",
|
||||
# "url" => User.avatar_url(actor)
|
||||
# },
|
||||
# "image" => %{
|
||||
# "type" => "Image",
|
||||
# "url" => User.banner_url(actor)
|
||||
# }
|
||||
}
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
end
|
||||
|
||||
def render("following.json", %{actor: actor, page: page}) do
|
||||
following = Actor.get_followings(actor)
|
||||
|
||||
collection(following, actor.following_url, page)
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
end
|
||||
|
||||
def render("following.json", %{actor: actor}) do
|
||||
following = Actor.get_followings(actor)
|
||||
|
||||
%{
|
||||
"id" => actor.following_url,
|
||||
"type" => "OrderedCollection",
|
||||
"totalItems" => length(following),
|
||||
"first" => collection(following, actor.following_url, 1)
|
||||
}
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
end
|
||||
|
||||
def render("followers.json", %{actor: actor, page: page}) do
|
||||
followers = Actor.get_followers(actor)
|
||||
|
||||
collection(followers, actor.followers_url, page)
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
end
|
||||
|
||||
def render("followers.json", %{actor: actor}) do
|
||||
followers = Actor.get_followers(actor)
|
||||
|
||||
%{
|
||||
"id" => actor.followers_url,
|
||||
"type" => "OrderedCollection",
|
||||
"totalItems" => length(followers),
|
||||
"first" => collection(followers, actor.followers_url, 1)
|
||||
}
|
||||
|> 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
|
||||
|
||||
{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
|
||||
end
|
||||
|
||||
def render("activity.json", %{activity: activity}) do
|
||||
%{
|
||||
"id" => activity.data.url <> "/activity",
|
||||
"type" => "Create",
|
||||
"actor" => activity.data.organizer_actor.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 account -> account.url 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
|
||||
@@ -3,7 +3,7 @@ defmodule EventosWeb.EventView do
|
||||
View for Events
|
||||
"""
|
||||
use EventosWeb, :view
|
||||
alias EventosWeb.{EventView, AccountView, GroupView, AddressView}
|
||||
alias EventosWeb.{EventView, ActorView, GroupView, AddressView}
|
||||
|
||||
def render("index.json", %{events: events}) do
|
||||
%{data: render_many(events, EventView, "event_simple.json")}
|
||||
@@ -32,9 +32,9 @@ defmodule EventosWeb.EventView do
|
||||
description: event.description,
|
||||
begins_on: event.begins_on,
|
||||
ends_on: event.ends_on,
|
||||
organizer: render_one(event.organizer_account, AccountView, "acccount_basic.json"),
|
||||
organizer: render_one(event.organizer_actor, ActorView, "acccount_basic.json"),
|
||||
group: render_one(event.organizer_group, GroupView, "group_basic.json"),
|
||||
participants: render_many(event.participants, AccountView, "show_basic.json"),
|
||||
participants: render_many(event.participants, ActorView, "show_basic.json"),
|
||||
address: render_one(event.address, AddressView, "address.json"),
|
||||
}
|
||||
end
|
||||
|
||||
@@ -3,7 +3,7 @@ defmodule EventosWeb.GroupView do
|
||||
View for Groups
|
||||
"""
|
||||
use EventosWeb, :view
|
||||
alias EventosWeb.{GroupView, AccountView}
|
||||
alias EventosWeb.{GroupView, ActorView}
|
||||
|
||||
def render("index.json", %{groups: groups}) do
|
||||
%{data: render_many(groups, GroupView, "group_simple.json")}
|
||||
@@ -32,7 +32,7 @@ defmodule EventosWeb.GroupView do
|
||||
description: group.description,
|
||||
suspended: group.suspended,
|
||||
url: group.url,
|
||||
members: render_many(group.members, AccountView, "acccount_basic.json"),
|
||||
members: render_many(group.members, ActorView, "acccount_basic.json"),
|
||||
events: render_many(group.organized_events, EventView, "event_simple.json")
|
||||
}
|
||||
end
|
||||
|
||||
@@ -4,7 +4,7 @@ defmodule EventosWeb.UserView do
|
||||
"""
|
||||
use EventosWeb, :view
|
||||
alias EventosWeb.UserView
|
||||
alias EventosWeb.AccountView
|
||||
alias EventosWeb.ActorView
|
||||
|
||||
def render("index.json", %{users: users}) do
|
||||
%{data: render_many(users, UserView, "user_simple.json")}
|
||||
@@ -28,14 +28,14 @@ defmodule EventosWeb.UserView do
|
||||
def render("user_simple.json", %{user: user}) do
|
||||
%{id: user.id,
|
||||
role: user.role,
|
||||
account: render_one(user.account, AccountView, "acccount_basic.json")
|
||||
actor: render_one(user.actor, ActorView, "acccount_basic.json")
|
||||
}
|
||||
end
|
||||
|
||||
def render("user.json", %{user: user}) do
|
||||
%{id: user.id,
|
||||
role: user.role,
|
||||
account: render_one(user.account, AccountView, "account.json")
|
||||
actor: render_one(user.actor, ActorView, "actor.json")
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user