2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-06-10 12:17:09 -05:00
|
|
|
# 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 message is about a user, pass the user object.
|
|
|
|
# limit_once_per: (seconds) Limit sending the given type of message once every X seconds.
|
|
|
|
# The default is 24 hours. Set to false to always send the message.
|
|
|
|
|
|
|
|
class GroupMessage
|
|
|
|
include Rails.application.routes.url_helpers
|
|
|
|
|
2021-09-14 17:59:25 -05:00
|
|
|
RECENT_MESSAGE_PERIOD = 3.months
|
|
|
|
|
2013-06-10 12:17:09 -05:00
|
|
|
def self.create(group_name, message_type, opts = {})
|
|
|
|
GroupMessage.new(group_name, message_type, opts).create
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(group_name, message_type, opts = {})
|
|
|
|
@group_name = group_name
|
|
|
|
@message_type = message_type
|
|
|
|
@opts = opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2022-09-13 10:43:24 -05:00
|
|
|
return false if sent_recently?
|
|
|
|
|
|
|
|
post =
|
|
|
|
PostCreator.create(
|
|
|
|
Discourse.system_user,
|
|
|
|
target_group_names: [@group_name],
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
subtype: TopicSubtype.system_message,
|
|
|
|
title: I18n.t("system_messages.#{@message_type}.subject_template", message_params),
|
|
|
|
raw: I18n.t("system_messages.#{@message_type}.text_body_template", message_params),
|
|
|
|
)
|
|
|
|
remember_message_sent
|
|
|
|
post
|
2013-06-10 12:17:09 -05:00
|
|
|
end
|
|
|
|
|
2022-09-13 10:43:24 -05:00
|
|
|
def delete_previous!(respect_sent_recently: true, match_raw: true)
|
|
|
|
return false if respect_sent_recently && sent_recently?
|
|
|
|
|
2021-09-14 17:59:25 -05:00
|
|
|
posts =
|
|
|
|
Post
|
|
|
|
.joins(topic: { topic_allowed_groups: :group })
|
|
|
|
.where(
|
|
|
|
topic: {
|
|
|
|
posts_count: 1,
|
|
|
|
user_id: Discourse.system_user,
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
subtype: TopicSubtype.system_message,
|
|
|
|
title: I18n.t("system_messages.#{@message_type}.subject_template", message_params),
|
|
|
|
topic_allowed_groups: {
|
|
|
|
groups: {
|
|
|
|
name: @group_name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.where("posts.created_at > ?", RECENT_MESSAGE_PERIOD.ago)
|
2021-12-10 07:17:39 -06:00
|
|
|
|
|
|
|
if match_raw
|
|
|
|
posts =
|
|
|
|
posts.where(
|
|
|
|
raw: I18n.t("system_messages.#{@message_type}.text_body_template", message_params).rstrip,
|
|
|
|
)
|
|
|
|
end
|
2021-09-14 17:59:25 -05:00
|
|
|
|
|
|
|
posts.find_each { |post| PostDestroyer.new(Discourse.system_user, post).destroy }
|
|
|
|
end
|
|
|
|
|
2013-06-10 12:17:09 -05:00
|
|
|
def message_params
|
2013-08-26 15:15:59 -05:00
|
|
|
@message_params ||=
|
|
|
|
begin
|
2014-06-30 08:21:26 -05:00
|
|
|
h = { base_url: Discourse.base_url }.merge(@opts[:message_params] || {})
|
2013-08-26 15:15:59 -05:00
|
|
|
if @opts[:user]
|
2024-05-16 10:11:24 -05:00
|
|
|
h.merge!(
|
|
|
|
username: @opts[:user].username,
|
|
|
|
user_url: user_path(@opts[:user].encoded_username(lower: true)),
|
|
|
|
)
|
2023-01-09 06:20:10 -06:00
|
|
|
end
|
|
|
|
h
|
2013-08-26 15:15:59 -05:00
|
|
|
end
|
2013-06-10 12:17:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def sent_recently?
|
|
|
|
return false if @opts[:limit_once_per] == false
|
2019-12-03 03:05:53 -06:00
|
|
|
Discourse.redis.get(sent_recently_key).present?
|
2013-06-10 12:17:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# default is to send no more than once every 24 hours (24 * 60 * 60 = 86,400 seconds)
|
|
|
|
def remember_message_sent
|
2019-12-03 03:05:53 -06:00
|
|
|
unless @opts[:limit_once_per] == false
|
|
|
|
Discourse.redis.setex(sent_recently_key, @opts[:limit_once_per].try(:to_i) || 86_400, 1)
|
2023-01-09 06:20:10 -06:00
|
|
|
end
|
2013-06-10 12:17:09 -05:00
|
|
|
end
|
|
|
|
|
2014-02-17 23:09:24 -06:00
|
|
|
def sent_recently_key
|
|
|
|
"grpmsg:#{@group_name}:#{@message_type}:#{@opts[:user] ? @opts[:user].username : ""}"
|
|
|
|
end
|
2013-06-10 12:17:09 -05:00
|
|
|
end
|