Update
This commit is contained in:
parent
b596606c14
commit
58049c93d4
8 changed files with 115 additions and 149 deletions
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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),
|
||||||
end
|
:ok <- HTTPSignature.verify(conn, remote_actor),
|
||||||
|
remote_user <- Follow.create_remote_user(remote_actor),
|
||||||
def receive(conn, %{"type" => "Like"} = activity) do
|
follow <- Follow.create_follow(%{user: remote_user, target: target_user}),
|
||||||
# Process Like
|
:ok <- Utils.send_accept_activity(remote_actor, target_user, follow, activity) do
|
||||||
json(conn, %{"status" => "Like received"})
|
json(conn, %{"status" => "Follow accepted"})
|
||||||
end
|
else
|
||||||
|
error ->
|
||||||
def receive(conn, %{"type" => "Create"} = activity) do
|
IO.inspect(error, label: "Follow error")
|
||||||
# Create object and save
|
json(conn, %{"error" => "Failed to process Follow"})
|
||||||
json(conn, %{"status" => "Object created"})
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def receive(conn, _params) do
|
|
||||||
json(conn, %{"status" => "Unhandled type"})
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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!()
|
||||||
|
|
|
@ -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.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue