diff --git a/app/controllers/activity_feeds_controller.rb b/app/controllers/activity_feeds_controller.rb new file mode 100644 index 000000000..3b9a173af --- /dev/null +++ b/app/controllers/activity_feeds_controller.rb @@ -0,0 +1,7 @@ +class ActivityFeedsController < ApplicationController + before_filter :authenticate_user! + + def index + @activity_feeds = current_user.activity_feeds + end +end diff --git a/app/controllers/stub_controller.rb b/app/controllers/stub_controller.rb new file mode 100644 index 000000000..67a69c51a --- /dev/null +++ b/app/controllers/stub_controller.rb @@ -0,0 +1,24 @@ +class StubController < AbstractController::Base + include AbstractController::Rendering + include AbstractController::Layouts + include AbstractController::Helpers + include AbstractController::Translation + include AbstractController::AssetPaths + include ActionController::UrlWriter + + # Uncomment if you want to use helpers + # defined in ApplicationHelper in your views + # helper ApplicationHelper + + # Make sure your controller can find views + self.view_paths = "app/views" + + # You can define custom helper methods to be used in views here + # helper_method :current_admin + # def current_admin; nil; end + + def partial_to_string(partial_name, locals) + render_to_string(partial_name, :locals => locals) + end + +end diff --git a/app/helpers/activity_feeds_helper.rb b/app/helpers/activity_feeds_helper.rb new file mode 100644 index 000000000..54a624bad --- /dev/null +++ b/app/helpers/activity_feeds_helper.rb @@ -0,0 +1,2 @@ +module ActivityFeedsHelper +end diff --git a/app/models/activity_feed.rb b/app/models/activity_feed.rb new file mode 100644 index 000000000..d636301e9 --- /dev/null +++ b/app/models/activity_feed.rb @@ -0,0 +1,11 @@ +class ActivityFeed < ActiveRecord::Base + belongs_to :user + + def render_body(partial_name, locals={}) + av = ActionView::Base.new(Rails::Configuration.new.view_path) + av.render( + :partial => 'app/views/activity_feeds/partials/' + partial_name + '.haml', + :locals => locals + ) + end +end diff --git a/app/models/activity_feed_observer.rb b/app/models/activity_feed_observer.rb new file mode 100644 index 000000000..22f16dfe3 --- /dev/null +++ b/app/models/activity_feed_observer.rb @@ -0,0 +1,55 @@ +class ActivityFeedObserver < ActiveRecord::Observer + observe :issue, :comment + + def after_create(record) + case record.class.to_s + when 'User' + ActivityFeed.create(:user => record, :body => render_body('new_user_notification')) + when 'Issue' + recipients = record.collect_recipient_ids + recipients.each do |recipient_id| + recipient = User.find(recipient_id) + UserMailer.delay.new_issue_notification(record, recipient) if User.find(recipient).notifier.can_notify && User.find(recipient).notifier.new_issue + ActivityFeed.create(:user => record, :body => render_body('new_issue_notification', {:user => recipient, :issue => record})) + end + + UserMailer.delay.issue_assign_notification(record, record.user) if record.user_id_was != record.user_id && record.user.notifier.issue_assign && record.user.notifier.can_notify + ActivityFeed.create(:user => record.user, :body => render_body('issue_assign_notification', {:user => record.user, :issue => record})) + when 'Comment' + subscribes = record.commentable.subscribes + subscribes.each do |subscribe| + if record.user_id != subscribe.user_id + if record.reply? subscribe + UserMailer.delay.new_comment_reply_notification(record, subscribe.user) if record.can_notify_on_reply?(subscribe) + ActivityFeed.create(:user => record.user, :body => render_body('new_comment_reply_notification', {:user => subscribe.user, :comment => record})) + else + UserMailer.delay.new_comment_notification(record, subscribe.user) if record.can_notify_on_new_comment?(subscribe) + ActivityFeed.create(:user => record.user, :body => render_body('new_comment_notification', {:user => subscribe.user, :comment => record})) + end + end + end + end + end + + def after_update(record) + case record.class.to_s + when 'Issue' + UserMailer.delay.issue_assign_notification(record, record.user) if record.user_id_was != record.user_id && record.user.notifier.issue_assign && record.user.notifier.can_notify + ActivityFeed.create(:user => record.user, :body => render_body('issue_assign_notification', {:user => record.user, :issue => record})) + end + end + + def render_body(partial_name, locals={}) + #ac = ActionController::Base.new + #ac.render_to_string( + # 'app/views/activity_feeds/partials/' + partial_name + '.haml', + # :locals => locals + #) + + #ac = ActionView::Base.new([], locals) + #ac.render(:inline => 'app/views/activity_feeds/partials/' + partial_name + '.haml') + + StubController.new.partial_to_string('activity_feeds/partials/' + partial_name, locals) + end + +end diff --git a/app/models/comment.rb b/app/models/comment.rb index 8ba9c05d0..6b6f5a92a 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -7,22 +7,35 @@ class Comment < ActiveRecord::Base # FIXME after_create :subscribe_on_reply, :unless => "commentable_type == 'Grit::Commit'" - after_create :deliver_new_comment_notification, :unless => "commentable_type == 'Grit::Commit'" + #after_create :deliver_new_comment_notification, :unless => "commentable_type == 'Grit::Commit'" + + def reply?(subscribe) + self.commentable.comments.exists?(:user_id => subscribe.user.id) + end + + def can_notify_on_reply?(subscribe) + User.find(subscribe.user).notifier.new_comment_reply && User.find(subscribe.user).notifier.can_notify + end + + def can_notify_on_new_comment?(subscribe) + User.find(subscribe.user).notifier.new_comment && User.find(subscribe.user).notifier.can_notify + end protected - def deliver_new_comment_notification - subscribes = self.commentable.subscribes - subscribes.each do |subscribe| - if self.user_id != subscribe.user_id && User.find(subscribe.user).notifier.new_comment_reply && User.find(subscribe.user).notifier.can_notify - if self.commentable.comments.exists?(:user_id => subscribe.user.id) - UserMailer.delay.new_comment_reply_notification(self, subscribe.user) - else - UserMailer.delay.new_comment_notification(self, subscribe.user) - end - end - end - end + #def deliver_new_comment_notification + # subscribes = self.commentable.subscribes + # subscribes.each do |subscribe| + # # TODO: new_comment and new_comment_reply - you need to check each variant, not only new_comment_reply... + # if self.user_id != subscribe.user_id && User.find(subscribe.user).notifier.new_comment_reply && User.find(subscribe.user).notifier.can_notify + # if self.reply? subscribe + # UserMailer.delay.new_comment_reply_notification(self, subscribe.user) + # else + # UserMailer.delay.new_comment_notification(self, subscribe.user) + # end + # end + # end + #end def subscribe_on_reply self.commentable.subscribes.create(:user_id => self.user_id) if !self.commentable.subscribes.exists?(:user_id => self.user_id) diff --git a/app/models/issue.rb b/app/models/issue.rb index efeffbf13..0a0084c95 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -19,9 +19,9 @@ class Issue < ActiveRecord::Base after_create :set_serial_id after_create :subscribe_users - after_create :deliver_new_issue_notification - after_create :deliver_issue_assign_notification - after_update :deliver_issue_assign_notification + #after_create :deliver_new_issue_notification + #after_create :deliver_issue_assign_notification + #after_update :deliver_issue_assign_notification after_update :subscribe_issue_assigned_user def assign_uname @@ -45,17 +45,17 @@ class Issue < ActiveRecord::Base self.save! end - def deliver_new_issue_notification - recipients = collect_recipient_ids - recipients.each do |recipient_id| - recipient = User.find(recipient_id) - UserMailer.delay.new_issue_notification(self, recipient) if User.find(recipient).notifier.can_notify && User.find(recipient).notifier.new_issue - end - end + #def deliver_new_issue_notification + # recipients = collect_recipient_ids + # recipients.each do |recipient_id| + # recipient = User.find(recipient_id) + # UserMailer.delay.new_issue_notification(self, recipient) if User.find(recipient).notifier.can_notify && User.find(recipient).notifier.new_issue + # end + #end - def deliver_issue_assign_notification - UserMailer.delay.issue_assign_notification(self, self.user) if self.user_id_was != self.user_id && self.user.notifier.issue_assign && self.user.notifier.can_notify - end + #def deliver_issue_assign_notification + # UserMailer.delay.issue_assign_notification(self, self.user) if self.user_id_was != self.user_id && self.user.notifier.issue_assign && self.user.notifier.can_notify + #end def subscribe_users recipients = collect_recipient_ids diff --git a/app/models/user.rb b/app/models/user.rb index 16a399caa..5183b86b8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,6 +8,8 @@ class User < ActiveRecord::Base has_one :notifier, :class_name => 'Settings::Notifier' #:notifier + has_many :activity_feeds + has_many :authentications, :dependent => :destroy has_many :build_lists, :dependent => :destroy diff --git a/app/views/activity_feeds/partials/issue_assign_notification.haml b/app/views/activity_feeds/partials/issue_assign_notification.haml new file mode 100644 index 000000000..414f8e6c3 --- /dev/null +++ b/app/views/activity_feeds/partials/issue_assign_notification.haml @@ -0,0 +1,7 @@ +%p== Здравствуйте, #{user.name}. + + +%p Вам была назначена задача #{ link_to issue.title, [issue.project, issue] } + + +%p== Команда поддержки «ROSA Build System» diff --git a/app/views/activity_feeds/partials/new_comment_notification.haml b/app/views/activity_feeds/partials/new_comment_notification.haml new file mode 100644 index 000000000..dd09ab701 --- /dev/null +++ b/app/views/activity_feeds/partials/new_comment_notification.haml @@ -0,0 +1,9 @@ +%p== Здравствуйте, #{user.name}. + + +%p К задаче #{ link_to comment.commentable.title, [comment.commentable.project, comment.commentable] } был добавлен новый комментарий. + +%p "#{ comment.body }" + + +%p== Команда поддержки «ROSA Build System» diff --git a/app/views/activity_feeds/partials/new_comment_reply_notification.haml b/app/views/activity_feeds/partials/new_comment_reply_notification.haml new file mode 100644 index 000000000..9aa223deb --- /dev/null +++ b/app/views/activity_feeds/partials/new_comment_reply_notification.haml @@ -0,0 +1,9 @@ +%p== Здравствуйте, #{user.name}. + + +%p На Ваш комментарий в задаче #{ link_to comment.commentable.title, [comment.commentable.project, comment.commentable] } был дан ответ. + +%p "#{ comment.body }" + + +%p== Команда поддержки «ROSA Build System» diff --git a/app/views/activity_feeds/partials/new_issue_notification.haml b/app/views/activity_feeds/partials/new_issue_notification.haml new file mode 100644 index 000000000..a5a139fd6 --- /dev/null +++ b/app/views/activity_feeds/partials/new_issue_notification.haml @@ -0,0 +1,7 @@ +%p== Здравствуйте, #{user.name}. + + +%p К проекту #{ link_to issue.project.name, project_path(issue.project) } была добавлена задача #{ link_to issue.title, [issue.project, issue] } + + +%p== Команда поддержки «ROSA Build System» diff --git a/app/views/activity_feeds/partials/new_user_notification.haml b/app/views/activity_feeds/partials/new_user_notification.haml new file mode 100644 index 000000000..191e2ad5f --- /dev/null +++ b/app/views/activity_feeds/partials/new_user_notification.haml @@ -0,0 +1,12 @@ +%p== Здравствуйте, #{user.name}. + + +%p Вы зарегистрированы на проекте «ROSA Build System» и теперь можете войти в систему. + + +%p + ==Ваш email : #{user.email} + %br/ + ==Ваш пароль: #{user.password} + +%p== Команда поддержки «ROSA Build System» diff --git a/config/application.rb b/config/application.rb index 901d86784..c9d1d710a 100644 --- a/config/application.rb +++ b/config/application.rb @@ -24,7 +24,7 @@ module Rosa # config.plugins = [ :exception_notification, :ssl_requirement, :all ] # Activate observers that should always be running. - config.active_record.observers = :event_log_observer + config.active_record.observers = :event_log_observer, :activity_feed_observer # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. diff --git a/db/migrate/20120124065207_create_activity_feeds.rb b/db/migrate/20120124065207_create_activity_feeds.rb new file mode 100644 index 000000000..6321f4b58 --- /dev/null +++ b/db/migrate/20120124065207_create_activity_feeds.rb @@ -0,0 +1,14 @@ +class CreateActivityFeeds < ActiveRecord::Migration + def self.up + create_table :activity_feeds do |t| + t.integer :user_id, :null => false + t.text :body + + t.timestamps + end + end + + def self.down + drop_table :activity_feeds + end +end diff --git a/db/schema.rb b/db/schema.rb index 825f237ba..038fc0423 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,14 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120117110723) do +ActiveRecord::Schema.define(:version => 20120124065207) do + + create_table "activity_feeds", :force => true do |t| + t.integer "user_id", :null => false + t.text "body" + t.datetime "created_at" + t.datetime "updated_at" + end create_table "arches", :force => true do |t| t.string "name", :null => false @@ -167,13 +174,6 @@ ActiveRecord::Schema.define(:version => 20120117110723) do add_index "issues", ["project_id", "serial_id"], :name => "index_issues_on_project_id_and_serial_id", :unique => true - create_table "permissions", :force => true do |t| - t.integer "right_id" - t.integer "role_id" - t.datetime "created_at" - t.datetime "updated_at" - end - create_table "platforms", :force => true do |t| t.string "description" t.string "name" @@ -270,14 +270,6 @@ ActiveRecord::Schema.define(:version => 20120117110723) do t.string "owner_type" end - create_table "rights", :force => true do |t| - t.string "name", :null => false - t.string "controller", :null => false - t.string "action", :null => false - t.datetime "created_at" - t.datetime "updated_at" - end - create_table "rpms", :force => true do |t| t.string "name", :null => false t.integer "arch_id", :null => false diff --git a/spec/controllers/activity_feeds_controller_spec.rb b/spec/controllers/activity_feeds_controller_spec.rb new file mode 100644 index 000000000..54ce3026a --- /dev/null +++ b/spec/controllers/activity_feeds_controller_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe ActivityFeedsController do + +end diff --git a/spec/factories/activity_feeds.rb b/spec/factories/activity_feeds.rb new file mode 100644 index 000000000..d0e1984cc --- /dev/null +++ b/spec/factories/activity_feeds.rb @@ -0,0 +1,6 @@ +# Read about factories at http://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :activity_feed do + end +end \ No newline at end of file diff --git a/spec/helpers/activity_feeds_helper_spec.rb b/spec/helpers/activity_feeds_helper_spec.rb new file mode 100644 index 000000000..623c526ec --- /dev/null +++ b/spec/helpers/activity_feeds_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the ActivityFeedsHelper. For example: +# +# describe ActivityFeedsHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# helper.concat_strings("this","that").should == "this that" +# end +# end +# end +describe ActivityFeedsHelper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/activity_feed_observer_spec.rb b/spec/models/activity_feed_observer_spec.rb new file mode 100644 index 000000000..378ee2377 --- /dev/null +++ b/spec/models/activity_feed_observer_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe ActivityFeedObserver do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/activity_feed_spec.rb b/spec/models/activity_feed_spec.rb new file mode 100644 index 000000000..c77e1d83a --- /dev/null +++ b/spec/models/activity_feed_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe ActivityFeed do + pending "add some examples to (or delete) #{__FILE__}" +end