This commit is contained in:
Mirai Kumiko 2025-07-04 10:25:40 +02:00
parent b35e18cd20
commit 82f55f7afe
Signed by: miraikumiko
GPG key ID: 3F178B1B5E0CB278
80 changed files with 6687 additions and 5 deletions

View file

@ -0,0 +1,43 @@
defmodule NullaWeb.ActivityController do
use NullaWeb, :controller
alias Nulla.Activities
alias Nulla.Activities.Activity
action_fallback NullaWeb.FallbackController
def index(conn, _params) do
activities = Activities.list_activities()
render(conn, :index, activities: activities)
end
def create(conn, %{"activity" => activity_params}) do
with {:ok, %Activity{} = activity} <- Activities.create_activity(activity_params) do
conn
|> put_status(:created)
|> put_resp_header("location", ~p"/api/activities/#{activity}")
|> render(:show, activity: activity)
end
end
def show(conn, %{"id" => id}) do
activity = Activities.get_activity!(id)
render(conn, :show, activity: activity)
end
def update(conn, %{"id" => id, "activity" => activity_params}) do
activity = Activities.get_activity!(id)
with {:ok, %Activity{} = activity} <- Activities.update_activity(activity, activity_params) do
render(conn, :show, activity: activity)
end
end
def delete(conn, %{"id" => id}) do
activity = Activities.get_activity!(id)
with {:ok, %Activity{}} <- Activities.delete_activity(activity) do
send_resp(conn, :no_content, "")
end
end
end

View file

@ -0,0 +1,41 @@
defmodule NullaWeb.ActivityJSON do
alias Nulla.Activities.Activity
@doc """
Renders a list of activities.
"""
def index(%{activities: activities}) do
%{data: for(activity <- activities, do: data(activity))}
end
@doc """
Renders a single activity.
"""
def show(%{activity: activity}) do
%{data: data(activity)}
end
defp data(%Activity{} = activity) do
%{
id: activity.id,
ap_id: activity.ap_id,
type: activity.type,
actor: activity.actor,
object: activity.object,
to: activity.to,
cc: activity.cc
}
end
def activitypub(%Activity{} = activity) do
Jason.OrderedObject.new(
"@context": "https://www.w3.org/ns/activitystreams",
id: activity.ap_id,
type: activity.type,
actor: activity.actor,
object: activity.object,
to: activity.to,
cc: activity.cc
)
end
end

View file

@ -0,0 +1,43 @@
defmodule NullaWeb.ActorController do
use NullaWeb, :controller
alias Nulla.Actors
alias Nulla.Actors.Actor
action_fallback NullaWeb.FallbackController
def index(conn, _params) do
actors = Actors.list_actors()
render(conn, :index, actors: actors)
end
def create(conn, %{"actor" => actor_params}) do
with {:ok, %Actor{} = actor} <- Actors.create_actor(actor_params) do
conn
|> put_status(:created)
|> put_resp_header("location", ~p"/api/actors/#{actor}")
|> render(:show, actor: actor)
end
end
def show(conn, %{"id" => id}) do
actor = Actors.get_actor!(id)
render(conn, :show, actor: actor)
end
def update(conn, %{"id" => id, "actor" => actor_params}) do
actor = Actors.get_actor!(id)
with {:ok, %Actor{} = actor} <- Actors.update_actor(actor, actor_params) do
render(conn, :show, actor: actor)
end
end
def delete(conn, %{"id" => id}) do
actor = Actors.get_actor!(id)
with {:ok, %Actor{}} <- Actors.delete_actor(actor) do
send_resp(conn, :no_content, "")
end
end
end

View file

@ -0,0 +1,49 @@
defmodule NullaWeb.ActorJSON do
alias Nulla.Actors.Actor
@doc """
Renders a list of actors.
"""
def index(%{actors: actors}) do
%{data: for(actor <- actors, do: data(actor))}
end
@doc """
Renders a single actor.
"""
def show(%{actor: actor}) do
%{data: data(actor)}
end
defp data(%Actor{} = actor) do
%{
id: actor.id,
acct: actor.acct,
ap_id: actor.ap_id,
type: actor.type,
following: actor.following,
followers: actor.followers,
inbox: actor.inbox,
outbox: actor.outbox,
featured: actor.featured,
featuredTags: actor.featuredTags,
preferredUsername: actor.preferredUsername,
name: actor.name,
summary: actor.summary,
url: actor.url,
manuallyApprovesFollowers: actor.manuallyApprovesFollowers,
discoverable: actor.discoverable,
indexable: actor.indexable,
published: actor.published,
memorial: actor.memorial,
publicKey: actor.publicKey,
tag: actor.tag,
attachment: actor.attachment,
endpoints: actor.endpoints,
icon: actor.icon,
image: actor.image,
vcard_bday: actor.vcard_bday,
vcard_Address: actor.vcard_Address
}
end
end

View file

@ -0,0 +1,25 @@
defmodule NullaWeb.ChangesetJSON do
@doc """
Renders changeset errors.
"""
def error(%{changeset: changeset}) do
# When encoded, the changeset returns its errors
# as a JSON object. So we just pass it forward.
%{errors: Ecto.Changeset.traverse_errors(changeset, &translate_error/1)}
end
defp translate_error({msg, opts}) do
# You can make use of gettext to translate error messages by
# uncommenting and adjusting the following code:
# if count = opts[:count] do
# Gettext.dngettext(NullaWeb.Gettext, "errors", msg, msg, count, opts)
# else
# Gettext.dgettext(NullaWeb.Gettext, "errors", msg, opts)
# end
Enum.reduce(opts, msg, fn {key, value}, acc ->
String.replace(acc, "%{#{key}}", fn _ -> to_string(value) end)
end)
end
end

View file

@ -0,0 +1,24 @@
defmodule NullaWeb.FallbackController do
@moduledoc """
Translates controller action results into valid `Plug.Conn` responses.
See `Phoenix.Controller.action_fallback/1` for more details.
"""
use NullaWeb, :controller
# This clause handles errors returned by Ecto's insert/update/delete.
def call(conn, {:error, %Ecto.Changeset{} = changeset}) do
conn
|> put_status(:unprocessable_entity)
|> put_view(json: NullaWeb.ChangesetJSON)
|> render(:error, changeset: changeset)
end
# This clause is an example of how to handle resources that cannot be found.
def call(conn, {:error, :not_found}) do
conn
|> put_status(:not_found)
|> put_view(html: NullaWeb.ErrorHTML, json: NullaWeb.ErrorJSON)
|> render(:"404")
end
end

View file

@ -0,0 +1,45 @@
defmodule NullaWeb.MediaAttachmentController do
use NullaWeb, :controller
alias Nulla.MediaAttachments
alias Nulla.MediaAttachments.MediaAttachment
action_fallback NullaWeb.FallbackController
def index(conn, _params) do
media_attachments = MediaAttachments.list_media_attachments()
render(conn, :index, media_attachments: media_attachments)
end
def create(conn, %{"media_attachment" => media_attachment_params}) do
with {:ok, %MediaAttachment{} = media_attachment} <-
MediaAttachments.create_media_attachment(media_attachment_params) do
conn
|> put_status(:created)
|> put_resp_header("location", ~p"/api/media_attachments/#{media_attachment}")
|> render(:show, media_attachment: media_attachment)
end
end
def show(conn, %{"id" => id}) do
media_attachment = MediaAttachments.get_media_attachment!(id)
render(conn, :show, media_attachment: media_attachment)
end
def update(conn, %{"id" => id, "media_attachment" => media_attachment_params}) do
media_attachment = MediaAttachments.get_media_attachment!(id)
with {:ok, %MediaAttachment{} = media_attachment} <-
MediaAttachments.update_media_attachment(media_attachment, media_attachment_params) do
render(conn, :show, media_attachment: media_attachment)
end
end
def delete(conn, %{"id" => id}) do
media_attachment = MediaAttachments.get_media_attachment!(id)
with {:ok, %MediaAttachment{}} <- MediaAttachments.delete_media_attachment(media_attachment) do
send_resp(conn, :no_content, "")
end
end
end

View file

@ -0,0 +1,29 @@
defmodule NullaWeb.MediaAttachmentJSON do
alias Nulla.MediaAttachments.MediaAttachment
@doc """
Renders a list of media_attachments.
"""
def index(%{media_attachments: media_attachments}) do
%{data: for(media_attachment <- media_attachments, do: data(media_attachment))}
end
@doc """
Renders a single media_attachment.
"""
def show(%{media_attachment: media_attachment}) do
%{data: data(media_attachment)}
end
defp data(%MediaAttachment{} = media_attachment) do
%{
id: media_attachment.id,
type: media_attachment.type,
mediaType: media_attachment.mediaType,
url: media_attachment.url,
name: media_attachment.name,
width: media_attachment.width,
height: media_attachment.height
}
end
end

View file

@ -0,0 +1,43 @@
defmodule NullaWeb.NoteController do
use NullaWeb, :controller
alias Nulla.Notes
alias Nulla.Notes.Note
action_fallback NullaWeb.FallbackController
def index(conn, _params) do
notes = Notes.list_notes()
render(conn, :index, notes: notes)
end
def create(conn, %{"note" => note_params}) do
with {:ok, %Note{} = note} <- Notes.create_note(note_params) do
conn
|> put_status(:created)
|> put_resp_header("location", ~p"/api/notes/#{note}")
|> render(:show, note: note)
end
end
def show(conn, %{"id" => id}) do
note = Notes.get_note!(id)
render(conn, :show, note: note)
end
def update(conn, %{"id" => id, "note" => note_params}) do
note = Notes.get_note!(id)
with {:ok, %Note{} = note} <- Notes.update_note(note, note_params) do
render(conn, :show, note: note)
end
end
def delete(conn, %{"id" => id}) do
note = Notes.get_note!(id)
with {:ok, %Note{}} <- Notes.delete_note(note) do
send_resp(conn, :no_content, "")
end
end
end

View file

@ -0,0 +1,32 @@
defmodule NullaWeb.NoteJSON do
alias Nulla.Notes.Note
@doc """
Renders a list of notes.
"""
def index(%{notes: notes}) do
%{data: for(note <- notes, do: data(note))}
end
@doc """
Renders a single note.
"""
def show(%{note: note}) do
%{data: data(note)}
end
defp data(%Note{} = note) do
%{
id: note.id,
inReplyTo: note.inReplyTo,
published: note.published,
url: note.url,
visibility: note.visibility,
to: note.to,
cc: note.cc,
sensitive: note.sensitive,
content: note.content,
language: note.language
}
end
end

View file

@ -0,0 +1,43 @@
defmodule NullaWeb.RelationController do
use NullaWeb, :controller
alias Nulla.Relations
alias Nulla.Relations.Relation
action_fallback NullaWeb.FallbackController
def index(conn, _params) do
relations = Relations.list_relations()
render(conn, :index, relations: relations)
end
def create(conn, %{"relation" => relation_params}) do
with {:ok, %Relation{} = relation} <- Relations.create_relation(relation_params) do
conn
|> put_status(:created)
|> put_resp_header("location", ~p"/api/relations/#{relation}")
|> render(:show, relation: relation)
end
end
def show(conn, %{"id" => id}) do
relation = Relations.get_relation!(id)
render(conn, :show, relation: relation)
end
def update(conn, %{"id" => id, "relation" => relation_params}) do
relation = Relations.get_relation!(id)
with {:ok, %Relation{} = relation} <- Relations.update_relation(relation, relation_params) do
render(conn, :show, relation: relation)
end
end
def delete(conn, %{"id" => id}) do
relation = Relations.get_relation!(id)
with {:ok, %Relation{}} <- Relations.delete_relation(relation) do
send_resp(conn, :no_content, "")
end
end
end

View file

@ -0,0 +1,35 @@
defmodule NullaWeb.RelationJSON do
alias Nulla.Relations.Relation
@doc """
Renders a list of relations.
"""
def index(%{relations: relations}) do
%{data: for(relation <- relations, do: data(relation))}
end
@doc """
Renders a single relation.
"""
def show(%{relation: relation}) do
%{data: data(relation)}
end
defp data(%Relation{} = relation) do
%{
id: relation.id,
following: relation.following,
followed_by: relation.followed_by,
showing_replies: relation.showing_replies,
showings_reblogs: relation.showings_reblogs,
notifying: relation.notifying,
muting: relation.muting,
muting_notifications: relation.muting_notifications,
blocking: relation.blocking,
blocked_by: relation.blocked_by,
domain_blocking: relation.domain_blocking,
requested: relation.requested,
note: relation.note
}
end
end

View file

@ -0,0 +1,42 @@
defmodule NullaWeb.UserSessionController do
use NullaWeb, :controller
alias Nulla.Accounts
alias NullaWeb.UserAuth
def create(conn, %{"_action" => "registered"} = params) do
create(conn, params, "Account created successfully!")
end
def create(conn, %{"_action" => "password_updated"} = params) do
conn
|> put_session(:user_return_to, ~p"/users/settings")
|> create(params, "Password updated successfully!")
end
def create(conn, params) do
create(conn, params, "Welcome back!")
end
defp create(conn, %{"user" => user_params}, info) do
%{"email" => email, "password" => password} = user_params
if user = Accounts.get_user_by_email_and_password(email, password) do
conn
|> put_flash(:info, info)
|> UserAuth.log_in_user(user, user_params)
else
# In order to prevent user enumeration attacks, don't disclose whether the email is registered.
conn
|> put_flash(:error, "Invalid email or password")
|> put_flash(:email, String.slice(email, 0, 160))
|> redirect(to: ~p"/users/log_in")
end
end
def delete(conn, _params) do
conn
|> put_flash(:info, "Logged out successfully.")
|> UserAuth.log_out_user()
end
end