simple_discussion 0.9.2 → 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 584ec2c3ab8802d72c6f363f9b10ea08065caa9f
4
- data.tar.gz: c53ec10ed28f235c1e5d30df9f76b8ef27c2ba88
3
+ metadata.gz: 51e4842c5231c0e38727a827f611702098fec999
4
+ data.tar.gz: adbe142113f0fbfb27f9fdc008a9ee43f712d4e5
5
5
  SHA512:
6
- metadata.gz: '09cce64a0c017def10cc52dca5482101f62bfcf36669f72de4afc555f1e54b70256d0eb8a16cc661ab55fd58614ffd95c1c6d1a6886d0d900c39db8b71e94048'
7
- data.tar.gz: acb759aa73030f40137b16475a2d51d4d2f0ae387c3592494334eaac4468b33ebb0256f77029d1af1221c9db6dc43301d252cfea7b4e0d5d249b98e6185c370a
6
+ metadata.gz: 384f95fab63ec0c666f1c3eabaa83e29cac9ff87f6dab4cd62d660e741ade40ef38f42ca47ff18c0b62ee56c281fb910230d41362ebbef8dffbddf74e65560c1
7
+ data.tar.gz: 1dbddb156f0e9df228df2787850dcfec7a8b58f32abfdd32251e6f66f9cea2a4c35ad5ce3a82a97dcbbb94a5cd0da0cc11cf32b4db1c83e2037c2763e1132bcc
data/README.md CHANGED
@@ -90,6 +90,19 @@ rails g simple_discussion:helpers
90
90
 
91
91
  **NOTE:** Keep in mind that the more customization you do, the tougher gem upgrades will be in the future.
92
92
 
93
+ ### Email And Slack Notifications
94
+
95
+ By default, SimpleDiscussion will attempt to send email and slack notifications for users subscribed to threads. To turn these off you can do the following in `config/initializers/simple_discussion.rb`
96
+
97
+ ```ruby
98
+ SimpleDiscussion.setup do |config|
99
+ config.send_email_notifications = false # Default: true
100
+ config.send_slack_notifications = false # Default: true
101
+ end
102
+ ```
103
+
104
+ Slack notifications require you to set `simple_discussion_slack_url` in your `config/secrets.yml`. If you don't have this value set, it will not attempt Slack notifications even if they are enabled.
105
+
93
106
  ## Development
94
107
 
95
108
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -9,7 +9,7 @@ class SimpleDiscussion::ForumPostsController < SimpleDiscussion::ApplicationCont
9
9
  @forum_post.user_id = current_user.id
10
10
 
11
11
  if @forum_post.save
12
- #ForumNotificationJob.perform_later(@forum_post.id)
12
+ SimpleDiscussion::ForumPostNotificationJob.perform_later(@forum_post)
13
13
  redirect_to simple_discussion.forum_thread_path(@forum_thread, anchor: "forum_post_#{@forum_post.id}")
14
14
  else
15
15
  render template: "simple_discussion/forum_threads/show"
@@ -42,7 +42,7 @@ class SimpleDiscussion::ForumThreadsController < SimpleDiscussion::ApplicationCo
42
42
  @forum_thread.forum_posts.each{ |post| post.user_id = current_user.id }
43
43
 
44
44
  if @forum_thread.save
45
- #ForumThreadNotificationJob.perform_later(@forum_thread.forum_posts.first.id)
45
+ SimpleDiscussion::ForumThreadNotificationJob.perform_later(@forum_thread)
46
46
  redirect_to simple_discussion.forum_thread_path(@forum_thread)
47
47
  else
48
48
  render action: :new
@@ -0,0 +1,42 @@
1
+ class SimpleDiscussion::ForumPostNotificationJob < ApplicationJob
2
+ include SimpleDiscussion::Engine.routes.url_helpers
3
+
4
+ def perform(forum_post)
5
+ send_emails(forum_post) if SimpleDiscussion.send_email_notifications
6
+ send_webhook(forum_post) if SimpleDiscussion.send_slack_notifications
7
+ end
8
+
9
+ def send_emails(forum_post)
10
+ forum_thread = forum_post.forum_thread
11
+ users = forum_thread.subscribed_users - [forum_post.user]
12
+ users.each do |user|
13
+ SimpleDiscussion::UserMailer.new_post(forum_post, user).deliver_later
14
+ end
15
+ end
16
+
17
+ def send_webhook(forum_post)
18
+ slack_webhook_url = Rails.application.secrets.simple_discussion_slack_url
19
+ return if slack_webhook_url.blank?
20
+
21
+ forum_thread = forum_post.forum_thread
22
+ payload = {
23
+ fallback: "#{forum_post.user.name} replied to <#{forum_thread_url(forum_thread, anchor: ActionView::RecordIdentifier.dom_id(forum_post))}|#{forum_thread.title}>",
24
+ pretext: "#{forum_post.user.name} replied to <#{forum_thread_url(forum_thread, anchor: ActionView::RecordIdentifier.dom_id(forum_post))}|#{forum_thread.title}>",
25
+ fields: [
26
+ {
27
+ title: "Thread",
28
+ value: forum_thread.title,
29
+ short: true
30
+ },
31
+ {
32
+ title: "Posted By",
33
+ value: forum_post.user.name,
34
+ short: true,
35
+ },
36
+ ],
37
+ ts: forum_post.created_at.to_i
38
+ }
39
+
40
+ SimpleDiscussion::Slack.new(slack_webhook_url).post(payload)
41
+ end
42
+ end
@@ -0,0 +1,40 @@
1
+ class SimpleDiscussion::ForumThreadNotificationJob < ApplicationJob
2
+ include SimpleDiscussion::Engine.routes.url_helpers
3
+
4
+ def perform(forum_thread)
5
+ send_emails(forum_thread) if SimpleDiscussion.send_email_notifications
6
+ send_webhook(forum_thread) if SimpleDiscussion.send_slack_notifications
7
+ end
8
+
9
+ def send_emails(forum_thread)
10
+ forum_thread.notify_users.each do |user|
11
+ SimpleDiscussion::UserMailer.new_thread(forum_thread, user).deliver_later
12
+ end
13
+ end
14
+
15
+ def send_webhook(forum_thread)
16
+ slack_webhook_url = Rails.application.secrets.simple_discussion_slack_url
17
+ return if slack_webhook_url.blank?
18
+
19
+ forum_post = forum_thread.forum_posts.first
20
+ payload = {
21
+ fallback: "A new discussion was started: <#{forum_thread_url(forum_thread, anchor: ActionView::RecordIdentifier.dom_id(forum_post))}|#{forum_thread.title}>",
22
+ pretext: "A new discussion was started: <#{forum_thread_url(forum_thread, anchor: ActionView::RecordIdentifier.dom_id(forum_post))}|#{forum_thread.title}>",
23
+ fields: [
24
+ {
25
+ title: "Thread",
26
+ value: forum_thread.title,
27
+ short: true
28
+ },
29
+ {
30
+ title: "Posted By",
31
+ value: forum_post.user.name,
32
+ short: true,
33
+ },
34
+ ],
35
+ ts: forum_post.created_at.to_i
36
+ }
37
+
38
+ SimpleDiscussion::Slack.new(slack_webhook_url).post(payload)
39
+ end
40
+ end
@@ -0,0 +1,28 @@
1
+ class SimpleDiscussion::UserMailer < ApplicationMailer
2
+ # You can set the default `from` address in ApplicationMailer
3
+
4
+ helper SimpleDiscussion::ForumPostsHelper
5
+ helper SimpleDiscussion::Engine.routes.url_helpers
6
+
7
+ def new_thread(forum_thread, recipient)
8
+ @forum_thread = forum_thread
9
+ @forum_post = forum_thread.forum_posts.first
10
+ @recipient = recipient
11
+
12
+ mail(
13
+ to: "#{@recipient.name} <#{@recipient.email}>",
14
+ subject: @forum_thread.title
15
+ )
16
+ end
17
+
18
+ def new_post(forum_post, recipient)
19
+ @forum_post = forum_post
20
+ @forum_thread = forum_post.forum_thread
21
+ @recipient = recipient
22
+
23
+ mail(
24
+ to: "#{@recipient.name} <#{@recipient.email}>",
25
+ subject: "New post in #{@forum_thread.title}"
26
+ )
27
+ end
28
+ end
@@ -72,4 +72,12 @@ class ForumThread < ApplicationRecord
72
72
  "You're not receiving notifications from this thread."
73
73
  end
74
74
  end
75
+
76
+ # These are the users to notify on a new thread. Currently this does nothing,
77
+ # but you can override this to provide whatever functionality you like here.
78
+ #
79
+ # For example: You might use this to send all moderators an email of new threads.
80
+ def notify_users
81
+ []
82
+ end
75
83
  end
@@ -0,0 +1,12 @@
1
+ <div style="position: relative">
2
+ <%= gravatar_image_tag @forum_post.user.email, style: "float: left" %>
3
+
4
+ <div style="margin-left:60px">
5
+ <p><strong><%= @forum_post.user.name %></strong> <small>commented:</small></p>
6
+ <%= formatted_content @forum_post.body %>
7
+ </div>
8
+ </div>
9
+
10
+ <br />
11
+
12
+ <p><%= link_to "Reply to this comment", forum_thread_url(@forum_post.forum_thread, anchor: "forum_post_#{@forum_post.id}"), style: "background:#be2126; color:#fff; text-decoration:none; padding: 10px 20px" %></p>
@@ -0,0 +1,12 @@
1
+ <div style="position: relative">
2
+ <%= gravatar_image_tag @forum_post.user.email, style: "float: left" %>
3
+
4
+ <div style="margin-left:60px">
5
+ <p><strong><%= @forum_post.user.name %></strong> <small>commented:</small></p>
6
+ <%= formatted_content @forum_post.body %>
7
+ </div>
8
+ </div>
9
+
10
+ <br />
11
+
12
+ <p><%= link_to "Reply to this comment", forum_thread_url(@forum_post.forum_thread, anchor: "forum_post_#{@forum_post.id}"), style: "background:#be2126; color:#fff; text-decoration:none; padding: 10px 20px" %></p>
@@ -3,60 +3,20 @@ require 'friendly_id'
3
3
  require 'gravatar_image_tag'
4
4
  require 'will_paginate'
5
5
 
6
- require "simple_discussion/version"
7
- require "simple_discussion/engine"
8
- require "simple_discussion/forum_user"
9
-
10
- require 'will_paginate/view_helpers/action_view'
6
+ require 'simple_discussion/engine'
7
+ require 'simple_discussion/forum_user'
8
+ require 'simple_discussion/slack'
9
+ require 'simple_discussion/version'
10
+ require 'simple_discussion/will_paginate'
11
11
 
12
12
  module SimpleDiscussion
13
- # This code serves two purposes
14
- # 1. It patches will_paginate to work with scoped and mounted Rails engines
15
- # by adding in the url_builder option
16
- # 2. It adds Bootstrap 4 styling to will_paginate
17
-
18
- class BootstrapLinkRenderer < WillPaginate::ActionView::LinkRenderer
19
-
20
- # This method adds the `url_builder` option so we can pass in the
21
- # mounted Rails engine's scope for will_paginate
22
- def url(page)
23
- @base_url_params ||= begin
24
- url_params = merge_get_params(default_url_params)
25
- merge_optional_params(url_params)
26
- end
27
-
28
- url_params = @base_url_params.dup
29
- add_current_page_param(url_params, page)
30
-
31
- # Add optional url_builder support
32
- (@options[:url_builder] || @template).url_for(url_params)
33
- end
34
-
35
- protected
36
- def html_container(html)
37
- tag :nav, tag(:ul, html, class: ul_class)
38
- end
39
-
40
- def page_number(page)
41
- item_class = if(page == current_page)
42
- 'active page-item'
43
- else
44
- 'page-item'
45
- end
46
-
47
- tag :li, link(page, page, :rel => rel_value(page), :class => 'page-link'), :class => item_class
48
- end
49
-
50
- def gap
51
- tag :li, link('&hellip;'.html_safe, '#', :class => 'page-link'), :class => 'page-item disabled'
52
- end
53
-
54
- def previous_or_next_page(page, text, classname)
55
- tag :li, link(text, page || '#', :class => 'page-link'), :class => [(classname[0..3] if @options[:page_links]), (classname if @options[:page_links]), ('disabled' unless page), 'page-item'].join(' ')
56
- end
57
-
58
- def ul_class
59
- ["pagination", container_attributes[:class]].compact.join(" ")
60
- end
13
+ # Define who owns the subscription
14
+ mattr_accessor :send_email_notifications
15
+ mattr_accessor :send_slack_notifications
16
+ @@send_email_notifications = true
17
+ @@send_slack_notifications = true
18
+
19
+ def self.setup
20
+ yield self
61
21
  end
62
22
  end
@@ -1,5 +1,10 @@
1
1
  module SimpleDiscussion
2
2
  class Engine < ::Rails::Engine
3
3
  engine_name 'simple_discussion'
4
+
5
+ # Grab the Rails default url options and use them for sending notifications
6
+ config.after_initialize do
7
+ SimpleDiscussion::Engine.routes.default_url_options = ActionMailer::Base.default_url_options
8
+ end
4
9
  end
5
10
  end
@@ -0,0 +1,22 @@
1
+ require 'open-uri'
2
+ require 'net/http'
3
+
4
+ module SimpleDiscussion
5
+ class Slack
6
+ attr_reader :url
7
+
8
+ def initialize(url)
9
+ @url = url
10
+ end
11
+
12
+ def post(payload)
13
+ uri = URI.parse(url)
14
+ request = Net::HTTP::Post.new(uri)
15
+ req_options = { use_ssl: uri.scheme == "https", }
16
+ request.body = "payload=#{payload.to_json}"
17
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
18
+ http.request(request)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,3 +1,3 @@
1
1
  module SimpleDiscussion
2
- VERSION = "0.9.2"
2
+ VERSION = "0.9.3"
3
3
  end
@@ -0,0 +1,53 @@
1
+ require 'will_paginate/view_helpers/action_view'
2
+
3
+ module SimpleDiscussion
4
+ # This code serves two purposes
5
+ # 1. It patches will_paginate to work with scoped and mounted Rails engines
6
+ # by adding in the url_builder option
7
+ # 2. It adds Bootstrap 4 styling to will_paginate
8
+
9
+ class BootstrapLinkRenderer < WillPaginate::ActionView::LinkRenderer
10
+
11
+ # This method adds the `url_builder` option so we can pass in the
12
+ # mounted Rails engine's scope for will_paginate
13
+ def url(page)
14
+ @base_url_params ||= begin
15
+ url_params = merge_get_params(default_url_params)
16
+ merge_optional_params(url_params)
17
+ end
18
+
19
+ url_params = @base_url_params.dup
20
+ add_current_page_param(url_params, page)
21
+
22
+ # Add optional url_builder support
23
+ (@options[:url_builder] || @template).url_for(url_params)
24
+ end
25
+
26
+ protected
27
+ def html_container(html)
28
+ tag :nav, tag(:ul, html, class: ul_class)
29
+ end
30
+
31
+ def page_number(page)
32
+ item_class = if(page == current_page)
33
+ 'active page-item'
34
+ else
35
+ 'page-item'
36
+ end
37
+
38
+ tag :li, link(page, page, :rel => rel_value(page), :class => 'page-link'), :class => item_class
39
+ end
40
+
41
+ def gap
42
+ tag :li, link('&hellip;'.html_safe, '#', :class => 'page-link'), :class => 'page-item disabled'
43
+ end
44
+
45
+ def previous_or_next_page(page, text, classname)
46
+ tag :li, link(text, page || '#', :class => 'page-link'), :class => [(classname[0..3] if @options[:page_links]), (classname if @options[:page_links]), ('disabled' unless page), 'page-item'].join(' ')
47
+ end
48
+
49
+ def ul_class
50
+ ["pagination", container_attributes[:class]].compact.join(" ")
51
+ end
52
+ end
53
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_discussion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Oliver
@@ -102,6 +102,9 @@ files:
102
102
  - app/controllers/simple_discussion/notifications_controller.rb
103
103
  - app/helpers/simple_discussion/forum_posts_helper.rb
104
104
  - app/helpers/simple_discussion/forum_threads_helper.rb
105
+ - app/jobs/simple_discussion/forum_post_notification_job.rb
106
+ - app/jobs/simple_discussion/forum_thread_notification_job.rb
107
+ - app/mailers/simple_discussion/user_mailer.rb
105
108
  - app/models/forum_category.rb
106
109
  - app/models/forum_post.rb
107
110
  - app/models/forum_subscription.rb
@@ -117,6 +120,8 @@ files:
117
120
  - app/views/simple_discussion/forum_threads/index.html.erb
118
121
  - app/views/simple_discussion/forum_threads/new.html.erb
119
122
  - app/views/simple_discussion/forum_threads/show.html.erb
123
+ - app/views/simple_discussion/user_mailer/new_post.html.erb
124
+ - app/views/simple_discussion/user_mailer/new_thread.html.erb
120
125
  - bin/console
121
126
  - bin/setup
122
127
  - config/routes.rb
@@ -130,7 +135,9 @@ files:
130
135
  - lib/simple_discussion.rb
131
136
  - lib/simple_discussion/engine.rb
132
137
  - lib/simple_discussion/forum_user.rb
138
+ - lib/simple_discussion/slack.rb
133
139
  - lib/simple_discussion/version.rb
140
+ - lib/simple_discussion/will_paginate.rb
134
141
  - simple_discussion.gemspec
135
142
  homepage: https://github.com/excid3/simple_discussion
136
143
  licenses: