Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
liamwhite committed Aug 8, 2024
2 parents 773991c + 2a89162 commit 25353b9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 47 deletions.
44 changes: 16 additions & 28 deletions lib/philomena/notifications/creator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,22 @@ defmodule Philomena.Notifications.Creator do
|> insert_notifications(notification_schema, unique_key)
end

# TODO: the following cannot be accomplished with a single query expression
# due to this Ecto bug: https://github.com/elixir-ecto/ecto/issues/4430

defp convert_to_notification(subscription, [{name, object_id}]) do
now = DateTime.utc_now(:second)

from s in subscription,
select: %{
^name => type(^object_id, :integer),
user_id: s.user_id,
created_at: ^now,
updated_at: ^now,
read: false
}
end

defp convert_to_notification(subscription, [{name1, object_id1}, {name2, object_id2}]) do
now = DateTime.utc_now(:second)

from s in subscription,
select: %{
^name1 => type(^object_id1, :integer),
^name2 => type(^object_id2, :integer),
user_id: s.user_id,
created_at: ^now,
updated_at: ^now,
read: false
}
defp convert_to_notification(subscription, extra) do
now = dynamic([_], type(^DateTime.utc_now(:second), :utc_datetime))

base = %{
user_id: dynamic([s], s.user_id),
created_at: now,
updated_at: now,
read: false
}

extra =
Map.new(extra, fn {field, value} ->
{field, dynamic([_], type(^value, :integer))}
end)

from(subscription, select: ^Map.merge(base, extra))
end

defp subscription_query(subscription, notification_author) do
Expand Down
26 changes: 7 additions & 19 deletions lib/philomena_web/plugs/load_poll_plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,20 @@ defmodule PhilomenaWeb.LoadPollPlug do
alias Philomena.Polls.Poll
alias Philomena.Repo

import Plug.Conn, only: [assign: 3]
import Canada.Can, only: [can?: 3]
import Ecto.Query

def init(opts),
do: opts

def call(%{assigns: %{topic: topic}} = conn, opts) do
show_hidden = Keyword.get(opts, :show_hidden, false)
def init(opts), do: opts

def call(%{assigns: %{topic: topic}} = conn, _opts) do
Poll
|> where(topic_id: ^topic.id)
|> Repo.one()
|> maybe_hide_poll(conn, show_hidden)
end

defp maybe_hide_poll(nil, conn, _show_hidden),
do: PhilomenaWeb.NotFoundPlug.call(conn)

defp maybe_hide_poll(%{hidden_from_users: false} = poll, conn, _show_hidden),
do: assign(conn, :poll, poll)
|> case do
nil ->
PhilomenaWeb.NotFoundPlug.call(conn)

defp maybe_hide_poll(poll, %{assigns: %{current_user: user}} = conn, show_hidden) do
case show_hidden or can?(user, :show, poll) do
true -> assign(conn, :poll, poll)
false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
poll ->
Plug.Conn.assign(conn, :poll, poll)
end
end
end

0 comments on commit 25353b9

Please sign in to comment.