defmodule GitGudWeb.Api.Mobile.WikiController do
@moduledoc """
Wiki viewer.
Read-only: the wiki is a git repository of markdown files, and editing
one on a phone means resolving conflicts on a phone. The page list
walks the wiki repo's HEAD tree, so a repo whose wiki was never
initialized returns an empty list rather than a 404 — "no pages yet"
is a state the client renders, not an error.
"""
use GitGudWeb, :controller
import GitGudWeb.Api.Mobile.Helpers
alias GitGud.Wikis
alias GitGudWeb.Api.Mobile.Auth
alias GitGudWeb.Api.Mobile.JSON
def index(conn, %{"owner" => owner, "name" => name}) do
with {:ok, user} <- Auth.require_scope(conn, "wiki:read"),
{:ok, repo} <- fetch_repo(conn, user, owner, name) do
json(conn, %{
"pages" => repo |> Wikis.list_pages() |> Enum.map(&JSON.wiki_page_meta/1),
"initialized" => Wikis.initialized?(repo)
})
else
{:error, conn} -> conn
end
end
def show(conn, %{"owner" => owner, "name" => name, "slug" => slug}) do
with {:ok, user} <- Auth.require_scope(conn, "wiki:read"),
{:ok, repo} <- fetch_repo(conn, user, owner, name),
body when is_binary(body) <- Wikis.get_page(repo, slug) do
json(conn, %{
"slug" => slug,
"title" => slug,
"body" => body,
"body_html" => to_string(GitGud.Markdown.to_html(body, repo: repo))
})
else
{:error, conn} -> conn
nil -> send_error(conn, 404, "no wiki page '#{slug}'")
end
end
end
neiam
/gitgud
Git Gud
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
1
Log in to mark this repository.
1.5 KiB · text
History
b868907