stream interface, data handling logic

This commit is contained in:
Rupenieks 2020-05-19 15:19:35 +02:00
parent 6e0a8a143a
commit 110f368bca
4 changed files with 248 additions and 57 deletions

View file

@ -52,3 +52,14 @@ export async function zulipApiRequest(this: IExecuteFunctions | IWebhookFunction
throw error;
}
}
export function validateJSON(json: string | undefined): any { // tslint:disable-line:no-any
let result;
try {
result = JSON.parse(json!);
} catch (exception) {
result = undefined;
}
return result;
}

View file

@ -48,42 +48,6 @@ export const streamFields = [
/* -------------------------------------------------------------------------- */
/* stream:create */
/* -------------------------------------------------------------------------- */
{
displayName: 'Name',
name: 'name',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'stream',
],
operation: [
'create',
],
},
},
description: 'Name of stream to create.',
},
{
displayName: 'Description',
name: 'description',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'stream',
],
operation: [
'create',
],
},
},
description: 'Description of stream to create.',
},
{
displayName: 'JSON Parameters',
name: 'jsonParameters',
@ -146,6 +110,27 @@ export const streamFields = [
},
},
options: [
{
displayName: 'Announce',
name: 'announce',
type: 'boolean',
default: false,
description: 'If announce is True and one of the streams specified in subscriptions has to be created (i.e. doesnt exist to begin with), an announcement will be made notifying that a new stream was created.',
},
{
displayName: 'Authorization Errors Fatal',
name: 'authorizationErrorsFatal',
type: 'boolean',
default: false,
description: 'A boolean specifying whether authorization errors (such as when the requesting user is not authorized to access a private stream) should be considered fatal or not. When True, an authorization error is reported as such. When set to False, the returned JSON payload indicates that there was an authorization error, but the response is still considered a successful one.',
},
{
displayName: 'History Public to Subscribers',
name: 'historyPublicToSubscribers',
type: 'boolean',
default: false,
description: 'Whether the streams message history should be available to newly subscribed members, or users can only access messages they actually received while subscribed to the stream.',
},
{
displayName: 'Invite Only',
name: 'inviteOnly',
@ -179,18 +164,38 @@ export const streamFields = [
],
},
{
displayName: 'Authorization Errors Fatal',
name: 'authorizationsErrorsFatal',
type: 'boolean',
default: false,
description: 'A boolean specifying whether authorization errors (such as when the requesting user is not authorized to access a private stream) should be considered fatal or not. When True, an authorization error is reported as such. When set to False, the returned JSON payload indicates that there was an authorization error, but the response is still considered a successful one.',
},
{
displayName: 'History Public to Subscribers',
name: 'historyPublicToSubscribers',
type: 'boolean',
default: false,
description: 'Whether the streams message history should be available to newly subscribed members, or users can only access messages they actually received while subscribed to the stream.',
displayName: 'Subscriptions',
name: 'subscriptions',
type: 'fixedCollection',
required: true,
description: '"A list of dictionaries containing the the key name and value specifying the name of the stream to subscribe. If the stream does not exist a new stream is created.',
typeOptions: {
multipleValues: true,
},
options: [
{
displayName: 'Subscription Properties',
name: 'properties',
values: [
{
displayName: 'Name',
name: 'name',
type: 'string',
required: true,
default: '',
description: 'Name of Subscription.',
},
{
displayName: 'Description',
name: 'description',
type: 'string',
required: true,
default: '',
description: 'Description of Subscription.',
}
],
},
],
},
{
displayName: 'Stream Post Policy',
@ -215,14 +220,7 @@ export const streamFields = [
description: 'Only new members can post.'
},
],
},
{
displayName: 'Announce',
name: 'announce',
type: 'boolean',
default: false,
description: 'If announce is True and one of the streams specified in subscriptions has to be created (i.e. doesnt exist to begin with), an announcement will be made notifying that a new stream was created.',
},
}
]
},
/* -------------------------------------------------------------------------- */
@ -330,7 +328,7 @@ export const streamFields = [
],
},
},
description: 'ID of stream to delete.',
description: 'ID of stream to update.',
},
{
displayName: 'JSON Parameters',
@ -455,5 +453,26 @@ export const streamFields = [
},
]
},
/* -------------------------------------------------------------------------- */
/* stream:delete */
/* -------------------------------------------------------------------------- */
{
displayName: 'Stream ID',
name: 'streamId',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'stream',
],
operation: [
'delete',
],
},
},
description: 'ID of stream to delete.',
},
] as INodeProperties[];

View file

@ -0,0 +1,20 @@
export interface IStream {
subscriptions?: [{}];
invite_only?: boolean;
principals?: string[];
authorization_errors_fatal?: boolean;
history_public_to_subscribers?: boolean;
stream_post_policy?: number;
announce?: boolean;
include_public?: boolean;
include_subscribed?: boolean;
include_all_active?: boolean;
include_default?: boolean;
include_owner_subscribed?: boolean;
include_subscribers?: boolean;
description?: string;
new_name?: string;
is_private?: boolean;
is_announcement_only?: boolean;
}

View file

@ -23,6 +23,8 @@ import {
import { snakeCase } from 'change-case';
import { streamFields, streamOperations } from './StreamDescription';
import { userOperations, userFields } from './UserDescription';
import { IStream } from './StreamInterface';
import { validateJSON } from './GenericFunctions';
export class Zulip implements INodeType {
description: INodeTypeDescription = {
@ -220,6 +222,145 @@ export class Zulip implements INodeType {
responseData.uri = `${credentials!.url}${responseData.uri}`;
}
}
if (resource === 'stream') {
const body: IStream = {};
if (operation === 'getAll') {
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
if (additionalFields.includePublic) {
body.include_public = additionalFields.includePublic as boolean;
}
if (additionalFields.includeSubscribed) {
body.include_subscribed = additionalFields.includeSubscribed as boolean;
}
if (additionalFields.includeAllActive) {
body.include_all_active = additionalFields.includeAllActive as boolean;
}
if (additionalFields.includeDefault) {
body.include_default = additionalFields.includeDefault as boolean;
}
if (additionalFields.includeOwnersubscribed) {
body.include_owner_subscribed = additionalFields.includeOwnersubscribed as boolean;
}
responseData = await zulipApiRequest.call(this, 'GET', `/streams`, body);
}
if (operation === 'getSubscribed') {
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
if (additionalFields.includeSubscribers) {
body.include_subscribers = additionalFields.includeSubscribers as boolean;
}
responseData = await zulipApiRequest.call(this, 'GET', `/users/me/subscriptions`, body);
}
if (operation === 'create') {
const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean;
if (jsonParameters) {
const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string;
if (additionalFieldsJson !== '') {
if (validateJSON(additionalFieldsJson) !== undefined) {
Object.assign(body, JSON.parse(additionalFieldsJson));
} else {
throw new Error('Additional fields must be a valid JSON');
}
}
} else {
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
if (additionalFields.subscriptions) {
//@ts-ignore
body.subscriptions = additionalFields.subscriptions.properties as [{}];
}
if (additionalFields.inviteOnly) {
body.invite_only = additionalFields.inviteOnly as boolean;
}
if (additionalFields.principals) {
//@ts-ignore
body.principals = additionalFields.principals.properties as string[];
}
if (additionalFields.authorizationErrorsFatal) {
body.authorization_errors_fatal = additionalFields.authorizationErrorsFatal as boolean;
}
if (additionalFields.historyPublicToSubscribers) {
body.history_public_to_subscribers = additionalFields.historyPublicToSubscribers as boolean;
}
if (additionalFields.streamPostPolicy) {
body.stream_post_policy = additionalFields.streamPostPolicy as number;
}
if (additionalFields.announce) {
body.announce = additionalFields.announce as boolean;
}
}
responseData = await zulipApiRequest.call(this, 'POST', `/users/me/subscriptions`, body);
}
if (operation === 'delete') {
const streamId = this.getNodeParameter('streamId', i) as string;
responseData = await zulipApiRequest.call(this, 'POST', `streams/${streamId}`, {});
}
if (operation === 'update') {
const streamId = this.getNodeParameter('streamId', i) as string;
const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean;
if (jsonParameters) {
const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string;
if (additionalFieldsJson !== '') {
if (validateJSON(additionalFieldsJson) !== undefined) {
Object.assign(body, JSON.parse(additionalFieldsJson));
} else {
throw new Error('Additional fields must be a valid JSON');
}
}
} else {
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
if (additionalFields.description) {
body.description = additionalFields.description as string;
}
if (additionalFields.newName) {
body.new_name = additionalFields.newName as string;
}
if (additionalFields.isPrivate) {
body.is_private = additionalFields.isPrivate as boolean;
}
if (additionalFields.isAnnouncementOnly) {
body.is_announcement_only = additionalFields.isAnnouncementOnly as boolean;
}
if (additionalFields.streamPostPolicy) {
body.stream_post_policy = additionalFields.streamPostPolicy as number;
}
if (additionalFields.historyPublicToSubscribers) {
body.history_public_to_subscribers = additionalFields.historyPublicToSubscribers as boolean;
}
responseData = await zulipApiRequest.call(this, 'PATCH', `/streams/${streamId}`, body);
}
}
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else {