Compare commits

...

4 commits

Author SHA1 Message Date
58049c93d4
Update 2025-06-15 19:36:03 +02:00
b596606c14
Add actor.ex 2025-06-15 19:35:52 +02:00
62dbe3ef24
Update keygen.ex 2025-06-15 19:34:18 +02:00
57efda7638
Update migrations 2025-06-15 19:33:40 +02:00
22 changed files with 281 additions and 185 deletions

View file

@ -1,5 +1,5 @@
defmodule Nulla.KeyGen do defmodule Nulla.KeyGen do
def generate_keys do def gen do
rsa_key = :public_key.generate_key({:rsa, 2048, 65537}) rsa_key = :public_key.generate_key({:rsa, 2048, 65537})
{:RSAPrivateKey, :"two-prime", n, e, _d, _p, _q, _dp, _dq, _qi, _other} = rsa_key {:RSAPrivateKey, :"two-prime", n, e, _d, _p, _q, _dp, _dq, _qi, _other} = rsa_key

109
lib/nulla/models/actor.ex Normal file
View file

@ -0,0 +1,109 @@
defmodule Nulla.Models.Actor do
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query
alias Nulla.Repo
alias Nulla.Snowflake
alias Nulla.Models.User
alias Nulla.Models.Note
@primary_key {:id, :integer, autogenerate: false}
schema "actors" do
field :type, :string
field :following, :string
field :followers, :string
field :inbox, :string
field :outbox, :string
field :featured, :string
field :featuredTags, :string
field :preferredUsername, :string
field :name, :string
field :summary, :string
field :url, :string
field :manuallyApprovesFollowers, :boolean
field :discoverable, :boolean, default: true
field :indexable, :boolean, default: true
field :published, :utc_datetime
field :memorial, :boolean, default: false
field :publicKey, {:array, :map}
field :tag, {:array, :map}
field :attachment, {:array, :map}
field :endpoints, :map
field :icon, :map
field :image, :map
field :vcard_bday, :date
field :vcard_Address, :string
has_one :user, User
has_many :notes, Note
has_many :media_attachments, through: [:notes, :media_attachments]
end
@doc false
def changeset(actor, attrs) do
actor
|> cast(attrs, [
:id,
:type,
:following,
:followers,
:inbox,
:outbox,
:featured,
:featuredTags,
:preferredUsername,
:name,
:summary,
:url,
:manuallyApprovesFollowers,
:discoverable,
:indexable,
:published,
:memorial,
:publicKey,
:tag,
:attachment,
:endpoints,
:icon,
:image,
:vcard_bday,
:vcard_Address
])
|> validate_required([
:id,
:type,
:following,
:followers,
:inbox,
:outbox,
:featured,
:featuredTags,
:preferredUsername,
:name,
:summary,
:url,
:manuallyApprovesFollowers,
:discoverable,
:indexable,
:published,
:memorial,
:publicKey,
:tag,
:attachment,
:endpoints,
:icon,
:image,
:vcard_bday,
:vcard_Address
])
end
def create_user(attrs) when is_map(attrs) do
id = Snowflake.next_id()
%__MODULE__{}
|> changeset(attrs)
|> Ecto.Changeset.put_change(:id, id)
|> Repo.insert()
end
end

View file

@ -1,6 +1,8 @@
defmodule Nulla.Models.Follow do defmodule Nulla.Models.Follow do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
alias Nulla.Snowflake
alias Nulla.Models.Follow
@primary_key {:id, :integer, autogenerate: false} @primary_key {:id, :integer, autogenerate: false}
schema "follows" do schema "follows" do
@ -17,4 +19,13 @@ defmodule Nulla.Models.Follow do
|> validate_required([:user_id, :target_id]) |> validate_required([:user_id, :target_id])
|> unique_constraint([:user_id, :target_id]) |> unique_constraint([:user_id, :target_id])
end end
def create_follow(attrs) do
id = Snowflake.next_id()
%Follow{}
|> Follow.changeset(attrs)
|> Ecto.Changeset.put_change(:id, id)
|> Repo.insert()
end
end end

View file

@ -1,6 +1,8 @@
defmodule Nulla.Models.Notification do defmodule Nulla.Models.Notification do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
alias Nulla.Models.User
alias Nulla.Models.Actor
@primary_key {:id, :integer, autogenerate: false} @primary_key {:id, :integer, autogenerate: false}
schema "notifications" do schema "notifications" do
@ -8,8 +10,8 @@ defmodule Nulla.Models.Notification do
field :data, :map field :data, :map
field :read, :boolean, default: false field :read, :boolean, default: false
belongs_to :user, Nulla.Models.User belongs_to :user, User
belongs_to :actor, Nulla.Models.User belongs_to :actor, Actor
timestamps() timestamps()
end end

View file

@ -5,34 +5,18 @@ defmodule Nulla.Models.User do
alias Nulla.Repo alias Nulla.Repo
alias Nulla.Snowflake alias Nulla.Snowflake
alias Nulla.Models.User alias Nulla.Models.User
alias Nulla.Models.Actor
alias Nulla.Models.Session
@primary_key {:id, :integer, autogenerate: false} @primary_key {:id, :integer, autogenerate: false}
schema "users" do schema "users" do
field :username, :string
field :domain, :string
field :email, :string field :email, :string
field :password, :string field :password, :string
field :is_moderator, :boolean, default: false field :privateKeyPem, :string
field :realname, :string
field :bio, :string
field :location, :string
field :birthday, :date
field :fields, {:array, :map}
field :tags, {:array, :string}
field :follow_approval, :boolean, default: false
field :is_bot, :boolean, default: false
field :is_discoverable, :boolean, default: true
field :is_indexable, :boolean, default: true
field :is_memorial, :boolean, default: false
field :private_key, :string
field :public_key, :string
field :avatar, :string
field :banner, :string
field :last_active_at, :utc_datetime field :last_active_at, :utc_datetime
has_many :user_sessions, Nulla.Models.Session belongs_to :actor, Actor, define_field: false, foreign_key: :id, type: :integer
has_many :notes, Nulla.Models.Note has_many :user_sessions, Session
has_many :media_attachments, through: [:notes, :media_attachments]
timestamps(type: :utc_datetime) timestamps(type: :utc_datetime)
end end
@ -41,57 +25,24 @@ defmodule Nulla.Models.User do
def changeset(user, attrs) do def changeset(user, attrs) do
user user
|> cast(attrs, [ |> cast(attrs, [
:username,
:domain,
:email, :email,
:password, :password,
:is_moderator, :privateKeyPem,
:realname, :last_active_at,
:bio, :actor_id
:location,
:birthday,
:fields,
:follow_approval,
:is_bot,
:is_discoverable,
:is_indexable,
:is_memorial,
:private_key,
:public_key,
:avatar,
:banner,
:last_active_at
]) ])
|> validate_required([ |> validate_required([
:username,
:domain,
:email, :email,
:password, :password,
:is_moderator, :privateKeyPem,
:realname, :last_active_at,
:bio, :actor_id
:location,
:birthday,
:fields,
:follow_approval,
:is_bot,
:is_discoverable,
:is_indexable,
:is_memorial,
:private_key,
:public_key,
:avatar,
:banner,
:last_active_at
]) ])
end end
def create_user(attrs) do def create_user(attrs) when is_map(attrs) do
id = Snowflake.next_id() %__MODULE__{}
|> changeset(attrs)
%User{}
|> User.changeset(attrs)
|> Ecto.Changeset.put_change(:id, id)
|> Repo.insert() |> Repo.insert()
end end
@ -99,6 +50,13 @@ defmodule Nulla.Models.User do
def get_user_by_username!(username), do: Repo.get_by!(User, username: username) def get_user_by_username!(username), do: Repo.get_by!(User, username: username)
def get_user_by_username_and_domain(username, domain) do
from(u in User,
where: u.username == ^username and u.domain == ^domain
)
|> Repo.one()
end
def get_total_users_count(domain) do def get_total_users_count(domain) do
Repo.aggregate(from(u in User, where: u.domain == ^domain), :count, :id) Repo.aggregate(from(u in User, where: u.domain == ^domain), :count, :id)
end end

View file

@ -87,4 +87,38 @@ defmodule Nulla.Utils do
users users
end end
end end
def resolve_local_actor("https://" <> _ = uri) do
case URI.parse(uri).path do
"/@" <> username ->
instance_settings = InstanceSettings.get_instance_settings!()
domain = instance_settings.domain
case User.get_user_by_username_and_domain(username, domain) do
nil -> {:error, :not_found}
user -> {:ok, user}
end
_ ->
{:error, :invalid_actor}
end
end
def fetch_remote_actor(uri) do
request =
Finch.build(:get, uri, [
{"Accept", "application/activity+json"}
])
case Finch.request(request, MyApp.Finch) do
{:ok, %Finch.Response{status: 200, body: body}} ->
case Jason.decode(body) do
{:ok, data} -> {:ok, data}
_ -> {:error, :invalid_json}
end
_ ->
{:error, :actor_fetch_failed}
end
end
end end

View file

@ -1,25 +1,23 @@
defmodule NullaWeb.InboxController do defmodule NullaWeb.InboxController do
use NullaWeb, :controller use NullaWeb, :controller
alias Nulla.Models.Follow
alias Nulla.Utils
def receive(conn, %{"type" => "Follow"} = activity) do def inbox(
# Check signature conn,
# Verify actor and object %{"type" => "Follow", "actor" => actor_uri, "object" => target_uri} = activity
# Save follow to db ) do
# Send Accept or Reject with {:ok, target_user} <- Utils.resolve_local_actor(target_uri),
json(conn, %{"status" => "Follow received"}) {:ok, remote_actor} <- Utils.fetch_remote_actor(actor_uri),
:ok <- HTTPSignature.verify(conn, remote_actor),
remote_user <- Follow.create_remote_user(remote_actor),
follow <- Follow.create_follow(%{user: remote_user, target: target_user}),
:ok <- Utils.send_accept_activity(remote_actor, target_user, follow, activity) do
json(conn, %{"status" => "Follow accepted"})
else
error ->
IO.inspect(error, label: "Follow error")
json(conn, %{"error" => "Failed to process Follow"})
end end
def receive(conn, %{"type" => "Like"} = activity) do
# Process Like
json(conn, %{"status" => "Like received"})
end
def receive(conn, %{"type" => "Create"} = activity) do
# Create object and save
json(conn, %{"status" => "Object created"})
end
def receive(conn, _params) do
json(conn, %{"status" => "Unhandled type"})
end end
end end

View file

@ -5,33 +5,11 @@ defmodule NullaWeb.NoteController do
alias Nulla.Models.Note alias Nulla.Models.Note
alias Nulla.Models.InstanceSettings alias Nulla.Models.InstanceSettings
def index(conn, _params) do def show(conn, %{"username" => username, "id" => id}) do
notes = Notes.list_notes()
render(conn, :index, notes: notes)
end
def new(conn, _params) do
changeset = Notes.change_note(%Note{})
render(conn, :new, changeset: changeset)
end
def create(conn, %{"note" => note_params}) do
case Notes.create_note(note_params) do
{:ok, note} ->
conn
|> put_flash(:info, "Note created successfully.")
|> redirect(to: ~p"/notes/#{note}")
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, :new, changeset: changeset)
end
end
def show(conn, %{"username" => username, "note_id" => note_id}) do
accept = List.first(get_req_header(conn, "accept")) accept = List.first(get_req_header(conn, "accept"))
instance_settings = InstanceSettings.get_instance_settings!() instance_settings = InstanceSettings.get_instance_settings!()
domain = instance_settings.domain domain = instance_settings.domain
note = Note.get_note!(note_id) |> Repo.preload([:user, :media_attachments]) note = Note.get_note!(id) |> Repo.preload([:user, :media_attachments])
if username != note.user.username do if username != note.user.username do
conn conn
@ -48,38 +26,4 @@ defmodule NullaWeb.NoteController do
render(conn, :show, domain: domain, note: note, layout: false) render(conn, :show, domain: domain, note: note, layout: false)
end end
end end
# def show(conn, %{"id" => id}) do
# note = Notes.get_note!(id)
# render(conn, :show, note: note)
# end
def edit(conn, %{"id" => id}) do
note = Notes.get_note!(id)
changeset = Notes.change_note(note)
render(conn, :edit, note: note, changeset: changeset)
end
def update(conn, %{"id" => id, "note" => note_params}) do
note = Notes.get_note!(id)
case Notes.update_note(note, note_params) do
{:ok, note} ->
conn
|> put_flash(:info, "Note updated successfully.")
|> redirect(to: ~p"/notes/#{note}")
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, :edit, note: note, changeset: changeset)
end
end
def delete(conn, %{"id" => id}) do
note = Notes.get_note!(id)
{:ok, _note} = Notes.delete_note(note)
conn
|> put_flash(:info, "Note deleted successfully.")
|> redirect(to: ~p"/notes")
end
end end

View file

@ -5,7 +5,7 @@ defmodule NullaWeb.OutboxController do
alias Nulla.Models.Note alias Nulla.Models.Note
alias Nulla.Models.InstanceSettings alias Nulla.Models.InstanceSettings
def show(conn, %{"username" => username} = params) do def outbox(conn, %{"username" => username} = params) do
case Map.get(params, "page") do case Map.get(params, "page") do
"true" -> "true" ->
instance_settings = InstanceSettings.get_instance_settings!() instance_settings = InstanceSettings.get_instance_settings!()

View file

@ -20,12 +20,31 @@ defmodule NullaWeb.Router do
get "/.well-known/webfinger", WebfingerController, :index get "/.well-known/webfinger", WebfingerController, :index
get "/.well-known/nodeinfo", NodeinfoController, :index get "/.well-known/nodeinfo", NodeinfoController, :index
get "/nodeinfo/2.0", NodeinfoController, :show get "/nodeinfo/2.0", NodeinfoController, :show
post "/inbox", InboxController, :inbox
get "/@:username", UserController, :show scope "/auth" do
get "/@:username/outbox", OutboxController, :show get "/sign_in", AuthController, :sign_in
get "/@:username/following", FollowController, :following post "/sign_out", AuthController, :sign_out
get "/@:username/followers", FollowController, :followers get "/sign_up", AuthController, :sign_up
get "/@:username/:note_id", NoteController, :show end
scope "/users/:username" do
get "/", UserController, :show
get "/following", FollowController, :following
get "/followers", FollowController, :followers
post "/inbox", InboxController, :inbox
get "/outbox", OutboxController, :outbox
get "/statuses/:id", NoteController, :show
end
scope "/@:username" do
get "/", UserController, :show
get "/following", FollowController, :following
get "/followers", FollowController, :followers
post "/inbox", InboxController, :inbox
get "/outbox", OutboxController, :outbox
get "/:id", NoteController, :show
end
end end
# Other scopes may use custom stacks. # Other scopes may use custom stacks.

View file

@ -22,8 +22,8 @@ defmodule Nulla.Repo.Migrations.CreateInstanceSettings do
flush() flush()
execute(fn -> execute(fn ->
{public_key, private_key} = Nulla.KeyGen.generate_keys() {public_key, private_key} = Nulla.KeyGen.gen()
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second) now = DateTime.utc_now()
domain = domain =
Application.get_env(:nulla, NullaWeb.Endpoint, []) Application.get_env(:nulla, NullaWeb.Endpoint, [])

View file

@ -1,32 +0,0 @@
defmodule Nulla.Repo.Migrations.CreateUsers do
use Ecto.Migration
def change do
create table(:users, primary_key: false) do
add :id, :bigint, primary_key: true
add :username, :string, null: false, unique: true
add :domain, :string, null: false
add :email, :string
add :password, :string
add :is_moderator, :boolean, default: false, null: false
add :realname, :string
add :bio, :text
add :location, :string
add :birthday, :date
add :fields, :jsonb, default: "[]", null: false
add :tags, {:array, :string}
add :follow_approval, :boolean, default: false, null: false
add :is_bot, :boolean, default: false, null: false
add :is_discoverable, :boolean, default: true, null: false
add :is_indexable, :boolean, default: true, null: false
add :is_memorial, :boolean, default: false, null: false
add :private_key, :string, null: false
add :public_key, :string, null: false
add :avatar, :string
add :banner, :string
add :last_active_at, :utc_datetime
timestamps(type: :utc_datetime)
end
end
end

View file

@ -0,0 +1,33 @@
defmodule Nulla.Repo.Migrations.CreateActors do
use Ecto.Migration
def change do
create table(:actors, primary_key: false) do
add :id, :bigint, primary_key: true
add :type, :string
add :following, :string
add :followers, :string
add :inbox, :string
add :outbox, :string
add :featured, :string
add :featuredTags, :string
add :preferredUsername, :string
add :name, :string
add :summary, :string
add :url, :string
add :manuallyApprovesFollowers, :boolean
add :discoverable, :boolean, default: true
add :indexable, :boolean, default: true
add :published, :utc_datetime
add :memorial, :boolean, default: false
add :publicKey, :map
add :tag, {:array, :map}
add :attachment, {:array, :map}
add :endpoints, :map
add :icon, :map
add :image, :map
add :vcard_bday, :date
add :vcard_Address, :string
end
end
end

View file

@ -0,0 +1,20 @@
defmodule Nulla.Repo.Migrations.CreateUsers do
use Ecto.Migration
def change do
create table(:users, primary_key: false) do
add :id, :bigint, primary_key: true
add :email, :string
add :password, :string
add :privateKeyPem, :string
add :last_active_at, :utc_datetime
add :actor_id, references(:actors, column: :id, type: :bigint, on_delete: :delete_all),
null: false
timestamps(type: :utc_datetime)
end
create unique_index(:users, [:actor_id])
end
end

View file

@ -5,7 +5,7 @@ defmodule Nulla.Repo.Migrations.CreateNotifications do
create table(:notifications, primary_key: false) do create table(:notifications, primary_key: false) do
add :id, :bigint, primary_key: true add :id, :bigint, primary_key: true
add :user_id, references(:users, on_delete: :delete_all), null: false add :user_id, references(:users, on_delete: :delete_all), null: false
add :actor_id, references(:users, on_delete: :nilify_all) add :actor_id, references(:actors, on_delete: :nilify_all)
add :type, :string, null: false add :type, :string, null: false
add :data, :map add :data, :map
add :read, :boolean, default: false, null: false add :read, :boolean, default: false, null: false