Skip to content

Commit

Permalink
pch
Browse files Browse the repository at this point in the history
  • Loading branch information
liamwhite committed Oct 17, 2024
1 parent ece6f09 commit 9f27f05
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 11 deletions.
1 change: 1 addition & 0 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ config :philomena,
tag_file_root: System.fetch_env!("TAG_FILE_ROOT"),
site_domains: System.fetch_env!("SITE_DOMAINS"),
tag_url_root: System.fetch_env!("TAG_URL_ROOT"),
event_secret: System.fetch_env!("EVENT_SECRET_PHRASE"),
redis_host: System.get_env("REDIS_HOST", "localhost"),
proxy_host: System.get_env("PROXY_HOST"),
camo_host: System.get_env("CAMO_HOST"),
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ services:
- PASSWORD_PEPPER=dn2e0EpZrvBLoxUM3gfQveBhjf0bG/6/bYhrOyq3L3hV9hdo/bimJ+irbDWsuXLP
- TUMBLR_API_KEY=fuiKNFp9vQFvjLNvx4sUwti4Yb5yGutBN4Xh10LXZhhRKjWlV4
- OTP_SECRET_KEY=Wn7O/8DD+qxL0X4X7bvT90wOkVGcA90bIHww4twR03Ci//zq7PnMw8ypqyyT/b/C
- EVENT_SECRET_PHRASE=yfbWGiSxyMvsWbs7OdLCrXeACKXpT7DVmwitMBOfArSgEsJQ9QHHI3e0UkNIUaey
- ADVERT_FILE_ROOT=adverts
- AVATAR_FILE_ROOT=avatars
- BADGE_FILE_ROOT=badges
Expand Down
5 changes: 4 additions & 1 deletion lib/philomena/artist_links.ex
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ defmodule Philomena.ArtistLinks do

Multi.new()
|> Multi.update(:artist_link, artist_link_changeset)
|> Multi.run(:add_award, BadgeAwarder.award_callback(artist_link, verifying_user))
|> Multi.run(
:add_award,
BadgeAwarder.award_callback(artist_link.user, verifying_user, "Artist")
)
|> Repo.transaction()
|> case do
{:ok, %{artist_link: artist_link}} ->
Expand Down
18 changes: 8 additions & 10 deletions lib/philomena/artist_links/badge_awarder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@ defmodule Philomena.ArtistLinks.BadgeAwarder do

alias Philomena.Badges

@badge_title "Artist"

@doc """
Awards a badge to an artist with a verified link.
Awards a badge to a user.
If the badge with the title `"Artist"` does not exist, no award will be created.
If the badge with the given title does not exist, no award will be created.
If the user already has an award with that badge title, no award will be created.
Returns `{:ok, award}`, `{:ok, nil}`, or `{:error, changeset}`. The return value is
suitable for use as the return value to an `Ecto.Multi.run/3` callback.
"""
def award_badge(artist_link, verifying_user) do
with badge when not is_nil(badge) <- Badges.get_badge_by_title(@badge_title),
award when is_nil(award) <- Badges.get_badge_award_for(badge, artist_link.user) do
Badges.create_badge_award(verifying_user, artist_link.user, %{badge_id: badge.id})
def award_badge(user, verifying_user, title) do
with badge when not is_nil(badge) <- Badges.get_badge_by_title(title),
award when is_nil(award) <- Badges.get_badge_award_for(badge, user) do
Badges.create_badge_award(verifying_user, user, %{badge_id: badge.id})
else
_ ->
{:ok, nil}
Expand All @@ -29,9 +27,9 @@ defmodule Philomena.ArtistLinks.BadgeAwarder do
@doc """
Get a callback for issuing a badge award from within an `m:Ecto.Multi`.
"""
def award_callback(artist_link, verifying_user) do
def award_callback(user, verifying_user, title) do
fn _repo, _changes ->
award_badge(artist_link, verifying_user)
award_badge(user, verifying_user, title)
end
end
end
32 changes: 32 additions & 0 deletions lib/philomena_web/controllers/pch_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule PhilomenaWeb.PchController do
use PhilomenaWeb, :controller

alias Philomena.ArtistLinks.BadgeAwarder

def index(conn, _params) do
render(conn, title: "PCH Secret")
end

def create(conn, %{"event" => event_params}) do
user = conn.assigns.current_user
secret = event_secret()

case event_params do
%{"passphrase" => ^secret} ->
{:ok, _badge} = BadgeAwarder.award_badge(user, user, "PonyCon Holland")

conn
|> put_flash(:info, "Verification granted.")
|> redirect(to: ~p"/")

_ ->
conn
|> put_flash(:error, "Incorrect passphrase.")
|> redirect(to: ~p"/")
end
end

defp event_secret do
Application.fetch_env!(:philomena, :event_secret)
end
end
1 change: 1 addition & 0 deletions lib/philomena_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ defmodule PhilomenaWeb.Router do
:require_authenticated_user
]

resources "/pch", PchController, only: [:index, :new, :create]
resources "/registrations", RegistrationController, only: [:edit, :update], singleton: true
resources "/sessions", SessionController, only: [:delete], singleton: true

Expand Down
8 changes: 8 additions & 0 deletions lib/philomena_web/templates/pch/index.html.slime
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
h1 Enter passphrase for verification

= form_for :event, ~p"/pch", fn f ->
.field
= text_input f, :passphrase, class: "input", placeholder: "Passphrase", required: true

.field
= submit "Submit", class: "button"
3 changes: 3 additions & 0 deletions lib/philomena_web/views/pch_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule PhilomenaWeb.PchView do
use PhilomenaWeb, :view
end

0 comments on commit 9f27f05

Please sign in to comment.