From 3c1a25d5dce62cd172ee3e9445c414d5f53ba2fe Mon Sep 17 00:00:00 2001 From: Luna D Date: Tue, 28 Sep 2021 23:28:49 +0200 Subject: [PATCH] on the fly md conversion --- lib/philomena/badges/badge.ex | 5 ++++- lib/philomena/channels/channel.ex | 6 ++++-- lib/philomena/comments/comment.ex | 4 ++++ lib/philomena/commissions/commission.ex | 11 +++++++++-- lib/philomena/commissions/item.ex | 3 +++ lib/philomena/conversations/message.ex | 2 ++ lib/philomena/filters/filter.ex | 5 ++++- lib/philomena/galleries/gallery.ex | 4 +++- lib/philomena/images/image.ex | 4 ++++ lib/philomena/markdown_writer.ex | 13 +++++++++++++ lib/philomena/mod_notes/mod_note.ex | 2 ++ lib/philomena/posts/post.ex | 5 +++++ lib/philomena/reports/report.ex | 6 +++++- lib/philomena/tags/tag.ex | 3 +++ lib/philomena/users/user.ex | 3 +++ 15 files changed, 68 insertions(+), 8 deletions(-) create mode 100644 lib/philomena/markdown_writer.ex diff --git a/lib/philomena/badges/badge.ex b/lib/philomena/badges/badge.ex index 88376d43..3d5ecff3 100644 --- a/lib/philomena/badges/badge.ex +++ b/lib/philomena/badges/badge.ex @@ -3,9 +3,11 @@ defmodule Philomena.Badges.Badge do import Ecto.Changeset schema "badges" do + # fixme: unneeded field + field :description_md, :string, default: "" + field :title, :string field :description, :string, default: "" - field :description_md, :string, default: "" field :image, :string field :disable_award, :boolean, default: false field :priority, :boolean, default: false @@ -22,6 +24,7 @@ defmodule Philomena.Badges.Badge do badge |> cast(attrs, [:title, :description, :disable_award, :priority]) |> validate_required([:title]) + |> validate_length(:description, max: 1000, count: :bytes) end def image_changeset(badge, attrs) do diff --git a/lib/philomena/channels/channel.ex b/lib/philomena/channels/channel.ex index 9c93b83c..01a4796b 100644 --- a/lib/philomena/channels/channel.ex +++ b/lib/philomena/channels/channel.ex @@ -11,10 +11,12 @@ defmodule Philomena.Channels.Channel do # fixme: rails STI field :type, :string - field :short_name, :string - field :title, :string, default: "" + # fixme: this is unused field :description, :string field :description_md, :string + + field :short_name, :string + field :title, :string, default: "" field :tags, :string field :viewers, :integer, default: 0 field :nsfw, :boolean, default: false diff --git a/lib/philomena/comments/comment.ex b/lib/philomena/comments/comment.ex index f7ee6f5d..bdeafb79 100644 --- a/lib/philomena/comments/comment.ex +++ b/lib/philomena/comments/comment.ex @@ -1,6 +1,7 @@ defmodule Philomena.Comments.Comment do use Ecto.Schema import Ecto.Changeset + import Philomena.MarkdownWriter alias Philomena.Images.Image alias Philomena.Users.User @@ -35,6 +36,7 @@ defmodule Philomena.Comments.Comment do |> validate_length(:body, min: 1, max: 300_000, count: :bytes) |> change(attribution) |> put_name_at_post_time(attribution[:user]) + |> put_markdown(attrs, :body, :body_md) end def changeset(comment, attrs, edited_at \\ nil) do @@ -44,6 +46,7 @@ defmodule Philomena.Comments.Comment do |> validate_required([:body]) |> validate_length(:body, min: 1, max: 300_000, count: :bytes) |> validate_length(:edit_reason, max: 70, count: :bytes) + |> put_markdown(attrs, :body, :body_md) end def hide_changeset(comment, attrs, user) do @@ -64,6 +67,7 @@ defmodule Philomena.Comments.Comment do change(comment) |> put_change(:destroyed_content, true) |> put_change(:body, "") + |> put_change(:body_md, "") end defp put_name_at_post_time(changeset, nil), do: changeset diff --git a/lib/philomena/commissions/commission.ex b/lib/philomena/commissions/commission.ex index c27fdbb2..8db992f7 100644 --- a/lib/philomena/commissions/commission.ex +++ b/lib/philomena/commissions/commission.ex @@ -1,6 +1,7 @@ defmodule Philomena.Commissions.Commission do use Ecto.Schema import Ecto.Changeset + import Philomena.MarkdownWriter alias Philomena.Commissions.Item alias Philomena.Images.Image @@ -40,9 +41,15 @@ defmodule Philomena.Commissions.Commission do ]) |> drop_blank_categories() |> validate_required([:user_id, :information, :contact, :open]) - |> validate_length(:information, max: 700, count: :bytes) - |> validate_length(:contact, max: 700, count: :bytes) + |> validate_length(:information, max: 1000, 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())) + |> 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 defp drop_blank_categories(changeset) do diff --git a/lib/philomena/commissions/item.ex b/lib/philomena/commissions/item.ex index f8b10c4d..f67d6283 100644 --- a/lib/philomena/commissions/item.ex +++ b/lib/philomena/commissions/item.ex @@ -1,6 +1,7 @@ defmodule Philomena.Commissions.Item do use Ecto.Schema import Ecto.Changeset + import Philomena.MarkdownWriter alias Philomena.Commissions.Commission 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_inclusion(:item_type, Commission.types()) |> 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 diff --git a/lib/philomena/conversations/message.ex b/lib/philomena/conversations/message.ex index 365ad78b..d29c0d8a 100644 --- a/lib/philomena/conversations/message.ex +++ b/lib/philomena/conversations/message.ex @@ -1,6 +1,7 @@ defmodule Philomena.Conversations.Message do use Ecto.Schema import Ecto.Changeset + import Philomena.MarkdownWriter alias Philomena.Conversations.Conversation alias Philomena.Users.User @@ -29,5 +30,6 @@ defmodule Philomena.Conversations.Message do |> validate_required([:body]) |> put_assoc(:from, user) |> validate_length(:body, max: 300_000, count: :bytes) + |> put_markdown(attrs, :body, :body_md) end end diff --git a/lib/philomena/filters/filter.ex b/lib/philomena/filters/filter.ex index e00dbcba..fe92418e 100644 --- a/lib/philomena/filters/filter.ex +++ b/lib/philomena/filters/filter.ex @@ -10,9 +10,11 @@ defmodule Philomena.Filters.Filter do schema "filters" do belongs_to :user, User + # fixme: unneeded field + field :description_md, :string, default: "" + field :name, :string field :description, :string, default: "" - field :description_md, :string, default: "" field :system, :boolean field :public, :boolean field :hidden_complex_str, :string @@ -43,6 +45,7 @@ defmodule Philomena.Filters.Filter do :spoilered_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(:hidden_tag_list, :hidden_tag_ids) |> validate_required([:name]) diff --git a/lib/philomena/galleries/gallery.ex b/lib/philomena/galleries/gallery.ex index 8225bf19..c8b42bc2 100644 --- a/lib/philomena/galleries/gallery.ex +++ b/lib/philomena/galleries/gallery.ex @@ -14,10 +14,12 @@ defmodule Philomena.Galleries.Gallery do has_many :subscriptions, Subscription has_many :subscribers, through: [:subscriptions, :user] + # fixme: unneeded field + field :description_md, :string, default: "" + field :title, :string field :spoiler_warning, :string, default: "" field :description, :string, default: "" - field :description_md, :string, default: "" field :image_count, :integer field :order_position_asc, :boolean diff --git a/lib/philomena/images/image.ex b/lib/philomena/images/image.ex index a302d70a..1f261062 100644 --- a/lib/philomena/images/image.ex +++ b/lib/philomena/images/image.ex @@ -3,6 +3,7 @@ defmodule Philomena.Images.Image do import Ecto.Changeset import Ecto.Query + import Philomena.MarkdownWriter alias Philomena.ImageIntensities.ImageIntensity alias Philomena.ImageVotes.ImageVote @@ -122,6 +123,7 @@ defmodule Philomena.Images.Image do |> change(first_seen_at: now) |> change(attribution) |> validate_length(:description, max: 50_000, count: :bytes) + |> put_markdown(attrs, :description, :description_md) |> validate_format(:source_url, ~r/\Ahttps?:\/\//) end @@ -218,6 +220,7 @@ defmodule Philomena.Images.Image do image |> cast(attrs, [:description]) |> validate_length(:description, max: 50_000, count: :bytes) + |> put_markdown(attrs, :description, :description_md) end def hide_changeset(image, attrs, user) do @@ -272,6 +275,7 @@ defmodule Philomena.Images.Image do def scratchpad_changeset(image, attrs) do cast(image, attrs, [:scratchpad]) + |> put_markdown(attrs, :scratchpad, :scratchpad_md) end def remove_source_history_changeset(image) do diff --git a/lib/philomena/markdown_writer.ex b/lib/philomena/markdown_writer.ex new file mode 100644 index 00000000..8fcc5535 --- /dev/null +++ b/lib/philomena/markdown_writer.ex @@ -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 diff --git a/lib/philomena/mod_notes/mod_note.ex b/lib/philomena/mod_notes/mod_note.ex index acc3555b..76b05f76 100644 --- a/lib/philomena/mod_notes/mod_note.ex +++ b/lib/philomena/mod_notes/mod_note.ex @@ -1,6 +1,7 @@ defmodule Philomena.ModNotes.ModNote do use Ecto.Schema import Ecto.Changeset + import Philomena.MarkdownWriter alias Philomena.Users.User @@ -25,5 +26,6 @@ defmodule Philomena.ModNotes.ModNote do |> cast(attrs, [:notable_id, :notable_type, :body]) |> validate_required([:notable_id, :notable_type, :body]) |> validate_inclusion(:notable_type, ["User", "Report", "DnpEntry"]) + |> put_markdown(attrs, :body, :body_md) end end diff --git a/lib/philomena/posts/post.ex b/lib/philomena/posts/post.ex index 1e2c2b96..8badb384 100644 --- a/lib/philomena/posts/post.ex +++ b/lib/philomena/posts/post.ex @@ -1,6 +1,7 @@ defmodule Philomena.Posts.Post do use Ecto.Schema import Ecto.Changeset + import Philomena.MarkdownWriter alias Philomena.Users.User alias Philomena.Topics.Topic @@ -36,6 +37,7 @@ defmodule Philomena.Posts.Post do |> validate_required([:body]) |> validate_length(:body, min: 1, max: 300_000, count: :bytes) |> validate_length(:edit_reason, max: 70, count: :bytes) + |> put_markdown(attrs, :body, :body_md) end @doc false @@ -46,6 +48,7 @@ defmodule Philomena.Posts.Post do |> validate_length(:body, min: 1, max: 300_000, count: :bytes) |> change(attribution) |> put_name_at_post_time(attribution[:user]) + |> put_markdown(attrs, :body, :body_md) end @doc false @@ -58,6 +61,7 @@ defmodule Philomena.Posts.Post do |> change(attribution) |> change(topic_position: 0) |> put_name_at_post_time(attribution[:user]) + |> put_markdown(attrs, :body, :body_md) end def hide_changeset(post, attrs, user) do @@ -78,6 +82,7 @@ defmodule Philomena.Posts.Post do change(post) |> put_change(:destroyed_content, true) |> put_change(:body, "") + |> put_change(:body_md, "") end defp put_name_at_post_time(changeset, nil), do: changeset diff --git a/lib/philomena/reports/report.ex b/lib/philomena/reports/report.ex index 5fffe5ec..1061078f 100644 --- a/lib/philomena/reports/report.ex +++ b/lib/philomena/reports/report.ex @@ -1,6 +1,7 @@ defmodule Philomena.Reports.Report do use Ecto.Schema import Ecto.Changeset + import Philomena.MarkdownWriter alias Philomena.Users.User @@ -62,6 +63,7 @@ defmodule Philomena.Reports.Report do def creation_changeset(report, attrs, attribution) do report |> cast(attrs, [:category, :reason]) + |> validate_length(:reason, max: 10_000, count: :bytes) |> merge_category() |> change(attribution) |> validate_required([ @@ -78,9 +80,11 @@ defmodule Philomena.Reports.Report do defp merge_category(changeset) do reason = get_field(changeset, :reason) category = get_field(changeset, :category) + new_reason = joiner(category, reason) changeset - |> change(reason: joiner(category, reason)) + |> change(reason: new_reason) + |> put_markdown(%{reason: new_reason}, :reason, :reason_md) end defp joiner(category, ""), do: category diff --git a/lib/philomena/tags/tag.ex b/lib/philomena/tags/tag.ex index fe5e9491..1f255c35 100644 --- a/lib/philomena/tags/tag.ex +++ b/lib/philomena/tags/tag.ex @@ -2,6 +2,7 @@ defmodule Philomena.Tags.Tag do use Ecto.Schema import Ecto.Changeset import Ecto.Query + import Philomena.MarkdownWriter alias Philomena.Channels.Channel alias Philomena.DnpEntries.DnpEntry @@ -100,6 +101,7 @@ defmodule Philomena.Tags.Tag do |> cast(attrs, [:category, :description, :short_description, :mod_notes]) |> put_change(:implied_tag_list, Enum.map_join(tag.implied_tags, ",", & &1.name)) |> validate_required([]) + |> put_markdown(attrs, :description, :description_md) end def changeset(tag, attrs, implied_tags) do @@ -107,6 +109,7 @@ defmodule Philomena.Tags.Tag do |> cast(attrs, [:category, :description, :short_description, :mod_notes]) |> put_assoc(:implied_tags, implied_tags) |> validate_required([]) + |> put_markdown(attrs, :description, :description_md) end def image_changeset(tag, attrs) do diff --git a/lib/philomena/users/user.ex b/lib/philomena/users/user.ex index bc61db5c..07d50520 100644 --- a/lib/philomena/users/user.ex +++ b/lib/philomena/users/user.ex @@ -4,6 +4,7 @@ defmodule Philomena.Users.User do use Ecto.Schema import Ecto.Changeset + import Philomena.MarkdownWriter alias Philomena.Schema.TagList alias Philomena.Schema.Search @@ -365,6 +366,7 @@ defmodule Philomena.Users.User do |> cast(attrs, [:description, :personal_title]) |> validate_length(:description, max: 10_000, count: :bytes) |> validate_length(:personal_title, max: 24, count: :bytes) + |> put_markdown(attrs, :description, :description_md) |> validate_format( :personal_title, ~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 user |> cast(attrs, [:scratchpad]) + |> put_markdown(attrs, :scratchpad, :scratchpad_md) end def name_changeset(user, attrs) do