Permission update events
Some API operations trigger side effects that modify other resources. This article explains why this happens and how to handle it.
Why side effects occur
Root communities organize channels into channel groups. Channels can inherit permissions from their parent group, which means:
- When you change a channel group's permissions, every channel that inherits from it is affected
- When you delete a channel group, all its channels are deleted too
- When you modify an access rule, multiple channels and groups may have their effective permissions recalculated
This creates a challenge: a single API call can affect many resources beyond the one you targeted.
The problem
Suppose your code calls channelGroups.edit() to change a channel group's permissions. Behind the scenes, Root recalculates permissions for every channel that inherits from that group. Some channels might become visible to new roles. Others might lose access for certain members.
If your code maintains any state tied to channels (scheduled messages, cached data, custom metadata), that state is now potentially stale or invalid. You need to know what changed so you can update your own data.
The solution: event handlers
The Root API provides an eventHandlers parameter on methods that can trigger cascading changes. When you pass event handlers, the API calls them for each side effect before returning:
await rootServer.community.channelGroups.edit(request, {
'channel.edited': (evt) => {
// Called for each channel whose permissions changed
},
'channel.deleted': (evt) => {
// Called for each channel that was deleted
},
});
This lets you react immediately to side effects and keep your own state consistent.
When side effects occur
Here are common scenarios that trigger cascading changes:
-
Deleting a channel group: All channels within the group are also deleted. Your code receives
channel.deletedevents for each channel removed. -
Deleting a channel: If that channel was the only one your code could see in the group, your code loses visibility of the entire group. Your code receives both
channel.deletedandchannelGroup.deletedevents. -
Editing a channel group's permissions: Channels that inherit permissions from the group have their effective permissions recalculated. Your code receives
channel.editedevents for affected channels. -
Creating an access rule: If the rule grants a role visibility to a previously hidden channel group, channels inheriting from that group become visible. Your code receives
channel.editedevents reflecting the new permissions. -
Moving a channel to a different group: If the channel inherits permissions from its parent group, its effective permissions change to match the new group. Your code receives a
channel.editedevent with the updated permissions.