Produce API documentation for:
https://github.com/discourse/discourse/blob/main/app/services/user_updater.rb
Produce API documentation for:
https://github.com/discourse/discourse/blob/main/app/services/user_updater.rb
Retrieved content of app/services/user_updater.rb from discourse/discourse@main
The UserUpdater
class in Discourse is designed to encapsulate all the logic needed to update user-related attributes, preferences, and associations within the Discourse platform. This class facilitates the application of various user attributes through a centralized service layer.
The UserUpdater
class allows for the updating of a range of user attributes from basic profile information to user preferences and notification settings. It handles complex logic such as theme updates, category and tag watching/tracking, and ensuring that changes are compliant with system settings and permissions.
CATEGORY_IDS
: Maps user setting attributes related to category notifications (e.g., watched categories, tracked categories) to their corresponding notification levels.TAG_NAMES
: Similar to CATEGORY_IDS
, maps user setting attributes related to tag notifications to their notification levels.OPTION_ATTR
: An array listing various user options that can be updated, including email settings, theme settings, and UI preferences.The class is initialized with two parameters:
actor
: The user performing the update action.user
: The user being updated.initialize(actor, user)
: Constructor for creating an instance of the UserUpdater
service.update(attributes = {})
: Main method for applying updates to the user. Accepts a hash of attributes to be updated.
The update
method is the primary interface for making changes to a user’s settings and preferences. It requires a hash map of attributes that represent the changes to be applied. The service then validates and applies these changes within the constraints of the current system settings and the user’s permissions.
For example, if a user wants to change their email digest preference and their list of watched tags, the calling code would use the update
method with the respective attributes:
attributes = {
email_digests: false,
watched_tags: 'tag1,tag2'
}
user_updater = UserUpdater.new(actor_user, target_user)
user_updater.update(attributes)
This system allows for a flexible and centralized way of handling user updates, ensuring consistency and adherence to the platform’s rules and permissions.
actor
user has the necessary rights to make the requested changes.This service is a critical component of the Discourse platform, offering a comprehensive and secure approach to managing user data and preferences.