From bd83cf7f4c3e51ff40b564b9d1d417e0d6935546 Mon Sep 17 00:00:00 2001
From: Arpit Jalan <arpit@techapj.com>
Date: Fri, 18 Mar 2016 21:49:45 +0530
Subject: [PATCH] FEATURE: add group posts and mentions RSS

---
 app/controllers/groups_controller.rb       | 19 +++++++++++++++++++
 config/locales/server.en.yml               |  2 ++
 config/routes.rb                           |  3 +++
 spec/controllers/groups_controller_spec.rb | 16 ++++++++++++++++
 4 files changed, 40 insertions(+)

diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb
index e33dfa449e1..311a11b2f91 100644
--- a/app/controllers/groups_controller.rb
+++ b/app/controllers/groups_controller.rb
@@ -1,6 +1,7 @@
 class GroupsController < ApplicationController
 
   before_filter :ensure_logged_in, only: [:set_notifications]
+  skip_before_filter :preload_json, :check_xhr, only: [:posts_feed, :mentions_feed]
 
   def show
     render_serialized(find_group(:id), BasicGroupSerializer)
@@ -29,6 +30,15 @@ class GroupsController < ApplicationController
     render_serialized posts.to_a, GroupPostSerializer
   end
 
+  def posts_feed
+    group = find_group(:group_id)
+    @posts = group.posts_for(guardian).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)
+    render 'posts/latest', formats: [:rss]
+  end
+
   def topics
     group = find_group(:group_id)
     posts = group.posts_for(guardian, params[:before_post_id]).where(post_number: 1).limit(20)
@@ -41,6 +51,15 @@ class GroupsController < ApplicationController
     render_serialized posts.to_a, GroupPostSerializer
   end
 
+  def mentions_feed
+    group = find_group(:group_id)
+    @posts = group.mentioned_posts_for(guardian).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)
+    render 'posts/latest', formats: [:rss]
+  end
+
   def messages
     group = find_group(:group_id)
     posts = if guardian.can_see_group_messages?(group)
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 2f1a11bbed6..a85bd940cfb 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -204,6 +204,8 @@ en:
     hot: "Hot topics"
     top: "Top topics"
     posts: "Latest posts"
+    group_posts: "Latest posts from %{group_name}"
+    group_mentions: "Latest mentions from %{group_name}"
   too_late_to_edit: "That post was created too long ago. It can no longer be edited or deleted."
   revert_version_same: "The current version is same as the version you are trying to revert to."
 
diff --git a/config/routes.rb b/config/routes.rb
index ce8a19fc7db..fab10862bb6 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -354,6 +354,9 @@ Discourse::Application.routes.draw do
   get "posts/:username/flagged" => "posts#flagged_posts", constraints: {username: USERNAME_ROUTE_FORMAT}
 
   resources :groups do
+    get "posts.rss" => "groups#posts_feed", format: :rss
+    get "mentions.rss" => "groups#mentions_feed", format: :rss
+
     get 'members'
     get 'posts'
     get 'topics'
diff --git a/spec/controllers/groups_controller_spec.rb b/spec/controllers/groups_controller_spec.rb
index 8a42a6fe636..fded427a080 100644
--- a/spec/controllers/groups_controller_spec.rb
+++ b/spec/controllers/groups_controller_spec.rb
@@ -228,4 +228,20 @@ describe GroupsController do
 
   end
 
+  describe '.posts_feed' do
+    it 'renders RSS' do
+      get :posts_feed, group_id: group.name, format: :rss
+      expect(response).to be_success
+      expect(response.content_type).to eq('application/rss+xml')
+    end
+  end
+
+  describe '.mentions_feed' do
+    it 'renders RSS' do
+      get :mentions_feed, group_id: group.name, format: :rss
+      expect(response).to be_success
+      expect(response.content_type).to eq('application/rss+xml')
+    end
+  end
+
 end