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 Jul 13, 2024
2 parents 0e8010e + 68352bc commit ea20003
Show file tree
Hide file tree
Showing 67 changed files with 329 additions and 306 deletions.
4 changes: 2 additions & 2 deletions lib/philomena/adverts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ defmodule Philomena.Adverts do
"""
def create_advert(attrs \\ %{}) do
%Advert{}
|> Advert.save_changeset(attrs)
|> Advert.changeset(attrs)
|> Uploader.analyze_upload(attrs)
|> Repo.insert()
|> case do
Expand Down Expand Up @@ -150,7 +150,7 @@ defmodule Philomena.Adverts do
"""
def update_advert(%Advert{} = advert, attrs) do
advert
|> Advert.save_changeset(attrs)
|> Advert.changeset(attrs)
|> Repo.update()
end

Expand Down
21 changes: 4 additions & 17 deletions lib/philomena/adverts/advert.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ defmodule Philomena.Adverts.Advert do
use Ecto.Schema
import Ecto.Changeset

alias Philomena.Schema.Time

schema "adverts" do
field :image, :string
field :link, :string
field :title, :string
field :clicks, :integer, default: 0
field :impressions, :integer, default: 0
field :live, :boolean, default: false
field :start_date, :utc_datetime
field :finish_date, :utc_datetime
field :start_date, PhilomenaQuery.Ecto.RelativeDate
field :finish_date, PhilomenaQuery.Ecto.RelativeDate
field :restrictions, :string
field :notes, :string

Expand All @@ -24,29 +22,18 @@ defmodule Philomena.Adverts.Advert do
field :uploaded_image, :string, virtual: true
field :removed_image, :string, virtual: true

field :start_time, :string, virtual: true
field :finish_time, :string, virtual: true

timestamps(inserted_at: :created_at, type: :utc_datetime)
end

@doc false
def changeset(advert, attrs) do
advert
|> cast(attrs, [])
|> Time.propagate_time(:start_date, :start_time)
|> Time.propagate_time(:finish_date, :finish_time)
end

def save_changeset(advert, attrs) do
advert
|> cast(attrs, [:title, :link, :start_time, :finish_time, :live, :restrictions, :notes])
|> Time.assign_time(:start_time, :start_date)
|> Time.assign_time(:finish_time, :finish_date)
|> cast(attrs, [:title, :link, :start_date, :finish_date, :live, :restrictions, :notes])
|> validate_required([:title, :link, :start_date, :finish_date])
|> validate_inclusion(:restrictions, ["none", "nsfw", "sfw"])
end

@doc false
def image_changeset(advert, attrs) do
advert
|> cast(attrs, [
Expand Down
2 changes: 1 addition & 1 deletion lib/philomena/adverts/recorder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Philomena.Adverts.Recorder do
import Ecto.Query

def run(%{impressions: impressions, clicks: clicks}) do
now = DateTime.utc_now() |> DateTime.truncate(:second)
now = DateTime.utc_now(:second)

# Create insert statements for Ecto
impressions = Enum.map(impressions, &impressions_insert_all(&1, now))
Expand Down
11 changes: 5 additions & 6 deletions lib/philomena/artist_links/artist_link.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,10 @@ defmodule Philomena.ArtistLinks.ArtistLink do
end

def contact_changeset(artist_link, user) do
now = DateTime.utc_now() |> DateTime.truncate(:second)

change(artist_link)
artist_link
|> change()
|> 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")
end

Expand All @@ -111,9 +110,9 @@ defmodule Philomena.ArtistLinks.ArtistLink do

defp put_next_check_at(changeset) do
time =
DateTime.utc_now()
:second
|> DateTime.utc_now()
|> DateTime.add(60 * 2, :second)
|> DateTime.truncate(:second)

change(changeset, next_check_at: time)
end
Expand Down
4 changes: 1 addition & 3 deletions lib/philomena/badges/award.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ defmodule Philomena.Badges.Award do
end

defp put_awarded_on(%{data: %{awarded_on: nil}} = changeset) do
now = DateTime.utc_now() |> DateTime.truncate(:second)

put_change(changeset, :awarded_on, now)
put_change(changeset, :awarded_on, DateTime.utc_now(:second))
end

defp put_awarded_on(changeset), do: changeset
Expand Down
12 changes: 6 additions & 6 deletions lib/philomena/bans.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ defmodule Philomena.Bans do
"""
def create_fingerprint(creator, attrs \\ %{}) do
%Fingerprint{banning_user_id: creator.id}
|> Fingerprint.save_changeset(attrs)
|> Fingerprint.changeset(attrs)
|> Repo.insert()
end

Expand All @@ -74,7 +74,7 @@ defmodule Philomena.Bans do
"""
def update_fingerprint(%Fingerprint{} = fingerprint, attrs) do
fingerprint
|> Fingerprint.save_changeset(attrs)
|> Fingerprint.changeset(attrs)
|> Repo.update()
end

Expand Down Expand Up @@ -150,7 +150,7 @@ defmodule Philomena.Bans do
"""
def create_subnet(creator, attrs \\ %{}) do
%Subnet{banning_user_id: creator.id}
|> Subnet.save_changeset(attrs)
|> Subnet.changeset(attrs)
|> Repo.insert()
end

Expand All @@ -168,7 +168,7 @@ defmodule Philomena.Bans do
"""
def update_subnet(%Subnet{} = subnet, attrs) do
subnet
|> Subnet.save_changeset(attrs)
|> Subnet.changeset(attrs)
|> Repo.update()
end

Expand Down Expand Up @@ -245,7 +245,7 @@ defmodule Philomena.Bans do
def create_user(creator, attrs \\ %{}) do
changeset =
%User{banning_user_id: creator.id}
|> User.save_changeset(attrs)
|> User.changeset(attrs)

Multi.new()
|> Multi.insert(:user_ban, changeset)
Expand Down Expand Up @@ -276,7 +276,7 @@ defmodule Philomena.Bans do
"""
def update_user(%User{} = user, attrs) do
user
|> User.save_changeset(attrs)
|> User.changeset(attrs)
|> Repo.update()
end

Expand Down
18 changes: 4 additions & 14 deletions lib/philomena/bans/fingerprint.ex
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
defmodule Philomena.Bans.Fingerprint do
use Ecto.Schema
import Ecto.Changeset
import Philomena.Bans.IdGenerator

alias Philomena.Users.User
alias Philomena.Schema.Time
alias Philomena.Schema.BanId

schema "fingerprint_bans" do
belongs_to :banning_user, User

field :reason, :string
field :note, :string
field :enabled, :boolean, default: true
field :valid_until, :utc_datetime
field :valid_until, PhilomenaQuery.Ecto.RelativeDate
field :fingerprint, :string
field :generated_ban_id, :string

field :until, :string, virtual: true

timestamps(inserted_at: :created_at, type: :utc_datetime)
end

@doc false
def changeset(fingerprint_ban, attrs) do
fingerprint_ban
|> cast(attrs, [])
|> Time.propagate_time(:valid_until, :until)
end

def save_changeset(fingerprint_ban, attrs) do
fingerprint_ban
|> cast(attrs, [:reason, :note, :enabled, :fingerprint, :until])
|> Time.assign_time(:until, :valid_until)
|> BanId.put_ban_id("F")
|> cast(attrs, [:reason, :note, :enabled, :fingerprint, :valid_until])
|> put_ban_id("F")
|> validate_required([:reason, :enabled, :fingerprint, :valid_until])
|> check_constraint(:valid_until, name: :fingerprint_ban_duration_must_be_valid)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule Philomena.Schema.BanId do
defmodule Philomena.Bans.IdGenerator do
@moduledoc false

import Ecto.Changeset

def put_ban_id(%{data: %{generated_ban_id: nil}} = changeset, prefix) do
Expand Down
18 changes: 4 additions & 14 deletions lib/philomena/bans/subnet.ex
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
defmodule Philomena.Bans.Subnet do
use Ecto.Schema
import Ecto.Changeset
import Philomena.Bans.IdGenerator

alias Philomena.Users.User
alias Philomena.Schema.Time
alias Philomena.Schema.BanId

schema "subnet_bans" do
belongs_to :banning_user, User

field :reason, :string
field :note, :string
field :enabled, :boolean, default: true
field :valid_until, :utc_datetime
field :valid_until, PhilomenaQuery.Ecto.RelativeDate
field :specification, EctoNetwork.INET
field :generated_ban_id, :string

field :until, :string, virtual: true

timestamps(inserted_at: :created_at, type: :utc_datetime)
end

@doc false
def changeset(subnet_ban, attrs) do
subnet_ban
|> cast(attrs, [])
|> Time.propagate_time(:valid_until, :until)
end

def save_changeset(subnet_ban, attrs) do
subnet_ban
|> cast(attrs, [:reason, :note, :enabled, :specification, :until])
|> Time.assign_time(:until, :valid_until)
|> BanId.put_ban_id("S")
|> cast(attrs, [:reason, :note, :enabled, :specification, :valid_until])
|> put_ban_id("S")
|> validate_required([:reason, :enabled, :specification, :valid_until])
|> check_constraint(:valid_until, name: :subnet_ban_duration_must_be_valid)
|> mask_specification()
Expand Down
39 changes: 4 additions & 35 deletions lib/philomena/bans/user.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
defmodule Philomena.Bans.User do
use Ecto.Schema
import Ecto.Changeset
import Philomena.Bans.IdGenerator

alias Philomena.Users.User
alias Philomena.Repo
alias Philomena.Schema.Time
alias Philomena.Schema.BanId

schema "user_bans" do
belongs_to :user, User
Expand All @@ -14,48 +12,19 @@ defmodule Philomena.Bans.User do
field :reason, :string
field :note, :string
field :enabled, :boolean, default: true
field :valid_until, :utc_datetime
field :valid_until, PhilomenaQuery.Ecto.RelativeDate
field :generated_ban_id, :string
field :override_ip_ban, :boolean, default: false

field :username, :string, virtual: true
field :until, :string, virtual: true

timestamps(inserted_at: :created_at, type: :utc_datetime)
end

@doc false
def changeset(user_ban, attrs) do
user_ban
|> cast(attrs, [])
|> Time.propagate_time(:valid_until, :until)
|> populate_username()
end

def save_changeset(user_ban, attrs) do
user_ban
|> cast(attrs, [:reason, :note, :enabled, :override_ip_ban, :username, :until])
|> Time.assign_time(:until, :valid_until)
|> populate_user_id()
|> BanId.put_ban_id("U")
|> cast(attrs, [:reason, :note, :enabled, :override_ip_ban, :user_id, :valid_until])
|> put_ban_id("U")
|> validate_required([:reason, :enabled, :user_id, :valid_until])
|> check_constraint(:valid_until, name: :user_ban_duration_must_be_valid)
end

defp populate_username(changeset) do
case maybe_get_by(:id, get_field(changeset, :user_id)) do
nil -> changeset
user -> put_change(changeset, :username, user.name)
end
end

defp populate_user_id(changeset) do
case maybe_get_by(:name, get_field(changeset, :username)) do
nil -> changeset
%{id: id} -> put_change(changeset, :user_id, id)
end
end

defp maybe_get_by(_field, nil), do: nil
defp maybe_get_by(field, value), do: Repo.get_by(User, [{field, value}])
end
2 changes: 1 addition & 1 deletion lib/philomena/comments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ defmodule Philomena.Comments 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_reason = comment.edit_reason

Expand Down
4 changes: 2 additions & 2 deletions lib/philomena/comments/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ defmodule Philomena.Comments.Query do
|> Parser.parse(query_string, context)
end

def compile(user, query_string) do
query_string = query_string || ""
def compile(query_string, opts \\ []) do
user = Keyword.get(opts, :user)

case user do
nil ->
Expand Down
3 changes: 1 addition & 2 deletions lib/philomena/conversations/conversation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ defmodule Philomena.Conversations.Conversation do
end

defp set_last_message(changeset) do
changeset
|> change(last_message_at: DateTime.utc_now() |> DateTime.truncate(:second))
change(changeset, last_message_at: DateTime.utc_now(:second))
end

defp put_recipient(changeset) do
Expand Down
7 changes: 4 additions & 3 deletions lib/philomena/filters/filter.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
defmodule Philomena.Filters.Filter do
use Ecto.Schema
import Ecto.Changeset
import PhilomenaQuery.Ecto.QueryValidator

alias Philomena.Schema.TagList
alias Philomena.Schema.Search
alias Philomena.Images.Query
alias Philomena.Users.User
alias Philomena.Repo

Expand Down Expand Up @@ -48,8 +49,8 @@ defmodule Philomena.Filters.Filter do
|> validate_required([:name])
|> validate_my_downvotes(:spoilered_complex_str)
|> validate_my_downvotes(:hidden_complex_str)
|> Search.validate_search(:spoilered_complex_str, user)
|> Search.validate_search(:hidden_complex_str, user)
|> validate_query(:spoilered_complex_str, &Query.compile(&1, user: user))
|> validate_query(:hidden_complex_str, &Query.compile(&1, user: user))
|> unsafe_validate_unique([:user_id, :name], Repo)
end

Expand Down
4 changes: 2 additions & 2 deletions lib/philomena/filters/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ defmodule Philomena.Filters.Query do
|> Parser.parse(query_string, context)
end

def compile(user, query_string) do
query_string = query_string || ""
def compile(query_string, opts \\ []) do
user = Keyword.get(opts, :user)

case user do
nil ->
Expand Down
Loading

0 comments on commit ea20003

Please sign in to comment.