Class: GroupMessage
- Inherits:
-
Object
- Object
- GroupMessage
- Defined in:
- app/services/group_message.rb
Overview
GroupMessage sends a private message to a group. It will also avoid sending the same message repeatedly, which can happen with notifications to moderators when spam is detected.
Options:
user: (User) If the is about a user, pass the user object.
limit_once_per: (seconds) Limit sending the given type of once every X seconds.
The default is 24 hours. Set to false to always send the .
Constant Summary collapse
- RECENT_MESSAGE_PERIOD =
3.months
Class Method Summary collapse
Instance Method Summary collapse
- #create ⇒ Object
- #delete_previous!(respect_sent_recently: true, match_raw: true) ⇒ Object
-
#initialize(group_name, message_type, opts = {}) ⇒ GroupMessage
constructor
A new instance of GroupMessage.
- #message_params ⇒ Object
-
#remember_message_sent ⇒ Object
default is to send no more than once every 24 hours (24 * 60 * 60 = 86,400 seconds).
- #sent_recently? ⇒ Boolean
- #sent_recently_key ⇒ Object
Constructor Details
#initialize(group_name, message_type, opts = {}) ⇒ GroupMessage
Returns a new instance of GroupMessage.
22 23 24 25 26 |
# File 'app/services/group_message.rb', line 22 def initialize(group_name, , opts = {}) @group_name = group_name = @opts = opts end |
Class Method Details
.create(group_name, message_type, opts = {}) ⇒ Object
18 19 20 |
# File 'app/services/group_message.rb', line 18 def self.create(group_name, , opts = {}) GroupMessage.new(group_name, , opts).create end |
Instance Method Details
#create ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/services/group_message.rb', line 28 def create return false if sent_recently? post = PostCreator.create( Discourse.system_user, target_group_names: [@group_name], archetype: Archetype., subtype: TopicSubtype., title: I18n.t("system_messages.#{@message_type}.subject_template", ), raw: I18n.t("system_messages.#{@message_type}.text_body_template", ), ) post end |
#delete_previous!(respect_sent_recently: true, match_raw: true) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/services/group_message.rb', line 44 def delete_previous!(respect_sent_recently: true, match_raw: true) return false if respect_sent_recently && sent_recently? posts = Post .joins(topic: { topic_allowed_groups: :group }) .where( topic: { posts_count: 1, user_id: Discourse.system_user, archetype: Archetype., subtype: TopicSubtype., title: I18n.t("system_messages.#{@message_type}.subject_template", ), topic_allowed_groups: { groups: { name: @group_name, }, }, }, ) .where("posts.created_at > ?", RECENT_MESSAGE_PERIOD.ago) if match_raw posts = posts.where( raw: I18n.t("system_messages.#{@message_type}.text_body_template", ).rstrip, ) end posts.find_each { |post| PostDestroyer.new(Discourse.system_user, post).destroy } end |
#message_params ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/services/group_message.rb', line 76 def ||= begin h = { base_url: Discourse.base_url }.merge(@opts[:message_params] || {}) if @opts[:user] h.merge!( username: @opts[:user].username, user_url: user_path(@opts[:user].encoded_username(lower: true)), ) end h end end |
#remember_message_sent ⇒ Object
default is to send no more than once every 24 hours (24 * 60 * 60 = 86,400 seconds)
96 97 98 99 100 |
# File 'app/services/group_message.rb', line 96 def unless @opts[:limit_once_per] == false Discourse.redis.setex(sent_recently_key, @opts[:limit_once_per].try(:to_i) || 86_400, 1) end end |
#sent_recently? ⇒ Boolean
90 91 92 93 |
# File 'app/services/group_message.rb', line 90 def sent_recently? return false if @opts[:limit_once_per] == false Discourse.redis.get(sent_recently_key).present? end |
#sent_recently_key ⇒ Object
102 103 104 |
# File 'app/services/group_message.rb', line 102 def sent_recently_key "grpmsg:#{@group_name}:#{@message_type}:#{@opts[:user] ? @opts[:user].username : ""}" end |