Skip to content
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
12 changes: 4 additions & 8 deletions src/catalog/functions/createProduct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@
* limitations under the License.
*/

import { getMyUserId, getMyUserLid } from '../../conn';
import { convertToFile } from '../../util';
import {
OpaqueData,
ProductImageModel,
ProductModel,
UserPrefs,
} from '../../whatsapp';
import { OpaqueData, ProductImageModel, ProductModel } from '../../whatsapp';
import {
addProduct,
calculateFilehashFromBlob,
Expand Down Expand Up @@ -71,8 +67,8 @@ export async function createProduct(
const Product = new ProductModel();
Product.name = params.name.toString();

const mePNWid = UserPrefs.getMaybeMePnUser();
const meLIDWid = UserPrefs.getMaybeMeLidUser();
const mePNWid = getMyUserId();
const meLIDWid = getMyUserLid();

Product.catalogWid = mePNWid || meLIDWid;
Product.imageCdnUrl = url;
Expand Down
14 changes: 12 additions & 2 deletions src/catalog/functions/getMyCatalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
* limitations under the License.
*/

import { CatalogStore, UserPrefs } from '../../whatsapp';
import { getMyUserId } from '../../conn';
import { WPPError } from '../../util';
import { CatalogStore } from '../../whatsapp';
/**
* Get your current catalog
*
Expand All @@ -27,5 +29,13 @@ import { CatalogStore, UserPrefs } from '../../whatsapp';
* @return Your current catalog
*/
export async function getMyCatalog() {
return CatalogStore.get(UserPrefs.getMaybeMePnUser());
const wid = getMyUserId();

if (!wid)
throw new WPPError(
'no_self_user_wid_found_on_get_my_catalog',
`The logged account id is not found`
);

return CatalogStore.get(wid);
}
7 changes: 4 additions & 3 deletions src/chat/events/registerAckMessageEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
* limitations under the License.
*/

import { getMyUserId, getMyUserLid } from '../../conn';
import { internalEv } from '../../eventEmitter';
import * as webpack from '../../webpack';
import { MsgKey, MsgModel, MsgStore, UserPrefs, Wid } from '../../whatsapp';
import { MsgKey, MsgModel, MsgStore, Wid } from '../../whatsapp';
import { wrapModuleFunction } from '../../whatsapp/exportModule';
import {
handleChatSimpleReceipt,
Expand Down Expand Up @@ -47,8 +48,8 @@ function registerAckMessageEvent() {
const chatId: Wid = ackData.from;
const sender: Wid | undefined = ackData.participant || undefined;

const mePNWid = UserPrefs.getMaybeMePnUser();
const meLIDWid = UserPrefs.getMaybeMeLidUser();
const mePNWid = getMyUserId();
const meLIDWid = getMyUserLid();

const remote = ackData.from;
const fromMe =
Expand Down
5 changes: 3 additions & 2 deletions src/chat/functions/sendChargeMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
* limitations under the License.
*/

import { getMyUserId } from '../../conn';
import { getMyUserWid } from '../../conn/functions/getMyUserWid';
import { generateOrderUniqueId, WPPError } from '../../util';
import { CatalogStore, UserPrefs } from '../../whatsapp';
import { CatalogStore } from '../../whatsapp';
import {
currencyForCountryShortcode,
getCountryShortcodeByPhone,
Expand Down Expand Up @@ -180,7 +181,7 @@ export async function sendChargeMessage(
type: 'physical-goods',
payment_configuration: 'merchant_categorization_code',
currency: await currencyForCountryShortcode(
await getCountryShortcodeByPhone(UserPrefs.getMaybeMePnUser().user)
await getCountryShortcodeByPhone(getMyUserId()?.user)
),
total_amount: {
value: total_amount,
Expand Down
8 changes: 5 additions & 3 deletions src/conn/functions/getMigrationState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/

import { isLidMigrated, shouldHaveAccountLid } from '../../whatsapp/functions';
import { Lid1X1MigrationUtils, UserPrefs, Wid } from '../../whatsapp/misc';
import { Lid1X1MigrationUtils, Wid } from '../../whatsapp/misc';
import { getMyUserId } from './getMyUserId';
import { getMyUserLid } from './getMyUserLid';

/**
* Migration state information for the current WhatsApp account
Expand Down Expand Up @@ -93,15 +95,15 @@ export function getMigrationState(): MigrationState {
// Try to get current LID
try {
// Get me user's WID (without device ID)
state.currentLid = UserPrefs.getMaybeMeLidUser();
state.currentLid = getMyUserLid();
} catch (_) {
console.warn('getMaybeMeLidUser function is not available');
}

// Try to get current PN
try {
// Get me user's WID (without device ID)
state.currentPn = UserPrefs.getMaybeMePnUser();
state.currentPn = getMyUserId();
} catch (_) {
console.warn('getMaybeMePnUser function is not available');
}
Expand Down
30 changes: 30 additions & 0 deletions src/conn/functions/getMyUserLid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*!
* 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 { UserPrefs, Wid } from '../../whatsapp';

/**
* Return the current logged user LID
*
* @example
* ```javascript
* const lid = WPP.conn.getMyUserLid();
* console.log(lid.toString()); // Output: 123@lid
* ```
*/
export function getMyUserLid(): Wid | undefined {
return UserPrefs.getMaybeMeLidUser();
}
1 change: 1 addition & 0 deletions src/conn/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export {
export { getMigrationState, MigrationState } from './getMigrationState';
export { getMyDeviceId } from './getMyDeviceId';
export { getMyUserId } from './getMyUserId';
export { getMyUserLid } from './getMyUserLid';
export { getPlatform } from './getPlatform';
export { isAuthenticated } from './isAuthenticated';
export { isIdle } from './isIdle';
Expand Down
16 changes: 11 additions & 5 deletions src/status/functions/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@
*/

import { getMessageById } from '../../chat';
import { getMyUserId } from '../../conn';
import { WPPError } from '../../util';
import { MsgKey, StatusV3Store, UserPrefs } from '../../whatsapp';
import { MsgKey, StatusV3Store } from '../../whatsapp';
import { revokeStatus } from '../../whatsapp/functions';

export async function remove(msgId: string | MsgKey): Promise<boolean> {
const msg = await getMessageById(msgId);
try {
await revokeStatus(
StatusV3Store.get(UserPrefs.getMaybeMePnUser()) as any,
msg
);
const wid = getMyUserId();

if (!wid)
throw new WPPError(
'no_self_user_wid_found_on_remove_status',
`The logged account id is not found`
);

await revokeStatus(StatusV3Store.get(wid) as any, msg);
return true;
} catch (_error) {
throw new WPPError(
Expand Down
43 changes: 5 additions & 38 deletions src/whatsapp/misc/UserPrefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,49 +87,16 @@ export declare namespace UserPrefs {
exportModule(exports, 'UserPrefs', (m: any) => {
const hasNew = typeof m?.getMaybeMePnUser === 'function';
const hasOld = typeof m?.getMaybeMeUser === 'function';
if (!hasNew && !hasOld) return false; // não exporta se nenhuma existir

try {
// se houver prop com tipo errado, apaga antes de setar
if (
m &&
typeof m.getMaybeMeUser !== 'function' &&
//eslint-disable-next-line no-prototype-builtins
m.hasOwnProperty?.('getMaybeMeUser')
) {
try {
delete m.getMaybeMeUser;
} catch {}
}
if (
m &&
typeof m.getMaybeMePnUser !== 'function' &&
//eslint-disable-next-line no-prototype-builtins
m.hasOwnProperty?.('getMaybeMePnUser')
) {
try {
delete m.getMaybeMePnUser;
} catch {}
}
if (!hasNew && !hasOld) return false;

try {
if (hasNew && !hasOld) {
Object.defineProperty(m, 'getMaybeMeUser', {
value: m.getMaybeMePnUser.bind(m),
configurable: true,
writable: true,
});
m.getMaybeMeUser = m.getMaybeMePnUser.bind(m);
} else if (hasOld && !hasNew) {
Object.defineProperty(m, 'getMaybeMePnUser', {
value: m.getMaybeMeUser.bind(m),
configurable: true,
writable: true,
});
m.getMaybeMePnUser = m.getMaybeMeUser.bind(m);
}
} catch {
// silencioso: alguns módulos podem ser proxies/selados
}
} catch {}

// Alguns loaders usam o valor de retorno só como "truthy".
// Retorne 'true' ou o próprio 'm' — ambos funcionam.
return true;
});