on the fly md conversion

This commit is contained in:
Luna D 2021-09-28 23:28:49 +02:00
parent fe07a6b5ea
commit 3c1a25d5dc
No known key found for this signature in database
GPG key ID: 81AF416F2CC36FC8
15 changed files with 68 additions and 8 deletions

View file

@ -3,9 +3,11 @@ defmodule Philomena.Badges.Badge do
import Ecto.Changeset import Ecto.Changeset
schema "badges" do schema "badges" do
# fixme: unneeded field
field :description_md, :string, default: ""
field :title, :string field :title, :string
field :description, :string, default: "" field :description, :string, default: ""
field :description_md, :string, default: ""
field :image, :string field :image, :string
field :disable_award, :boolean, default: false field :disable_award, :boolean, default: false
field :priority, :boolean, default: false field :priority, :boolean, default: false
@ -22,6 +24,7 @@ defmodule Philomena.Badges.Badge do
badge badge
|> cast(attrs, [:title, :description, :disable_award, :priority]) |> cast(attrs, [:title, :description, :disable_award, :priority])
|> validate_required([:title]) |> validate_required([:title])
|> validate_length(:description, max: 1000, count: :bytes)
end end
def image_changeset(badge, attrs) do def image_changeset(badge, attrs) do

View file

@ -11,10 +11,12 @@ defmodule Philomena.Channels.Channel do
# fixme: rails STI # fixme: rails STI
field :type, :string field :type, :string
field :short_name, :string # fixme: this is unused
field :title, :string, default: ""
field :description, :string field :description, :string
field :description_md, :string field :description_md, :string
field :short_name, :string
field :title, :string, default: ""
field :tags, :string field :tags, :string
field :viewers, :integer, default: 0 field :viewers, :integer, default: 0
field :nsfw, :boolean, default: false field :nsfw, :boolean, default: false

View file

@ -1,6 +1,7 @@
defmodule Philomena.Comments.Comment do defmodule Philomena.Comments.Comment do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
import Philomena.MarkdownWriter
alias Philomena.Images.Image alias Philomena.Images.Image
alias Philomena.Users.User alias Philomena.Users.User
@ -35,6 +36,7 @@ defmodule Philomena.Comments.Comment do
|> validate_length(:body, min: 1, max: 300_000, count: :bytes) |> validate_length(:body, min: 1, max: 300_000, count: :bytes)
|> change(attribution) |> change(attribution)
|> put_name_at_post_time(attribution[:user]) |> put_name_at_post_time(attribution[:user])
|> put_markdown(attrs, :body, :body_md)
end end
def changeset(comment, attrs, edited_at \\ nil) do def changeset(comment, attrs, edited_at \\ nil) do
@ -44,6 +46,7 @@ defmodule Philomena.Comments.Comment do
|> validate_required([:body]) |> validate_required([:body])
|> validate_length(:body, min: 1, max: 300_000, count: :bytes) |> validate_length(:body, min: 1, max: 300_000, count: :bytes)
|> validate_length(:edit_reason, max: 70, count: :bytes) |> validate_length(:edit_reason, max: 70, count: :bytes)
|> put_markdown(attrs, :body, :body_md)
end end
def hide_changeset(comment, attrs, user) do def hide_changeset(comment, attrs, user) do
@ -64,6 +67,7 @@ defmodule Philomena.Comments.Comment do
change(comment) change(comment)
|> put_change(:destroyed_content, true) |> put_change(:destroyed_content, true)
|> put_change(:body, "") |> put_change(:body, "")
|> put_change(:body_md, "")
end end
defp put_name_at_post_time(changeset, nil), do: changeset defp put_name_at_post_time(changeset, nil), do: changeset

View file

@ -1,6 +1,7 @@
defmodule Philomena.Commissions.Commission do defmodule Philomena.Commissions.Commission do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
import Philomena.MarkdownWriter
alias Philomena.Commissions.Item alias Philomena.Commissions.Item
alias Philomena.Images.Image alias Philomena.Images.Image
@ -40,9 +41,15 @@ defmodule Philomena.Commissions.Commission do
]) ])
|> drop_blank_categories() |> drop_blank_categories()
|> validate_required([:user_id, :information, :contact, :open]) |> validate_required([:user_id, :information, :contact, :open])
|> validate_length(:information, max: 700, count: :bytes) |> validate_length(:information, max: 1000, count: :bytes)
|> validate_length(:contact, max: 700, count: :bytes) |> validate_length(:contact, max: 1000, count: :bytes)
|> validate_length(:will_create, max: 1000, count: :bytes)
|> validate_length(:will_not_create, max: 1000, count: :bytes)
|> validate_subset(:categories, Keyword.values(categories())) |> validate_subset(:categories, Keyword.values(categories()))
|> put_markdown(attrs, :information, :information_md)
|> put_markdown(attrs, :contact, :contact_md)
|> put_markdown(attrs, :will_create, :will_create_md)
|> put_markdown(attrs, :will_not_create, :will_not_create_md)
end end
defp drop_blank_categories(changeset) do defp drop_blank_categories(changeset) do

View file

@ -1,6 +1,7 @@
defmodule Philomena.Commissions.Item do defmodule Philomena.Commissions.Item do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
import Philomena.MarkdownWriter
alias Philomena.Commissions.Commission alias Philomena.Commissions.Commission
alias Philomena.Images.Image alias Philomena.Images.Image
@ -29,5 +30,7 @@ defmodule Philomena.Commissions.Item do
|> validate_number(:base_price, greater_than_or_equal_to: 0, less_than_or_equal_to: 99_999) |> validate_number(:base_price, greater_than_or_equal_to: 0, less_than_or_equal_to: 99_999)
|> validate_inclusion(:item_type, Commission.types()) |> validate_inclusion(:item_type, Commission.types())
|> foreign_key_constraint(:example_image_id, name: :fk_rails_56d368749a) |> foreign_key_constraint(:example_image_id, name: :fk_rails_56d368749a)
|> put_markdown(attrs, :description, :description_md)
|> put_markdown(attrs, :add_ons, :add_ons_md)
end end
end end

View file

@ -1,6 +1,7 @@
defmodule Philomena.Conversations.Message do defmodule Philomena.Conversations.Message do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
import Philomena.MarkdownWriter
alias Philomena.Conversations.Conversation alias Philomena.Conversations.Conversation
alias Philomena.Users.User alias Philomena.Users.User
@ -29,5 +30,6 @@ defmodule Philomena.Conversations.Message do
|> validate_required([:body]) |> validate_required([:body])
|> put_assoc(:from, user) |> put_assoc(:from, user)
|> validate_length(:body, max: 300_000, count: :bytes) |> validate_length(:body, max: 300_000, count: :bytes)
|> put_markdown(attrs, :body, :body_md)
end end
end end

View file

@ -10,9 +10,11 @@ defmodule Philomena.Filters.Filter do
schema "filters" do schema "filters" do
belongs_to :user, User belongs_to :user, User
# fixme: unneeded field
field :description_md, :string, default: ""
field :name, :string field :name, :string
field :description, :string, default: "" field :description, :string, default: ""
field :description_md, :string, default: ""
field :system, :boolean field :system, :boolean
field :public, :boolean field :public, :boolean
field :hidden_complex_str, :string field :hidden_complex_str, :string
@ -43,6 +45,7 @@ defmodule Philomena.Filters.Filter do
:spoilered_complex_str, :spoilered_complex_str,
:hidden_complex_str :hidden_complex_str
]) ])
|> validate_length(:description, max: 10_000, count: :bytes)
|> TagList.propagate_tag_list(:spoilered_tag_list, :spoilered_tag_ids) |> TagList.propagate_tag_list(:spoilered_tag_list, :spoilered_tag_ids)
|> TagList.propagate_tag_list(:hidden_tag_list, :hidden_tag_ids) |> TagList.propagate_tag_list(:hidden_tag_list, :hidden_tag_ids)
|> validate_required([:name]) |> validate_required([:name])

View file

@ -14,10 +14,12 @@ defmodule Philomena.Galleries.Gallery do
has_many :subscriptions, Subscription has_many :subscriptions, Subscription
has_many :subscribers, through: [:subscriptions, :user] has_many :subscribers, through: [:subscriptions, :user]
# fixme: unneeded field
field :description_md, :string, default: ""
field :title, :string field :title, :string
field :spoiler_warning, :string, default: "" field :spoiler_warning, :string, default: ""
field :description, :string, default: "" field :description, :string, default: ""
field :description_md, :string, default: ""
field :image_count, :integer field :image_count, :integer
field :order_position_asc, :boolean field :order_position_asc, :boolean

View file

@ -3,6 +3,7 @@ defmodule Philomena.Images.Image do
import Ecto.Changeset import Ecto.Changeset
import Ecto.Query import Ecto.Query
import Philomena.MarkdownWriter
alias Philomena.ImageIntensities.ImageIntensity alias Philomena.ImageIntensities.ImageIntensity
alias Philomena.ImageVotes.ImageVote alias Philomena.ImageVotes.ImageVote
@ -122,6 +123,7 @@ defmodule Philomena.Images.Image do
|> change(first_seen_at: now) |> change(first_seen_at: now)
|> change(attribution) |> change(attribution)
|> validate_length(:description, max: 50_000, count: :bytes) |> validate_length(:description, max: 50_000, count: :bytes)
|> put_markdown(attrs, :description, :description_md)
|> validate_format(:source_url, ~r/\Ahttps?:\/\//) |> validate_format(:source_url, ~r/\Ahttps?:\/\//)
end end
@ -218,6 +220,7 @@ defmodule Philomena.Images.Image do
image image
|> cast(attrs, [:description]) |> cast(attrs, [:description])
|> validate_length(:description, max: 50_000, count: :bytes) |> validate_length(:description, max: 50_000, count: :bytes)
|> put_markdown(attrs, :description, :description_md)
end end
def hide_changeset(image, attrs, user) do def hide_changeset(image, attrs, user) do
@ -272,6 +275,7 @@ defmodule Philomena.Images.Image do
def scratchpad_changeset(image, attrs) do def scratchpad_changeset(image, attrs) do
cast(image, attrs, [:scratchpad]) cast(image, attrs, [:scratchpad])
|> put_markdown(attrs, :scratchpad, :scratchpad_md)
end end
def remove_source_history_changeset(image) do def remove_source_history_changeset(image) do

View file

@ -0,0 +1,13 @@
defmodule Philomena.MarkdownWriter do
import Ecto.Changeset
alias PhilomenaWeb.TextileMarkdownRenderer
def put_markdown(obj, attrs, field, field_md) do
IO.inspect(attrs)
val = attrs[field] || attrs[to_string(field)] || ""
md = TextileMarkdownRenderer.render_one(%{body: val})
obj
|> put_change(field_md, md)
end
end

View file

@ -1,6 +1,7 @@
defmodule Philomena.ModNotes.ModNote do defmodule Philomena.ModNotes.ModNote do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
import Philomena.MarkdownWriter
alias Philomena.Users.User alias Philomena.Users.User
@ -25,5 +26,6 @@ defmodule Philomena.ModNotes.ModNote do
|> cast(attrs, [:notable_id, :notable_type, :body]) |> cast(attrs, [:notable_id, :notable_type, :body])
|> validate_required([:notable_id, :notable_type, :body]) |> validate_required([:notable_id, :notable_type, :body])
|> validate_inclusion(:notable_type, ["User", "Report", "DnpEntry"]) |> validate_inclusion(:notable_type, ["User", "Report", "DnpEntry"])
|> put_markdown(attrs, :body, :body_md)
end end
end end

View file

@ -1,6 +1,7 @@
defmodule Philomena.Posts.Post do defmodule Philomena.Posts.Post do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
import Philomena.MarkdownWriter
alias Philomena.Users.User alias Philomena.Users.User
alias Philomena.Topics.Topic alias Philomena.Topics.Topic
@ -36,6 +37,7 @@ defmodule Philomena.Posts.Post do
|> validate_required([:body]) |> validate_required([:body])
|> validate_length(:body, min: 1, max: 300_000, count: :bytes) |> validate_length(:body, min: 1, max: 300_000, count: :bytes)
|> validate_length(:edit_reason, max: 70, count: :bytes) |> validate_length(:edit_reason, max: 70, count: :bytes)
|> put_markdown(attrs, :body, :body_md)
end end
@doc false @doc false
@ -46,6 +48,7 @@ defmodule Philomena.Posts.Post do
|> validate_length(:body, min: 1, max: 300_000, count: :bytes) |> validate_length(:body, min: 1, max: 300_000, count: :bytes)
|> change(attribution) |> change(attribution)
|> put_name_at_post_time(attribution[:user]) |> put_name_at_post_time(attribution[:user])
|> put_markdown(attrs, :body, :body_md)
end end
@doc false @doc false
@ -58,6 +61,7 @@ defmodule Philomena.Posts.Post do
|> change(attribution) |> change(attribution)
|> change(topic_position: 0) |> change(topic_position: 0)
|> put_name_at_post_time(attribution[:user]) |> put_name_at_post_time(attribution[:user])
|> put_markdown(attrs, :body, :body_md)
end end
def hide_changeset(post, attrs, user) do def hide_changeset(post, attrs, user) do
@ -78,6 +82,7 @@ defmodule Philomena.Posts.Post do
change(post) change(post)
|> put_change(:destroyed_content, true) |> put_change(:destroyed_content, true)
|> put_change(:body, "") |> put_change(:body, "")
|> put_change(:body_md, "")
end end
defp put_name_at_post_time(changeset, nil), do: changeset defp put_name_at_post_time(changeset, nil), do: changeset

View file

@ -1,6 +1,7 @@
defmodule Philomena.Reports.Report do defmodule Philomena.Reports.Report do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
import Philomena.MarkdownWriter
alias Philomena.Users.User alias Philomena.Users.User
@ -62,6 +63,7 @@ defmodule Philomena.Reports.Report do
def creation_changeset(report, attrs, attribution) do def creation_changeset(report, attrs, attribution) do
report report
|> cast(attrs, [:category, :reason]) |> cast(attrs, [:category, :reason])
|> validate_length(:reason, max: 10_000, count: :bytes)
|> merge_category() |> merge_category()
|> change(attribution) |> change(attribution)
|> validate_required([ |> validate_required([
@ -78,9 +80,11 @@ defmodule Philomena.Reports.Report do
defp merge_category(changeset) do defp merge_category(changeset) do
reason = get_field(changeset, :reason) reason = get_field(changeset, :reason)
category = get_field(changeset, :category) category = get_field(changeset, :category)
new_reason = joiner(category, reason)
changeset changeset
|> change(reason: joiner(category, reason)) |> change(reason: new_reason)
|> put_markdown(%{reason: new_reason}, :reason, :reason_md)
end end
defp joiner(category, ""), do: category defp joiner(category, ""), do: category

View file

@ -2,6 +2,7 @@ defmodule Philomena.Tags.Tag do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
import Ecto.Query import Ecto.Query
import Philomena.MarkdownWriter
alias Philomena.Channels.Channel alias Philomena.Channels.Channel
alias Philomena.DnpEntries.DnpEntry alias Philomena.DnpEntries.DnpEntry
@ -100,6 +101,7 @@ defmodule Philomena.Tags.Tag do
|> cast(attrs, [:category, :description, :short_description, :mod_notes]) |> cast(attrs, [:category, :description, :short_description, :mod_notes])
|> put_change(:implied_tag_list, Enum.map_join(tag.implied_tags, ",", & &1.name)) |> put_change(:implied_tag_list, Enum.map_join(tag.implied_tags, ",", & &1.name))
|> validate_required([]) |> validate_required([])
|> put_markdown(attrs, :description, :description_md)
end end
def changeset(tag, attrs, implied_tags) do def changeset(tag, attrs, implied_tags) do
@ -107,6 +109,7 @@ defmodule Philomena.Tags.Tag do
|> cast(attrs, [:category, :description, :short_description, :mod_notes]) |> cast(attrs, [:category, :description, :short_description, :mod_notes])
|> put_assoc(:implied_tags, implied_tags) |> put_assoc(:implied_tags, implied_tags)
|> validate_required([]) |> validate_required([])
|> put_markdown(attrs, :description, :description_md)
end end
def image_changeset(tag, attrs) do def image_changeset(tag, attrs) do

View file

@ -4,6 +4,7 @@ defmodule Philomena.Users.User do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
import Philomena.MarkdownWriter
alias Philomena.Schema.TagList alias Philomena.Schema.TagList
alias Philomena.Schema.Search alias Philomena.Schema.Search
@ -365,6 +366,7 @@ defmodule Philomena.Users.User do
|> cast(attrs, [:description, :personal_title]) |> cast(attrs, [:description, :personal_title])
|> validate_length(:description, max: 10_000, count: :bytes) |> validate_length(:description, max: 10_000, count: :bytes)
|> validate_length(:personal_title, max: 24, count: :bytes) |> validate_length(:personal_title, max: 24, count: :bytes)
|> put_markdown(attrs, :description, :description_md)
|> validate_format( |> validate_format(
:personal_title, :personal_title,
~r/\A((?!site|admin|moderator|assistant|developer|\p{C}).)*\z/iu ~r/\A((?!site|admin|moderator|assistant|developer|\p{C}).)*\z/iu
@ -374,6 +376,7 @@ defmodule Philomena.Users.User do
def scratchpad_changeset(user, attrs) do def scratchpad_changeset(user, attrs) do
user user
|> cast(attrs, [:scratchpad]) |> cast(attrs, [:scratchpad])
|> put_markdown(attrs, :scratchpad, :scratchpad_md)
end end
def name_changeset(user, attrs) do def name_changeset(user, attrs) do