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

allow to register news according to the objects db type and the numbe… #2147

Merged
merged 1 commit into from
Oct 12, 2023
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
26 changes: 26 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,21 @@ class Admin extends utils.Adapter {
}
}

if (showIt && message.noObjects) {
const res = await this.getObjectListAsync({ include_docs: true });
const noObjects = res.rows.length;

showIt = eval(`${noObjects} ${message.noObjects}`);

Check warning

Code scanning / CodeQL

Unsafe code constructed from library input

This string concatenation which depends on [library input](1) is later [interpreted as code](2).
}

if (showIt && message.objectsDbType) {
const objectsDbType = await this.getObjectsDbType();

if (!message.objectsDbType.includes(objectsDbType)) {
showIt = false;
}
}

if (showIt) {
this.log.info(`register notification ${message.class}`);
await this.registerNotification('news', message.class, message.title.en + '\n' + message.content.en);
Expand Down Expand Up @@ -923,6 +938,17 @@ class Admin extends utils.Adapter {
}
}

/**
* Get the objects db type
* @return {Promise<string>}
*/
async getObjectsDbType() {
/** @ts-expect-error */
const diagData = await this.sendToHostAsync(this.host, 'getDiagData', 'normal');
/** @ts-expect-error */
return diagData.objectsType;
}

/**
* Get current npm version from controller
* @returns {Promise<string>}
Expand Down
7 changes: 7 additions & 0 deletions src/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,11 @@ class App extends Router {
.getCompactInstances()
.catch(() => null);

const objectsDbType = (await this.socket.getDiagData(this.state.currentHost, 'normal')).objectsType;

const objects = await this.objectsWorker.getObjects(true);
const noObjects = Object.keys(objects).length;

const checkNews = checkMessages(news, lastNewsId?.val, {
lang: I18n.getLanguage(),
adapters: this.state.adapters,
Expand All @@ -1295,6 +1300,8 @@ class App extends Router {
os: info ? info.os || '?' : '?',
activeRepo: this.state.systemConfig.common.activeRepo,
uuid,
objectsDbType,
noObjects,
});

if (checkNews?.length) {
Expand Down
20 changes: 20 additions & 0 deletions src/src/dialogs/NewsAdminDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ function checkConditions(condition: string, installedVersion: string): boolean {
}
}

type DbType = 'file' | 'jsonl' | 'redis'

interface Context {
adapters: Record<string, any>;
instances: ioBroker.InstanceObject[];
Expand All @@ -152,6 +154,10 @@ interface Context {
activeRepo: string;
uuid?: string;
lang: ioBroker.Languages;
/** Current configured database for objects */
objectsDbType: DbType;
/** Number of objects in the database */
noObjects: number;
}

interface Message {
Expand All @@ -178,6 +184,10 @@ interface Message {
linkTitle?: string;
/** E.g. a base64 encoded image like, data:image/png;base64,iVBORw0KG... */
img?: 'string';
/** e.g. >= 15000 to address installations with more than 15k objects */
noObjects?: string;
/** All object db types which this message is valid for */
objectsDbType?: (DbType)[];
}

export const checkMessages = (messages: Message[], lastMessageId: string, context: Context) => {
Expand Down Expand Up @@ -245,6 +255,16 @@ export const checkMessages = (messages: Message[], lastMessageId: string, contex
}
}

if (showIt && message.noObjects) {
showIt = eval(`${context.noObjects} ${message.noObjects}`);
}

if (showIt && message.objectsDbType) {
if (!message.objectsDbType.includes(context.objectsDbType)) {
showIt = false;
}
}

if (showIt) {
messagesToShow.push({
id: message.id,
Expand Down