From 8494817683ff0c62404b66e1a9661c0db7d5d4fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?mix3=40=EF=BD=BB=EF=BE=80=EF=BE=83=EF=BE=9E=EF=BD=B0?= =?UTF-8?q?=EF=BE=85=EF=BD=B2=EF=BE=84=EF=BE=8C=EF=BD=A8=EF=BD=B0=EF=BE=8A?= =?UTF-8?q?=EF=BE=9E=EF=BD=B0?= Date: Sun, 24 Dec 2017 18:40:00 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=88=E3=83=A9=E3=83=B3=E3=82=B6=E3=82=AF?= =?UTF-8?q?=E3=82=B7=E3=83=A7=E3=83=B3=E3=82=92=E4=BD=BF=E3=81=86=E3=82=88?= =?UTF-8?q?=E3=81=86=E3=81=AB=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/users_controller.rb | 56 +++++++++++++++++++------ app/helpers/users_helper.rb | 26 ++++++++---- app/models/user.rb | 19 +++++++-- app/models/word.rb | 2 + app/views/users/_detected.html.erb | 14 ++----- app/views/users/_user_header.html.erb | 6 --- app/views/users/report_history.html.erb | 12 +----- app/views/words/_words.html.erb | 2 +- 8 files changed, 83 insertions(+), 54 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 490dd6e..4609ed3 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -16,6 +16,11 @@ def show end render_404 if @user.nil? + User.transaction do + @user.lock! + @user.update_tw_account(@user_tw_account) + end + #Words取得 @words = @user.words.all words_p = @words.partition{|w| w.alive?} @@ -32,26 +37,42 @@ def show end def report_history - #ログインしていないと見られない - if !logged_in? - redirect_to root_path - return + #ログインしていないと見られない + if !logged_in? + redirect_to root_path + return + end + @client = client_new + + current_user.reports.all.uniq{ |rp| rp.reported_id }.each do |rp| + if rp.reported.screen_name.nil? or rp.reported.screen_name.blank? + User.transaction do + rp.reported.lock! + acc = @client.user(rp.reported.twid.to_i) + rp.reported.update_tw_account(acc) + end end - @client = client_new + end + + @rphists = current_user.reports.all.order("created_at DESC") end def report if params[:ajax_tag] == 'report' @user = User.find(params[:reported_id]) - @client = client_new - @wordstr = params[:word] - @user_tw_account = @client.user(@user.twid.to_i) render_404 if @user.nil? + if params[:word].blank? @result = "blank" return end + + @client = client_new + @wordstr = params[:word] + @user_tw_account = @client.user(@user.twid.to_i) + @word = @user.words.find_by(name: params[:word]) + if !(@word.nil?) if !(@word.report_available?) #通報期限切れ @@ -59,14 +80,23 @@ def report elsif @word.detected @result = "alreadyreported" else - @word.detect_by(current_user) - current_user.reports.create(reported: @user, word_str: params[:word], succeed: true) - current_user.save + User.transaction do + me = current_user + @user.lock! # 摘発した相手のデータを更新するのでロックする + me.lock! + @word.detect_by(me) + me.reports.create(reported: @user, word_str: params[:word], succeed: true) + me.save! + end @result = "success" end else - current_user.reports.create(reported: @user, word_str: params[:word], succeed: false) - current_user.save + User.transaction do + me = current_user + me.lock! + me.reports.create(reported: @user, word_str: params[:word], succeed: false) + me.save! + end @result = "fail" end else diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index 65a6260..9d7fa78 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -2,17 +2,27 @@ module UsersHelper def render_users_home @client = client_new - @user = current_user @user_tw_account = @client.user(current_user.twid.to_i) + @user = current_user - #鍵垢判定 - if @user.is_secret != @user_tw_account.protected? - @user.is_secret = @user_tw_account.protected? - @user.save - end + User.transaction do + @user.lock! - @user.refresh_wordcaches(@client) - @user.words_reset(@client) + @user.update_tw_account(@user_tw_account) + + @user.refresh_wordcaches(@client) + @user.words_reset(@client) + + @detected_word = [] + @user.words.select{ |w| w.detected && !w.noticed_detection }.each do |word| + word.noticed_detection = true + word.save! + detector = User.find(word.detectorid) + next if detector.nil? + word.detectoraccount = @client.user(detector.twid.to_i) + @detected_word.push(word) + end + end #Words取得 @words = @user.words.all diff --git a/app/models/user.rb b/app/models/user.rb index 411a522..2e3c446 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -61,10 +61,10 @@ def refresh_wordcaches(client) end yesterdayswords.each do |w| - w.save + w.save! end todayswords.each do |w| - w.save + w.save! end self.get_todays_score(nil) @@ -101,7 +101,6 @@ def words_reset(client) def get_score(client) #基準スコア(アップデート前のスコア)に単語スコアとレポートスコアを加算しキャッシュに保存 self.current_score_cache = self.score + self.get_words_score(nil) + self.get_reports_score(nil) - self.save return self.current_score_cache end @@ -114,7 +113,7 @@ def get_todays_score(client) self.reports.each do |rp| self.todayscore += rp.succeed ? 100 : -20 if rp.today? end - self.save + self.save! return self.todayscore end @@ -134,4 +133,16 @@ def get_reports_score(client) end return reportscore end + + def update_tw_account(user_tw_account) + self.name = user_tw_account.name + self.screen_name = user_tw_account.screen_name + self.url = user_tw_account.url.to_s + self.imgurl = ApplicationController.helpers.get_twpic_uri(user_tw_account) + #鍵垢判定 + if self.is_secret != user_tw_account.protected? + self.is_secret = user_tw_account.protected? + end + self.save! + end end diff --git a/app/models/word.rb b/app/models/word.rb index 25e167b..d14a896 100644 --- a/app/models/word.rb +++ b/app/models/word.rb @@ -42,4 +42,6 @@ def yesterday? def report_available? return (self.created_at.localtime("+09:00") > (Time.now - 1.hour).localtime("+09:00").beginning_of_day) end + + attr_accessor :detectoraccount end diff --git a/app/views/users/_detected.html.erb b/app/views/users/_detected.html.erb index c57f2db..9996c28 100644 --- a/app/views/users/_detected.html.erb +++ b/app/views/users/_detected.html.erb @@ -1,21 +1,13 @@ -<% @words.select{|w| w.detected && !w.noticed_detection }.each do |word| %> - -<% word.noticed_detection = true %> -<% word.save %> -<% detector = User.find(word.detectorid) %> -<% next if detector.nil? %> -<% detectoraccount = @client.user(detector.twid.to_i) %> - +<% @detected_word.each do |word| %> - <% end %> diff --git a/app/views/users/_user_header.html.erb b/app/views/users/_user_header.html.erb index 3f9abba..fb2ab50 100644 --- a/app/views/users/_user_header.html.erb +++ b/app/views/users/_user_header.html.erb @@ -1,9 +1,3 @@ -<% user.name = @user_tw_account.name %> -<% user.screen_name = @user_tw_account.screen_name %> -<% user.url = @user_tw_account.url.to_s %> -<% user.imgurl = get_twpic_uri(@user_tw_account) %> -<% user.save %> -
<%= link_to image_tag(user.imgurl, size: "96x96" , alt: user.name, class: "mr-3 rounded-circle"), user.url, target:"_new" %>
diff --git a/app/views/users/report_history.html.erb b/app/views/users/report_history.html.erb index f5ca029..941e704 100644 --- a/app/views/users/report_history.html.erb +++ b/app/views/users/report_history.html.erb @@ -1,5 +1,4 @@

摘発履歴

-<% rphists = current_user.reports.all.order("created_at DESC") %> @@ -11,16 +10,7 @@ - <% rphists.each do |rp| %> - - <% if rp.reported.screen_name.nil? or rp.reported.screen_name.blank? %> - <% acc = @client.rp(rp.reported.twid.to_i) %> - <% rp.reported.name = acc.name %> - <% rp.reported.screen_name = acc.screen_name %> - <% rp.reported.url = acc.url.to_s %> - <% rp.reported.imgurl = get_twpic_uri(acc) %> - <% rp.reported.save %> - <% end %> + <% @rphists.each do |rp| %> diff --git a/app/views/words/_words.html.erb b/app/views/words/_words.html.erb index f4047aa..ced7181 100644 --- a/app/views/words/_words.html.erb +++ b/app/views/words/_words.html.erb @@ -25,6 +25,6 @@
<%= "#{rp.reported.name}(@#{rp.reported.screen_name})" %> <%= rp.word_str %><%= @user.get_score(nil) %>
-<% if !@todayswords.empty? %> +<% if !@todayswords.empty? && @todayswords.first.cached_at != nil %>

※ <%= smart_time_to_str(@todayswords.first.cached_at) %> 現在の情報です

<% end %>