RocketChat / Rocket.Chat.Apps-engine

The Rocket.Chat Apps engine and definitions.
https://rocketchat.github.io/Rocket.Chat.Apps-engine/
MIT License
116 stars 118 forks source link

feat: `onUpdate` hook for apps #778

Closed KevLehman closed 4 months ago

KevLehman commented 4 months ago

What? :boat:

https://rocketchat.atlassian.net/browse/CONN-289 Added hook to be called when an app is updated

Why? :thinking:

Because why not 🤷🏽‍♂️ Serious: allows apps to do stuff only when an update happens, like sending messages or create cron jobs.

Links :earth_americas:

Related PR to get user on context: https://github.com/RocketChat/Rocket.Chat/pull/32719

PS :eyes:

d-gubert commented 4 months ago

Thanks for this!

I just think we're missing here the signature for the method in the App class, like the onInstall event here for example. Regarding contextual information about the event, I'd say it would be cool to know the user who triggered the update and maybe what the previous version was? I'm open to ideas :)

d-gubert commented 4 months ago

Example usage:


export class ExampleApp extends App {
    public async onUpdate(context: IAppUpdateContext, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify): Promise<void> {
        console.log("Hello from update", context);

        const message = modify.getCreator().startMessage();

        const room = await read.getRoomReader().getById("GENERAL");

        if (!room) {
            throw new Error('oh no');
        }

        message.setText("Hello, thank you for updating!").setRoom(room);

        await modify.getCreator().finish(message);
    }
}