Skip to content

Commit

Permalink
email updates
Browse files Browse the repository at this point in the history
adding email update feature to forum threads. User can opt in to
receive email updates per thread. They can stop following threads too.
  • Loading branch information
griggsk committed Oct 21, 2012
1 parent d82b984 commit 8e7a0b9
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 3 deletions.
24 changes: 22 additions & 2 deletions app/controllers/message_posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def show
redirect_to forum_message_post_url(@forum, @message_post.thread, :anchor => @message_post.id)
else
@child_posts = @message_post.child_posts.paginate :page => params[:page], :order => 'created_at ASC'
@followers = @message_post.followers
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @message_post }
Expand Down Expand Up @@ -53,7 +54,10 @@ def create
flash[:notice] = "Posted: #{@message_post.subject}"
format.html do
if @message_post.thread
@message_post.thread.update_attribute(:updated_at, Time.now)
@message_post.thread.update_attribute(:updated_at, Time.now)
@message_post.thread.followers.each do |poster|
Notifier.email_follower( @message_post, poster).deliver
end
redirect_to forum_message_post_url(@forum, @message_post.thread, :anchor => @message_post.id,
:page => @message_post.thread.child_posts.last_page_number_for)
else
Expand Down Expand Up @@ -99,7 +103,23 @@ def destroy
end



# DELETE /forums/1/message_posts/1/stop_following
# DELETE /forums/1/message_posts/1.xml/stop_following
def stop_following
@message_post = MessagePost.find(params[:id]) #@forum.message_posts.find(params[:id])
posts = @message_post.posts_with_followers
posts.each do |post|
if post.user == current_user
post.update_attribute(:email_forum_activity, 0)
end
end
respond_to do |format|
flash[:notice] = "Stopped following Post '#{@message_post.subject}'."
format.html { redirect_to forum_message_post_url(@forum, @message_post.thread) }
format.xml { head :ok }
end
end

protected
def get_forum
@forum ||= Forum.find(params[:forum_id])
Expand Down
16 changes: 16 additions & 0 deletions app/models/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,20 @@ def published_blog_post_updated(post, updating_user)
@post_link = "Full Post: #{blog_post_url(post)}"
@admin_link = "Blog Admin: #{blog_dashboard_url}"
end

def email_follower(post, follower)
subject "[eFrog - #{post.thread.forum.title.truncate(23)}] #{post.subject.truncate(23)}"
recipients follower.email
from SiteSetting.read_setting('site email') || 'root'
content_type "text/html"
sent_on Time.now

@follower = follower.full_name
@thread = post.thread
@post_title = ActionController::Base.helpers.sanitize(post.subject)
@post_body = ActionController::Base.helpers.sanitize(post.body)
@forum = @thread.forum
@link = forum_message_post_url(@forum, @thread)
end

end
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ def forums
@forums ||= Forum.find(user_groups.map { |g| g.forums.keys }.flatten.uniq)
end

def following_posts
return self.message_posts.find :all, :conditions=>'email_forum_activity = 1'
end

def deliver_password_reset_instructions!
# authlogic provides this:
reset_perishable_token!
Expand Down
4 changes: 4 additions & 0 deletions app/views/message_posts/_message_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<% @followers ||= []%>
<% if current_user -%>
<div id="message_box">
<% opts = message_post.new_record? ? { :url => forum_message_posts_path(@forum) } : { :url => forum_message_post_path(@forum, message_post), :method => :put } %>
<%= form_for message_post, opts do |f| %>
<%= f.error_messages %>
<%= f.hidden_field(:forum_id) %>
<%= f.hidden_field(:thread_id) %>
<%if( !message_post.new_record? || !@followers.include?(current_user) )%>
<p>Email updates to me: <%= f.check_box :email_forum_activity, :value => 1 %></p>
<%end%>
<p>Subject: <%= f.text_field :subject %></p>
<p><%= f.text_area :body, :cols => 60, :rows => 10 %></p>
<p><%= submit_tag "Post it!" %></p>
Expand Down
11 changes: 10 additions & 1 deletion app/views/message_posts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@

<h3>Reply to this thread:</h3>
<%= render :partial => "message_form",
:locals => { :message_post => MessagePost.new(:thread => @message_post, :subject => "RE: #{@message_post.subject}") } %>
:locals => { :message_post => MessagePost.new(:thread => @message_post, :subject => "RE: #{@message_post.subject}"), :followers => [@followers] }
%>


<p class="small_text">
Expand All @@ -44,4 +45,12 @@
<%= link_to 'delete this message thread', forum_message_post_path(@forum, @message_post), :method => :delete,
:confirm => 'Are you sure you want to delete this thread and all the messages in it?' %>
<% end %>
<br />

<h3>Following this Thread</h3>
<ul>
<% for user in @followers -%>
<li><%= user.full_name%> <%if user == current_user%>(<%= link_to 'stop following', stop_following_forum_message_post_path(@forum, @message_post), :method => :post %>)<%end%></li>
<% end -%>
</ul>

12 changes: 12 additions & 0 deletions app/views/notifier/email_follower.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
A new post by <%= @follower%>
<br><br>
Full Thread: <%= @link %>
<br><br>
--------
<%= @post_body %>
--------
<br><br>
This message was sent to you because you signed up to receive email updates for new comments to <%=@thread.subject%> from the eFrog Forum <%=@forum.title%>.
To stop receiving emails go to the <%= link_to('post', @link) %> and click on the 'stop following' link next to your name in the 'Following this Thread' section of the page.
<br><br>
This is an automated email. Do not respond.
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
resources :message_posts do
member do
get "/:user_credentials/feed" => "message_posts#show", :as => :feed
post :stop_following
end
end
end
Expand Down

0 comments on commit 8e7a0b9

Please sign in to comment.