Shorten truncated utc_now pattern

This commit is contained in:
Liam 2024-07-09 21:20:00 -04:00
parent 6c94595f8c
commit 161a5faf57
18 changed files with 36 additions and 52 deletions

View file

@ -4,7 +4,7 @@ defmodule Philomena.Adverts.Recorder do
import Ecto.Query import Ecto.Query
def run(%{impressions: impressions, clicks: clicks}) do def run(%{impressions: impressions, clicks: clicks}) do
now = DateTime.utc_now() |> DateTime.truncate(:second) now = DateTime.utc_now(:second)
# Create insert statements for Ecto # Create insert statements for Ecto
impressions = Enum.map(impressions, &impressions_insert_all(&1, now)) impressions = Enum.map(impressions, &impressions_insert_all(&1, now))

View file

@ -88,11 +88,10 @@ defmodule Philomena.ArtistLinks.ArtistLink do
end end
def contact_changeset(artist_link, user) do def contact_changeset(artist_link, user) do
now = DateTime.utc_now() |> DateTime.truncate(:second) artist_link
|> change()
change(artist_link)
|> put_change(:contacted_by_user_id, user.id) |> put_change(:contacted_by_user_id, user.id)
|> put_change(:contacted_at, now) |> put_change(:contacted_at, DateTime.utc_now(:second))
|> put_change(:aasm_state, "contacted") |> put_change(:aasm_state, "contacted")
end end
@ -111,9 +110,9 @@ defmodule Philomena.ArtistLinks.ArtistLink do
defp put_next_check_at(changeset) do defp put_next_check_at(changeset) do
time = time =
DateTime.utc_now() :second
|> DateTime.utc_now()
|> DateTime.add(60 * 2, :second) |> DateTime.add(60 * 2, :second)
|> DateTime.truncate(:second)
change(changeset, next_check_at: time) change(changeset, next_check_at: time)
end end

View file

@ -26,9 +26,7 @@ defmodule Philomena.Badges.Award do
end end
defp put_awarded_on(%{data: %{awarded_on: nil}} = changeset) do defp put_awarded_on(%{data: %{awarded_on: nil}} = changeset) do
now = DateTime.utc_now() |> DateTime.truncate(:second) put_change(changeset, :awarded_on, DateTime.utc_now(:second))
put_change(changeset, :awarded_on, now)
end end
defp put_awarded_on(changeset), do: changeset defp put_awarded_on(changeset), do: changeset

View file

@ -111,7 +111,7 @@ defmodule Philomena.Comments do
""" """
def update_comment(%Comment{} = comment, editor, attrs) do def update_comment(%Comment{} = comment, editor, attrs) do
now = DateTime.utc_now() |> DateTime.truncate(:second) now = DateTime.utc_now(:second)
current_body = comment.body current_body = comment.body
current_reason = comment.edit_reason current_reason = comment.edit_reason

View file

@ -67,8 +67,7 @@ defmodule Philomena.Conversations.Conversation do
end end
defp set_last_message(changeset) do defp set_last_message(changeset) do
changeset change(changeset, last_message_at: DateTime.utc_now(:second))
|> change(last_message_at: DateTime.utc_now() |> DateTime.truncate(:second))
end end
defp put_recipient(changeset) do defp put_recipient(changeset) do

View file

@ -369,7 +369,7 @@ defmodule Philomena.Images do
end end
defp source_change_attributes(attribution, image, source, added, user) do defp source_change_attributes(attribution, image, source, added, user) do
now = DateTime.utc_now() |> DateTime.truncate(:second) now = DateTime.utc_now(:second)
user_id = user_id =
case user do case user do
@ -465,7 +465,7 @@ defmodule Philomena.Images do
end end
defp tag_change_attributes(attribution, image, tag, added, user) do defp tag_change_attributes(attribution, image, tag, added, user) do
now = DateTime.utc_now() |> DateTime.truncate(:second) now = DateTime.utc_now(:second)
user_id = user_id =
case user do case user do
@ -708,7 +708,7 @@ defmodule Philomena.Images do
|> where([t], t.image_id in ^image_ids and t.tag_id in ^removed_tags) |> where([t], t.image_id in ^image_ids and t.tag_id in ^removed_tags)
|> select([t], [t.image_id, t.tag_id]) |> select([t], [t.image_id, t.tag_id])
now = DateTime.utc_now() |> DateTime.truncate(:second) now = DateTime.utc_now(:second)
tag_change_attributes = Map.merge(tag_change_attributes, %{created_at: now, updated_at: now}) tag_change_attributes = Map.merge(tag_change_attributes, %{created_at: now, updated_at: now})
tag_attributes = %{name: "", slug: "", created_at: now, updated_at: now} tag_attributes = %{name: "", slug: "", created_at: now, updated_at: now}

View file

@ -120,11 +120,9 @@ defmodule Philomena.Images.Image do
end end
def creation_changeset(image, attrs, attribution) do def creation_changeset(image, attrs, attribution) do
now = DateTime.utc_now() |> DateTime.truncate(:second)
image image
|> cast(attrs, [:anonymous, :source_url, :description]) |> cast(attrs, [:anonymous, :source_url, :description])
|> change(first_seen_at: now) |> change(first_seen_at: DateTime.utc_now(:second))
|> change(attribution) |> change(attribution)
|> validate_length(:description, max: 50_000, count: :bytes) |> validate_length(:description, max: 50_000, count: :bytes)
|> validate_format(:source_url, ~r/\Ahttps?:\/\//) |> validate_format(:source_url, ~r/\Ahttps?:\/\//)
@ -340,7 +338,7 @@ defmodule Philomena.Images.Image do
def approve_changeset(image) do def approve_changeset(image) do
change(image) change(image)
|> put_change(:approved, true) |> put_change(:approved, true)
|> put_change(:first_seen_at, DateTime.truncate(DateTime.utc_now(), :second)) |> put_change(:first_seen_at, DateTime.utc_now(:second))
end end
def cache_changeset(image) do def cache_changeset(image) do

View file

@ -72,7 +72,7 @@ defmodule Philomena.Interactions do
end end
def migrate_interactions(source, target) do def migrate_interactions(source, target) do
now = DateTime.utc_now() |> DateTime.truncate(:second) now = DateTime.utc_now(:second)
source = Repo.preload(source, [:hiders, :favers, :upvoters, :downvoters]) source = Repo.preload(source, [:hiders, :favers, :upvoters, :downvoters])
new_hides = Enum.map(source.hiders, &%{image_id: target.id, user_id: &1.id, created_at: now}) new_hides = Enum.map(source.hiders, &%{image_id: target.id, user_id: &1.id, created_at: now})

View file

@ -41,7 +41,7 @@ defmodule Philomena.PollVotes do
""" """
def create_poll_votes(user, poll, attrs) do def create_poll_votes(user, poll, attrs) do
now = DateTime.utc_now() |> DateTime.truncate(:second) now = DateTime.utc_now(:second)
poll_votes = filter_options(user, poll, now, attrs) poll_votes = filter_options(user, poll, now, attrs)
Multi.new() Multi.new()

View file

@ -50,7 +50,7 @@ defmodule Philomena.Posts do
""" """
def create_post(topic, attributes, params \\ %{}) do def create_post(topic, attributes, params \\ %{}) do
now = DateTime.utc_now() now = DateTime.utc_now(:second)
topic_query = topic_query =
Topic Topic
@ -161,7 +161,7 @@ defmodule Philomena.Posts do
""" """
def update_post(%Post{} = post, editor, attrs) do def update_post(%Post{} = post, editor, attrs) do
now = DateTime.utc_now() |> DateTime.truncate(:second) now = DateTime.utc_now(:second)
current_body = post.body current_body = post.body
current_reason = post.edit_reason current_reason = post.edit_reason

View file

@ -15,7 +15,7 @@ defmodule Philomena.Schema.Approval do
%{changes: %{body: body}, valid?: true} = changeset, %{changes: %{body: body}, valid?: true} = changeset,
%User{} = user %User{} = user
) do ) do
now = now_time() now = DateTime.utc_now(:second)
# 14 * 24 * 60 * 60 # 14 * 24 * 60 * 60
two_weeks = 1_209_600 two_weeks = 1_209_600
@ -40,6 +40,4 @@ defmodule Philomena.Schema.Approval do
do: change(changeset, body: Regex.replace(@image_embed_regex, body, "[")) do: change(changeset, body: Regex.replace(@image_embed_regex, body, "["))
def maybe_strip_images(changeset, _user), do: changeset def maybe_strip_images(changeset, _user), do: changeset
defp now_time(), do: DateTime.truncate(DateTime.utc_now(), :second)
end end

View file

@ -15,7 +15,7 @@ defmodule Philomena.TagChanges do
# TODO: this is substantially similar to Images.batch_update/4. # TODO: this is substantially similar to Images.batch_update/4.
# Perhaps it should be extracted. # Perhaps it should be extracted.
def mass_revert(ids, attributes) do def mass_revert(ids, attributes) do
now = DateTime.utc_now() |> DateTime.truncate(:second) now = DateTime.utc_now(:second)
tag_change_attributes = Map.merge(attributes, %{created_at: now, updated_at: now}) tag_change_attributes = Map.merge(attributes, %{created_at: now, updated_at: now})
tag_attributes = %{name: "", slug: "", created_at: now, updated_at: now} tag_attributes = %{name: "", slug: "", created_at: now, updated_at: now}

View file

@ -46,7 +46,7 @@ defmodule Philomena.Topics do
""" """
def create_topic(forum, attribution, attrs \\ %{}) do def create_topic(forum, attribution, attrs \\ %{}) do
now = DateTime.utc_now() |> DateTime.truncate(:second) now = DateTime.utc_now(:second)
topic = topic =
%Topic{} %Topic{}

View file

@ -75,11 +75,10 @@ defmodule Philomena.Topics.Topic do
end end
def lock_changeset(topic, attrs, user) do def lock_changeset(topic, attrs, user) do
now = DateTime.utc_now() |> DateTime.truncate(:second) topic
|> change()
change(topic)
|> cast(attrs, [:lock_reason]) |> cast(attrs, [:lock_reason])
|> put_change(:locked_at, now) |> put_change(:locked_at, DateTime.utc_now(:second))
|> put_change(:locked_by_id, user.id) |> put_change(:locked_by_id, user.id)
|> validate_required([:lock_reason]) |> validate_required([:lock_reason])
end end

View file

@ -215,8 +215,7 @@ defmodule Philomena.Users.User do
Confirms the account by setting `confirmed_at`. Confirms the account by setting `confirmed_at`.
""" """
def confirm_changeset(user) do def confirm_changeset(user) do
now = DateTime.utc_now() |> DateTime.truncate(:second) change(user, confirmed_at: DateTime.utc_now(:second))
change(user, confirmed_at: now)
end end
@doc """ @doc """
@ -259,9 +258,7 @@ defmodule Philomena.Users.User do
end end
def lock_changeset(user) do def lock_changeset(user) do
locked_at = DateTime.utc_now() |> DateTime.truncate(:second) change(user, locked_at: DateTime.utc_now(:second))
change(user, locked_at: locked_at)
end end
def unlock_changeset(user) do def unlock_changeset(user) do
@ -378,14 +375,12 @@ defmodule Philomena.Users.User do
end end
def name_changeset(user, attrs) do def name_changeset(user, attrs) do
now = DateTime.utc_now() |> DateTime.truncate(:second)
user user
|> cast(attrs, [:name]) |> cast(attrs, [:name])
|> validate_name() |> validate_name()
|> put_slug() |> put_slug()
|> unique_constraints() |> unique_constraints()
|> put_change(:last_renamed_at, now) |> put_change(:last_renamed_at, DateTime.utc_now(:second))
end end
def avatar_changeset(user, attrs) do def avatar_changeset(user, attrs) do
@ -428,7 +423,7 @@ defmodule Philomena.Users.User do
end end
def deactivate_changeset(user, moderator) do def deactivate_changeset(user, moderator) do
now = DateTime.utc_now() |> DateTime.truncate(:second) now = DateTime.utc_now(:second)
change(user, deleted_at: now, deleted_by_user_id: moderator.id) change(user, deleted_at: now, deleted_by_user_id: moderator.id)
end end

View file

@ -117,7 +117,7 @@ defmodule PhilomenaQuery.RelativeDate do
def parse_absolute(input) do def parse_absolute(input) do
case DateTime.from_iso8601(input) do case DateTime.from_iso8601(input) do
{:ok, datetime, _offset} -> {:ok, datetime, _offset} ->
{:ok, datetime |> DateTime.truncate(:second)} {:ok, DateTime.truncate(datetime, :second)}
_error -> _error ->
{:error, "Parse error"} {:error, "Parse error"}
@ -144,19 +144,17 @@ defmodule PhilomenaQuery.RelativeDate do
""" """
@spec parse_relative(String.t()) :: {:ok, DateTime.t()} | {:error, any()} @spec parse_relative(String.t()) :: {:ok, DateTime.t()} | {:error, any()}
def parse_relative(input) do def parse_relative(input) do
now = DateTime.utc_now(:second)
case relative_date(input) do case relative_date(input) do
{:ok, [moon: _moon], _1, _2, _3, _4} -> {:ok, [moon: _moon], _1, _2, _3, _4} ->
{:ok, {:ok, DateTime.add(now, 31_536_000_000, :second)}
DateTime.utc_now() |> DateTime.add(31_536_000_000, :second) |> DateTime.truncate(:second)}
{:ok, [now: _now], _1, _2, _3, _4} -> {:ok, [now: _now], _1, _2, _3, _4} ->
{:ok, DateTime.utc_now() |> DateTime.truncate(:second)} {:ok, now}
{:ok, [relative_date: [amount, scale, direction]], _1, _2, _3, _4} -> {:ok, [relative_date: [amount, scale, direction]], _1, _2, _3, _4} ->
{:ok, {:ok, DateTime.add(now, amount * scale * direction, :second)}
DateTime.utc_now()
|> DateTime.add(amount * scale * direction, :second)
|> DateTime.truncate(:second)}
_error -> _error ->
{:error, "Parse error"} {:error, "Parse error"}

View file

@ -48,7 +48,7 @@ defmodule PhilomenaWeb.StatsUpdater do
|> Phoenix.HTML.Safe.to_iodata() |> Phoenix.HTML.Safe.to_iodata()
|> IO.iodata_to_binary() |> IO.iodata_to_binary()
now = DateTime.utc_now() |> DateTime.truncate(:second) now = DateTime.utc_now(:second)
static_page = %{ static_page = %{
title: "Statistics", title: "Statistics",

View file

@ -210,7 +210,7 @@ defmodule PhilomenaWeb.UserAuth do
defp signed_in_path(_conn), do: "/" defp signed_in_path(_conn), do: "/"
defp update_usages(conn, user) do defp update_usages(conn, user) do
now = DateTime.utc_now() |> DateTime.truncate(:second) now = DateTime.utc_now(:second)
UserIpUpdater.cast(user.id, conn.remote_ip, now) UserIpUpdater.cast(user.id, conn.remote_ip, now)
UserFingerprintUpdater.cast(user.id, conn.assigns.fingerprint, now) UserFingerprintUpdater.cast(user.id, conn.assigns.fingerprint, now)