Skip to content

Commit

Permalink
feat: Added "Like Message" operation
Browse files Browse the repository at this point in the history
refactor: validationGuidRegex
  • Loading branch information
umanamente committed Apr 13, 2023
1 parent 8c52e27 commit fc786ff
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 26 deletions.
4 changes: 3 additions & 1 deletion credentials/HabiticaApi.credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
INodeProperties,
} from 'n8n-workflow';

export const AUTHOR_ID = 'a1abf9df-d89d-4f5e-9e80-26dc3c481042';

export class HabiticaApi implements ICredentialType {
name = 'habiticaApi';
displayName = 'Habitica API';
Expand All @@ -27,7 +29,7 @@ export class HabiticaApi implements ICredentialType {
{
displayName: 'Author ID',
name: 'authorId',
default: 'a1abf9df-d89d-4f5e-9e80-26dc3c481042',
default: AUTHOR_ID,
type: 'hidden',
},
// hidden SCRIPT_NAME property
Expand Down
7 changes: 6 additions & 1 deletion nodes/Habitica/operations/chat_messages/Header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { resourceChatMessage } from "./ResourceName";
import { postChatMessageIntoGroupOperation, postChatMessageIntoGroupParameters } from "./functions/ChatMessagePostIntoGroup";
import { IResourceDef } from "../common/CommonDefinitions";
import { markAllAsReadOperation, markAllAsReadParameters } from "./functions/ChatMessageMarkAllAsRead";
import { likeMessageOperation, likeMessageParameters } from "./functions/ChatMessageLike";

export const chatMessagesResourceDefinitions: IResourceDef = {
resource: resourceChatMessage,
Expand All @@ -18,7 +19,11 @@ export const chatMessagesResourceDefinitions: IResourceDef = {
{
operation: markAllAsReadOperation,
parameters: markAllAsReadParameters,
}
},
{
operation: likeMessageOperation,
parameters: likeMessageParameters,
},
],
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { INodeProperties, INodePropertyOptions } from "n8n-workflow";
import { parameterSelectGroup } from "../../../parameters/ParameterSelectGroup";

export const likeMessageOperation: INodePropertyOptions =
{
name: 'Like Message',
value: 'likeMessage',
action: 'Like',
description: 'Like a chat message',
routing: {
request: {
method: 'POST',
url: '=groups/{{$parameter.groupId}}/chat/{{$parameter.messageId}}/like',
},
output: {
postReceive: [
{
type: 'rootProperty',
properties: {
property: 'data',
},
},
],
},
},
};

export const likeMessageParameters : INodeProperties[] = [
// select group
{
...parameterSelectGroup,
displayOptions: {
show: {
operation: [
likeMessageOperation.value,
],
},
},
},
// message ID
// (doesn't make sense to select from a list here,
// it should be obtained from "Get All Messages" node at runtime)
{
displayName: 'Message ID',
name: 'messageId',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
operation: [
likeMessageOperation.value,
],
},
},
},
];
10 changes: 10 additions & 0 deletions nodes/Habitica/parameters/Common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { INodePropertyModeValidation } from "n8n-workflow";
import { AUTHOR_ID } from "../../../credentials/HabiticaApi.credentials";

export const validationGuidRegex : INodePropertyModeValidation = {
type: 'regex',
properties: {
regex: "[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}",
errorMessage: 'The ID must look like this: "' + AUTHOR_ID + '"',
},
};
12 changes: 4 additions & 8 deletions nodes/Habitica/parameters/ParameterSelectGroup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { IDataObject, ILoadOptionsFunctions, INodeListSearchResult, INodeProperties } from "n8n-workflow";
import { habiticaApiRequest } from "../common/HabiticaApiRequest";
import { validationGuidRegex } from "./Common";
import { AUTHOR_ID } from "../../../credentials/HabiticaApi.credentials";


// search groups
Expand Down Expand Up @@ -54,15 +56,9 @@ export const parameterSelectGroup: INodeProperties = {
type: 'string',
hint: 'Enter a group ID',
validation: [
{
type: 'regex',
properties: {
regex: "[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}",
errorMessage: 'The ID must look like this: "884046fa-bc7f-43e2-b1df-b202dc419dcf"',
},
},
validationGuidRegex,
],
placeholder: '884046fa-bc7f-43e2-b1df-b202dc419dcf',
placeholder: AUTHOR_ID,
},
],
};
12 changes: 4 additions & 8 deletions nodes/Habitica/parameters/ParameterSelectPartyMember.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { INodeProperties } from "n8n-workflow";
import { validationGuidRegex } from "./Common";
import { AUTHOR_ID } from "../../../credentials/HabiticaApi.credentials";

/**
* base parameter for selecting a party member
Expand Down Expand Up @@ -27,15 +29,9 @@ export const parameterSelectUserPartyMember: INodeProperties = {
type: 'string',
hint: 'Enter a party member ID',
validation: [
{
type: 'regex',
properties: {
regex: "[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}",
errorMessage: 'The ID must look like this: "a1abf9df-d89d-4f5e-9e80-26dc3c481042"',
},
},
validationGuidRegex,
],
placeholder: 'a1abf9df-d89d-4f5e-9e80-26dc3c481042',
placeholder: AUTHOR_ID,
},
],
};
12 changes: 4 additions & 8 deletions nodes/Habitica/parameters/ParameterSelectTask.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { IDataObject, ILoadOptionsFunctions, INodeListSearchResult, INodeProperties } from "n8n-workflow";
import { habiticaApiRequest } from "../common/HabiticaApiRequest";
import { validationGuidRegex } from "./Common";
import { AUTHOR_ID } from "../../../credentials/HabiticaApi.credentials";


// searchTasks
Expand Down Expand Up @@ -42,15 +44,9 @@ export const parameterSelectTask: INodeProperties = {
type: 'string',
hint: 'Enter a task ID',
validation: [
{
type: 'regex',
properties: {
regex: "[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}",
errorMessage: 'The ID must look like this: "884046fa-bc7f-43e2-b1df-b202dc419dcf"',
},
},
validationGuidRegex,
],
placeholder: '884046fa-bc7f-43e2-b1df-b202dc419dcf',
placeholder: AUTHOR_ID,
},
],
};

0 comments on commit fc786ff

Please sign in to comment.