docbrown/app/controllers/buffer_updates_controller.rb
Ben Halpern 1552df98d2
Add ability for trusted users to suggest social copy for posts (#2306)
* Add ability for trusted users to suggest social copy for posts

* Fix schema

* Fix tests

* Fix test
2019-04-04 17:01:58 -04:00

50 lines
1.5 KiB
Ruby

class BufferUpdatesController < ApplicationController
after_action :verify_authorized
def create
authorize BufferUpdate
@article = Article.find(params[:buffer_update][:article_id])
create_main_tweet
create_satellite_tweets
@article.update(last_buffered: Time.current)
redirect_back(fallback_location: "/mod")
end
def create_main_tweet
BufferUpdate.create(
article_id: @article.id,
composer_user_id: current_user.id,
body_text: params[:buffer_update][:body_text],
social_service_name: "twitter",
buffer_profile_id_code: ApplicationConfig["BUFFER_TWITTER_ID"],
status: "pending",
)
end
def create_satellite_tweets
tags_names = @article.decorate.cached_tag_list_array
tags_names.each do |name|
tag = Tag.find_by(name: name)
if tag&.buffer_profile_id_code.present?
BufferUpdate.create(
article_id: @article.id,
composer_user_id: current_user.id,
body_text: params[:buffer_update][:body_text],
social_service_name: "twitter",
buffer_profile_id_code: tag.buffer_profile_id_code,
tag_id: params[:buffer_update][:tag_id],
status: "pending",
)
end
end
end
def modified_body_text
@user = @article.user
if @user.twitter_username.present?
params[:buffer_update][:body_text] + "\n{ author: @#{@user.twitter_username} } #DEVCommunity"
else
params[:buffer_update][:body_text]
end
end
end