Skip to content
This repository was archived by the owner on Nov 5, 2025. It is now read-only.

[NEW] Add ON_PRE_SETTING_UPDATE hook to apps#417

Merged
d-gubert merged 12 commits into
alphafrom
feat/onPreSettingUpdate
May 31, 2021
Merged

[NEW] Add ON_PRE_SETTING_UPDATE hook to apps#417
d-gubert merged 12 commits into
alphafrom
feat/onPreSettingUpdate

Conversation

@shiqimei

@shiqimei shiqimei commented May 13, 2021

Copy link
Copy Markdown
Contributor

What? ⛵

This PR introduces a new onPreSettingUpdate hook into apps. In this way, app developers can validate new setting update before it takes effects.

Why? 🤔

RFC 37

Links 🌎

PS 👀

@shiqimei

shiqimei commented May 20, 2021

Copy link
Copy Markdown
Contributor Author
import {
    IAppAccessors, IConfigurationExtend, IConfigurationModify, IEnvironmentRead, IHttp, ILogger, IRead,
} from '@rocket.chat/apps-engine/definition/accessors';
import { App } from '@rocket.chat/apps-engine/definition/App';
import { InvalidSettingException } from '@rocket.chat/apps-engine/definition/exceptions';
import { IAppInfo } from '@rocket.chat/apps-engine/definition/metadata';
import { ISetting, SettingType } from '@rocket.chat/apps-engine/definition/settings';
import { ISettingUpdateContext } from '../../apps-engine/definition/settings/ISettingUpdateContext';

export class AppsSettings extends App {
    constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
        super(info, logger, accessors);
    }

    public async initialize(configurationExtend: IConfigurationExtend, environmentRead: IEnvironmentRead): Promise<void> {
        configurationExtend.settings.provideSetting({
            id: 'Telegram',
            public: true,
            type: SettingType.SELECT,
            packageValue: 'none',
            value: 'none',
            values: [
                {
                    key: 'none',
                    i18nLabel: 'none',
                },
                {
                    key: 'name',
                    i18nLabel: 'name',
                },
                {
                    key: 'username',
                    i18nLabel: 'username',
                },
                {
                    key: 'nickname',
                    i18nLabel: 'nickname',
                },
            ],
            i18nLabel: 'telegram_agents_display_name',
            i18nDescription: 'telegram_agents_display_name_description',
            required: true,
        });
    }

    public async onPreSettingUpdate(context: ISettingUpdateContext, configurationModify: IConfigurationModify, read: IRead, http: IHttp): Promise<ISetting> {
        const { oldSetting, newSetting: setting } = context;
        console.log({ oldSetting: oldSetting.value, newSetting: setting.value });

        switch (setting.id) {
            case 'Telegram': {
                switch (setting.value) {
                    case 'none':
                        throw new InvalidSettingException('The value of the setting Telegram cannot be `none`');
                }
                break;
            }
        }
        return setting;
    }
}

The app for testing:
apps-settings_0.0.1.zip

screeshots:

image

@shiqimei shiqimei requested a review from a team May 20, 2021 11:29
@codecov

codecov Bot commented May 20, 2021

Copy link
Copy Markdown

Codecov Report

Merging #417 (4a521e0) into alpha (a507056) will increase coverage by 0.04%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##            alpha     #417      +/-   ##
==========================================
+ Coverage   48.67%   48.72%   +0.04%     
==========================================
  Files         103      103              
  Lines        3244     3247       +3     
  Branches      476      478       +2     
==========================================
+ Hits         1579     1582       +3     
  Misses       1665     1665              
Impacted Files Coverage Δ
src/server/managers/AppSettingsManager.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a507056...4a521e0. Read the comment docs.

Comment thread src/definition/App.ts Outdated
Comment thread src/server/ProxiedApp.ts Outdated
Comment thread src/definition/exceptions/Toast.ts Outdated
@shiqimei shiqimei requested a review from d-gubert May 26, 2021 09:00
Comment thread src/definition/exceptions/Toast.ts Outdated
@shiqimei shiqimei requested a review from d-gubert May 28, 2021 12:42
@d-gubert d-gubert merged commit 95eabb7 into alpha May 31, 2021
@d-gubert d-gubert deleted the feat/onPreSettingUpdate branch May 31, 2021 18:58
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants