-
Notifications
You must be signed in to change notification settings - Fork 118
/
App.ts
231 lines (207 loc) · 7.63 KB
/
App.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import {
IAppAccessors,
IAppInstallationContext,
IAppUninstallationContext,
IConfigurationExtend,
IConfigurationModify,
IEnvironmentRead,
IHttp,
ILogger,
IModify,
IPersistence,
IRead,
} from './accessors';
import { AppStatus } from './AppStatus';
import { IApp } from './IApp';
import { IAppAuthorInfo } from './metadata/IAppAuthorInfo';
import { IAppInfo } from './metadata/IAppInfo';
import { ISetting } from './settings';
import { ISettingUpdateContext } from './settings/ISettingUpdateContext';
export abstract class App implements IApp {
private status: AppStatus = AppStatus.UNKNOWN;
/**
* Create a new App, this is called whenever the server starts up and initiates the Apps.
* Note, your implementation of this class should call `super(name, id, version)` so we have it.
* Also, please use the `initialize()` method to do items instead of the constructor as the constructor
* *might* be called more than once but the `initialize()` will only be called once.
*/
protected constructor(private readonly info: IAppInfo, private readonly logger: ILogger, private readonly accessors?: IAppAccessors) {
this.logger.debug(`Constructed the App ${this.info.name} (${this.info.id})`,
`v${this.info.version} which depends on the API v${this.info.requiredApiVersion}!`,
`Created by ${this.info.author.name}`);
this.setStatus(AppStatus.CONSTRUCTED);
}
public getStatus(): AppStatus {
return this.status;
}
/**
* Get the name of this App.
*
* @return {string} the name
*/
public getName(): string {
return this.info.name;
}
/**
* Gets the sluggified name of this App.
*
* @return {string} the name slugged
*/
public getNameSlug(): string {
return this.info.nameSlug;
}
/**
* Gets the username of this App's app user.
*
* @return {string} the username of the app user
*
* @deprecated This method will be removed in the next major version.
* Please use read.getAppUser instead.
*/
public getAppUserUsername(): string {
return `${ this.info.nameSlug }.bot`;
}
/**
* Get the ID of this App, please see <link> for how to obtain an ID for your App.
*
* @return {number} the ID
*/
public getID(): string {
return this.info.id;
}
/**
* Get the version of this App, using http://semver.org/.
*
* @return {string} the version
*/
public getVersion(): string {
return this.info.version;
}
/**
* Get the description of this App, mostly used to show to the clients/administrators.
*
* @return {string} the description
*/
public getDescription(): string {
return this.info.description;
}
/**
* Gets the API Version which this App depends on (http://semver.org/).
* This property is used for the dependency injections.
*
* @return {string} the required api version
*/
public getRequiredApiVersion(): string {
return this.info.requiredApiVersion;
}
/**
* Gets the information regarding the author/maintainer of this App.
*
* @return author information
*/
public getAuthorInfo(): IAppAuthorInfo {
return this.info.author;
}
/**
* Gets the entirity of the App's information.
*
* @return App information
*/
public getInfo(): IAppInfo {
return this.info;
}
/**
* Gets the ILogger instance for this App.
*
* @return the logger instance
*/
public getLogger(): ILogger {
return this.logger;
}
public getAccessors(): IAppAccessors {
return this.accessors;
}
/**
* Method which will be called when the App is initialized. This is the recommended place
* to add settings and slash commands. If an error is thrown, all commands will be unregistered.
*/
public async initialize(configurationExtend: IConfigurationExtend, environmentRead: IEnvironmentRead): Promise<void> {
await this.extendConfiguration(configurationExtend, environmentRead);
}
/**
* Method which is called when this App is enabled and can be called several
* times during this instance's life time. Once after the `ititialize()` is called,
* pending it doesn't throw an error, and then anytime the App is enabled by the user.
* If this method, `onEnable()`, returns false, then this App will not
* actually be enabled (ex: a setting isn't configured).
*
* @return whether the App should be enabled or not
*/
public async onEnable(environment: IEnvironmentRead, configurationModify: IConfigurationModify): Promise<boolean> {
return true;
}
/**
* Method which is called when this App is disabled and it can be called several times.
* If this App was enabled and then the user disabled it, this method will be called.
*/
public async onDisable(configurationModify: IConfigurationModify): Promise<void> {
return;
}
/**
* Method which is called when the App is uninstalled and it is called one single time.
*
* This method will NOT be called when an App is getting disabled manually, ONLY when
* it's being uninstalled from Rocket.Chat.
*/
public async onUninstall(context: IAppUninstallationContext, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify): Promise<void> {
return;
}
/**
* Method which is called when the App is installed and it is called one single time.
*
* This method is NOT called when the App is updated.
*/
public async onInstall(context: IAppInstallationContext, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify): Promise<void> {
return;
}
/**
* 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.
*
* @param setting the setting which was updated
* @param configurationModify the accessor to modifiy the system
* @param reader the reader accessor
* @param http an accessor to the outside world
*/
public async onSettingUpdated(setting: ISetting, configurationModify: IConfigurationModify, read: IRead, http: IHttp): Promise<void> {
return;
}
/**
* Method which is called before a setting which belongs to this App is going to be updated
* by an external system and not this App itself. The setting passed is the newly updated one.
*
* @param setting the setting which is going to be updated
* @param configurationModify the accessor to modifiy the system
* @param reader the reader accessor
* @param http an accessor to the outside world
*/
public async onPreSettingUpdate(context: ISettingUpdateContext, configurationModify: IConfigurationModify, read: IRead, http: IHttp): Promise<ISetting> {
return context.newSetting;
}
/**
* Method will be called during initialization. It allows for adding custom configuration options and defaults
* @param configuration
*/
protected async extendConfiguration(configuration: IConfigurationExtend, environmentRead: IEnvironmentRead): Promise<void> {
return;
}
/**
* Sets the status this App is now at, use only when 100% true (it's protected for a reason).
*
* @param status the new status of this App
*/
protected async setStatus(status: AppStatus): Promise<void> {
this.logger.debug(`The status is now: ${ status }`);
this.status = status;
}
}