From 832ad2615141b5d11b215181d1cf405689bfceee Mon Sep 17 00:00:00 2001 From: allburov Date: Sun, 17 Mar 2024 19:04:34 +0700 Subject: [PATCH] [core] Add /messages-admin-only fix #274 --- .../content/en/docs/how-to/engines/index.md | 38 ++++++++++--------- .../content/en/docs/how-to/groups/index.md | 37 +++++++++++++++--- .../content/en/docs/overview/changelog.md | 12 +++++- src/api/groups.controller.ts | 32 +++++++++++++++- src/core/abc/session.abc.ts | 8 ++++ src/core/engines/webjs/session.webjs.core.ts | 16 ++++++++ src/structures/groups.dto.ts | 2 +- 7 files changed, 117 insertions(+), 28 deletions(-) diff --git a/docs/site/content/en/docs/how-to/engines/index.md b/docs/site/content/en/docs/how-to/engines/index.md index ade7ca7a..103c947d 100644 --- a/docs/site/content/en/docs/how-to/engines/index.md +++ b/docs/site/content/en/docs/how-to/engines/index.md @@ -156,24 +156,26 @@ please [create an issue](https://github.com/devlikeapro/whatsapp-http-api/issues | `POST /api/contacts/block` | ✔️ | ➖ | ➖ | | `POST /api/contacts/unblock` | ✔️ | ➖ | ➖ | -| **Groups** | WEBJS | NOWEB | VENOM | -|--------------------------------------------------------------------|:-----:|:-----:|:-----:| -| `POST /api/{session}/groups` | ✔️ | ✔️ | ➖ | -| `GET /api/{session}/groups` | ✔️ | ✔️ | ➖ | -| `GET /api/{session}/groups/{id}` | ✔️ | ✔️ | ➖ | -| `DELETE /api/{session}/groups/{id}` | ✔️ | ➖ | ➖ | -| `GET /api/{session}/groups/{id}/settings/security/info-admin-only` | ✔️ | ➖ | ➖ | -| `PUT /api/{session}/groups/{id}/settings/security/info-admin-only` | ✔️ | ➖ | ➖ | -| `POST /api/{session}/groups/{id}/leave` | ✔️ | ✔️ | ➖ | -| `PUT /api/{session}/groups/{id}/description` | ✔️ | ✔️ | ➖ | -| `PUT /api/{session}/groups/{id}/subject` | ✔️ | ✔️ | ➖ | -| `GET /api/{session}/groups/{id}/invite-code` | ✔️ | ✔️ | ➖ | -| `POST /api/{session}/groups/{id}/invite-code/revoke` | ✔️ | ✔️ | ➖ | -| `GET /api/{session}/groups/{id}/participants` | ✔️ | ✔️ | ➖ | -| `POST /api/{session}/groups/{id}/participants/add` | ✔️ | ✔️ | ➖ | -| `POST /api/{session}/groups/{id}/participants/remove` | ✔️ | ✔️ | ➖ | -| `POST /api/{session}/groups/{id}/admin/promote` | ✔️ | ✔️ | ➖ | -| `POST /api/{session}/groups/{id}/admin/demote` | ✔️ | ✔️ | ➖ | +| **Groups** | WEBJS | NOWEB | VENOM | +|------------------------------------------------------------------------|:-----:|:-----:|:-----:| +| `POST /api/{session}/groups` | ✔️ | ✔️ | ➖ | +| `GET /api/{session}/groups` | ✔️ | ✔️ | ➖ | +| `GET /api/{session}/groups/{id}` | ✔️ | ✔️ | ➖ | +| `DELETE /api/{session}/groups/{id}` | ✔️ | ➖ | ➖ | +| `GET /api/{session}/groups/{id}/settings/security/info-admin-only` | ✔️ | ➖ | ➖ | +| `PUT /api/{session}/groups/{id}/settings/security/info-admin-only` | ✔️ | ➖ | ➖ | +| `GET /api/{session}/groups/{id}/settings/security/messages-admin-only` | ✔️ | ➖ | ➖ | +| `PUT /api/{session}/groups/{id}/settings/security/messages-admin-only` | ✔️ | ➖ | ➖ | +| `POST /api/{session}/groups/{id}/leave` | ✔️ | ✔️ | ➖ | +| `PUT /api/{session}/groups/{id}/description` | ✔️ | ✔️ | ➖ | +| `PUT /api/{session}/groups/{id}/subject` | ✔️ | ✔️ | ➖ | +| `GET /api/{session}/groups/{id}/invite-code` | ✔️ | ✔️ | ➖ | +| `POST /api/{session}/groups/{id}/invite-code/revoke` | ✔️ | ✔️ | ➖ | +| `GET /api/{session}/groups/{id}/participants` | ✔️ | ✔️ | ➖ | +| `POST /api/{session}/groups/{id}/participants/add` | ✔️ | ✔️ | ➖ | +| `POST /api/{session}/groups/{id}/participants/remove` | ✔️ | ✔️ | ➖ | +| `POST /api/{session}/groups/{id}/admin/promote` | ✔️ | ✔️ | ➖ | +| `POST /api/{session}/groups/{id}/admin/demote` | ✔️ | ✔️ | ➖ | | | WEBJS | NOWEB | VENOM | |---------------------------------------------------|:-----:|:-----:|:-----:| diff --git a/docs/site/content/en/docs/how-to/groups/index.md b/docs/site/content/en/docs/how-to/groups/index.md index 912f09fc..eeafa729 100644 --- a/docs/site/content/en/docs/how-to/groups/index.md +++ b/docs/site/content/en/docs/how-to/groups/index.md @@ -87,22 +87,47 @@ Request: } ``` -### Settings -#### Security for group info +### Security - update group info Updates the group settings to only allow admins to edit group info (title, description, photo). +`PUT /api/{session}/groups/{groupId}/settings/security/info-admin-only` +```json +{ + "adminsOnly": true +} +``` + +Get the group settings to only allow admins to edit group info (title, description, photo). + `GET /api/{session}/groups/{groupId}/settings/security/info-admin-only` ```json { "adminsOnly": true } ``` -`PUT /api/{session}/groups/{groupId}/settings/security/info-admin-only` -The request doesn't require any request's body and -**can't be used to disable the security setting (allow ANY to edit group info)**. -If you wish to have that ability - please [create an issue](https://github.com/devlikeapro/whatsapp-http-api/issues) +Returns `true` if the setting was properly updated. This can return false if the user does not have the necessary permissions. +### Security - who can send messages +Updates the group settings to only allow admins to send messages. + +`PUT /api/{session}/groups/{groupId}/settings/security/messages-admin-only` +```json +{ + "adminsOnly": true +} +``` +Returns `true` if the setting was properly updated. This can return false if the user does not have the necessary permissions. + +Get the group settings to only allow admins to send messages. + +`GET /api/{session}/groups/{groupId}/settings/security/messages-admin-only` +```json +{ + "adminsOnly": true +} +``` + ### Participants #### Get participants diff --git a/docs/site/content/en/docs/overview/changelog.md b/docs/site/content/en/docs/overview/changelog.md index b5c0d7a3..8a68f08c 100644 --- a/docs/site/content/en/docs/overview/changelog.md +++ b/docs/site/content/en/docs/overview/changelog.md @@ -21,7 +21,17 @@ You even can **subscribe to get new updates** there! ## 2024.3 - Add [message.reaction]({{< relref "/docs/how-to/webhooks#messagereaction" >}}) webhook in **WEBJS** and **NOWEB** engines ([#275](https://github.com/devlikeapro/whatsapp-http-api/issues/275)). - - 👉 **NOWEB** engine note - reactions were sent in `'message'` and `'message.any'` events, not it's available **only** in `'message.reaction'`! + - **NOWEB** engine note - reactions were sent in `'message'` and `'message.any'` events, not it's available **only** in `'message.reaction'`! +- Add security settings [Security - who can send messages]({{< relref "/docs/how-to/groups##security---who-can-send-messages" >}}) `PUT /api/{session}/groups/{groupId}/settings/security/messages-admin-only` ([#274](https://github.com/devlikeapro/whatsapp-http-api/issues/274)) + +### 👉 Breaking changes +- **NOWEB** - reactions were sent in `'message'` and `'message.any'` events, not it's available **only** in `'message.reaction'`! +- Add **required body** in `PUT /api/{session}/groups/{groupId}/settings/security/info-admin-only` endpoint. +```json +{ + "adminsOnly": false +} +``` ## 2024.2 - Add support for [MongoDB as storage for Session data]({{< relref "/docs/how-to/storages" >}}) diff --git a/src/api/groups.controller.ts b/src/api/groups.controller.ts index a7f14031..f0da8681 100644 --- a/src/api/groups.controller.ts +++ b/src/api/groups.controller.ts @@ -11,6 +11,7 @@ import { ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger'; import { SessionManager } from '../core/abc/manager.abc'; import { WhatsappSession } from '../core/abc/session.abc'; +import { MessageContactVcardRequest } from '../structures/chatting.dto'; import { CreateGroupRequest, DescriptionRequest, @@ -56,11 +57,12 @@ export class GroupsController { summary: 'Updates the group settings to only allow admins to edit group info (title, description, photo).', }) - setGroupAdminOnly( + setInfoAdminOnly( @SessionParam session: WhatsappSession, @Param('id') id: string, + @Body() request: SettingsSecurityChangeInfo, ) { - return session.setInfoAdminsOnly(id, true); + return session.setInfoAdminsOnly(id, request.adminsOnly); } @Get(':id/settings/security/info-admin-only') @@ -76,6 +78,32 @@ export class GroupsController { return session.getInfoAdminsOnly(id); } + @Put(':id/settings/security/messages-admin-only') + @SessionApiParam + @ApiOperation({ + summary: + 'Updates the group settings to only allow admins to send messages.', + }) + setMessagesAdminOnly( + @SessionParam session: WhatsappSession, + @Param('id') id: string, + @Body() request: SettingsSecurityChangeInfo, + ) { + return session.setMessagesAdminsOnly(id, request.adminsOnly); + } + + @Get(':id/settings/security/messages-admin-only') + @SessionApiParam + @ApiOperation({ + summary: 'Gets the group settings to only allow admins to send messages.', + }) + getMessagesAdminOnly( + @SessionParam session: WhatsappSession, + @Param('id') id: string, + ): Promise { + return session.getMessagesAdminsOnly(id); + } + @Delete(':id') @SessionApiParam @ApiOperation({ summary: 'Delete the group.' }) diff --git a/src/core/abc/session.abc.ts b/src/core/abc/session.abc.ts index 9c6e2fcf..179d641a 100644 --- a/src/core/abc/session.abc.ts +++ b/src/core/abc/session.abc.ts @@ -338,6 +338,14 @@ export abstract class WhatsappSession { throw new NotImplementedByEngineError(); } + public getMessagesAdminsOnly(id): Promise { + throw new NotImplementedByEngineError(); + } + + public setMessagesAdminsOnly(id, value) { + throw new NotImplementedByEngineError(); + } + public deleteGroup(id) { throw new NotImplementedByEngineError(); } diff --git a/src/core/engines/webjs/session.webjs.core.ts b/src/core/engines/webjs/session.webjs.core.ts index 880d6ed1..6fe61ea0 100644 --- a/src/core/engines/webjs/session.webjs.core.ts +++ b/src/core/engines/webjs/session.webjs.core.ts @@ -381,6 +381,7 @@ export class WhatsappSessionWebJSCore extends WhatsappSession { public async getInfoAdminsOnly(id): Promise { const groupChat = (await this.whatsapp.getChatById(id)) as GroupChat; return { + // Undocumented property, can be changed in the future // @ts-ignore adminsOnly: groupChat.groupMetadata.restrict, }; @@ -391,6 +392,21 @@ export class WhatsappSessionWebJSCore extends WhatsappSession { return groupChat.setInfoAdminsOnly(value); } + public async getMessagesAdminsOnly(id): Promise { + const groupChat = (await this.whatsapp.getChatById(id)) as GroupChat; + // @ts-ignore + return { + // Undocumented property, can be changed in the future + // @ts-ignore + adminsOnly: groupChat.groupMetadata.announce, + }; + } + + public async setMessagesAdminsOnly(id, value) { + const groupChat = (await this.whatsapp.getChatById(id)) as GroupChat; + return groupChat.setMessagesAdminsOnly(value); + } + public getGroups() { return this.whatsapp .getChats() diff --git a/src/structures/groups.dto.ts b/src/structures/groups.dto.ts index 1c24b596..8644b728 100644 --- a/src/structures/groups.dto.ts +++ b/src/structures/groups.dto.ts @@ -9,7 +9,7 @@ export class Participant { } export class SettingsSecurityChangeInfo { - adminsOnly: boolean; + adminsOnly: boolean = true; } /**