From 4c4410225ec43cac08dda5678fc1972068fde766 Mon Sep 17 00:00:00 2001 From: OsamaSayegh Date: Wed, 15 Nov 2017 03:28:54 +0300 Subject: [PATCH] UX: cap likes 2 (#5237) --- .../discourse/widgets/actions-summary.js.es6 | 2 +- .../discourse/widgets/post-menu.js.es6 | 7 +++++-- .../post_action_users_controller.rb | 16 ++++++++++++++- config/locales/client.en.yml | 3 +++ .../post_action_users_controller_spec.rb | 20 +++++++++++++++++++ 5 files changed, 44 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/discourse/widgets/actions-summary.js.es6 b/app/assets/javascripts/discourse/widgets/actions-summary.js.es6 index 0e8212f35ad..c784fea47a5 100644 --- a/app/assets/javascripts/discourse/widgets/actions-summary.js.es6 +++ b/app/assets/javascripts/discourse/widgets/actions-summary.js.es6 @@ -30,7 +30,7 @@ createWidget('small-user-list', { let description = null; if (atts.description) { - description = I18n.t(atts.description, { icons: '' }); + description = I18n.t(atts.description, { count: atts.count }); } // oddly post_url is on the user diff --git a/app/assets/javascripts/discourse/widgets/post-menu.js.es6 b/app/assets/javascripts/discourse/widgets/post-menu.js.es6 index 80ab77838e0..af926dc30e9 100644 --- a/app/assets/javascripts/discourse/widgets/post-menu.js.es6 +++ b/app/assets/javascripts/discourse/widgets/post-menu.js.es6 @@ -353,11 +353,13 @@ export default createWidget('post-menu', { const contents = [ h('nav.post-controls.clearfix', postControls) ]; if (state.likedUsers.length) { + const remaining = state.total - state.likedUsers.length; contents.push(this.attach('small-user-list', { users: state.likedUsers, - addSelf: attrs.liked, + addSelf: attrs.liked && remaining === 0, listClassName: 'who-liked', - description: 'post.actions.people.like' + description: remaining > 0 ? 'post.actions.people.like_capped' : 'post.actions.people.like', + count: remaining })); } @@ -413,6 +415,7 @@ export default createWidget('post-menu', { return this.store.find('post-action-user', { id: attrs.id, post_action_type_id: LIKE_ACTION }).then(users => { state.likedUsers = users.map(avatarAtts); + state.total = users.totalRows; }); }, diff --git a/app/controllers/post_action_users_controller.rb b/app/controllers/post_action_users_controller.rb index 46b6328d612..e1bdf163dfb 100644 --- a/app/controllers/post_action_users_controller.rb +++ b/app/controllers/post_action_users_controller.rb @@ -6,6 +6,9 @@ class PostActionUsersController < ApplicationController params.require(:id) post_action_type_id = params[:post_action_type_id].to_i + page = params[:page].to_i + page_size = (params[:limit] || 200).to_i + finder = Post.where(id: params[:id].to_i) finder = finder.with_deleted if guardian.is_staff? @@ -14,7 +17,9 @@ class PostActionUsersController < ApplicationController post_actions = post.post_actions.where(post_action_type_id: post_action_type_id) .includes(:user) + .offset(page * page_size) .order('post_actions.created_at asc') + .limit(page_size) if !guardian.can_see_post_actors?(post.topic, post_action_type_id) if !current_user @@ -23,6 +28,15 @@ class PostActionUsersController < ApplicationController post_actions = post_actions.where(user_id: current_user.id) end - render_serialized(post_actions.to_a, PostActionUserSerializer, root: 'post_action_users') + action_type = PostActionType.types.key(post_action_type_id) + total_count = post["#{action_type}_count"] + + data = { post_action_users: serialize_data(post_actions.to_a, PostActionUserSerializer) } + + if total_count > page_size + data[:total_rows_post_action_users] = total_count + end + + render_json_dump(data) end end diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 9ff7234a5ad..c566ded478d 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -1956,6 +1956,9 @@ en: notify_user: "sent a message" bookmark: "bookmarked this" like: "liked this" + like_capped: + one: "and {{count}} other liked this" + other: "and {{count}} others liked this" vote: "voted for this" by_you: off_topic: "You flagged this as off-topic" diff --git a/spec/controllers/post_action_users_controller_spec.rb b/spec/controllers/post_action_users_controller_spec.rb index 702194b896a..fdd6add07cd 100644 --- a/spec/controllers/post_action_users_controller_spec.rb +++ b/spec/controllers/post_action_users_controller_spec.rb @@ -61,4 +61,24 @@ describe PostActionUsersController do expect(response).to be_success end + + it "paginates post actions" do + user_ids = [] + 5.times do + user = Fabricate(:user) + user_ids << user["id"] + PostAction.act(user, post, PostActionType.types[:like]) + end + + get :index, params: { id: post.id, post_action_type_id: PostActionType.types[:like], page: 1, limit: 2 }, format: :json + json = JSON.parse(response.body) + + users = json["post_action_users"] + total = json["total_rows_post_action_users"] + + expect(users.length).to eq(2) + expect(users.map { |u| u["id"] }).to eq(user_ids[2..3]) + + expect(total).to eq(5) + end end