Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: onUpdate hook for apps #778

Merged
merged 7 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions deno-runtime/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions deno-runtime/handlers/app/handleOnUpdate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { App } from '@rocket.chat/apps-engine/definition/App.ts';

import { AppObjectRegistry } from '../../AppObjectRegistry.ts';
import { AppAccessorsInstance } from '../../lib/accessors/mod.ts';

export default async function handleOnUpdate(params: unknown): Promise<boolean> {
const app = AppObjectRegistry.get<App>('app');

if (typeof app?.onUpdate !== 'function') {
throw new Error('App must contain an onUpdate function', {
cause: 'invalid_app',
});
}

if (!Array.isArray(params)) {
throw new Error('Invalid params', { cause: 'invalid_param_type' });
}

const [context] = params as [Record<string, unknown>];

await app.onUpdate(
context,
AppAccessorsInstance.getReader(),
AppAccessorsInstance.getHttp(),
AppAccessorsInstance.getPersistence(),
AppAccessorsInstance.getModifier(),
);

return true;
}
4 changes: 4 additions & 0 deletions deno-runtime/handlers/app/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import handleOnSettingUpdated from './handleOnSettingUpdated.ts';
import handleListener from '../listener/handler.ts';
import handleUIKitInteraction, { uikitInteractions } from '../uikit/handler.ts';
import { AppObjectRegistry } from '../../AppObjectRegistry.ts';
import handleOnUpdate from './handleOnUpdate.ts';

export default async function handleApp(method: string, params: unknown): Promise<Defined | JsonRpcError> {
const [, appMethod] = method.split(':');
Expand Down Expand Up @@ -83,6 +84,9 @@ export default async function handleApp(method: string, params: unknown): Promis
case 'onSettingUpdated':
result = await handleOnSettingUpdated(params);
break;
case 'onUpdate':
result = await handleOnUpdate(params);
break;
default:
throw new JsonRpcError('Method not found', -32601);
}
Expand Down
8 changes: 8 additions & 0 deletions src/definition/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
IModify,
IPersistence,
IRead,
IAppUpdateContext,
} from './accessors';
import { AppStatus } from './AppStatus';
import type { IApp } from './IApp';
Expand Down Expand Up @@ -181,6 +182,13 @@ export abstract class App implements IApp {
*/
public async onInstall(context: IAppInstallationContext, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify): Promise<void> {}

/**
* Method which is called when the App is updated and it is called one single time.
*
* This method is NOT called when the App is installed.
*/
public async onUpdate(context: IAppUpdateContext, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify): Promise<void> {}

/**
* Method which is called whenever a setting which belongs to this App has been updated
* by an external system and not this App itself. The setting passed is the newly updated one.
Expand Down
6 changes: 6 additions & 0 deletions src/definition/accessors/IAppUpdateContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { IUser } from '../users';

export interface IAppUpdateContext {
user?: IUser;
oldAppVersion: string;
}
1 change: 1 addition & 0 deletions src/definition/accessors/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './IApiExtend';
export * from './IAppAccessors';
export * from './IAppInstallationContext';
export * from './IAppUpdateContext';
export * from './IAppUninstallationContext';
export * from './ICloudWorkspaceRead';
export * from './IConfigurationExtend';
Expand Down
1 change: 1 addition & 0 deletions src/definition/metadata/AppMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export enum AppMethod {
ONDISABLE = 'onDisable',
ONINSTALL = 'onInstall',
ONUNINSTALL = 'onUninstall',
ONUPDATE = 'onUpdate',
ON_PRE_SETTING_UPDATE = 'onPreSettingUpdate',
ONSETTINGUPDATED = 'onSettingUpdated',
SETSTATUS = 'setStatus',
Expand Down
26 changes: 25 additions & 1 deletion src/server/AppManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,11 @@ export class AppManager {
this.apps.delete(app.getID());
}

public async update(appPackage: Buffer, permissionsGranted: Array<IPermission>, updateOptions = { loadApp: true }): Promise<AppFabricationFulfillment> {
public async update(
appPackage: Buffer,
permissionsGranted: Array<IPermission>,
updateOptions: { loadApp?: boolean; user?: IUser } = { loadApp: true },
): Promise<AppFabricationFulfillment> {
const aff = new AppFabricationFulfillment();
const result = await this.getParser().unpackageApp(appPackage);

Expand Down Expand Up @@ -718,6 +722,8 @@ export class AppManager {
.catch(() => {});
}

await this.updateApp(app, updateOptions.user, old.info.version);

return aff;
}

Expand Down Expand Up @@ -925,6 +931,24 @@ export class AppManager {
return result;
}

private async updateApp(app: ProxiedApp, user: IUser | null, oldAppVersion: string): Promise<boolean> {
let result: boolean;

try {
await app.call(AppMethod.ONUPDATE, { oldAppVersion, user });

result = true;
} catch (e) {
const status = AppStatus.ERROR_DISABLED;

result = false;

await app.setStatus(status);
}

return result;
}

private async initializeApp(storageItem: IAppStorageItem, app: ProxiedApp, saveToDb = true, silenceStatus = false): Promise<boolean> {
let result: boolean;

Expand Down
Loading