Side effects with system_user monkey patch?

My team would like a plugin where the system user is a random person from our PR team, as users respond better to real people than robots (even if it’s made obvious that it’s an automated action/message!)

In response, I wrote this code in a plugin:

Discourse.module_eval do
	def self.pr_member
		Group.find_by(name: "PRTeam").users.sample
	end

	def self.system_user
		self.pr_member
	end

	def self.site_contact_user
		self.pr_member
	end
end

This seems to work fine, but are there any side effects I should be aware of for this? Is there a better way?

1 Like

You can set many (most?) messages to come from a regular user in the site settings, so I’m not sure why this would be necessary?

3 Likes

That’s one–we’d like a group.

You mean a random user from a group? It might be a better idea to update the site contact username from within a plugin. You could create a background job that assigns a random member of the group if you wish.

Most likely. The system user is used all over the place. Returning a different, random user every time Discourse.system_user is called might lead to random problems. Also, Discourse often assumes that the system user has -1 as ID (Discourse.SYSTEM_USER_ID isn’t always used) and non-human users (ID < 0) are often excluded in SQL queries, etc. None of that will work anymore, so’ll have to live with the consequences.

In short: Don’t do it or you might break Discourse in unforeseen ways.

2 Likes

This sounds like a better solution to my problem. Where is the site contact username used?

It’s used every time a system message is sent.

https://github.com/discourse/discourse/blob/master/lib/system_message.rb

3 Likes