docbrown/app/controllers/admin/articles_controller.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

131 lines
3.6 KiB
Ruby

module Admin
class ArticlesController < Admin::ApplicationController
layout "admin"
after_action only: %i[update unpin] do
Audit::Logger.log(:moderator, current_user, params.dup)
end
ARTICLES_ALLOWED_PARAMS = %i[featured
social_image
body_markdown
approved
email_digest_eligible
main_image_background_hex_color
featured_number
user_id
co_author_ids_list
published_at].freeze
def index
case params[:state]
when /top-/
months_ago = params[:state].split("-")[1].to_i.months.ago
@articles = articles_top(months_ago)
when "chronological"
@articles = articles_chronological
else
@articles = articles_mixed
@featured_articles = articles_featured
end
@pinned_article = PinnedArticle.get
@articles = @articles.where.not(id: @pinned_article) if @pinned_article
end
def show
@article = Article.find(params[:id])
end
def update
article = Article.find(params[:id])
if article.update(article_params.merge(admin_update: true))
flash[:success] = I18n.t("admin.articles_controller.saved")
else
flash[:danger] = article.errors_as_sentence
end
redirect_to admin_article_path(article.id)
end
def unpin
article = Article.find(params[:id])
PinnedArticle.remove
respond_to do |format|
format.html do
flash[:danger] = I18n.t("admin.articles_controller.unpinned")
redirect_to admin_article_path(article.id)
end
format.js do
render partial: "admin/articles/individual_article", locals: { article: article }, content_type: "text/html"
end
end
end
def pin
article = Article.find(params[:id])
PinnedArticle.set(article)
respond_to do |format|
format.html do
flash[:success] = I18n.t("admin.articles_controller.pinned")
redirect_to admin_article_path(article.id)
end
format.js do
render partial: "admin/articles/individual_article", locals: { article: article }, content_type: "text/html"
end
end
end
private
def articles_top(months_ago)
Article.published
.where("published_at > ?", months_ago)
.includes(user: [:notes])
.limited_columns_internal_select
.order(public_reactions_count: :desc)
.page(params[:page])
.per(50)
end
def articles_chronological
Article.published
.includes(user: [:notes])
.limited_columns_internal_select
.order(published_at: :desc)
.page(params[:page])
.per(50)
end
def articles_mixed
Article.published
.includes(user: [:notes])
.limited_columns_internal_select
.order(hotness_score: :desc)
.page(params[:page])
.per(30)
end
def articles_featured
Article.published.or(Article.where(published_from_feed: true))
.featured
.where("featured_number > ?", Time.current.to_i)
.includes(:user)
.limited_columns_internal_select
.order(featured_number: :desc)
end
def article_params
params.require(:article).permit(ARTICLES_ALLOWED_PARAMS)
end
def authorize_admin
authorize Article, :access?, policy_class: InternalPolicy
end
end
end