2.0 KiB · text 5af55d9
defmodule GitGud.PullRequests.PrComment do
use Ecto.Schema
import Ecto.Changeset
alias GitGud.Accounts.User
alias GitGud.Federation.Actor
alias GitGud.PullRequests.PullRequest
schema "pr_comments" do
field :body, :string
field :file_path, :string
field :line, :integer
field :commit_sha, :binary
# Set when the comment arrived via an inbound `Create(Note)` activity.
field :activity_url, :string
field :moderated_at, :utc_datetime
field :moderation_note, :string
field :original_body, :string
belongs_to :pull_request, PullRequest
belongs_to :author, User
belongs_to :source_actor, Actor
belongs_to :moderated_by, User
timestamps(type: :utc_datetime)
end
def changeset(comment, attrs) do
comment
|> cast(attrs, [:body, :file_path, :line, :commit_sha])
|> validate_required([:body])
|> validate_length(:body, min: 1, max: 65_535)
end
@doc "Changeset for a comment created from an inbound federated Note."
def federated_changeset(comment, attrs) do
comment
|> cast(attrs, [:body, :activity_url, :source_actor_id, :pull_request_id])
|> validate_required([:body, :source_actor_id, :pull_request_id])
|> validate_length(:body, max: 65_535)
|> unique_constraint(:activity_url, name: :pr_comments_activity_url_index)
end
def federated?(%__MODULE__{source_actor_id: sid}) when is_integer(sid), do: true
def federated?(_), do: false
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