mirror of
https://github.com/Retrospring/retrospring.git
synced 2025-01-19 03:36:05 +01:00
Add the ability to post to services with a tag
This commit is contained in:
parent
3c58257d21
commit
683fffd734
10 changed files with 101 additions and 6 deletions
|
@ -28,6 +28,17 @@ class ServicesController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
service = current_user.services.find(params[:id])
|
||||||
|
service.post_tag = params[:service][:post_tag]
|
||||||
|
if service.save
|
||||||
|
flash[:success] = "Service updated successfully"
|
||||||
|
else
|
||||||
|
flash[:error] = "Failed to update service"
|
||||||
|
end
|
||||||
|
redirect_to services_path
|
||||||
|
end
|
||||||
|
|
||||||
def failure
|
def failure
|
||||||
Rails.logger.info "oauth error: #{params.inspect}"
|
Rails.logger.info "oauth error: #{params.inspect}"
|
||||||
flash[:error] = t('flash.service.failure')
|
flash[:error] = t('flash.service.failure')
|
||||||
|
|
|
@ -3,7 +3,7 @@ require 'cgi'
|
||||||
module SocialHelper::TwitterMethods
|
module SocialHelper::TwitterMethods
|
||||||
include MarkdownHelper
|
include MarkdownHelper
|
||||||
|
|
||||||
def prepare_tweet(answer)
|
def prepare_tweet(answer, post_tag = nil)
|
||||||
question_content = twitter_markdown answer.question.content.gsub(/\@(\w+)/, '\1')
|
question_content = twitter_markdown answer.question.content.gsub(/\@(\w+)/, '\1')
|
||||||
original_question_length = question_content.length
|
original_question_length = question_content.length
|
||||||
answer_content = twitter_markdown answer.content
|
answer_content = twitter_markdown answer.content
|
||||||
|
@ -19,8 +19,16 @@ module SocialHelper::TwitterMethods
|
||||||
tweet_text = ""
|
tweet_text = ""
|
||||||
|
|
||||||
until parsed_tweet[:valid]
|
until parsed_tweet[:valid]
|
||||||
tweet_text = "#{question_content[0..122]}#{'…' if original_question_length > [123, question_content.length].min}" \
|
shortened_question = "#{question_content[0..122]}#{'…' if original_question_length > [123, question_content.length].min}"
|
||||||
" — #{answer_content[0..123]}#{'…' if original_answer_length > [124, answer_content.length].min} #{answer_url}"
|
shortened_answer = "#{answer_content[0..123]}#{'…' if original_answer_length > [124, answer_content.length].min}"
|
||||||
|
components = [
|
||||||
|
shortened_question,
|
||||||
|
'—',
|
||||||
|
shortened_answer,
|
||||||
|
post_tag,
|
||||||
|
answer_url
|
||||||
|
]
|
||||||
|
tweet_text = components.compact.join(' ')
|
||||||
|
|
||||||
parsed_tweet = Twitter::TwitterText::Validation::parse_tweet(tweet_text)
|
parsed_tweet = Twitter::TwitterText::Validation::parse_tweet(tweet_text)
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ class Service < ApplicationRecord
|
||||||
|
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
validates_uniqueness_of :uid, scope: :type
|
validates_uniqueness_of :uid, scope: :type
|
||||||
|
validates_length_of :post_tag, maximum: 20
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,6 @@ class Services::Twitter < Service
|
||||||
end
|
end
|
||||||
|
|
||||||
def post_tweet(answer)
|
def post_tweet(answer)
|
||||||
client.update! prepare_tweet(answer)
|
client.update! prepare_tweet(answer, self.post_tag)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -20,3 +20,9 @@
|
||||||
service_path(service),
|
service_path(service),
|
||||||
data: { confirm: t('views.settings.service.confirm', service: service.provider.capitalize) },
|
data: { confirm: t('views.settings.service.confirm', service: service.provider.capitalize) },
|
||||||
method: :delete
|
method: :delete
|
||||||
|
|
||||||
|
.col-md-6.mt-2
|
||||||
|
= bootstrap_form_for(service, as: 'service', url: update_service_path(service)) do |f|
|
||||||
|
= f.text_field :post_tag, class: 'form-control', label: 'tag', label_as_placeholder: true,
|
||||||
|
append: f.submit('Update', class: 'btn btn-primary'), maxlength: 20,
|
||||||
|
help: "Automatically append a tag to your shared answers. #{'A # symbol is not automatically prepended.' if service.provider == 'twitter'}"
|
||||||
|
|
|
@ -77,6 +77,7 @@ Rails.application.routes.draw do
|
||||||
|
|
||||||
# resources :services, only: [:index, :destroy]
|
# resources :services, only: [:index, :destroy]
|
||||||
match '/settings/services', to: 'services#index', via: 'get', as: :services
|
match '/settings/services', to: 'services#index', via: 'get', as: :services
|
||||||
|
match '/settings/services/:id', to: 'services#update', via: 'patch', as: :update_service
|
||||||
match '/settings/services/:id', to: 'services#destroy', via: 'delete', as: :service
|
match '/settings/services/:id', to: 'services#destroy', via: 'delete', as: :service
|
||||||
controller :services do
|
controller :services do
|
||||||
scope "/auth", as: "auth" do
|
scope "/auth", as: "auth" do
|
||||||
|
|
5
db/migrate/20220104180510_add_post_tag_to_services.rb
Normal file
5
db/migrate/20220104180510_add_post_tag_to_services.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
class AddPostTagToServices < ActiveRecord::Migration[5.2]
|
||||||
|
def change
|
||||||
|
add_column :services, :post_tag, :string, limit: 20
|
||||||
|
end
|
||||||
|
end
|
|
@ -192,6 +192,7 @@ ActiveRecord::Schema.define(version: 2022_01_05_171216) do
|
||||||
t.string "nickname"
|
t.string "nickname"
|
||||||
t.datetime "created_at"
|
t.datetime "created_at"
|
||||||
t.datetime "updated_at"
|
t.datetime "updated_at"
|
||||||
|
t.string "post_tag", limit: 20
|
||||||
t.index ["user_id"], name: "index_services_on_user_id"
|
t.index ["user_id"], name: "index_services_on_user_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -38,4 +38,45 @@ describe ServicesController, type: :controller do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context '#update' do
|
||||||
|
subject { patch :update, params: params }
|
||||||
|
|
||||||
|
context 'not signed in' do
|
||||||
|
let(:params) { { id: 1 } }
|
||||||
|
|
||||||
|
it 'redirects to sign in page' do
|
||||||
|
subject
|
||||||
|
expect(response).to redirect_to(new_user_session_path)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'user with Twitter connection' do
|
||||||
|
before { sign_in user }
|
||||||
|
|
||||||
|
let(:user) { FactoryBot.create(:user) }
|
||||||
|
let(:service) { Services::Twitter.create(user: user, uid: 12) }
|
||||||
|
let(:params) { { id: service.id, service: { post_tag: post_tag } } }
|
||||||
|
|
||||||
|
context 'tag is valid' do
|
||||||
|
let(:post_tag) { '#askaraccoon' }
|
||||||
|
|
||||||
|
it 'updates a service connection' do
|
||||||
|
expect { subject }.to change { service.reload.post_tag }.to('#askaraccoon')
|
||||||
|
expect(response).to redirect_to(services_path)
|
||||||
|
expect(flash[:success]).to eq("Service updated successfully")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'tag is too long' do
|
||||||
|
let(:post_tag) { 'a' * 21 } # 1 character over the limit
|
||||||
|
|
||||||
|
it 'shows an error' do
|
||||||
|
subject
|
||||||
|
expect(response).to redirect_to(services_path)
|
||||||
|
expect(flash[:error]).to eq("Failed to update service")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,9 +4,11 @@ require 'rails_helper'
|
||||||
|
|
||||||
describe SocialHelper::TwitterMethods, :type => :helper do
|
describe SocialHelper::TwitterMethods, :type => :helper do
|
||||||
let(:user) { FactoryBot.create(:user) }
|
let(:user) { FactoryBot.create(:user) }
|
||||||
|
let(:question_content) { 'q' * 255 }
|
||||||
|
let(:answer_content) { 'a' * 255 }
|
||||||
let(:answer) { FactoryBot.create(:answer, user: user,
|
let(:answer) { FactoryBot.create(:answer, user: user,
|
||||||
content: 'a' * 255,
|
content: answer_content,
|
||||||
question_content: 'q' * 255) }
|
question_content: question_content) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
stub_const("APP_CONFIG", {
|
stub_const("APP_CONFIG", {
|
||||||
|
@ -25,6 +27,25 @@ describe SocialHelper::TwitterMethods, :type => :helper do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when a suffix has been passed' do
|
||||||
|
let(:question_content) { 'question' }
|
||||||
|
let(:answer_content) { 'answer' }
|
||||||
|
|
||||||
|
subject { prepare_tweet(answer, '#askracc') }
|
||||||
|
|
||||||
|
it 'should include the suffix after the link' do
|
||||||
|
expect(subject).to eq("question — answer #askracc https://example.com/#{user.screen_name}/a/#{answer.id}")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when a suffix has been passed and the tweet needs to be shortened' do
|
||||||
|
subject { prepare_tweet(answer, '#askracc') }
|
||||||
|
|
||||||
|
it 'should shorten the tweet while keeping the suffix intact' do
|
||||||
|
expect(subject).to eq("#{'q' * 120}… — #{'a' * 120}… #askracc https://example.com/#{user.screen_name}/a/#{answer.id}")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when the question and answer are short' do
|
context 'when the question and answer are short' do
|
||||||
before do
|
before do
|
||||||
answer.question.content = 'Why are raccoons so good?'
|
answer.question.content = 'Why are raccoons so good?'
|
||||||
|
|
Loading…
Reference in a new issue