diff --git a/assets/package-lock.json b/assets/package-lock.json index 5e817ba..8ed0c9b 100644 --- a/assets/package-lock.json +++ b/assets/package-lock.json @@ -1641,11 +1641,6 @@ "resolve-dir": "0.1.1" } }, - "font-awesome": { - "version": "4.7.0", - "resolved": "https://registry.npmjs.org/font-awesome/-/font-awesome-4.7.0.tgz", - "integrity": "sha1-j6jPBBGhoxr9B7BtKQK7n8gVoTM=" - }, "for-in": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz", diff --git a/assets/package.json b/assets/package.json index a028f26..93d35e1 100644 --- a/assets/package.json +++ b/assets/package.json @@ -8,7 +8,6 @@ }, "dependencies": { "bootstrap": "^3.3.7", - "font-awesome": "^4.7.0", "jquery": "^3.3.1", "phoenix": "file:../deps/phoenix", "phoenix_html": "file:../deps/phoenix_html" diff --git a/config/prod.exs b/config/prod.exs index 109d3aa..3fe2d5b 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -15,12 +15,15 @@ use Mix.Config # which you typically run after static files are built. config :forum, ForumWeb.Endpoint, load_from_system_env: true, - url: [host: "forum.tilde.team", port: 4002], + # url: [host: "forum.tilde.team", port: 4002], + http: [port: {:system, "PORT"}], cache_static_manifest: "priv/static/cache_manifest.json" # Do not print debug messages in production config :logger, level: :info +config :phoenix, :serve_endpoints, true + # ## SSL Support # # To get SSL working, you will need to add the `https` key diff --git a/domains b/domains new file mode 100644 index 0000000..06f890e --- /dev/null +++ b/domains @@ -0,0 +1 @@ +forum.tilde.team \ No newline at end of file diff --git a/lib/forum/forum.ex b/lib/forum/forum.ex index 7211b49..199de11 100644 --- a/lib/forum/forum.ex +++ b/lib/forum/forum.ex @@ -39,6 +39,15 @@ defmodule Forum do Repo.get_by!(User, name: name) end + @doc """ + Gets user id by name + """ + def get_user_id_by_name(name) do + from(u in User, where: u.name == ^name, select: [u.id]) + |> Repo.one() + |> hd + end + @doc """ Creates a user. diff --git a/lib/forum/thread.ex b/lib/forum/thread.ex index e95e31b..7041523 100644 --- a/lib/forum/thread.ex +++ b/lib/forum/thread.ex @@ -5,6 +5,7 @@ defmodule Forum.Thread do schema "threads" do field(:name, :string) + field(:sticky, :boolean) has_many(:posts, Forum.Post) belongs_to(:user, Forum.User) @@ -15,7 +16,7 @@ defmodule Forum.Thread do @doc false def changeset(%Thread{} = thread, attrs) do thread - |> cast(attrs, [:name, :user_id]) + |> cast(attrs, [:name, :user_id, :sticky]) |> validate_required([:name, :user_id]) |> foreign_key_constraint(:user_id) end diff --git a/lib/forum/user.ex b/lib/forum/user.ex index cfb251f..590d0b8 100644 --- a/lib/forum/user.ex +++ b/lib/forum/user.ex @@ -22,6 +22,9 @@ defmodule Forum.User do |> unique_constraint(:name) end + @doc """ + Gets contents of tagline.txt for user + """ def tagline(user) do resp = get("~#{user.name}/tagline.txt") @@ -36,4 +39,18 @@ defmodule Forum.User do "something went wrong..." end end + + @doc """ + Gets tilde url for user + """ + def profile_url(user) do + "https://tilde.team/~#{user.name}/" + end + + @doc """ + Gets path to avatar for user + """ + def pfp(user) do + profile_url(user) <> "avatar.png" + end end diff --git a/lib/forum/util.ex b/lib/forum/util.ex index e906319..e0a6f06 100644 --- a/lib/forum/util.ex +++ b/lib/forum/util.ex @@ -1,31 +1,43 @@ defmodule Forum.Util do + @moduledoc """ + General utilities for the Forum + """ + import Plug.Conn - alias Forum.Repo - alias Forum.User - + @doc """ + Converts Markdown to HTML + """ def md(text) do text |> Earmark.as_html!() |> Phoenix.HTML.raw() end - def get_session_user(conn) do - Repo.get_by(User, name: current_username(conn)) - end - + @doc """ + Gets the logged-in username for a conn + """ def current_username(conn) do get_session(conn, :current_user) end + @doc """ + Can current user sudo? + """ def is_sudoer?(conn) do get_session(conn, :sudoer) end + @doc """ + + """ def can_edit?(conn, user) do user.name == current_username(conn) or is_sudoer?(conn) end + @doc """ + + """ def pp_time(time) do time |> Timex.local() @@ -33,17 +45,19 @@ defmodule Forum.Util do |> String.downcase() end - def pfp(user) do - profile_url(user) <> "avatar.png" - end - - def profile_url(user) do - "https://tilde.team/~#{user.name}/" - end + @doc """ + """ def paginate(conn, kerosene) do if kerosene.total_pages > 1 do - Kerosene.HTML.paginate(conn, kerosene, next_label: ">>", previous_label: "<<") + Kerosene.HTML.paginate( + conn, + kerosene, + next_label: ">>", + previous_label: "<<", + first_label: "first", + last_label: "last" + ) end end end diff --git a/lib/forum_web.ex b/lib/forum_web.ex index 274551c..d900288 100644 --- a/lib/forum_web.ex +++ b/lib/forum_web.ex @@ -43,6 +43,7 @@ defmodule ForumWeb do import ForumWeb.ErrorHelpers import ForumWeb.Gettext + alias Forum.User alias Forum.Util end end diff --git a/lib/forum_web/controllers/post_controller.ex b/lib/forum_web/controllers/post_controller.ex index f953bd9..a4c0720 100644 --- a/lib/forum_web/controllers/post_controller.ex +++ b/lib/forum_web/controllers/post_controller.ex @@ -4,20 +4,16 @@ defmodule ForumWeb.PostController do def create(conn, %{"post" => post_params}) do case Forum.create_post(post_params) do {:ok, post} -> - post = Forum.Repo.preload(post, :thread) - conn |> put_flash(:info, "reply created successfully.") - |> redirect(to: thread_path(conn, :show, post.thread.id)) + |> redirect(to: thread_path(conn, :show, post.thread_id)) {:error, %Ecto.Changeset{} = changeset} -> - user = Util.get_session_user(conn) - render( conn, "new.html", changeset: changeset, - user_id: user.id, + user_id: Forum.get_user_id_by_name(Util.current_username(conn)), thread_id: post_params["thread_id"] ) end diff --git a/lib/forum_web/controllers/thread_controller.ex b/lib/forum_web/controllers/thread_controller.ex index 664af0b..5f778ce 100644 --- a/lib/forum_web/controllers/thread_controller.ex +++ b/lib/forum_web/controllers/thread_controller.ex @@ -9,20 +9,39 @@ defmodule ForumWeb.ThreadController do alias Forum.User def index(conn, params) do + stickies = + Thread + |> order_by(desc: :updated_at) + |> where(sticky: true) + |> preload(:user) + |> Repo.all() + {threads, kerosene} = Thread |> order_by(desc: :id) + |> where(sticky: false) |> preload(:user) |> Repo.paginate(params) tagline = Tesla.get("https://tilde.team/~ben/api/").body - render(conn, "index.html", threads: threads, kerosene: kerosene, tagline: tagline) + + render( + conn, + "index.html", + stickies: stickies, + threads: threads, + kerosene: kerosene, + tagline: tagline + ) end def new(conn, _params) do - changeset = Forum.change_thread(%Thread{}) - user = Util.get_session_user(conn) - render(conn, "new.html", changeset: changeset, user_id: user.id) + changeset = + Forum.change_thread(%Thread{ + user_id: conn |> Util.current_username() |> Forum.get_user_id_by_name() + }) + + render(conn, "new.html", changeset: changeset) end def create(conn, %{"thread" => thread_params}) do diff --git a/lib/forum_web/templates/post/show.html.eex b/lib/forum_web/templates/post/show.html.eex index 9be58d1..7551044 100644 --- a/lib/forum_web/templates/post/show.html.eex +++ b/lib/forum_web/templates/post/show.html.eex @@ -3,7 +3,7 @@
- <%= @post.user.name %> avatar + <%= @post.user.name %> avatar
diff --git a/lib/forum_web/templates/thread/form.html.eex b/lib/forum_web/templates/thread/form.html.eex index f12fd5e..3ff7359 100644 --- a/lib/forum_web/templates/thread/form.html.eex +++ b/lib/forum_web/templates/thread/form.html.eex @@ -6,16 +6,22 @@ <% IO.inspect @changeset %> <% end %> - <%= if assigns[:user_id] do - hidden_input f, :user_id, value: assigns[:user_id] - end %> + <%= hidden_input f, :user_id %>
- <%= text_input f, :name, placeholder: "my cool new thread", class: "form-control" %> + <%= label f, :name, "thread title" %> + <%= text_input f, :name, placeholder: "my cool new thread", class: "form-control", autofocus: true %> <%= error_tag f, :name %>
+ <%= if Util.is_sudoer?(@conn) do %> +
+ <%= label f, :sticky, "sticky thread?" %> + <%= checkbox f, :sticky %> +
+ <% end %> +
- <%= submit "create", class: "btn btn-primary" %> + <%= submit "save", class: "btn btn-primary" %>
<% end %> diff --git a/lib/forum_web/templates/thread/index.html.eex b/lib/forum_web/templates/thread/index.html.eex index db174ed..6d294ca 100644 --- a/lib/forum_web/templates/thread/index.html.eex +++ b/lib/forum_web/templates/thread/index.html.eex @@ -1,23 +1,19 @@

tilde~forum

<%= @tagline %> + +<%= if Enum.any?(@stickies) do %> +
+

sticky threads

+<% end %> + +<%= for sticky <- @stickies do %> + <%= render "thread.html", conn: @conn, thread: sticky %> +<% end %> +
<%= for thread <- @threads do %> -
- - -
+ <%= render "thread.html", conn: @conn, thread: thread %> <% end %> diff --git a/lib/forum_web/templates/thread/new.html.eex b/lib/forum_web/templates/thread/new.html.eex index 8dc0e3b..6851fb1 100644 --- a/lib/forum_web/templates/thread/new.html.eex +++ b/lib/forum_web/templates/thread/new.html.eex @@ -1,4 +1,6 @@ -

start new thread

+ <%= render "form.html", Map.put(assigns, :action, thread_path(@conn, :create)) %> diff --git a/lib/forum_web/templates/thread/show.html.eex b/lib/forum_web/templates/thread/show.html.eex index 2c69441..07893a3 100644 --- a/lib/forum_web/templates/thread/show.html.eex +++ b/lib/forum_web/templates/thread/show.html.eex @@ -1,6 +1,6 @@ <%= if Util.can_edit?(@conn, @thread.user) do %>
- <%= link "edit thread title", to: thread_path(@conn, :edit, @thread), class: "btn btn-info btn-xs" %> + <%= link "edit thread", to: thread_path(@conn, :edit, @thread), class: "btn btn-info btn-xs" %>
<% end %> diff --git a/lib/forum_web/templates/thread/thread.html.eex b/lib/forum_web/templates/thread/thread.html.eex new file mode 100644 index 0000000..7bf88bb --- /dev/null +++ b/lib/forum_web/templates/thread/thread.html.eex @@ -0,0 +1,15 @@ +
+ + +
\ No newline at end of file diff --git a/lib/forum_web/templates/user/show.html.eex b/lib/forum_web/templates/user/show.html.eex index d139288..b57d8ab 100644 --- a/lib/forum_web/templates/user/show.html.eex +++ b/lib/forum_web/templates/user/show.html.eex @@ -1,9 +1,9 @@