docbrown/app/services/articles/builder.rb
Anna Buianova ff76cdd3c5
Scheduling articles (#17939)
* Article query spec for scheduled articles

* Added scheduled article badge on the user dashboard

* Added published_at field to editor options

* Accept and validate published_at from editor

* Refactor published_at validation

* Allow 1-minute difference in published_at

* Notice on an unpublished article page

* Added specs for 'Click to edit' link on scheduled article preview page

* ContextNotification model

* Articles::Publish worker

* Added specs for articles publish worker

* Schedule publish articles worker

* Added tests to check for scheduled posts in feeds

* Don't allow managing scheduled articles

* Don't send notifications for scheduled articles

* Set published_at in Articles::Updater when publishing

* Published_at value in post options

* Pass timezone and set published_at accordingly

* Limit setting published_at to the future

* Readonly published_at for articles that were already published

* Chagning published_at format in editor v1 (start)

* Changed published_at format in frontmatter, specs

* Added specs for updating published_at from frontmatter

* Fixed accepting past published_at for articles published_from_feed

* Enabled published_at validation: don't allow updating published_at for already published articles

* Validate published_at on create

* Added a spec for updating published_at for exported articles

* Fixed specs related to creating articles with past published_at

* Fixed specs related to past published_at for articles

* Added a hack so that admins would be able to update published_at

* Switch button text schedule/publish when changin publishedAt

* Fixed saving published_at with timezone

* Added a feature flag for scheduling articles

* Default text in markdown editor depends on feature flag

* Enable article editor cache again

* Fixed the default value in the markdown editor

* Fix sitemaps spec

* Removed tooltip

* Fixed articles update specs

* Added missing locales

* Fixed article create specs

* Fixed spec

* Removed commented code

* Returned enabling extensions in the schema

* Returned accidentally deleted constraint

* Make articles query spec more stable

Co-authored-by: Jeremy Friesen <jeremy.n.friesen@gmail.com>

* Removed commented code

* Removed unused code

* A clearer policy

Co-authored-by: Jeremy Friesen <jeremy.n.friesen@gmail.com>

* Use StringInquirer for article current state

* Added a note and todo to articles factory past trait

* Remove duplicated PropType

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>

* Refactor query in the Articles::PublishWorker

Co-authored-by: Mac Siri <krairit.siri@gmail.com>

* Refactor articleForm.jsx

Co-authored-by: Mac Siri <krairit.siri@gmail.com>

* Removed specs that are no longer relevant

* Removed useless onKeyUp on a hidden input

* Refactored articleForm

* Hide scheduling from post options when published_at is readonly

* Run sends notifications worker every 5 minutes instead of every minute

Co-authored-by: Jeremy Friesen <jeremy.n.friesen@gmail.com>
Co-authored-by: Suzanne Aitchison <suzanne@forem.com>
Co-authored-by: Mac Siri <krairit.siri@gmail.com>
2022-07-07 17:32:49 +03:00

103 lines
2.8 KiB
Ruby

module Articles
class Builder
LINE_BREAK = "\n".freeze
def initialize(user, tag, prefill)
@user = user
@tag = tag
@prefill = prefill
@editor_version2 = @user&.setting&.editor_version == "v2"
end
def self.call(...)
new(...).call
end
# the Builder returns a pair of [article, needs_authorization?]
# => `needs_authorization? can be either true or false
def call
return [tag_user_editor_v2, true] if tag && editor_version2
return [tag_user, true] if tag&.submission_template.present? && user
return [prefill_user_editor_v2, true] if prefill.present? && editor_version2
return [prefill_user, true] if prefill.present? && user
return [tag_article, false] if tag
return [user_editor_v2, false] if editor_version2
[user_editor_v1, false]
end
private
attr_reader :user, :tag, :prefill, :editor_version2
def tag_user_editor_v2
submission_template = tag.submission_template_customized(user.name).to_s
Article.new(
body_markdown: submission_template.split("---").last.to_s.strip,
cached_tag_list: tag.name,
processed_html: "",
user_id: user.id,
title: normalized_text(submission_template, "title:"),
)
end
def tag_user
Article.new(
body_markdown: tag.submission_template_customized(user.name),
processed_html: "",
user_id: user.id,
)
end
def prefill_user_editor_v2
Article.new(
body_markdown: prefill.split("---").last.to_s.strip,
cached_tag_list: normalized_text(prefill, "tags:"),
processed_html: "",
user_id: user.id,
title: normalized_text(prefill, "title:"),
)
end
def prefill_user
Article.new(
body_markdown: prefill,
processed_html: "",
user_id: user.id,
)
end
def tag_article
Article.new(
body_markdown: "---\ntitle: \npublished: false\ndescription: \ntags: #{tag.name}\n---\n\n",
processed_html: "",
user_id: user&.id,
)
end
def user_editor_v2
Article.new(user_id: user.id)
end
def user_editor_v1
published_at = Time.current.strftime("%Y-%m-%d %H:%M %z")
published_at_str = FeatureFlag.enabled?(:schedule_articles) ? "# published_at: #{published_at}\n" : ""
body = "---\ntitle: \npublished: false\ndescription: " \
"\ntags: \n# cover_image: https://direct_url_to_image.jpg" \
"\n# Use a ratio of 100:42 for best results.\n#{published_at_str}---\n\n"
Article.new(
body_markdown: body,
processed_html: "",
user_id: user&.id,
)
end
def normalized_text(source, split_pattern)
text = source.split(split_pattern).second.to_s
text.split(LINE_BREAK).first.to_s.strip
end
end
end