Skip to content

Commit

Permalink
feat: Added WPP.privacy.setLastSeen function
Browse files Browse the repository at this point in the history
  • Loading branch information
icleitoncosta committed Jul 26, 2024
1 parent 90e24ae commit 2ebdc06
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/privacy/functions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*!
* 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.
*/

export { get } from './get';
export { getDisallowedList } from './getDisallowedList';
export { setAbout } from './setAbout';
export { setAddGroup } from './setAddGroup';
export { setLastSeen } from './setLastSeen';
export { setOnline } from './setOnline';
export { setProfilePic } from './setProfilePic';
export { setReadReceipts } from './setReadReceipts';
117 changes: 117 additions & 0 deletions src/privacy/functions/prepareDisallowedList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*!
* 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 { WPPError } from '../../util';
import { Wid } from '../../whatsapp';
import {
getPrivacyDisallowedListTable,
PrivacyDisallowedListType,
} from '../../whatsapp/functions';

export async function prepareDisallowedList(
type: PrivacyDisallowedListType,
value: any,
list?: { id: string; action: 'add' | 'remove' }[]
): Promise<{
ids: Wid[];
dhash: number | null;
idsFormatted: { wid: Wid; action: 'add' | 'remove' }[];
allUsers: Wid[];
}> {
if (value !== 'contact_blacklist') {
return {
ids: [],
dhash: null,
idsFormatted: [],
allUsers: [],
};
}
if (
typeof type !== 'string' ||
!Object.values(PrivacyDisallowedListType).includes(type)
) {
throw new WPPError(
'incorrect_type',
`Incorrect type ${type || '<empty>'} for get disalowed list`,
{
type,
}
);
}
const actual = await getPrivacyDisallowedListTable().get(type);
if (!list || list?.length === 0) {
if (!actual) {
throw new WPPError(
'disallowed_list_is_mandatory',
`Disallowed list is empty, please send disallowed list param`
);
}
return {
ids: actual!.disallowedList.map(
(i) => new Wid(i, { intentionallyUsePrivateConstructor: true })
),
dhash: actual.dhash,
idsFormatted: [],
allUsers: actual!.disallowedList.map(
(i) => new Wid(i, { intentionallyUsePrivateConstructor: true })
),
};
} else if (actual == null) {
list = list?.filter((i) => i.action !== 'remove');
return {
ids: list!.map(
(i) => new Wid(i.id, { intentionallyUsePrivateConstructor: true })
),
dhash: null,
idsFormatted: list!.map((i) => {
return {
wid: new Wid(i.id, { intentionallyUsePrivateConstructor: true }),
action: 'add',
};
}),
allUsers: list!.map(
(i) => new Wid(i.id, { intentionallyUsePrivateConstructor: true })
),
};
}
const filtered = actual.disallowedList.filter(
(i) => !list.some((item) => item.action === 'remove' && item.id === i)
);
const filteredList = list.filter((i) => i.action !== 'remove');

const allUsers = ([] as Wid[]).concat(
filteredList!.map(
(i) => new Wid(i.id, { intentionallyUsePrivateConstructor: true })
),
filtered.map(
(i) => new Wid(i, { intentionallyUsePrivateConstructor: true })
)
);

return {
ids: list!.map(
(i) => new Wid(i.id, { intentionallyUsePrivateConstructor: true })
),
dhash: actual.dhash,
idsFormatted: list!.map((i) => {
return {
wid: new Wid(i.id, { intentionallyUsePrivateConstructor: true }),
action: i.action,
};
}),
allUsers: allUsers,
};
}
87 changes: 87 additions & 0 deletions src/privacy/functions/setLastSeen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*!
* 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.
*/

/**
* Set who can see your last seen status.
*
* @example
* ```javascript
* Set value for who can see your last seen like 'all'
* await WPP.privacy.setLastSeen('all');
*
* Set value for who can see your last seen like 'none'
* await WPP.privacy.setLastSeen('none');
*
* Set value for who can see your last seen like 'only your contacts'
* await WPP.privacy.setLastSeen('contacts');
*
* Set value for who can see your last seen like 'your contacts', but with excepts
* await WPP.privacy.setLastSeen('contact_blacklist', [
* { id: '[number]@c.us', action: 'add' },
* { id: '[number]@c.us', action: 'remove' }
* ]);
* ```
*
* @category Privacy
*/

import { WPPError } from '../../util';
import {
getUserPrivacySettings,
PrivacyDisallowedListType,
setPrivacyForOneCategory,
} from '../../whatsapp/functions';
import { prepareDisallowedList } from './prepareDisallowedList';

export enum setLastSeenTypes {
all = 'all',
contacts = 'contacts',
none = 'none',
contact_blacklist = 'contact_blacklist',
}
export async function setLastSeen(
value: setLastSeenTypes,
disallowedList?: { id: string; action: 'add' | 'remove' }[]
): Promise<setLastSeenTypes> {
if (
typeof value !== 'string' ||
!Object.values(setLastSeenTypes).includes(value)
) {
throw new WPPError(
'incorrect_type',
`Incorrect type ${value || '<empty>'} for set last seen privacy`,
{
value,
}
);
}
const disallowed = await prepareDisallowedList(
PrivacyDisallowedListType.LastSeen,
value,
disallowedList
);
await setPrivacyForOneCategory(
{
name: 'last',
value: value,
dhash: value === 'contact_blacklist' ? disallowed.dhash : null,
users:
value === 'contact_blacklist' ? disallowed.idsFormatted : undefined,
},
value === 'contact_blacklist' ? disallowed.allUsers : undefined
);
return getUserPrivacySettings().lastSeen as any;
}
13 changes: 12 additions & 1 deletion src/whatsapp/functions/getPushname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@ import { exportModule } from '../exportModule';

/**
* @whatsapp 135963 >= 2.2310.5
* @whatsapp WAWebUserPrefsGeneral >= 2.3000.0
*/
export declare function getPushname(): string;
export declare function getUserPrivacySettings(): {
about: string;
callAdd: string;
groupAdd: string;
lastSeen: string;
online: string;
profilePicture: string;
readReceipts: string;
};

exportModule(
exports,
{
getPushname: 'getPushname',
getUserPrivacySettings: 'getUserPrivacySettings',
},
(m) => m.getPushname && m.setBrowserId
(m) => m.getPushname && m.setBrowserId && m.getUserPrivacySettings
);

0 comments on commit 2ebdc06

Please sign in to comment.