Change structure
This commit is contained in:
parent
01c2c57933
commit
ddfb58c041
16 changed files with 153 additions and 66 deletions
32
lib/nulla_web/controllers/web/actor_controller.ex
Normal file
32
lib/nulla_web/controllers/web/actor_controller.ex
Normal file
|
@ -0,0 +1,32 @@
|
|||
defmodule NullaWeb.Web.ActorController do
|
||||
use NullaWeb, :controller
|
||||
alias Nulla.Models.Actor
|
||||
alias Nulla.Models.Relation
|
||||
alias Nulla.Models.Note
|
||||
|
||||
def show(conn, %{"username" => username}) do
|
||||
domain = NullaWeb.Endpoint.host()
|
||||
|
||||
case Actor.get_actor(preferredUsername: username, domain: domain) do
|
||||
nil ->
|
||||
conn
|
||||
|> put_status(:not_found)
|
||||
|> json(%{error: "Not Found"})
|
||||
|
||||
%Actor{} = actor ->
|
||||
following = Relation.count_following(actor.id)
|
||||
followers = Relation.count_followers(actor.id)
|
||||
notes = Note.get_latest_notes(actor.id)
|
||||
|
||||
render(
|
||||
conn,
|
||||
:show,
|
||||
actor: actor,
|
||||
following: following,
|
||||
followers: followers,
|
||||
notes: notes,
|
||||
layout: false
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
66
lib/nulla_web/controllers/web/auth_controller.ex
Normal file
66
lib/nulla_web/controllers/web/auth_controller.ex
Normal file
|
@ -0,0 +1,66 @@
|
|||
defmodule NullaWeb.Web.AuthController do
|
||||
use NullaWeb, :controller
|
||||
alias Nulla.Models.User
|
||||
alias Nulla.Models.Actor
|
||||
alias Nulla.Models.InstanceSettings
|
||||
|
||||
def sign_in(conn, %{"email" => email, "password" => password}) do
|
||||
user = User.get_user(email: email)
|
||||
|
||||
if user do
|
||||
if Argon2.verify_pass(password, user.password) do
|
||||
conn
|
||||
|> put_session(:user_id, user.id)
|
||||
|> redirect(to: ~p"/")
|
||||
else
|
||||
conn
|
||||
|> put_flash(:error, "Invalid login or password.")
|
||||
|> redirect(to: ~p"/auth/sign_in")
|
||||
end
|
||||
else
|
||||
conn
|
||||
|> put_flash(:error, "User not exist.")
|
||||
|> redirect(to: ~p"/auth/sign_in")
|
||||
end
|
||||
end
|
||||
|
||||
def sign_out(conn, _params) do
|
||||
conn
|
||||
|> configure_session(drop: true)
|
||||
|> put_flash(:info, "You have been logged out.")
|
||||
|> redirect(to: ~p"/")
|
||||
end
|
||||
|
||||
def sign_up(conn, %{"username" => username, "email" => email, "password" => password}) do
|
||||
instance_settings = InstanceSettings.get_instance_settings!()
|
||||
|
||||
if not instance_settings.registration do
|
||||
conn
|
||||
|> put_flash(:error, "Registration is disabled.")
|
||||
|> redirect(to: ~p"/")
|
||||
else
|
||||
domain = NullaWeb.Endpoint.host()
|
||||
hashed_password = Argon2.hash_pwd_salt(password)
|
||||
|
||||
{publicKeyPem, privateKeyPem} = Nulla.KeyGen.gen()
|
||||
|
||||
with {:ok, actor} <- Actor.create_actor_minimal(username, domain, publicKeyPem),
|
||||
{:ok, user} <-
|
||||
User.create_user(%{
|
||||
id: actor.id,
|
||||
email: email,
|
||||
password: hashed_password,
|
||||
privateKeyPem: privateKeyPem,
|
||||
last_active_at: DateTime.utc_now()
|
||||
}) do
|
||||
conn
|
||||
|> put_session(:user_id, user.id)
|
||||
|> put_flash(:info, "You're registred!")
|
||||
|> redirect(to: ~p"/")
|
||||
else
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
render(conn, "sign_up.html", changeset: changeset)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
44
lib/nulla_web/controllers/web/note_controller.ex
Normal file
44
lib/nulla_web/controllers/web/note_controller.ex
Normal file
|
@ -0,0 +1,44 @@
|
|||
defmodule NullaWeb.Web.NoteController do
|
||||
use NullaWeb, :controller
|
||||
alias Nulla.Repo
|
||||
alias Nulla.ActivityPub
|
||||
alias Nulla.Models.Note
|
||||
|
||||
def show(conn, %{"username" => username, "id" => id}) do
|
||||
case Integer.parse(id) do
|
||||
{int_id, ""} ->
|
||||
note = Note.get_note(id: int_id) |> Repo.preload([:actor, :media_attachments])
|
||||
|
||||
cond do
|
||||
is_nil(note) ->
|
||||
conn
|
||||
|> put_status(:not_found)
|
||||
|> json(%{error: "Not Found"})
|
||||
|> halt()
|
||||
|
||||
username != note.actor.preferredUsername ->
|
||||
conn
|
||||
|> put_status(:not_found)
|
||||
|> json(%{error: "Not Found"})
|
||||
|> halt()
|
||||
|
||||
true ->
|
||||
format = Phoenix.Controller.get_format(conn)
|
||||
|
||||
if format == "activity+json" do
|
||||
conn
|
||||
|> put_resp_content_type("application/activity+json")
|
||||
|> json(ActivityPub.note(note))
|
||||
else
|
||||
render(conn, :show, note: note, layout: false)
|
||||
end
|
||||
end
|
||||
|
||||
_ ->
|
||||
conn
|
||||
|> put_status(:not_found)
|
||||
|> json(%{error: "Not Found"})
|
||||
|> halt()
|
||||
end
|
||||
end
|
||||
end
|
15
lib/nulla_web/controllers/web/page_controller.ex
Normal file
15
lib/nulla_web/controllers/web/page_controller.ex
Normal file
|
@ -0,0 +1,15 @@
|
|||
defmodule NullaWeb.Web.PageController do
|
||||
use NullaWeb, :controller
|
||||
|
||||
def home(conn, _params) do
|
||||
render(conn, :home, layout: false)
|
||||
end
|
||||
|
||||
def sign_in(conn, _params) do
|
||||
render(conn, :sign_in, layout: false)
|
||||
end
|
||||
|
||||
def sign_up(conn, _params) do
|
||||
render(conn, :sign_up, layout: false)
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue