n8n/packages/nodes-base/nodes/Matrix/Matrix.node.ts
Iván Ovejero 6dcdb30bf4
refactor: Apply more nodelinting rules (#3324)
* ✏️ Alphabetize lint rules

* 🔥 Remove duplicates

*  Update `lintfix` script

* 👕 Apply `node-param-operation-without-no-data-expression` (#3329)

* 👕 Apply `node-param-operation-without-no-data-expression`

* 👕 Add exceptions

* 👕 Apply `node-param-description-weak` (#3328)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply `node-param-option-value-duplicate` (#3331)

* 👕 Apply `node-param-description-miscased-json` (#3337)

* 👕 Apply `node-param-display-name-excess-inner-whitespace` (#3335)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply `node-param-type-options-missing-from-limit` (#3336)

* Rule workig as intended

* ✏️ Uncomment rules

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply `node-param-option-name-duplicate` (#3338)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply `node-param-description-wrong-for-simplify` (#3334)

*  fix

*  exceptions

*  changed rule ignoring from file to line

* 👕 Apply `node-param-resource-without-no-data-expression` (#3339)

* 👕 Apply `node-param-display-name-untrimmed` (#3341)

* 👕 Apply `node-param-display-name-miscased-id` (#3340)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply `node-param-resource-with-plural-option` (#3342)

* 👕 Apply `node-param-description-wrong-for-upsert` (#3333)

*  fix

*  replaced record with contact in description

*  fix

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply `node-param-option-description-identical-to-name` (#3343)

* 👕 Apply `node-param-option-name-containing-star` (#3347)

* 👕 Apply `node-param-display-name-wrong-for-update-fields` (#3348)

* 👕 Apply `node-param-option-name-wrong-for-get-all` (#3345)

*  fix

*  exceptions

* 👕 Apply node-param-display-name-wrong-for-simplify (#3344)

* Rule working as intended

* Uncomented other rules

* 👕 Undo and add exceptions

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

*  Alphabetize lint rules

*  Restore `lintfix` script

Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com>
Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com>
2022-05-20 23:47:24 +02:00

180 lines
3.7 KiB
TypeScript

import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
ILoadOptionsFunctions,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
handleMatrixCall,
matrixApiRequest,
} from './GenericFunctions';
import {
accountOperations
} from './AccountDescription';
import {
eventFields,
eventOperations,
} from './EventDescription';
import {
mediaFields,
mediaOperations,
} from './MediaDescription';
import {
messageFields,
messageOperations,
} from './MessageDescription';
import {
roomFields,
roomOperations,
} from './RoomDescription';
import {
roomMemberFields,
roomMemberOperations,
} from './RoomMemberDescription';
export class Matrix implements INodeType {
description: INodeTypeDescription = {
displayName: 'Matrix',
name: 'matrix',
icon: 'file:matrix.png',
group: ['output'],
version: 1,
description: 'Consume Matrix API',
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
defaults: {
name: 'Matrix',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'matrixApi',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Account',
value: 'account',
},
{
name: 'Event',
value: 'event',
},
{
name: 'Media',
value: 'media',
},
{
name: 'Message',
value: 'message',
},
{
name: 'Room',
value: 'room',
},
{
name: 'Room Member',
value: 'roomMember',
},
],
default: 'message',
},
...accountOperations,
...eventOperations,
...eventFields,
...mediaOperations,
...mediaFields,
...messageOperations,
...messageFields,
...roomOperations,
...roomFields,
...roomMemberOperations,
...roomMemberFields,
],
};
methods = {
loadOptions: {
async getChannels(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const joinedRoomsResponse = await matrixApiRequest.call(this, 'GET', '/joined_rooms');
await Promise.all(joinedRoomsResponse.joined_rooms.map(async (roomId: string) => {
try {
const roomNameResponse = await matrixApiRequest.call(this, 'GET', `/rooms/${roomId}/state/m.room.name`);
returnData.push({
name: roomNameResponse.name,
value: roomId,
});
} catch (error) {
// TODO: Check, there is probably another way to get the name of this private-chats
returnData.push({
name: `Unknown: ${roomId}`,
value: roomId,
});
}
}));
returnData.sort((a, b) => {
if (a.name < b.name) { return -1; }
if (a.name > b.name) { return 1; }
return 0;
});
return returnData;
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData() as IDataObject[];
const returnData: IDataObject[] = [];
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
for (let i = 0; i < items.length; i++) {
try {
const responseData = await handleMatrixCall.call(this, items[i], i, resource, operation);
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else {
returnData.push(responseData as IDataObject);
}
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ error: error.message });
continue;
}
throw error;
}
}
return [this.helpers.returnJsonArray(returnData)];
}
}