Lemmy Plugins
A plugin system for Lemmy is currently in experimental state. At this point the design is not final, so as plugin developer you can help us design a sensible API. You can post any experiences or problems with plugins in the pull request linked above. Only once there is some developer feedback will the plugin system be merged and released.
To get started writing plugins, follow the instructions in Local Development to setup a local test instance from git. Checkout the plugin-system
branch. See the Extism documentation for information how to write a plugin, and have a look at the sample plugin in Golang. To test your plugin, place the compiled .wasm
file in the plugins
folder and start Lemmy.
Plugins are invoked on specific hooks. API hooks are defined based on the HTTP method and path, with the form api_before_{method}_{path}
. You can find the name of specific plugin hooks by running Lemmy, invoking the desired API call and grepping the logs for Calling plugin hook
. Then declare hook in your plugin code.
API before
hooks
These are called before a given API endpoint is invoked, with the raw JSON data submitted by the user, and need to return data in the same format. At this point the data can be invalid, so the actions may still be rejected. These hooks are mainly useful to modify data before it is stored in the database. For example writing a slur filter, automatically marking posts as nsfw or permanently rewriting URLs.
Examples:
api_before_post
with CreatePostapi_post_user_register
with Registerapi_post_community_delete
with DeleteCommunity
API after
hooks
Called after an endpoint was successfully invoked and gets passed the API return data. It also needs to return the same back, which allows temporarily modifying responses or adding extra data to responses. These hooks can also be used to trigger notifications or other external actions.
Examples:
api_after_get_post_list
with GetPostsResponseapi_after_post_comment_like
with CommentResponseapi_after_post_community_ban_user
with BanFromCommunityResponse
Federation hooks
The data structures for federation are completely different from those used in the API, which is why they have separate plugin hooks. Like with the API, there are before
hooks which can be used to permanently change data before it is written to the database. There are also after hooks which can be used for notifications etc.
At the moment only the following hook is available, more will be added later:
federation_before_receive_post
with PostInsertFormfederation_after_receive_post
with Post