mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-01-20 06:37:59 +01:00
Deduplicate select expressions in convert_to_notification
This commit is contained in:
parent
c173162c1e
commit
e704319dd9
1 changed files with 13 additions and 25 deletions
|
@ -47,34 +47,22 @@ defmodule Philomena.Notifications.Creator do
|
||||||
|> insert_notifications(notification_schema, unique_key)
|
|> insert_notifications(notification_schema, unique_key)
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: the following cannot be accomplished with a single query expression
|
defp convert_to_notification(subscription, extra) do
|
||||||
# due to this Ecto bug: https://github.com/elixir-ecto/ecto/issues/4430
|
now = dynamic([_], type(^DateTime.utc_now(:second), :utc_datetime))
|
||||||
|
|
||||||
defp convert_to_notification(subscription, [{name, object_id}]) do
|
base = %{
|
||||||
now = DateTime.utc_now(:second)
|
user_id: dynamic([s], s.user_id),
|
||||||
|
created_at: now,
|
||||||
|
updated_at: now,
|
||||||
|
read: false
|
||||||
|
}
|
||||||
|
|
||||||
from s in subscription,
|
extra =
|
||||||
select: %{
|
Map.new(extra, fn {field, value} ->
|
||||||
^name => type(^object_id, :integer),
|
{field, dynamic([_], type(^value, :integer))}
|
||||||
user_id: s.user_id,
|
end)
|
||||||
created_at: ^now,
|
|
||||||
updated_at: ^now,
|
|
||||||
read: false
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
defp convert_to_notification(subscription, [{name1, object_id1}, {name2, object_id2}]) do
|
from(subscription, select: ^Map.merge(base, extra))
|
||||||
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
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp subscription_query(subscription, notification_author) do
|
defp subscription_query(subscription, notification_author) do
|
||||||
|
|
Loading…
Reference in a new issue