FEATURE: Support filtering of groups page by category if in url

FEATURE: Support filtering of groups page by category if in url

diff --git a/app/assets/javascripts/discourse/components/group-activity-filter.js.es6 b/app/assets/javascripts/discourse/components/group-activity-filter.js.es6
new file mode 100644
index 0000000..d100e27
--- /dev/null
+++ b/app/assets/javascripts/discourse/components/group-activity-filter.js.es6
@@ -0,0 +1,3 @@
+export default Ember.Component.extend({
+  tagName: 'li'
+});
diff --git a/app/assets/javascripts/discourse/controllers/group-activity-posts.js.es6 b/app/assets/javascripts/discourse/controllers/group-activity-posts.js.es6
index c2434c8..991ce0b 100644
--- a/app/assets/javascripts/discourse/controllers/group-activity-posts.js.es6
+++ b/app/assets/javascripts/discourse/controllers/group-activity-posts.js.es6
@@ -2,6 +2,7 @@ import { fmt } from 'discourse/lib/computed';
 
 export default Ember.Controller.extend({
   group: Ember.inject.controller(),
+  groupActivity: Ember.inject.controller(),
   loading: false,
   emptyText: fmt('type', 'groups.empty.%@'),
 
@@ -14,7 +15,9 @@ export default Ember.Controller.extend({
         const beforePostId = posts[posts.length-1].get('id');
         const group = this.get('group.model');
 
-        const opts = { beforePostId, type: this.get('type') };
+        let categoryId = this.get('groupActivity.category_id');
+        const opts = { beforePostId, type: this.get('type'), categoryId };
+
         group.findPosts(opts).then(newPosts => {
           posts.addObjects(newPosts);
           this.set('loading', false);
diff --git a/app/assets/javascripts/discourse/controllers/group-activity.js.es6 b/app/assets/javascripts/discourse/controllers/group-activity.js.es6
index 955822f..de4bda4 100644
--- a/app/assets/javascripts/discourse/controllers/group-activity.js.es6
+++ b/app/assets/javascripts/discourse/controllers/group-activity.js.es6
@@ -2,6 +2,7 @@ import computed from 'ember-addons/ember-computed-decorators';
 
 export default Ember.Controller.extend({
   application: Ember.inject.controller(),
+  queryParams: ['category_id'],
 
   @computed('model.is_group_user')
   showGroupMessages(isGroupUser) {
diff --git a/app/assets/javascripts/discourse/models/group.js.es6 b/app/assets/javascripts/discourse/models/group.js.es6
index c7e5eed..c26544e 100644
--- a/app/assets/javascripts/discourse/models/group.js.es6
+++ b/app/assets/javascripts/discourse/models/group.js.es6
@@ -203,12 +203,13 @@ const Group = RestModel.extend({
   findPosts(opts) {
     opts = opts || {};
 
-    const type = opts['type'] || 'posts';
+    const type = opts.type || 'posts';
 
     var data = {};
     if (opts.beforePostId) { data.before_post_id = opts.beforePostId; }
+    if (opts.categoryId) { data.category_id = parseInt(opts.categoryId); }
 
-    return ajax(`/groups/${this.get('name')}/${type}.json`, { data: data }).then(posts => {
+    return ajax(`/groups/${this.get('name')}/${type}.json`, { data }).then(posts => {
       return posts.map(p => {
         p.user = User.create(p.user);
         p.topic = Topic.create(p.topic);
diff --git a/app/assets/javascripts/discourse/routes/group-activity-posts.js.es6 b/app/assets/javascripts/discourse/routes/group-activity-posts.js.es6
index 03cd7ec..1127d36 100644
--- a/app/assets/javascripts/discourse/routes/group-activity-posts.js.es6
+++ b/app/assets/javascripts/discourse/routes/group-activity-posts.js.es6
@@ -6,8 +6,9 @@ export function buildGroupPage(type) {
       return I18n.t(`groups.${type}`);
     },
 
-    model() {
-      return this.modelFor("group").findPosts({ type });
+    model(params, transition) {
+      let categoryId = Ember.get(transition, 'queryParams.category_id');
+      return this.modelFor("group").findPosts({ type, categoryId });
     },
 
     setupController(controller, model) {
diff --git a/app/assets/javascripts/discourse/templates/components/group-activity-filter.hbs b/app/assets/javascripts/discourse/templates/components/group-activity-filter.hbs
new file mode 100644
index 0000000..68623d0
--- /dev/null
+++ b/app/assets/javascripts/discourse/templates/components/group-activity-filter.hbs
@@ -0,0 +1,3 @@
+{{#link-to (concat 'group.activity.' filter) (query-params category_id=categoryId)}}
+  {{i18n (concat 'groups.' filter)}}
+{{/link-to}}
diff --git a/app/assets/javascripts/discourse/templates/group/activity.hbs b/app/assets/javascripts/discourse/templates/group/activity.hbs
index d03a14e..dcc8386 100644
--- a/app/assets/javascripts/discourse/templates/group/activity.hbs
+++ b/app/assets/javascripts/discourse/templates/group/activity.hbs
@@ -1,21 +1,10 @@
 <div class='group-activity container'>
   {{#mobile-nav class='group-activity-nav' desktopClass="pull-left nav nav-stacked" currentPath=application.currentPath}}
-    <li>
-      {{#link-to 'group.activity.posts'}}{{i18n 'groups.posts'}}{{/link-to}}
-    </li>
-
-    <li>
-      {{#link-to 'group.activity.topics'}}{{i18n 'groups.topics'}}{{/link-to}}
-    </li>
-
-    <li>
-      {{#link-to 'group.activity.mentions'}}{{i18n 'groups.mentions'}}{{/link-to}}
-    </li>
-
+    {{group-activity-filter filter="posts" categoryId=category_id}}
+    {{group-activity-filter filter="topics" categoryId=category_id}}
+    {{group-activity-filter filter="mentions" categoryId=category_id}}
     {{#if showGroupMessages}}
-      <li>
-        {{#link-to 'group.activity.messages'}}{{i18n 'groups.messages'}}{{/link-to}}
-      </li>
+      {{group-activity-filter filter="messages"}}
     {{/if}}
   {{/mobile-nav}}
 
diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb
index 2785e69..bd9f079 100644
--- a/app/controllers/groups_controller.rb
+++ b/app/controllers/groups_controller.rb
@@ -69,13 +69,19 @@ class GroupsController < ApplicationController
 
   def posts
     group = find_group(:group_id)
-    posts = group.posts_for(guardian, params[:before_post_id]).limit(20)
+    posts = group.posts_for(
+      guardian,
+      params.permit(:before_post_id, :category_id)
+    ).limit(20)
     render_serialized posts.to_a, GroupPostSerializer
   end
 
   def posts_feed
     group = find_group(:group_id)
-    @posts = group.posts_for(guardian).limit(50)
+    @posts = group.posts_for(
+      guardian,
+      params.permit(:before_post_id, :category_id)
+    ).limit(50)
     @title = "#{SiteSetting.title} - #{I18n.t("rss_description.group_posts", group_name: group.name)}"
     @link = Discourse.base_url
     @description = I18n.t("rss_description.group_posts", group_name: group.name)
@@ -84,19 +90,28 @@ class GroupsController < ApplicationController
 
   def topics
     group = find_group(:group_id)
-    posts = group.posts_for(guardian, params[:before_post_id]).where(post_number: 1).limit(20)
+    posts = group.posts_for(
+      guardian,
+      params.permit(:before_post_id, :category_id)
+    ).where(post_number: 1).limit(20)
     render_serialized posts.to_a, GroupPostSerializer
   end
 
   def mentions
     group = find_group(:group_id)
-    posts = group.mentioned_posts_for(guardian, params[:before_post_id]).limit(20)
+    posts = group.mentioned_posts_for(
+      guardian,
+      params.permit(:before_post_id, :category_id)
+    ).limit(20)
     render_serialized posts.to_a, GroupPostSerializer
   end
 
   def mentions_feed
     group = find_group(:group_id)
-    @posts = group.mentioned_posts_for(guardian).limit(50)
+    @posts = group.mentioned_posts_for(
+      guardian,
+      params.permit(:before_post_id, :category_id)
+    ).limit(50)
     @title = "#{SiteSetting.title} - #{I18n.t("rss_description.group_mentions", group_name: group.name)}"
     @link = Discourse.base_url
     @description = I18n.t("rss_description.group_mentions", group_name: group.name)
@@ -106,7 +121,10 @@ class GroupsController < ApplicationController
   def messages
     group = find_group(:group_id)
     posts = if guardian.can_see_group_messages?(group)
-      group.messages_for(guardian, params[:before_post_id]).where(post_number: 1).limit(20).to_a
+      group.messages_for(
+        guardian,

[... diff too long, it was truncated ...]

GitHub sha: 076df104

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/questions-a-propos-des-groupes/159658/18