defmodule GitGud.Issues.IssueComment do
use Ecto.Schema
import Ecto.Changeset
alias GitGud.Accounts.User
alias GitGud.Issues.Issue
schema "issue_comments" do
field :body, :string
field :moderated_at, :utc_datetime
field :moderation_note, :string
field :original_body, :string
belongs_to :issue, Issue
belongs_to :author, User
belongs_to :moderated_by, User
timestamps(type: :utc_datetime)
end
def changeset(comment, attrs) do
comment
|> cast(attrs, [:body])
|> validate_required([:body])
|> validate_length(:body, min: 1, max: 65_535)
end
def moderate_changeset(comment, moderator, note) do
original = comment.original_body || comment.body
change(comment, %{
original_body: original,
body: "",
moderation_note: note,
moderated_by_id: moderator.id,
moderated_at: DateTime.utc_now(:second)
})
end
def restore_changeset(comment) do
change(comment, %{
body: comment.original_body || comment.body,
original_body: nil,
moderation_note: nil,
moderated_by_id: nil,
moderated_at: nil
})
end
def moderated?(%__MODULE__{moderated_at: nil}), do: false
def moderated?(%__MODULE__{}), do: true
end
1.2 KiB · text
5af55d9