Skip to content

Commit

Permalink
feat: Added WPP.contact.subscribePresence (close wppconnect-team#2283)
Browse files Browse the repository at this point in the history
  • Loading branch information
icleitoncosta committed Oct 24, 2024
1 parent a34963a commit 6a19f1f
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/contact/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ export { getProfilePictureUrl } from './getProfilePictureUrl';
export { getStatus } from './getStatus';
export { ContactListOptions, list } from './list';
export { queryExists } from './queryExists';
export { subscribePresence } from './subscribePresence';
export { unsubscribePresence } from './unsubscribePresence';
52 changes: 52 additions & 0 deletions src/contact/functions/subscribePresence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*!
* 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 { PresenceStore, Wid, WidFactory } from '../../whatsapp';
import { subscribePresence as sendSubscribePresence } from '../../whatsapp/functions';

/**
* Subscribe presente from a contact
*
* @example
* ```javascript
* await WPP.contact.subscribePresence('[number]@c.us');
* ```
*
* @category Contact
*/

export async function subscribePresence(
ids: string | string[]
): Promise<Wid[]> {
if (!Array.isArray(ids)) {
ids = [ids];
}

const result = [];
for (const id of ids) {
try {
const wid = WidFactory.createWid(id);
await sendSubscribePresence(wid);
if (PresenceStore.get(wid)) {
result.push(wid);
continue;
}
await PresenceStore.find(wid);
result.push(wid);
} catch (error) {}
}
return result;
}
49 changes: 49 additions & 0 deletions src/contact/functions/unsubscribePresence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*!
* 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 { PresenceStore, Wid, WidFactory } from '../../whatsapp';

/**
* Unsubscribe presence of a contact
*
* @example
* ```javascript
* await WPP.contact.unsubscribePresence('[number]@c.us');
* ```
*
* @category Contact
*/

export async function unsubscribePresence(
ids: string | string[]
): Promise<Wid[]> {
if (!Array.isArray(ids)) {
ids = [ids];
}

const result = [];
for (const id of ids) {
const wid = WidFactory.createWid(id);
const presence = PresenceStore.get(wid);
if (!presence) {
result.push(wid);
continue;
}
presence.delete();
result.push(wid);
}
return result;
}
1 change: 1 addition & 0 deletions src/whatsapp/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export * from './setPushname';
export * from './status';
export * from './STATUS_JID';
export * from './statusEnable';
export * from './subscribePresence';
export * from './syncABPropsTask';
export * from './typeAttributeFromProtobuf';
export * from './unixTime';
Expand Down
31 changes: 31 additions & 0 deletions src/whatsapp/functions/subscribePresence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*!
* 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 { Wid } from '..';
import { exportModule } from '../exportModule';

/**
* @whatsapp WAWebContactPresenceBridge
*/
export declare function subscribePresence(id: Wid, tcToken?: any): Promise<any>;

exportModule(
exports,
{
subscribePresence: 'subscribePresence',
},
(m) => m.subscribePresence
);

0 comments on commit 6a19f1f

Please sign in to comment.