@@ -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
|
||||
|
||||
@@ -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
7
lib/eventos/activity.ex
Normal file
@@ -0,0 +1,7 @@
|
||||
defmodule Eventos.Activity do
|
||||
@moduledoc """
|
||||
Represents an activity
|
||||
"""
|
||||
|
||||
defstruct [:id, :data, :local, :actor, :recipients, :notifications]
|
||||
end
|
||||
@@ -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
|
||||
|
||||
27
lib/eventos/events/comment.ex
Normal file
27
lib/eventos/events/comment.ex
Normal 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
|
||||
@@ -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])
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user