2023-01-27 03:22:44 -08:00
|
|
|
import type { IDataObject, ILoadOptionsFunctions, INodePropertyOptions } from 'n8n-workflow';
|
|
|
|
import { NodeOperationError } from 'n8n-workflow';
|
2021-09-21 10:38:24 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { apiRequest } from '../transport';
|
2021-09-21 10:38:24 -07:00
|
|
|
|
|
|
|
// Get all the available channels
|
|
|
|
export async function getChannels(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const endpoint = 'channels';
|
|
|
|
const responseData = await apiRequest.call(this, 'GET', endpoint, {});
|
|
|
|
|
|
|
|
if (responseData === undefined) {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'No data got returned');
|
|
|
|
}
|
|
|
|
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
let name: string;
|
|
|
|
for (const data of responseData) {
|
2022-08-17 08:50:24 -07:00
|
|
|
if (data.delete_at !== 0 || !data.display_name || !data.name) {
|
2021-09-21 10:38:24 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
name = `${data.team_display_name} - ${data.display_name || data.name} (${
|
|
|
|
data.type === 'O' ? 'public' : 'private'
|
|
|
|
})`;
|
2021-09-21 10:38:24 -07:00
|
|
|
|
|
|
|
returnData.push({
|
|
|
|
name,
|
|
|
|
value: data.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
returnData.sort((a, b) => {
|
2022-08-17 08:50:24 -07:00
|
|
|
if (a.name < b.name) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a.name > b.name) {
|
|
|
|
return 1;
|
|
|
|
}
|
2021-09-21 10:38:24 -07:00
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all the channels in a team
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function getChannelsInTeam(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
): Promise<INodePropertyOptions[]> {
|
2021-09-21 10:38:24 -07:00
|
|
|
const teamId = this.getCurrentNodeParameter('teamId');
|
|
|
|
const endpoint = `users/me/teams/${teamId}/channels`;
|
|
|
|
const responseData = await apiRequest.call(this, 'GET', endpoint, {});
|
|
|
|
|
|
|
|
if (responseData === undefined) {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'No data got returned');
|
|
|
|
}
|
|
|
|
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
let name: string;
|
|
|
|
for (const data of responseData) {
|
2022-08-17 08:50:24 -07:00
|
|
|
if (data.delete_at !== 0 || !data.display_name || !data.name) {
|
2021-09-21 10:38:24 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const channelTypes: IDataObject = {
|
2022-08-17 08:50:24 -07:00
|
|
|
D: 'direct',
|
|
|
|
G: 'group',
|
|
|
|
O: 'public',
|
|
|
|
P: 'private',
|
2021-09-21 10:38:24 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
name = `${data.display_name} (${channelTypes[data.type as string]})`;
|
|
|
|
|
|
|
|
returnData.push({
|
|
|
|
name,
|
|
|
|
value: data.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
returnData.sort((a, b) => {
|
2022-08-17 08:50:24 -07:00
|
|
|
if (a.name < b.name) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a.name > b.name) {
|
|
|
|
return 1;
|
|
|
|
}
|
2021-09-21 10:38:24 -07:00
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getTeams(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const endpoint = 'users/me/teams';
|
|
|
|
const responseData = await apiRequest.call(this, 'GET', endpoint, {});
|
|
|
|
|
|
|
|
if (responseData === undefined) {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'No data got returned');
|
|
|
|
}
|
|
|
|
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
let name: string;
|
|
|
|
for (const data of responseData) {
|
|
|
|
if (data.delete_at !== 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = `${data.display_name} (${data.type === 'O' ? 'public' : 'private'})`;
|
|
|
|
|
|
|
|
returnData.push({
|
|
|
|
name,
|
|
|
|
value: data.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
returnData.sort((a, b) => {
|
2022-08-17 08:50:24 -07:00
|
|
|
if (a.name < b.name) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a.name > b.name) {
|
|
|
|
return 1;
|
|
|
|
}
|
2021-09-21 10:38:24 -07:00
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getUsers(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const endpoint = 'users';
|
|
|
|
const responseData = await apiRequest.call(this, 'GET', endpoint, {});
|
|
|
|
|
|
|
|
if (responseData === undefined) {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'No data got returned');
|
|
|
|
}
|
|
|
|
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
for (const data of responseData) {
|
|
|
|
if (data.delete_at !== 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
returnData.push({
|
|
|
|
name: data.username,
|
|
|
|
value: data.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
returnData.sort((a, b) => {
|
2022-08-17 08:50:24 -07:00
|
|
|
if (a.name < b.name) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a.name > b.name) {
|
|
|
|
return 1;
|
|
|
|
}
|
2021-09-21 10:38:24 -07:00
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|