Skip to content

Commit

Permalink
Merge pull request #140 from true-runes/development
Browse files Browse the repository at this point in the history
v2.0.0
  • Loading branch information
nikukyugamer authored Jun 23, 2022
2 parents 3b5abe4 + f64d930 commit 979de62
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 30 deletions.
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ GEM
gapic-common (>= 0.7, < 2.a)
google-cloud-errors (~> 1.0)
google-protobuf (3.21.1)
google-protobuf (3.21.1-x86_64-darwin)
google-protobuf (3.21.1-x86_64-linux)
googleapis-common-protos (1.3.12)
google-protobuf (~> 3.14)
Expand All @@ -187,6 +188,9 @@ GEM
grpc (1.46.3)
google-protobuf (~> 3.19)
googleapis-common-protos-types (~> 1.0)
grpc (1.46.3-x86_64-darwin)
google-protobuf (~> 3.19)
googleapis-common-protos-types (~> 1.0)
grpc (1.46.3-x86_64-linux)
google-protobuf (~> 3.19)
googleapis-common-protos-types (~> 1.0)
Expand Down Expand Up @@ -244,6 +248,8 @@ GEM
nio4r (2.5.8)
nokogiri (1.13.6-aarch64-linux)
racc (~> 1.4)
nokogiri (1.13.6-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.13.6-x86_64-linux)
racc (~> 1.4)
os (1.1.4)
Expand Down Expand Up @@ -373,6 +379,7 @@ GEM

PLATFORMS
aarch64-linux
x86_64-darwin-21
x86_64-linux

DEPENDENCIES
Expand Down
16 changes: 0 additions & 16 deletions app/controllers/check_vote_controller.rb

This file was deleted.

21 changes: 21 additions & 0 deletions app/controllers/check_votes_and_bonuses_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class CheckVotesAndBonusesController < ApplicationController
def index
screen_name = Presenter::Common.normalized_screen_name(params[:screen_name])
user = User.find_by(screen_name: screen_name)

# TODO: エラーハンドリング
return render json: {} if user.blank?

gss2022_tweets = user.tweets.gensosenkyo_2022_votes_for_api
unite_attacks_tweets = user.tweets.unite_attacks_votes_for_api
short_stories = user.tweets.short_stories
fav_quotes = user.tweets.fav_quotes
sosenkyo_campaigns = user.tweets.sosenkyo_campaigns

@gss2022_tweets = gss2022_tweets.map(&:id_number)
@unite_attacks_tweets = unite_attacks_tweets.map(&:id_number)
@short_stories = short_stories.map(&:id_number)
@fav_quotes = fav_quotes.map(&:id_number)
@sosenkyo_campaigns = sosenkyo_campaigns.map(&:id_number)
end
end
27 changes: 27 additions & 0 deletions app/lib/check_visible_and_invisible_tweet_id_numbers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class CheckVisibleAndInvisibleTweetIdNumbers
INTERVAL_SECONDS = 5

def self.exec(tweet_id_numbers, client_account_key: :ayy)
client = TwitterRestApi.client(account_key: client_account_key)

all_invisible_tweet_id_numbers = []

# 不可視の場合には client.statuses([tweet_id_number, ...]) の戻り値に返ってこない仕様を利用する
tweet_id_numbers.each_slice(100) do |this_tweet_id_numbers|
visible_tweet_objects = client.statuses(this_tweet_id_numbers) # API が消費される
visible_tweet_id_numbers = visible_tweet_objects.map(&:id) # #id により id_number を取得できる

# 配列 から 配列 を引き算して、不可視のツイートの id_number を取り出す
invisible_tweet_id_numbers = this_tweet_id_numbers - visible_tweet_id_numbers

all_invisible_tweet_id_numbers << invisible_tweet_id_numbers
all_invisible_tweet_id_numbers.flatten!

sleep INTERVAL_SECONDS
end

all_visible_tweet_id_numbers = Tweet.all.map(&:id_number) - all_invisible_tweet_id_numbers

{ visible: all_visible_tweet_id_numbers, invisible: all_invisible_tweet_id_numbers}
end
end
5 changes: 5 additions & 0 deletions app/lib/presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,10 @@ def self.japanese_clock_time_strftime(time, with_seconds: true)

time.strftime(str)
end

def self.normalized_screen_name(screen_name)
screen_name.gsub!(' ', '')
screen_name.gsub('@', '')
end
end
end
75 changes: 62 additions & 13 deletions app/models/tweet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@ def self.filter_by_tweeted_at(from, to)
where(tweeted_at: from..to)
end

def self.gensosenkyo_2022_votes
valid_term_votes
.not_retweet
.contains_hashtag('幻水総選挙2022')
.not_by_gensosenkyo_main
.order(tweeted_at: :asc)
.order(id_number: :asc)
end

def self.not_by_gensosenkyo_family
# gensosenkyo: 1471724029,
# sub_gensosenkyo: 1388758231825018881
Expand Down Expand Up @@ -65,20 +56,78 @@ def self.valid_term_votes
where(tweeted_at: begin_datetime..end_datetime)
end

def self.short_stories
# スプレッドシートに書き込むことを前提としているので、制限は緩い
def self.gensosenkyo_2022_votes
not_retweet # 元データに入っていないから、おそらく不要
.contains_hashtag('幻水総選挙2022')
.not_by_gensosenkyo_family # 元データに入っていないから、おそらく不要
.order(tweeted_at: :asc) # id_number で並べているから、おそらく不要
.order(id_number: :asc)
end

# API では "is_public" と 期間 に注意する
def self.gensosenkyo_2022_votes_for_api
where(is_public: true)
.valid_term_votes
.not_retweet
.not_by_gensosenkyo_family
.contains_hashtag('幻水総選挙2022')
.order(tweeted_at: :asc)
.order(id_number: :asc)
end

# スプレッドシートに書き込むことを前提としているので、制限は緩い
def self.unite_attacks_votes
not_retweet
.not_by_gensosenkyo_family
.contains_hashtag('幻水総選挙2022協力攻撃')
.order(tweeted_at: :asc)
.order(id_number: :asc)
end

# API では "is_public" と 期間 に注意する
def self.unite_attacks_votes_for_api
where(is_public: true)
.valid_term_votes
.not_retweet
.not_by_gensosenkyo_family
.contains_hashtag('幻水総選挙2022協力攻撃')
.order(tweeted_at: :asc)
.order(id_number: :asc)
end

# スプレッドシートに書き込むことを前提としているので、制限は緩い
def self.short_stories
not_retweet
.contains_hashtag('幻水総選挙お題小説')
.where(tweeted_at: ..Time.zone.parse('2022-06-26 23:59:59'))
.not_by_gensosenkyo_family
.where(
tweeted_at: Time.zone.parse('2022-05-01 12:00:00')..Time.zone.parse('2022-07-31 23:59:59')
)
.order(tweeted_at: :asc)
.order(id_number: :asc)
end

# スプレッドシートに書き込むことを前提としているので、制限は緩い
def self.fav_quotes
not_retweet
.not_by_gensosenkyo_family
.contains_hashtag('幻水総選挙推し台詞')
.where(tweeted_at: ..Time.zone.parse('2022-06-26 23:59:59'))
.not_by_gensosenkyo_family
.where(
tweeted_at: Time.zone.parse('2022-05-01 12:00:00')..Time.zone.parse('2022-07-31 23:59:59')
)
.order(tweeted_at: :asc)
.order(id_number: :asc)
end

# スプレッドシートに書き込むことを前提としているので、制限は緩い
def self.sosenkyo_campaigns
not_retweet
.contains_hashtag('幻水総選挙運動')
.not_by_gensosenkyo_family
.where(
tweeted_at: Time.zone.parse('2022-05-01 12:00:00')..Time.zone.parse('2022-07-31 23:59:59')
)
.order(tweeted_at: :asc)
.order(id_number: :asc)
end
Expand Down
9 changes: 9 additions & 0 deletions app/services/natural_language/suggest_character_names.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module NaturalLanguage
class SuggestCharacterNames
def self.exec(tweet_or_dm)
analyze_syntax = tweet_or_dm.analyze_syntax

PickupCharacterNames.exec(analyze_syntax)
end
end
end
5 changes: 5 additions & 0 deletions app/views/check_votes_and_bonuses/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
json.set! 'gss2022', @gss2022_tweets
json.set! 'unite_attacks', @unite_attacks_tweets
json.set! 'short_stories', @short_stories
json.set! 'fav_quotes', @fav_quotes
json.set! 'sosenkyo_campaigns', @sosenkyo_campaigns
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
get 'health_check', to: 'health_check#index', format: 'json'
get 'unite_attacks', to: 'unite_attacks#index', format: 'json'
get 'result_illustration_applications', to: 'result_illustration_applications#index', format: 'json'
get 'check_vote', to: 'check_vote#index', format: 'json'
get 'check_votes_and_bonuses', to: 'check_votes_and_bonuses#index', format: 'json'
end
27 changes: 27 additions & 0 deletions lib/tasks/change_is_public_attributes.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace :change_is_public_attributes do
desc 'ツイートが非公開または削除になっていないかを調べ、変更があれば UPDATE を実行する'
task all: :environment do
tweets = Tweet.all
tweet_id_numbers = tweets.map(&:id_number)

# app/lib/check_visible_and_invisible_tweet_id_numbers.rb を参照のこと
visible_and_invisible_tweet_id_numbers = CheckVisibleAndInvisibleTweetIdNumbers.exec(tweet_id_numbers)

visible_tweet_id_numbers = visible_and_invisible_tweet_id_numbers[:visible]
invisible_tweet_id_numbers = visible_and_invisible_tweet_id_numbers[:invisible]

ActiveRecord::Base.transaction do
# 鍵ツイート => 公開ツイート の場合
Tweet.where(id_number: visible_tweet_id_numbers).each do |tweet|
tweet.update!(is_public: true) unless tweet.public?
end

# 公開ツイート => 鍵ツイート or 削除ツイート の場合
Tweet.where(id_number: invisible_tweet_id_numbers).each do |tweet|
tweet.update!(is_public: false) if tweet.public?
end
end

puts '[DONE] check_tweet_is_invisible:exec'
end
end
31 changes: 31 additions & 0 deletions spec/requests/check_votes_and_bonuses_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'rails_helper'

RSpec.describe CheckVotesAndBonusesController, type: :request do
describe '#index' do
context 'パラメータ screen_name が' do
it 'データベースに存在しないとき、期待通りのレスポンスが返ってくること' do
get check_votes_and_bonuses_path(screen_name: '@test_user')

expect(response).to have_http_status :ok
expect(response.body).to eq '{}'
end

it 'データベースに存在するとき、期待通りのレスポンスが返ってくること' do
create(:user)

get check_votes_and_bonuses_path(screen_name: '@test_screen_name')

expect(response).to have_http_status :ok
expect(JSON.parse(response.body)).to eq(
{
"gss2022" => [],
"unite_attacks" => [],
"short_stories" => [],
"fav_quotes" => [],
"sosenkyo_campaigns" => []
}
)
end
end
end
end

0 comments on commit 979de62

Please sign in to comment.