diff --git a/app/mailers/forum_mails.rb b/app/mailers/forum_mails.rb index 80f3355..710a905 100644 --- a/app/mailers/forum_mails.rb +++ b/app/mailers/forum_mails.rb @@ -8,6 +8,22 @@ class ForumMails < ActionMailer::Base @options = options mail(:to => forum_user.email, :subject => "Réinitialisation de votre mot de passe.") end + + + def topic_notification(forum_topic, forum_user, options = {}) + @forum_user = forum_user + @forum_topic = forum_topic + @options = options + mail(:to => forum_user.email, :subject => "Forum Pic Vert - Nouveau fil de discussion") + + end + + def message_notification(forum_message, forum_user, options = {}) + @forum_user = forum_user + @forum_message = forum_message + @options = options + mail(:to => forum_user.email, :subject => "Forum Pic Vert - Nouveau message") + end diff --git a/app/models/followed_topic.rb b/app/models/followed_topic.rb new file mode 100644 index 0000000..331f369 --- /dev/null +++ b/app/models/followed_topic.rb @@ -0,0 +1,4 @@ +class FollowedTopic < ActiveRecord::Base + belongs_to :forum_user + belongs_to :forum_topic +end diff --git a/app/models/forum_message.rb b/app/models/forum_message.rb index 811fc81..34f2107 100644 --- a/app/models/forum_message.rb +++ b/app/models/forum_message.rb @@ -6,8 +6,43 @@ class ForumMessage < ActiveRecord::Base validates :description, :presence => true - belongs_to :topic, :class_name => "ForumTopic" + belongs_to :topic, :class_name => "ForumTopic", :foreign_key => :forum_topic_id belongs_to :forum_user + + + + after_create do + topic = ForumTopic.find(self.forum_topic_id) + + user_to_notify = [] + + ForumUser.where(:new_messages_notifications => true).each do |forum_user| + + user_to_notify << forum_user + + + end + FollowedTopic.where(:forum_topic_id => self.forum_topic_id).each do |followed_topic| + user_to_notify << followed_topic.forum_user + end + + if self.forum_user.follow_participated_topics + folowed_topic = forum_user.followed_topics.find_or_initialize_by_forum_topic_id(topic.id) + folowed_topic.save + end + + + user_to_notify.uniq! + + user_to_notify.each do |forum_user| + ForumMails.message_notification(self, forum_user).deliver + end + + end + + + + def username if self.forum_user self.forum_user.firstname.to_s+" "+ diff --git a/app/models/forum_topic.rb b/app/models/forum_topic.rb index 53d3812..fcab7eb 100644 --- a/app/models/forum_topic.rb +++ b/app/models/forum_topic.rb @@ -4,8 +4,14 @@ class ForumTopic < ActiveRecord::Base belongs_to :forum_user #attr_accessible :description, :title, :forum_messages_attributes, :category_id - + has_many :followed_topics + has_many :forum_users, :through => :followed_topics do + def enabled + where(":followed_topic.enabled = ?", true) + end + end + has_many :active_followers has_many :forum_messages accepts_nested_attributes_for :forum_messages @@ -18,6 +24,49 @@ class ForumTopic < ActiveRecord::Base default_scope :include => :forum_messages, :order => "forum_messages.created_at DESC" + after_create do + topic = self + + user_to_notify = [] + + ForumUser.where(:new_messages_notifications => true).each do |forum_user| + + user_to_notify << forum_user + + + end + + ForumUser.where(:new_topic_notifications => true).each do |forum_user| + + + user_to_notify << forum_user + + end + + ForumUser.where(:follow_new_topics => true).each do |forum_user| + folowed_topic = forum_user.followed_topics.find_or_initialize_by_forum_topic_id(topic.id) + folowed_topic.save + + + end + + FollowedTopic.where(:forum_topic_id => topic.id).each do |followed_topic| + user_to_notify << followed_topic.forum_user + end + + if self.forum_user.follow_participated_topics + folowed_topic = forum_user.followed_topics.find_or_initialize_by_forum_topic_id(topic.id) + folowed_topic.save + end + + + user_to_notify.uniq! + + user_to_notify.each do |forum_user| + ForumMails.topic_notification(topic, forum_user).deliver + end + + end def username if self.forum_user diff --git a/app/models/forum_user.rb b/app/models/forum_user.rb index a735938..e2c7df8 100644 --- a/app/models/forum_user.rb +++ b/app/models/forum_user.rb @@ -1,6 +1,14 @@ class ForumUser < ActiveRecord::Base #attr_accessible :bio, :email, :firstname, :name, :password, :password_confirmation, :avatar, :moderator + + has_many :followed_topics + has_many :forum_topics, :through => :followed_topics do + def enabled + where(":followed_topic.enabled = ?", true) + end + end + has_secure_password validates_presence_of :password, :on => :create validates :email, :presence => true, :uniqueness => true, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i diff --git a/app/views/forum/forum_users/_form.haml b/app/views/forum/forum_users/_form.haml index e73b5d5..f03324b 100644 --- a/app/views/forum/forum_users/_form.haml +++ b/app/views/forum/forum_users/_form.haml @@ -10,6 +10,24 @@ = f.input :avatar, :label => "Image de profil" + %h4 Notifications + %p + Les notifications vous sont envoyées par mail, vous pouvez décider ci-dessous de les recevoir ou non : + %br + %br + = f.input :new_messages_notifications, :label => "recevoir une notification pour tout les nouveaux messages" + = f.input :new_topic_notifications, :label => "recevoir une notification lorsqu'un nouveau fil de discussion est créé" + %h4 Suivi des fils de discussions + %p + Lorsque vous suivez un fil de discussion vous recevez un mail à chaque nouveau message. + %br + %br + =f.input :follow_new_topics, :label => "suivre automatiquement tout les nouveaux fils de discussions" + =f.input :follow_participated_topics, :label => "suivre automatiquement tout les fils de discussions auxquels je participe" + + + + %br =f.submit "Sauvegarder", :class => "btn btn-primary" diff --git a/app/views/forum_mails/message_notification.html.haml b/app/views/forum_mails/message_notification.html.haml new file mode 100644 index 0000000..a5c0975 --- /dev/null +++ b/app/views/forum_mails/message_notification.html.haml @@ -0,0 +1,6 @@ +%h2 Nouveau message sur le fil de discussion : +%h1= @forum_message.topic.title +%p=link_to forum_forum_topic_url(:id => @forum_message.topic.id, :anchor => "forum_message_#{@forum_message.id}"), forum_forum_topic_url(:id => @forum_message.topic.id, :anchor => "forum_message_#{@forum_message.id}") + +%h2 Contenu : +=sanitize @forum_message.description diff --git a/app/views/forum_mails/topic_notification.html.haml b/app/views/forum_mails/topic_notification.html.haml new file mode 100644 index 0000000..9d2243d --- /dev/null +++ b/app/views/forum_mails/topic_notification.html.haml @@ -0,0 +1,6 @@ +%h2 Nouveau fil de discussion : +%h1= @forum_topic.title +%p=link_to forum_forum_topic_url(:id => @forum_topic.id), forum_forum_topic_url(:id => @forum_topic.id) + +%h2 Contenu : +=sanitize @forum_topic.forum_messages.order("created_at ASC").first.description diff --git a/db/migrate/20141121211204_add_notification_to_forum_users.rb b/db/migrate/20141121211204_add_notification_to_forum_users.rb new file mode 100644 index 0000000..34ca202 --- /dev/null +++ b/db/migrate/20141121211204_add_notification_to_forum_users.rb @@ -0,0 +1,11 @@ +class AddNotificationToForumUsers < ActiveRecord::Migration + def change + + add_column :forum_users, :new_messages_notifications, :boolean #recevoir une notification pour tout les nouveaux messages + add_column :forum_users, :new_topic_notifications, :boolean #recevoir une notification lorsqu'un nouveau fil de discussion est lancé + #suivi sur les topics + add_column :forum_users, :follow_new_topics, :boolean #suivre automatiquement tout les nouveaux fils de discussions + add_column :forum_users, :follow_participated_topics, :boolean #suivre automatiquement tout les fils de discussions auxquels je participe + + end +end diff --git a/db/migrate/20141121211542_create_followed_topics.rb b/db/migrate/20141121211542_create_followed_topics.rb new file mode 100644 index 0000000..ae3900c --- /dev/null +++ b/db/migrate/20141121211542_create_followed_topics.rb @@ -0,0 +1,11 @@ +class CreateFollowedTopics < ActiveRecord::Migration + def change + create_table :followed_topics do |t| + t.references :forum_user, index: true + t.references :forum_topic, index: true + t.boolean :enabled + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 3d1d9e7..1dd0fb1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20141030173048) do +ActiveRecord::Schema.define(version: 20141121211542) do create_table "admins", force: true do |t| t.string "email", default: "", null: false @@ -71,6 +71,17 @@ ActiveRecord::Schema.define(version: 20141030173048) do t.datetime "updated_at" end + create_table "followed_topics", force: true do |t| + t.integer "forum_user_id" + t.integer "forum_topic_id" + t.boolean "enabled" + t.datetime "created_at" + t.datetime "updated_at" + end + + add_index "followed_topics", ["forum_topic_id"], name: "index_followed_topics_on_forum_topic_id", using: :btree + add_index "followed_topics", ["forum_user_id"], name: "index_followed_topics_on_forum_user_id", using: :btree + create_table "forum_categories", force: true do |t| t.string "title" t.text "description" @@ -143,6 +154,10 @@ ActiveRecord::Schema.define(version: 20141030173048) do t.boolean "new_message_on_my_topics_notification" t.datetime "last_new_message_on_my_topics_notification" t.string "auth_token" + t.boolean "new_messages_notifications" + t.boolean "new_topic_notifications" + t.boolean "follow_new_topics" + t.boolean "follow_participated_topics" end create_table "forums", force: true do |t| diff --git a/test/fixtures/followed_topics.yml b/test/fixtures/followed_topics.yml new file mode 100644 index 0000000..2fbd035 --- /dev/null +++ b/test/fixtures/followed_topics.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html + +one: + forum_user_id: + forum_topic_id: + enabled: false + +two: + forum_user_id: + forum_topic_id: + enabled: false diff --git a/test/models/followed_topic_test.rb b/test/models/followed_topic_test.rb new file mode 100644 index 0000000..7515a7d --- /dev/null +++ b/test/models/followed_topic_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class FollowedTopicTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end