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

Implemented new functions for list #2517

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export * as util from './util';
export * as newsletter from './newsletter';
export * as whatsapp from './whatsapp';
export * as order from './order';
export * as list from './list';

export {
emit,
Expand Down
67 changes: 67 additions & 0 deletions src/list/functions/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*!
* Copyright 2021 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { assertIsBusiness } from '../../assert';
import { WPPError } from '../../util';
import {
getAllLabelColors,
getNextLabelId,
labelAddAction,
} from '../../whatsapp/functions';
import { colorIsInLabelPalette, getLabelById, getNewLabelColor } from '.';

export interface NewLabelOptions {
/**
* If it's decimal, send it as a number. If it's hexadecimal, send it as a string.
* If labelColor is omitted, the color will be generated automatically
*/
labelColor?: string | number;
}

/**
* Add a new label
* Use await WPP.labels.getLabelColorPalette() to get the list of available colors
* @example
* ```javascript
* await WPP.labels.addNewLabel(`Name of label`);
* //or
* await WPP.labels.addNewLabel(`Name of label`, { labelColor: '#dfaef0' });
* ```
* //or with color index
* await WPP.labels.addNewLabel(`Name of label`, { labelColor: 16 });
* ```
*/
export async function create(labelName: string, options: NewLabelOptions = {}) {
assertIsBusiness();

let labelColor = options.labelColor || undefined;

if (!labelColor) labelColor = await getNewLabelColor();

if (typeof labelColor === 'string' && labelColor.length > 2) {
labelColor = getAllLabelColors().findIndex(
(value: string) => value === labelColor
);
}
labelColor = parseInt(labelColor.toString());

if (!(await colorIsInLabelPalette(labelColor))) {
throw new WPPError('color_not_in_pallet', `Color not in pallet`);
}
const labelId = await getNextLabelId();
await labelAddAction(labelName, labelColor);
return await getLabelById(labelId.toString());
}
17 changes: 17 additions & 0 deletions src/list/functions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*!
* Copyright 2024 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export { create } from './create';
19 changes: 19 additions & 0 deletions src/list/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*!
* Copyright 2022 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import './patch';

export * from './functions/';
28 changes: 28 additions & 0 deletions src/list/patch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*!
* Copyright 2024 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as webpack from '../webpack';
import { wrapModuleFunction } from '../whatsapp/exportModule';
import { isListsEnabled } from '../whatsapp/functions';

webpack.onFullReady(applyPatch, 1000);

// Force show lists buttons
function applyPatch() {
wrapModuleFunction(isListsEnabled, () => {
return true;
});
}
1 change: 1 addition & 0 deletions src/whatsapp/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export * from './joinGroupViaInvite';
export * from './keepMessage';
export * from './labelAddAction';
export * from './labelAddAction';
export * from './listsFunctions';
export * from './markSeen';
export * from './mediaTypeFromProtobuf';
export * from './membershipApprovalRequestAction';
Expand Down
61 changes: 61 additions & 0 deletions src/whatsapp/functions/listsFunctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*!
* Copyright 2024 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { exportModule } from '../exportModule';
import { ChatModel, LabelModel } from '../models';

/** @whatsapp 165820
*/
export declare function createNewListAction(
name: string,
chats: ChatModel[],
colorId: number,
e?: any
): Promise<any>;
export declare function deleteListAction(
a?: any,
b?: any,
c?: any
): Promise<any>;
export declare function editListAction(options: {
entryPoint?: number;
labelModel: LabelModel;
newColor: number;
newName?: string;
updatedAssociatedChats?: ChatModel[];
}): Promise<any>;

export declare function isListsEnabled(): boolean;
export declare function shouldListsSettingsItemBeVisible(): boolean;

exportModule(
exports,
{
createNewListAction: 'createNewListAction',
deleteListAction: 'deleteListAction',
editListAction: 'editListAction',
},
(m) => m.createNewListAction && m.deleteListAction && m.editListAction
);

exportModule(
exports,
{
isListsEnabled: 'isListsEnabled',
shouldListsSettingsItemBeVisible: 'shouldListsSettingsItemBeVisible',
},
(m) => m.isListsEnabled && m.shouldListsSettingsItemBeVisible
);
Loading