FEATURE: automatically archive welcome messages for site_contact_user

This de-clutters the sent messages box for site_contact_user, making it again usable
This commit is contained in:
Sam 2016-01-10 16:46:11 +11:00
parent 1a223b9d10
commit 6fabb341f1
3 changed files with 41 additions and 7 deletions

View File

@ -0,0 +1,24 @@
class ArchiveSystemMessagesWithNoReplies < ActiveRecord::Migration
def up
# backdate archival of system messages send on behalf of site_contact_user
execute <<SQL
INSERT INTO user_archived_messages (user_id, topic_id, created_at, updated_at)
SELECT p.user_id, p.topic_id, p.created_at, p.updated_at
FROM posts p
JOIN topics t ON t.id = p.topic_id
LEFT JOIN user_archived_messages um ON um.user_id = p.user_id AND um.topic_id = p.topic_id
WHERE t.subtype = 'system_message' AND
t.posts_count = 1 AND
t.archetype = 'private_message' AND
um.id IS NULL AND
p.user_id IS NOT NULL AND
p.topic_id IS NOT NULL AND
p.post_number = 1
SQL
end
def down
end
end

View File

@ -23,12 +23,17 @@ class SystemMessage
title = I18n.t("system_messages.#{type}.subject_template", params)
raw = I18n.t("system_messages.#{type}.text_body_template", params)
PostCreator.create(Discourse.site_contact_user,
post = PostCreator.create(Discourse.site_contact_user,
title: title,
raw: raw,
archetype: Archetype.private_message,
target_usernames: @recipient.username,
subtype: TopicSubtype.system_message)
UserArchivedMessage.create!(user: Discourse.site_contact_user, topic: post.topic)
post
end
def create_from_system_user(type, params = {})

View File

@ -4,22 +4,27 @@ require 'topic_subtype'
describe SystemMessage do
let!(:admin) { Fabricate(:admin) }
context 'send' do
let(:user) { Fabricate(:user) }
let(:system_message) { SystemMessage.new(user) }
let(:post) { system_message.create(:welcome_invite) }
let(:topic) { post.topic }
it 'should create a post correctly' do
admin = Fabricate(:admin)
user = Fabricate(:user)
SiteSetting.site_contact_username = admin.username
system_message = SystemMessage.new(user)
post = system_message.create(:welcome_invite)
topic = post.topic
expect(post).to be_present
expect(post).to be_valid
expect(topic).to be_private_message
expect(topic).to be_valid
expect(topic.subtype).to eq(TopicSubtype.system_message)
expect(topic.allowed_users.include?(user)).to eq(true)
expect(topic.allowed_users.include?(admin)).to eq(true)
expect(UserArchivedMessage.where(user_id: admin.id, topic_id: topic.id).length).to eq(1)
end
end