* Initial work for @-mention notifications from posts * Revert article.published changes to article updater, add clarifying comments * Extract article preview into reusable partial for notification views * Clean up Article Updater * Address + remove some FIXMEs * Add a whole buncha specs for @-mention functionality in posts YAY * Refactor create all spec to use shared examples, add clarifying comments * Add guard clause to create all service * Update new mention and notifiable action specs * Some additional cleanup * Add specs + shared examples to SendEmailNotificationWorker spec * Use aggregate_failures where applicable Co-authored-by: Michael Kohl <citizen428@dev.to> * Cleanup and address code review comments * Add MentionDecorator + relevant specs * Address comments/issues flagged by @rhymes * Optimize plucking user_ids when checking for article followers Co-authored-by: Michael Kohl <citizen428@dev.to>
69 lines
1.9 KiB
Ruby
69 lines
1.9 KiB
Ruby
module Articles
|
|
class Creator
|
|
def initialize(user, article_params, event_dispatcher = Webhook::DispatchEvent)
|
|
@user = user
|
|
@article_params = article_params
|
|
@event_dispatcher = event_dispatcher
|
|
end
|
|
|
|
def self.call(...)
|
|
new(...).call
|
|
end
|
|
|
|
def call
|
|
rate_limit!
|
|
|
|
article = save_article
|
|
|
|
if article.persisted?
|
|
# Subscribe author to notifications for all comments on their article.
|
|
NotificationSubscription.create(user: user, notifiable_id: article.id, notifiable_type: "Article",
|
|
config: "all_comments")
|
|
|
|
# Send notifications to any mentioned users, followed by any users who follow the article's author.
|
|
Notification.send_to_mentioned_users_and_followers(article) if article.published?
|
|
dispatch_event(article)
|
|
end
|
|
|
|
article
|
|
end
|
|
|
|
private
|
|
|
|
attr_reader :user, :article_params, :event_dispatcher
|
|
|
|
def rate_limit!
|
|
rate_limit_to_use = if user.decorate.considered_new?
|
|
:published_article_antispam_creation
|
|
else
|
|
:published_article_creation
|
|
end
|
|
|
|
user.rate_limiter.check_limit!(rate_limit_to_use)
|
|
end
|
|
|
|
def dispatch_event(article)
|
|
return unless article.published?
|
|
|
|
event_dispatcher.call("article_created", article)
|
|
end
|
|
|
|
def save_article
|
|
series = article_params[:series]
|
|
tags = article_params[:tags]
|
|
|
|
# convert tags from array to a string
|
|
if tags.present?
|
|
article_params.delete(:tags)
|
|
article_params[:tag_list] = tags.join(", ")
|
|
end
|
|
|
|
article = Article.new(article_params)
|
|
article.user_id = user.id
|
|
article.show_comments = true
|
|
article.collection = Collection.find_series(series, user) if series.present?
|
|
article.save
|
|
article
|
|
end
|
|
end
|
|
end
|