Skip to content

Commit 1833182

Browse files
authored
用 actions 表来代替 Like, Folllow, Block 等功能之前的 Array 字段存储. (#857)
1 parent 858f7ee commit 1833182

35 files changed

+321
-294
lines changed

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ gem 'devise-encryptable'
5454
gem 'notifications'
5555
gem 'ruby-push-notifications'
5656

57+
# 赞、关注、收藏、屏蔽等功能的数据结构
58+
gem 'action-store'
59+
5760
# 分页
5861
gem 'will_paginate'
5962

Gemfile.lock

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
GEM
22
remote: https://gems.ruby-china.org/
33
specs:
4+
action-store (0.2.0)
5+
rails (>= 4.2.0, < 5.1)
46
actioncable (5.0.1)
57
actionpack (= 5.0.1)
68
nio4r (~> 1.2)
@@ -402,7 +404,7 @@ GEM
402404
url (0.3.2)
403405
warden (1.2.6)
404406
rack (>= 1.0)
405-
websocket-driver (0.6.4)
407+
websocket-driver (0.6.5)
406408
websocket-extensions (>= 0.1.0)
407409
websocket-extensions (0.1.2)
408410
will_paginate (3.1.5)
@@ -414,6 +416,7 @@ PLATFORMS
414416
ruby
415417

416418
DEPENDENCIES
419+
action-store
417420
auto-space
418421
better_errors
419422
binding_of_caller

app/controllers/api/v3/likes_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class LikesController < Api::V3::ApplicationController
1919
# - count [Integer] 已赞的数量
2020
def create
2121
current_user.like(likeable)
22+
likeable.reload
2223
data = { obj_type: params[:obj_type], obj_id: likeable.id, count: likeable.likes_count }
2324
render json: data
2425
end
@@ -31,6 +32,7 @@ def create
3132
# @return (see #create)
3233
def destroy
3334
current_user.unlike(likeable)
35+
likeable.reload
3436
data = { obj_type: params[:obj_type], obj_id: likeable.id, count: likeable.likes_count }
3537
render json: data
3638
end

app/controllers/api/v3/topics_controller.rb

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def index
2525
if params[:node_id].blank?
2626
@topics = Topic
2727
if current_user
28-
@topics = @topics.without_nodes(current_user.blocked_node_ids)
29-
@topics = @topics.without_users(current_user.blocked_user_ids)
28+
@topics = @topics.without_nodes(current_user.block_node_ids)
29+
@topics = @topics.without_users(current_user.block_user_ids)
3030
else
3131
@topics = @topics.without_hide_nodes
3232
end
@@ -62,9 +62,9 @@ def show
6262
if current_user
6363
# 处理通知
6464
current_user.read_topic(@topic)
65-
@meta[:followed] = @topic.followed?(current_user.id)
66-
@meta[:liked] = current_user.liked?(@topic)
67-
@meta[:favorited] = current_user.favorited_topic?(@topic.id)
65+
@meta[:followed] = current_user.follow_topic?(@topic)
66+
@meta[:liked] = current_user.like_topic?(@topic)
67+
@meta[:favorited] = current_user.favorite_topic?(@topic)
6868
end
6969
end
7070

@@ -147,17 +147,7 @@ def replies
147147

148148
@replies = Reply.unscoped.where(topic_id: @topic.id).order(:id).includes(:user)
149149
@replies = @replies.offset(params[:offset].to_i).limit(params[:limit].to_i)
150-
151-
@user_liked_reply_ids = []
152-
if current_user
153-
# 找出用户 like 过的 Reply,给 JS 处理 like 功能的状态
154-
@replies.each do |r|
155-
unless r.liked_user_ids.index(current_user.id).nil?
156-
@user_liked_reply_ids << r.id
157-
end
158-
end
159-
end
160-
150+
@user_liked_reply_ids = current_user&.like_reply_ids || []
161151
@meta = { user_liked_reply_ids: @user_liked_reply_ids }
162152
end
163153

@@ -184,15 +174,15 @@ def create_replies
184174
#
185175
# POST /api/v3/topics/:id/follow
186176
def follow
187-
@topic.push_follower(current_user.id)
177+
current_user.follow_topic(@topic)
188178
render json: { ok: 1 }
189179
end
190180

191181
# 取消关注话题
192182
#
193183
# POST /api/v3/topics/:id/unfollow
194184
def unfollow
195-
@topic.pull_follower(current_user.id)
185+
current_user.unfollow_topic(@topic)
196186
render json: { ok: 1 }
197187
end
198188

app/controllers/api/v3/users_controller.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def show
3434
@meta = { followed: false, blocked: false }
3535

3636
if current_user
37-
@meta[:followed] = current_user.followed?(@user)
38-
@meta[:blocked] = current_user.blocked_user?(@user)
37+
@meta[:followed] = current_user.follow_user?(@user)
38+
@meta[:blocked] = current_user.block_user?(@user)
3939
end
4040
end
4141

@@ -95,9 +95,7 @@ def favorites
9595
optional! :offset, type: Integer, default: 0
9696
optional! :limit, type: Integer, default: 20, values: 1..150
9797

98-
@topic_ids = @user.favorite_topic_ids.reverse[params[:offset].to_i, params[:limit].to_i]
99-
@topics = Topic.where(id: @topic_ids).fields_for_list.includes(:user)
100-
@topics = @topics.to_a.sort_by { |topic| @topic_ids.index(topic.id) }
98+
@topics = @user.favorite_topics.includes(:user).order("actions.id desc").offset(params[:offset]).limit(params[:limit])
10199
render 'topics'
102100
end
103101

@@ -112,7 +110,7 @@ def followers
112110
optional! :offset, type: Integer, default: 0
113111
optional! :limit, type: Integer, default: 20, values: 1..150
114112

115-
@users = @user.followers.fields_for_list.offset(params[:offset]).limit(params[:limit])
113+
@users = @user.follow_by_users.fields_for_list.order("actions.id asc").offset(params[:offset]).limit(params[:limit])
116114
end
117115

118116
# 获取某个用户的关注者列表
@@ -125,7 +123,7 @@ def following
125123
optional! :offset, type: Integer, default: 0
126124
optional! :limit, type: Integer, default: 20, values: 1..150
127125

128-
@users = @user.following.fields_for_list.offset(params[:offset]).limit(params[:limit])
126+
@users = @user.follow_users.fields_for_list.order("actions.id asc").offset(params[:offset]).limit(params[:limit])
129127
end
130128

131129
# 获取用户的已屏蔽的人(只能获取自己的)
@@ -138,10 +136,9 @@ def blocked
138136
optional! :offset, type: Integer, default: 0
139137
optional! :limit, type: Integer, default: 20, values: 1..150
140138

141-
raise AccessDenied.new('不可以获取其他人的 blocked_users 列表。') if current_user.id != @user.id
139+
raise AccessDenied.new('不可以获取其他人的 block_users 列表。') if current_user.id != @user.id
142140

143-
user_ids = current_user.blocked_user_ids[params[:offset].to_i, params[:limit].to_i]
144-
@users = User.where(id: user_ids)
141+
@users = current_user.block_users.fields_for_list.order("actions.id asc").offset(params[:offset]).limit(params[:limit])
145142
end
146143

147144
# 关注用户

app/controllers/likes_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class LikesController < ApplicationController
33
before_action :set_likeable
44

55
def index
6-
@users = @item.liked_users
6+
@users = @item.like_by_users.order('actions.id asc')
77
render :index, layout: false
88
end
99

app/controllers/topics_controller.rb

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def index
1616
@topics = Topic.last_actived.without_suggest
1717
@topics =
1818
if current_user
19-
@topics.without_nodes(current_user.blocked_node_ids)
20-
.without_users(current_user.blocked_user_ids)
19+
@topics.without_nodes(current_user.block_node_ids)
20+
.without_users(current_user.block_user_ids)
2121
else
2222
@topics.without_hide_nodes
2323
end
@@ -95,7 +95,6 @@ def show
9595

9696
@replies = Reply.unscoped.where(topic_id: @topic.id).order(:id).all
9797

98-
check_current_user_liked_replies
9998
check_current_user_status_for_topic
10099
set_special_node_active_menu
101100
fresh_when([@topic, @node, @show_raw, @replies, @has_followed, @has_favorited, @can_reply])
@@ -164,12 +163,12 @@ def unfavorite
164163
end
165164

166165
def follow
167-
@topic.push_follower(current_user.id)
166+
current_user.follow_topic(@topic)
168167
render plain: '1'
169168
end
170169

171170
def unfollow
172-
@topic.pull_follower(current_user.id)
171+
current_user.unfollow_topic(@topic)
173172
render plain: '1'
174173
end
175174

@@ -212,26 +211,14 @@ def ability_team_id
212211
team.id
213212
end
214213

215-
def check_current_user_liked_replies
216-
return false unless current_user
217-
218-
# 找出用户 like 过的 Reply,给 JS 处理 like 功能的状态
219-
@user_liked_reply_ids = []
220-
@replies.each do |r|
221-
unless r.liked_user_ids.index(current_user.id).nil?
222-
@user_liked_reply_ids << r.id
223-
end
224-
end
225-
end
226-
227214
def check_current_user_status_for_topic
228215
return false unless current_user
229216
# 通知处理
230217
current_user.read_topic(@topic, replies_ids: @replies.collect(&:id))
231218
# 是否关注过
232-
@has_followed = @topic.followed?(current_user.id)
219+
@has_followed = current_user.follow_topic?(@topic)
233220
# 是否收藏
234-
@has_favorited = current_user.favorited_topic?(@topic.id)
221+
@has_favorited = current_user.favorite_topic?(@topic)
235222
end
236223

237224
def set_special_node_active_menu

app/controllers/users/user_actions.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ def replies
2020
end
2121

2222
def favorites
23-
@topic_ids = @user.favorite_topic_ids.reverse.paginate(page: params[:page], per_page: 40)
24-
@topics = Topic.where(id: @topic_ids).fields_for_list
25-
@topics = @topics.to_a.sort_by { |topic| @topic_ids.index(topic.id) }
23+
@topics = @user.favorite_topics.includes(:user).order("actions.id desc").paginate(page: params[:page], per_page: 40)
2624
fresh_when([@topics])
2725
end
2826

@@ -57,7 +55,7 @@ def blocked
5755
render_404
5856
end
5957

60-
@blocked_users = User.where(id: current_user.blocked_user_ids).paginate(page: params[:page], per_page: 20)
58+
@block_users = @user.block_users.order("actions.id asc").paginate(page: params[:page], per_page: 20)
6159
end
6260

6361
def follow
@@ -71,12 +69,12 @@ def unfollow
7169
end
7270

7371
def followers
74-
@users = @user.followers.fields_for_list.paginate(page: params[:page], per_page: 60)
72+
@users = @user.follow_by_users.order("actions.id asc").paginate(page: params[:page], per_page: 60)
7573
fresh_when([@users])
7674
end
7775

7876
def following
79-
@users = @user.following.fields_for_list.paginate(page: params[:page], per_page: 60)
77+
@users = @user.follow_users.order("actions.id asc").paginate(page: params[:page], per_page: 60)
8078
render template: '/users/followers' if stale?(etag: [@users], template: 'users/followers')
8179
end
8280

app/helpers/likes_helper.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,18 @@ def likeable_tag(likeable, opts = {})
99
label = "#{likeable.likes_count} 个赞"
1010
label = '' if likeable.likes_count == 0
1111

12+
liked = false
13+
14+
if opts[:cache].blank? && current_user
15+
if likeable.is_a?(Topic)
16+
liked = current_user.like_topic_ids.include?(likeable.id)
17+
elsif likeable.is_a?(Reply)
18+
liked = current_user.like_reply_ids.include?(likeable.id)
19+
end
20+
end
21+
1222
title, state, icon_name =
13-
if opts[:cache].blank? && likeable.liked_by_user?(current_user)
23+
if opts[:cache].blank? && liked
1424
['取消赞', 'active', 'heart']
1525
else
1626
['赞', '', 'heart']

app/helpers/topics_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def topic_favorite_tag(topic, opts = {})
55
opts[:class] ||= ''
66
class_name = ''
77
link_title = '收藏'
8-
if current_user && current_user.favorite_topic_ids.include?(topic.id)
8+
if current_user && current_user.favorite_topic?(topic)
99
class_name = 'active'
1010
link_title = '取消收藏'
1111
end
@@ -24,7 +24,7 @@ def topic_follow_tag(topic, opts = {})
2424
return '' if owner?(topic)
2525
opts[:class] ||= ''
2626
class_name = 'follow'
27-
class_name += ' active' if topic.follower_ids.include?(current_user.id)
27+
class_name += ' active' if current_user.follow_topic?(topic)
2828
if opts[:class].present?
2929
class_name += ' ' + opts[:class]
3030
end

app/helpers/users_helper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def render_user_level_tag(user)
8080
def block_node_tag(node)
8181
return '' if current_user.blank?
8282
return '' if node.blank?
83-
blocked = current_user.blocked_node_ids.include?(node.id)
83+
blocked = current_user.block_node?(node)
8484
class_names = 'btn btn-default btn-sm button-block-node'
8585
icon = '<i class="fa fa-eye-slash"></i>'
8686
if blocked
@@ -94,7 +94,7 @@ def block_user_tag(user)
9494
return '' if current_user.blank?
9595
return '' if user.blank?
9696
return '' if current_user.id == user.id
97-
blocked = current_user.blocked_user_ids.include?(user.id)
97+
blocked = current_user.block_user?(user)
9898
class_names = 'button-block-user btn btn-default btn-block'
9999
icon = '<i class="fa fa-eye-slash"></i>'
100100
if blocked
@@ -108,7 +108,7 @@ def follow_user_tag(user, opts = {})
108108
return '' if current_user.blank?
109109
return '' if user.blank?
110110
return '' if current_user.id == user.id
111-
followed = current_user.followed?(user)
111+
followed = current_user.follow_user_ids.include?(user.id)
112112
opts[:class] ||= 'btn btn-primary btn-block'
113113
class_names = "button-follow-user #{opts[:class]}"
114114
icon = '<i class="fa fa-user"></i>'

app/models/concerns/likeable.rb

Lines changed: 0 additions & 17 deletions
This file was deleted.

app/models/reply.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
class Reply < ApplicationRecord
33
include MarkdownBody
44
include SoftDelete
5-
include Likeable
65
include Mentionable
76
include MentionTopic
87

@@ -98,9 +97,9 @@ def default_notification
9897
def notification_receiver_ids
9998
return @notification_receiver_ids if defined? @notification_receiver_ids
10099
# 加入帖子关注着
101-
follower_ids = self.topic.try(:follower_ids) || []
100+
follower_ids = self.topic.try(:follow_by_user_ids) || []
102101
# 加入回帖人的关注者
103-
follower_ids = follower_ids + (self.user.try(:follower_ids) || [])
102+
follower_ids = follower_ids + (self.user.try(:follow_by_user_ids) || [])
104103
# 加入发帖人
105104
follower_ids << self.topic.try(:user_id)
106105
# 去重复

0 commit comments

Comments
 (0)