106
lib/eventos_web/controllers/activity_pub_controller.ex
Normal file
106
lib/eventos_web/controllers/activity_pub_controller.ex
Normal file
@@ -0,0 +1,106 @@
|
||||
defmodule EventosWeb.ActivityPubController do
|
||||
use EventosWeb, :controller
|
||||
alias Eventos.{Accounts, Accounts.Account, Events, Events.Event}
|
||||
alias EventosWeb.ActivityPub.{ObjectView, AccountView}
|
||||
alias Eventos.Service.ActivityPub
|
||||
alias Eventos.Service.Federator
|
||||
|
||||
require Logger
|
||||
|
||||
action_fallback(:errors)
|
||||
|
||||
def account(conn, %{"username" => username}) do
|
||||
with %Account{} = account <- Accounts.get_account_by_username(username) do
|
||||
conn
|
||||
|> put_resp_header("content-type", "application/activity+json")
|
||||
|> json(AccountView.render("account.json", %{account: account}))
|
||||
end
|
||||
end
|
||||
|
||||
def event(conn, %{"username" => username, "slug" => slug}) do
|
||||
with %Event{} = event <- Events.get_event_full_by_username_and_slug!(username, slug) do
|
||||
conn
|
||||
|> put_resp_header("content-type", "application/activity+json")
|
||||
|> json(ObjectView.render("event.json", %{event: event}))
|
||||
end
|
||||
end
|
||||
|
||||
# def following(conn, %{"username" => username, "page" => page}) do
|
||||
# with %Account{} = account <- Accounts.get_account_by_username(username) do
|
||||
# {page, _} = Integer.parse(page)
|
||||
#
|
||||
# conn
|
||||
# |> put_resp_header("content-type", "application/activity+json")
|
||||
# |> json(UserView.render("following.json", %{account: account, page: page}))
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# def following(conn, %{"nickname" => nickname}) do
|
||||
# with %User{} = user <- User.get_cached_by_nickname(nickname),
|
||||
# {:ok, user} <- Pleroma.Web.WebFinger.ensure_keys_present(user) do
|
||||
# conn
|
||||
# |> put_resp_header("content-type", "application/activity+json")
|
||||
# |> json(UserView.render("following.json", %{user: user}))
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# def followers(conn, %{"nickname" => nickname, "page" => page}) do
|
||||
# with %User{} = user <- User.get_cached_by_nickname(nickname),
|
||||
# {:ok, user} <- Pleroma.Web.WebFinger.ensure_keys_present(user) do
|
||||
# {page, _} = Integer.parse(page)
|
||||
#
|
||||
# conn
|
||||
# |> put_resp_header("content-type", "application/activity+json")
|
||||
# |> json(UserView.render("followers.json", %{user: user, page: page}))
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# def followers(conn, %{"nickname" => nickname}) do
|
||||
# with %User{} = user <- User.get_cached_by_nickname(nickname),
|
||||
# {:ok, user} <- Pleroma.Web.WebFinger.ensure_keys_present(user) do
|
||||
# conn
|
||||
# |> put_resp_header("content-type", "application/activity+json")
|
||||
# |> json(UserView.render("followers.json", %{user: user}))
|
||||
# end
|
||||
# end
|
||||
|
||||
def outbox(conn, %{"username" => username, "page" => page}) do
|
||||
with {page, ""} = Integer.parse(page),
|
||||
%Account{} = account <- Accounts.get_account_by_username(username) do
|
||||
conn
|
||||
|> put_resp_header("content-type", "application/activity+json")
|
||||
|> json(AccountView.render("outbox.json", %{account: account, page: page}))
|
||||
end
|
||||
end
|
||||
|
||||
def outbox(conn, %{"username" => username}) do
|
||||
outbox(conn, %{"username" => username, "page" => "0"})
|
||||
end
|
||||
|
||||
# TODO: Ensure that this inbox is a recipient of the message
|
||||
def inbox(%{assigns: %{valid_signature: true}} = conn, params) do
|
||||
Federator.enqueue(:incoming_ap_doc, params)
|
||||
json(conn, "ok")
|
||||
end
|
||||
|
||||
def inbox(conn, params) do
|
||||
headers = Enum.into(conn.req_headers, %{})
|
||||
|
||||
if !String.contains?(headers["signature"] || "", params["actor"]) do
|
||||
Logger.info("Signature not from author, relayed message, fetching from source")
|
||||
ActivityPub.fetch_event_from_url(params["object"]["id"])
|
||||
else
|
||||
Logger.info("Signature error")
|
||||
Logger.info("Could not validate #{params["actor"]}")
|
||||
Logger.info(inspect(conn.req_headers))
|
||||
end
|
||||
|
||||
json(conn, "ok")
|
||||
end
|
||||
|
||||
def errors(conn, _e) do
|
||||
conn
|
||||
|> put_status(500)
|
||||
|> json("error")
|
||||
end
|
||||
end
|
||||
42
lib/eventos_web/controllers/comment_controller.ex
Normal file
42
lib/eventos_web/controllers/comment_controller.ex
Normal file
@@ -0,0 +1,42 @@
|
||||
defmodule EventosWeb.CommentController do
|
||||
use EventosWeb, :controller
|
||||
|
||||
alias Eventos.Events
|
||||
alias Eventos.Events.Comment
|
||||
|
||||
action_fallback EventosWeb.FallbackController
|
||||
|
||||
def index(conn, _params) do
|
||||
comments = Events.list_comments()
|
||||
render(conn, "index.json", comments: comments)
|
||||
end
|
||||
|
||||
def create(conn, %{"comment" => comment_params}) do
|
||||
with {:ok, %Comment{} = comment} <- Events.create_comment(comment_params) do
|
||||
conn
|
||||
|> put_status(:created)
|
||||
|> put_resp_header("location", comment_path(conn, :show, comment))
|
||||
|> render("show.json", comment: comment)
|
||||
end
|
||||
end
|
||||
|
||||
def show(conn, %{"id" => id}) do
|
||||
comment = Events.get_comment!(id)
|
||||
render(conn, "show.json", comment: comment)
|
||||
end
|
||||
|
||||
def update(conn, %{"id" => id, "comment" => comment_params}) do
|
||||
comment = Events.get_comment!(id)
|
||||
|
||||
with {:ok, %Comment{} = comment} <- Events.update_comment(comment, comment_params) do
|
||||
render(conn, "show.json", comment: comment)
|
||||
end
|
||||
end
|
||||
|
||||
def delete(conn, %{"id" => id}) do
|
||||
comment = Events.get_comment!(id)
|
||||
with {:ok, %Comment{}} <- Events.delete_comment(comment) do
|
||||
send_resp(conn, :no_content, "")
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -15,7 +15,6 @@ defmodule EventosWeb.GroupController do
|
||||
end
|
||||
|
||||
def create(conn, %{"group" => group_params}) do
|
||||
group_params = Map.put(group_params, "uri", "h")
|
||||
group_params = Map.put(group_params, "url", "h")
|
||||
with {:ok, %Group{} = group} <- Groups.create_group(group_params) do
|
||||
conn
|
||||
|
||||
8
lib/eventos_web/controllers/inboxes_controller.ex
Normal file
8
lib/eventos_web/controllers/inboxes_controller.ex
Normal file
@@ -0,0 +1,8 @@
|
||||
defmodule EventosWeb.InboxesController do
|
||||
|
||||
use EventosWeb, :controller
|
||||
|
||||
def create(conn) do
|
||||
|
||||
end
|
||||
end
|
||||
67
lib/eventos_web/controllers/nodeinfo_controller.ex
Normal file
67
lib/eventos_web/controllers/nodeinfo_controller.ex
Normal file
@@ -0,0 +1,67 @@
|
||||
defmodule EventosWeb.NodeinfoController do
|
||||
use EventosWeb, :controller
|
||||
|
||||
alias EventosWeb
|
||||
alias Eventos.{Accounts, Events}
|
||||
|
||||
@instance Application.get_env(:eventos, :instance)
|
||||
|
||||
def schemas(conn, _params) do
|
||||
response = %{
|
||||
links: [
|
||||
%{
|
||||
rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
|
||||
href: EventosWeb.Endpoint.url() <> "/nodeinfo/2.0.json"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
json(conn, response)
|
||||
end
|
||||
|
||||
# Schema definition: https://github.com/jhass/nodeinfo/blob/master/schemas/2.0/schema.json
|
||||
def nodeinfo(conn, %{"version" => "2.0.json"}) do
|
||||
import Logger
|
||||
Logger.debug(inspect @instance)
|
||||
#stats = Stats.get_stats()
|
||||
|
||||
response = %{
|
||||
version: "2.0",
|
||||
software: %{
|
||||
name: "eventos",
|
||||
version: Keyword.get(@instance, :version)
|
||||
},
|
||||
protocols: ["activitypub"],
|
||||
services: %{
|
||||
inbound: [],
|
||||
outbound: []
|
||||
},
|
||||
openRegistrations: Keyword.get(@instance, :registrations_open),
|
||||
usage: %{
|
||||
users: %{
|
||||
#total: stats.user_count || 0
|
||||
total: Accounts.count_users()
|
||||
},
|
||||
localPosts: Events.count_local_events(),
|
||||
localComments: Events.count_local_comments(),
|
||||
#localPosts: stats.status_count || 0
|
||||
},
|
||||
metadata: %{
|
||||
nodeName: Keyword.get(@instance, :name)
|
||||
}
|
||||
}
|
||||
|
||||
conn
|
||||
|> put_resp_header(
|
||||
"content-type",
|
||||
"application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.0#; charset=utf-8"
|
||||
)
|
||||
|> json(response)
|
||||
end
|
||||
|
||||
def nodeinfo(conn, _) do
|
||||
conn
|
||||
|> put_status(404)
|
||||
|> json(%{error: "Nodeinfo schema version not handled"})
|
||||
end
|
||||
end
|
||||
11
lib/eventos_web/controllers/outboxes_controller.ex
Normal file
11
lib/eventos_web/controllers/outboxes_controller.ex
Normal file
@@ -0,0 +1,11 @@
|
||||
defmodule EventosWeb.OutboxesController do
|
||||
|
||||
use EventosWeb, :controller
|
||||
|
||||
def show(conn) do
|
||||
account = Guardian.Plug.current_resource(conn).account
|
||||
events = account.events
|
||||
|
||||
render(conn, "index.json", events: events)
|
||||
end
|
||||
end
|
||||
21
lib/eventos_web/controllers/web_finger_controller.ex
Normal file
21
lib/eventos_web/controllers/web_finger_controller.ex
Normal file
@@ -0,0 +1,21 @@
|
||||
defmodule EventosWeb.WebFingerController do
|
||||
use EventosWeb, :controller
|
||||
|
||||
alias Eventos.Service.WebFinger
|
||||
|
||||
def host_meta(conn, _params) do
|
||||
xml = WebFinger.host_meta()
|
||||
|
||||
conn
|
||||
|> put_resp_content_type("application/xrd+xml")
|
||||
|> send_resp(200, xml)
|
||||
end
|
||||
|
||||
def webfinger(conn, %{"resource" => resource}) do
|
||||
with {:ok, response} <- WebFinger.webfinger(resource, "JSON") do
|
||||
json(conn, response)
|
||||
else
|
||||
_e -> send_resp(conn, 404, "Couldn't find user")
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user