-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 不可視になったツイートの状態を変更するタスクを作成した (#134)
- Loading branch information
1 parent
ecfc425
commit d42935e
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |