Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2018-05-17 11:32:23 +02:00
parent 2c1abe5e19
commit e14007bac5
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
45 changed files with 2916 additions and 111 deletions

View File

@ -9,6 +9,15 @@ use Mix.Config
config :eventos,
ecto_repos: [Eventos.Repo]
config :eventos, :instance,
name: "Localhost",
version: "1.0.0-dev",
registrations_open: true
config :mime, :types, %{
"application/activity+json" => ["activity-json"]
}
# Configures the endpoint
config :eventos, EventosWeb.Endpoint,
url: [host: "localhost"],

View File

@ -7,7 +7,7 @@ use Mix.Config
# watchers to your application. For example, we use it
# with brunch.io to recompile .js and .css sources.
config :eventos, EventosWeb.Endpoint,
http: [port: 4000],
http: [port: 4001],
debug_errors: true,
code_reloader: true,
check_origin: false,

3
js/.env Normal file
View File

@ -0,0 +1,3 @@
API_HOST=localhost
API_ORIGIN=http://localhost:4001
API_PATH=/api/v1

View File

@ -4,9 +4,15 @@ defmodule Eventos.Accounts.Account do
"""
use Ecto.Schema
import Ecto.Changeset
alias Eventos.Accounts
alias Eventos.Accounts.{Account, User}
alias Eventos.Groups.{Group, Member, Request}
alias Eventos.Events.Event
alias Eventos.Service.ActivityPub
import Logger
@type t :: %Account{description: String.t, id: integer(), inserted_at: DateTime.t, updated_at: DateTime.t, display_name: String.t, domain: String.t, private_key: String.t, public_key: String.t, suspended: boolean(), url: String.t, username: String.t, organized_events: list(), groups: list(), group_request: list(), user: User.t}
schema "accounts" do
field :description, :string
@ -15,7 +21,6 @@ defmodule Eventos.Accounts.Account do
field :private_key, :string
field :public_key, :string
field :suspended, :boolean, default: false
field :uri, :string
field :url, :string
field :username, :string
field :avatar_url, :string
@ -31,15 +36,78 @@ defmodule Eventos.Accounts.Account do
@doc false
def changeset(%Account{} = account, attrs) do
account
|> cast(attrs, [:username, :domain, :display_name, :description, :private_key, :public_key, :suspended, :uri, :url, :avatar_url, :banner_url])
|> validate_required([:username, :public_key, :suspended, :uri, :url])
|> cast(attrs, [:username, :domain, :display_name, :description, :private_key, :public_key, :suspended, :url])
|> validate_required([:username, :public_key, :suspended, :url])
|> unique_constraint(:username, name: :accounts_username_domain_index)
end
def registration_changeset(%Account{} = account, attrs) do
account
|> cast(attrs, [:username, :domain, :display_name, :description, :private_key, :public_key, :suspended, :uri, :url, :avatar_url, :banner_url])
|> validate_required([:username, :public_key, :suspended, :uri, :url])
|> cast(attrs, [:username, :domain, :display_name, :description, :private_key, :public_key, :suspended, :url])
|> validate_required([:username, :public_key, :suspended, :url])
|> unique_constraint(:username)
end
@email_regex ~r/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
def remote_account_creation(params) do
changes =
%Account{}
|> cast(params, [:description, :display_name, :url, :username, :public_key])
|> validate_required([:url, :username, :public_key])
|> unique_constraint(:username)
|> validate_format(:username, @email_regex)
|> validate_length(:description, max: 5000)
|> validate_length(:display_name, max: 100)
|> put_change(:local, false)
Logger.debug("Remote account creation")
Logger.debug(inspect changes)
changes
# if changes.valid? do
# case changes.changes[:info]["source_data"] do
# %{"followers" => followers} ->
# changes
# |> put_change(:follower_address, followers)
#
# _ ->
# followers = User.ap_followers(%User{nickname: changes.changes[:nickname]})
#
# changes
# |> put_change(:follower_address, followers)
# end
# else
# changes
# end
end
def get_or_fetch_by_url(url) do
if user = Accounts.get_account_by_url(url) do
user
else
case ActivityPub.make_account_from_url(url) do
{:ok, user} ->
user
_ -> {:error, "Could not fetch by AP id"}
end
end
end
@spec get_public_key_for_url(Account.t) :: {:ok, String.t}
def get_public_key_for_url(url) do
with %Account{} = account <- get_or_fetch_by_url(url) do
get_public_key_for_account(account)
else
_ -> :error
end
end
@spec get_public_key_for_account(Account.t) :: {:ok, String.t}
def get_public_key_for_account(%Account{} = account) do
{:ok, account.public_key}
end
@spec get_private_key_for_account(Account.t) :: {:ok, String.t}
def get_private_key_for_account(%Account{} = account) do
account.private_key
end
end

View File

@ -8,6 +8,9 @@ defmodule Eventos.Accounts do
alias Eventos.Repo
alias Eventos.Accounts.Account
alias Eventos.Accounts
alias Eventos.Service.ActivityPub
@doc """
Returns the list of accounts.
@ -110,6 +113,20 @@ defmodule Eventos.Accounts do
Account.changeset(account, %{})
end
@doc """
Returns a text representation of a local account like user@domain.tld
"""
def account_to_local_username_and_domain(account) do
"#{account.username}@#{Application.get_env(:my, EventosWeb.Endpoint)[:url][:host]}"
end
@doc """
Returns a webfinger representation of an account
"""
def account_to_webfinger_s(account) do
"acct:#{account_to_local_username_and_domain(account)}"
end
alias Eventos.Accounts.User
@doc """
@ -130,6 +147,34 @@ defmodule Eventos.Accounts do
Repo.preload(users, :account)
end
defp blank?(""), do: nil
defp blank?(n), do: n
def insert_or_update_account(data) do
data =
data
|> Map.put(:name, blank?(data[:display_name]) || data[:username])
cs = Account.remote_account_creation(data)
Repo.insert(cs, on_conflict: [set: [public_key: data.public_key]], conflict_target: [:username, :domain])
end
# def increase_event_count(%Account{} = account) do
# event_count = (account.info["event_count"] || 0) + 1
# new_info = Map.put(account.info, "note_count", note_count)
#
# cs = info_changeset(account, %{info: new_info})
#
# update_and_set_cache(cs)
# end
def count_users() do
Repo.one(
from u in User,
select: count(u.id)
)
end
@doc """
Gets a single user.
@ -151,6 +196,29 @@ defmodule Eventos.Accounts do
Repo.preload(user, :account)
end
def get_account_by_url(url) do
Repo.get_by(Account, url: url)
end
def get_account_by_username(username) do
Repo.get_by!(Account, username: username)
end
def get_or_fetch_by_url(url) do
if account = get_account_by_url(url) do
account
else
ap_try = ActivityPub.make_account_from_url(url)
case ap_try do
{:ok, account} ->
account
_ -> {:error, "Could not fetch by AP id"}
end
end
end
@doc """
Get an user by email
"""
@ -191,18 +259,17 @@ defmodule Eventos.Accounts do
Register user
"""
def register(%{email: email, password: password, username: username}) do
{:ok, {privkey, pubkey}} = RsaEx.generate_keypair("4096")
#{:ok, {privkey, pubkey}} = RsaEx.generate_keypair("4096")
{:ok, rsa_priv_key} = ExPublicKey.generate_key()
{:ok, rsa_pub_key} = ExPublicKey.public_key_from_private_key(rsa_priv_key)
avatar = gravatar(email)
account = Eventos.Accounts.Account.registration_changeset(%Eventos.Accounts.Account{}, %{
username: username,
domain: nil,
private_key: privkey,
public_key: pubkey,
uri: "h",
url: "h",
avatar_url: avatar,
private_key: rsa_priv_key |> ExPublicKey.pem_encode(),
public_key: rsa_pub_key |> ExPublicKey.pem_encode(),
url: EventosWeb.Endpoint.url() <> "/@" <> username,
})
user = Eventos.Accounts.User.registration_changeset(%Eventos.Accounts.User{}, %{

7
lib/eventos/activity.ex Normal file
View File

@ -0,0 +1,7 @@
defmodule Eventos.Activity do
@moduledoc """
Represents an activity
"""
defstruct [:id, :data, :local, :actor, :recipients, :notifications]
end

View File

@ -17,7 +17,8 @@ defmodule Eventos.Application do
supervisor(EventosWeb.Endpoint, []),
# Start your own worker by calling: Eventos.Worker.start_link(arg1, arg2, arg3)
# worker(Eventos.Worker, [arg1, arg2, arg3]),
worker(Guardian.DB.Token.SweeperServer, [])
worker(Guardian.DB.Token.SweeperServer, []),
worker(Eventos.Service.Federator, []),
]
# See https://hexdocs.pm/elixir/Supervisor.html

View File

@ -0,0 +1,27 @@
defmodule Eventos.Events.Comment do
use Ecto.Schema
import Ecto.Changeset
alias Eventos.Events.Event
alias Eventos.Accounts.Account
alias Eventos.Accounts.Comment
schema "comments" do
field :text, :string
field :url, :string
field :local, :boolean, default: true
belongs_to :account, Account, [foreign_key: :account_id]
belongs_to :event, Event, [foreign_key: :event_id]
belongs_to :in_reply_to_comment, Comment, [foreign_key: :in_reply_to_comment_id]
belongs_to :origin_comment, Comment, [foreign_key: :origin_comment_id]
timestamps()
end
@doc false
def changeset(comment, attrs) do
comment
|> cast(attrs, [:url, :text, :account_id, :event_id, :in_reply_to_comment_id])
|> validate_required([:url, :text, :account_id])
end
end

View File

@ -39,6 +39,8 @@ defmodule Eventos.Events.Event do
alias Eventos.Addresses.Address
schema "events" do
field :url, :string
field :local, :boolean, default: true
field :begins_on, Timex.Ecto.DateTimeWithTimezone
field :description, :string
field :ends_on, Timex.Ecto.DateTimeWithTimezone
@ -60,13 +62,13 @@ defmodule Eventos.Events.Event do
has_many :sessions, Session
belongs_to :address, Address
timestamps()
timestamps(type: :utc_datetime)
end
@doc false
def changeset(%Event{} = event, attrs) do
event
|> cast(attrs, [:title, :description, :begins_on, :ends_on, :organizer_account_id, :organizer_group_id, :category_id, :state, :status, :public, :thumbnail, :large_image, :publish_at])
|> cast(attrs, [:title, :description, :url, :begins_on, :ends_on, :organizer_account_id, :organizer_group_id, :category_id, :state, :status, :public, :thumbnail, :large_image, :publish_at])
|> cast_assoc(:tags)
|> cast_assoc(:address)
|> validate_required([:title, :description, :begins_on, :ends_on, :organizer_account_id, :category_id])

View File

@ -7,6 +7,7 @@ defmodule Eventos.Events do
alias Eventos.Repo
alias Eventos.Events.Event
alias Eventos.Events.Comment
alias Eventos.Accounts.Account
@doc """
@ -22,6 +23,36 @@ defmodule Eventos.Events do
Repo.all(Event)
end
def get_events_for_account(%Account{id: account_id} = _account, page \\ 1, limit \\ 10) do
start = (page - 1) * limit
query = from e in Event,
where: e.organizer_account_id == ^account_id,
limit: ^limit,
order_by: [desc: :id],
offset: ^start,
preload: [:organizer_account, :organizer_group, :category, :sessions, :tracks, :tags, :participants, :address]
events = Repo.all(query)
count_events = Repo.one(from e in Event, select: count(e.id))
{:ok, events, count_events}
end
def count_local_events do
Repo.one(
from e in Event,
select: count(e.id),
where: e.local == ^true
)
end
def count_local_comments do
Repo.one(
from c in Comment,
select: count(c.id),
where: c.local == ^true
)
end
@doc """
Gets a single event.
@ -38,6 +69,13 @@ defmodule Eventos.Events do
"""
def get_event!(id), do: Repo.get!(Event, id)
@doc """
Gets an event by it's URL
"""
def get_event_by_url!(url) do
Repo.get_by(Event, url: url)
end
@doc """
Gets a single event, with all associations loaded.
"""
@ -46,6 +84,25 @@ defmodule Eventos.Events do
Repo.preload(event, [:organizer_account, :organizer_group, :category, :sessions, :tracks, :tags, :participants, :address])
end
@doc """
Gets an event by it's URL
"""
def get_event_full_by_url!(url) do
event = Repo.get_by(Event, url: url)
Repo.preload(event, [:organizer_account, :organizer_group, :category, :sessions, :tracks, :tags, :participants, :address])
end
@spec get_event_full_by_username_and_slug!(String.t, String.t) :: Event.t
def get_event_full_by_username_and_slug!(username, slug) do
event = Repo.one(
from e in Event,
join: a in Account,
on: a.id == e.organizer_account_id and a.username == ^username,
where: e.slug == ^slug
)
Repo.preload(event, [:organizer_account, :organizer_group, :category, :sessions, :tracks, :tags, :participants, :address])
end
@doc """
Creates a event.
@ -706,4 +763,100 @@ defmodule Eventos.Events do
def change_track(%Track{} = track) do
Track.changeset(track, %{})
end
alias Eventos.Events.Comment
@doc """
Returns the list of comments.
## Examples
iex> list_comments()
[%Comment{}, ...]
"""
def list_comments do
Repo.all(Comment)
end
@doc """
Gets a single comment.
Raises `Ecto.NoResultsError` if the Comment does not exist.
## Examples
iex> get_comment!(123)
%Comment{}
iex> get_comment!(456)
** (Ecto.NoResultsError)
"""
def get_comment!(id), do: Repo.get!(Comment, id)
@doc """
Creates a comment.
## Examples
iex> create_comment(%{field: value})
{:ok, %Comment{}}
iex> create_comment(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_comment(attrs \\ %{}) do
%Comment{}
|> Comment.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a comment.
## Examples
iex> update_comment(comment, %{field: new_value})
{:ok, %Comment{}}
iex> update_comment(comment, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_comment(%Comment{} = comment, attrs) do
comment
|> Comment.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a Comment.
## Examples
iex> delete_comment(comment)
{:ok, %Comment{}}
iex> delete_comment(comment)
{:error, %Ecto.Changeset{}}
"""
def delete_comment(%Comment{} = comment) do
Repo.delete(comment)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking comment changes.
## Examples
iex> change_comment(comment)
%Ecto.Changeset{source: %Comment{}}
"""
def change_comment(%Comment{} = comment) do
Comment.changeset(comment, %{})
end
end

View File

@ -43,7 +43,6 @@ defmodule Eventos.Groups.Group do
field :suspended, :boolean, default: false
field :title, :string
field :slug, TitleSlug.Type
field :uri, :string
field :url, :string
many_to_many :members, Account, join_through: Member
has_many :organized_events, Event, [foreign_key: :organizer_group_id]
@ -56,8 +55,8 @@ defmodule Eventos.Groups.Group do
@doc false
def changeset(%Group{} = group, attrs) do
group
|> cast(attrs, [:title, :description, :suspended, :url, :uri, :address_id])
|> validate_required([:title, :description, :suspended, :url, :uri])
|> cast(attrs, [:title, :description, :suspended, :url, :address_id])
|> validate_required([:title, :description, :suspended, :url])
|> TitleSlug.maybe_generate_slug()
|> TitleSlug.unique_constraint()
end

View 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

View 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

View File

@ -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

View File

@ -0,0 +1,8 @@
defmodule EventosWeb.InboxesController do
use EventosWeb, :controller
def create(conn) do
end
end

View 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

View 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

View 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

View File

@ -0,0 +1,43 @@
defmodule EventosWeb.HTTPSignaturePlug do
alias Eventos.Service.HTTPSignatures
import Plug.Conn
require Logger
def init(options) do
options
end
def call(%{assigns: %{valid_signature: true}} = conn, _opts) do
conn
end
def call(conn, _opts) do
user = conn.params["actor"]
Logger.debug("Checking sig for #{user}")
with [signature | _] <- get_req_header(conn, "signature") do
cond do
signature && String.contains?(signature, user) ->
conn =
conn
|> put_req_header(
"(request-target)",
String.downcase("#{conn.method}") <> " #{conn.request_path}"
)
assign(conn, :valid_signature, HTTPSignatures.validate_conn(conn))
signature ->
Logger.debug("Signature not from actor")
assign(conn, :valid_signature, false)
true ->
Logger.debug("No signature header!")
conn
end
else
_ ->
Logger.debug("No signature header!")
conn
end
end
end

View File

@ -8,6 +8,15 @@ defmodule EventosWeb.Router do
plug :accepts, ["json"]
end
pipeline :well_known do
plug :accepts, ["json/application"]
end
pipeline :activity_pub do
plug :accepts, ["activity-json"]
plug(EventosWeb.HTTPSignaturePlug)
end
pipeline :api_auth do
plug :accepts, ["json"]
plug EventosWeb.AuthPipeline
@ -24,43 +33,73 @@ defmodule EventosWeb.Router do
scope "/api", EventosWeb do
pipe_through :api
post "/users", UserController, :register
post "/login", UserSessionController, :sign_in
resources "/groups", GroupController, only: [:index, :show]
resources "/events", EventController, only: [:index, :show]
get "/events/:id/ics", EventController, :export_to_ics
get "/events/:id/tracks", TrackController, :show_tracks_for_event
get "/events/:id/sessions", SessionController, :show_sessions_for_event
resources "/accounts", AccountController, only: [:index, :show]
resources "/tags", TagController, only: [:index, :show]
resources "/categories", CategoryController, only: [:index, :show]
resources "/sessions", SessionController, only: [:index, :show]
resources "/tracks", TrackController, only: [:index, :show]
resources "/addresses", AddressController, only: [:index, :show]
scope "/v1" do
post "/users", UserController, :register
post "/login", UserSessionController, :sign_in
resources "/groups", GroupController, only: [:index, :show]
resources "/events", EventController, only: [:index, :show]
resources "/comments", CommentController, only: [:show]
get "/events/:id/ics", EventController, :export_to_ics
get "/events/:id/tracks", TrackController, :show_tracks_for_event
get "/events/:id/sessions", SessionController, :show_sessions_for_event
resources "/accounts", AccountController, only: [:index, :show]
resources "/tags", TagController, only: [:index, :show]
resources "/categories", CategoryController, only: [:index, :show]
resources "/sessions", SessionController, only: [:index, :show]
resources "/tracks", TrackController, only: [:index, :show]
resources "/addresses", AddressController, only: [:index, :show]
end
end
# Other scopes may use custom stacks.
scope "/api", EventosWeb do
pipe_through :api_auth
get "/user", UserController, :show_current_account
post "/sign-out", UserSessionController, :sign_out
resources "/users", UserController, except: [:new, :edit, :show]
resources "/accounts", AccountController, except: [:new, :edit]
resources "/events", EventController
post "/events/:id/request", EventRequestController, :create_for_event
resources "/participants", ParticipantController
resources "/requests", EventRequestController
resources "/groups", GroupController, except: [:index, :show]
post "/groups/:id/request", GroupRequestController, :create_for_group
resources "/members", MemberController
resources "/requests", GroupRequestController
resources "/sessions", SessionController, except: [:index, :show]
resources "/tracks", TrackController, except: [:index, :show]
get "/tracks/:id/sessions", SessionController, :show_sessions_for_track
resources "/categories", CategoryController
resources "/tags", TagController
resources "/addresses", AddressController, except: [:index, :show]
scope "/v1" do
get "/user", UserController, :show_current_account
post "/sign-out", UserSessionController, :sign_out
resources "/users", UserController, except: [:new, :edit, :show]
resources "/accounts", AccountController, except: [:new, :edit]
resources "/events", EventController
resources "/comments", CommentController, except: [:new, :edit]
post "/events/:id/request", EventRequestController, :create_for_event
resources "/participant", ParticipantController
resources "/requests", EventRequestController
resources "/groups", GroupController, except: [:index, :show]
post "/groups/:id/request", GroupRequestController, :create_for_group
resources "/members", MemberController
resources "/requests", GroupRequestController
resources "/sessions", SessionController, except: [:index, :show]
resources "/tracks", TrackController, except: [:index, :show]
get "/tracks/:id/sessions", SessionController, :show_sessions_for_track
resources "/categories", CategoryController
resources "/tags", TagController
resources "/addresses", AddressController, except: [:index, :show]
end
end
scope "/.well-known", EventosWeb do
pipe_through :well_known
get "/host-meta", WebFingerController, :host_meta
get "/webfinger", WebFingerController, :webfinger
get "/nodeinfo", NodeinfoController, :schemas
end
scope "/nodeinfo", EventosWeb do
get("/:version", NodeinfoController, :nodeinfo)
end
scope "/", EventosWeb do
pipe_through :activity_pub
get "/@:username", ActivityPubController, :account
get "/@:username/outbox", ActivityPubController, :outbox
get "/@:username/:slug", ActivityPubController, :event
post "/@:username/inbox", ActivityPubController, :inbox
post "/inbox", ActivityPubController, :inbox
end
scope "/", EventosWeb do

View File

@ -25,7 +25,6 @@ defmodule EventosWeb.AccountView do
description: account.description,
# public_key: account.public_key,
suspended: account.suspended,
uri: account.uri,
url: account.url,
avatar_url: account.avatar_url,
banner_url: account.banner_url,
@ -40,7 +39,6 @@ defmodule EventosWeb.AccountView do
description: account.description,
# public_key: account.public_key,
suspended: account.suspended,
uri: account.uri,
url: account.url,
avatar_url: account.avatar_url,
banner_url: account.banner_url,

View 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

View 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

View File

@ -0,0 +1,18 @@
defmodule EventosWeb.CommentView do
use EventosWeb, :view
alias EventosWeb.CommentView
def render("index.json", %{comments: comments}) do
%{data: render_many(comments, CommentView, "comment.json")}
end
def render("show.json", %{comment: comment}) do
%{data: render_one(comment, CommentView, "comment.json")}
end
def render("comment.json", %{comment: comment}) do
%{id: comment.id,
url: comment.url,
text: comment.text}
end
end

View File

@ -23,7 +23,6 @@ defmodule EventosWeb.GroupView do
description: group.description,
suspended: group.suspended,
url: group.url,
uri: group.uri
}
end
@ -33,7 +32,6 @@ defmodule EventosWeb.GroupView do
description: group.description,
suspended: group.suspended,
url: group.url,
uri: group.uri,
members: render_many(group.members, AccountView, "acccount_basic.json"),
events: render_many(group.organized_events, EventView, "event_simple.json")
}

View File

@ -0,0 +1,276 @@
defmodule Eventos.Service.ActivityPub do
alias Eventos.Events
alias Eventos.Events.Event
alias Eventos.Service.ActivityPub.Transmogrifier
alias Eventos.Service.WebFinger
alias Eventos.Activity
alias Eventos.Accounts
alias Eventos.Accounts.Account
alias Eventos.Service.Federator
import Logger
import Eventos.Service.ActivityPub.Utils
def get_recipients(data) do
(data["to"] || []) ++ (data["cc"] || [])
end
def insert(map, local \\ true) when is_map(map) do
with map <- lazy_put_activity_defaults(map),
:ok <- insert_full_object(map) do
map = Map.put(map, "id", Ecto.UUID.generate())
activity = %Activity{
data: map,
local: local,
actor: map["actor"],
recipients: get_recipients(map)
}
# Notification.create_notifications(activity)
#stream_out(activity)
{:ok, activity}
else
%Activity{} = activity -> {:ok, activity}
error -> {:error, error}
end
end
def fetch_event_from_url(url) do
if object = Events.get_event_by_url!(url) do
{:ok, object}
else
Logger.info("Fetching #{url} via AP")
with true <- String.starts_with?(url, "http"),
{:ok, %{body: body, status_code: code}} when code in 200..299 <-
HTTPoison.get(
url,
[Accept: "application/activity+json"],
follow_redirect: true,
timeout: 10000,
recv_timeout: 20000
),
{:ok, data} <- Jason.decode(body),
nil <- Events.get_event_by_url!(data["id"]),
params <- %{
"type" => "Create",
"to" => data["to"],
"cc" => data["cc"],
"actor" => data["attributedTo"],
"object" => data
},
{:ok, activity} <- Transmogrifier.handle_incoming(params) do
{:ok, Events.get_event_by_url!(activity.data["object"]["id"])}
else
object = %Event{} -> {:ok, object}
e -> e
end
end
end
def create(%{to: to, actor: actor, context: context, object: object} = params) do
additional = params[:additional] || %{}
# only accept false as false value
local = !(params[:local] == false)
published = params[:published]
with create_data <-
make_create_data(
%{to: to, actor: actor, published: published, context: context, object: object},
additional
),
{:ok, activity} <- insert(create_data, local),
:ok <- maybe_federate(activity) do
# {:ok, actor} <- Accounts.increase_event_count(actor) do
{:ok, activity}
end
end
def accept(%{to: to, actor: actor, object: object} = params) do
# only accept false as false value
local = !(params[:local] == false)
with data <- %{"to" => to, "type" => "Accept", "actor" => actor, "object" => object},
{:ok, activity} <- insert(data, local),
:ok <- maybe_federate(activity) do
{:ok, activity}
end
end
def update(%{to: to, cc: cc, actor: actor, object: object} = params) do
# only accept false as false value
local = !(params[:local] == false)
with data <- %{
"to" => to,
"cc" => cc,
"type" => "Update",
"actor" => actor,
"object" => object
},
{:ok, activity} <- insert(data, local),
:ok <- maybe_federate(activity) do
{:ok, activity}
end
end
def follow(follower, followed, activity_id \\ nil, local \\ true) do
with data <- make_follow_data(follower, followed, activity_id),
{:ok, activity} <- insert(data, local),
:ok <- maybe_federate(activity) do
{:ok, activity}
end
end
def delete(%Event{url: url, organizer_account: account} = event, local \\ true) do
data = %{
"type" => "Delete",
"actor" => account.url,
"object" => url,
"to" => [account.url <> "/followers", "https://www.w3.org/ns/activitystreams#Public"]