-
-
Notifications
You must be signed in to change notification settings - Fork 271
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
feat: add group participants #132
Changes from all commits
e995a0e
4e58086
b77a157
89dea8c
cefd739
440d756
a8016e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
openapi: 3.0.0 | ||
info: | ||
title: WhatsApp API MultiDevice | ||
version: 3.10.1 | ||
version: 3.11.0 | ||
description: This API is used for sending whatsapp via API | ||
servers: | ||
- url: http://localhost:3000 | ||
|
@@ -773,7 +773,53 @@ paths: | |
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/SendResponse' | ||
$ref: '#/components/schemas/CreateGroupResponse' | ||
'400': | ||
description: Bad Request | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/ErrorBadRequest' | ||
'500': | ||
description: Internal Server Error | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/ErrorInternalServer' | ||
/group/participants: | ||
post: | ||
operationId: addParticipantToGroup | ||
tags: | ||
- group | ||
summary: Adding more participants to group | ||
requestBody: | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
properties: | ||
group_id: | ||
type: string | ||
example: '120363228882361111' | ||
participants: | ||
type: array | ||
items: | ||
type: string | ||
example: | ||
- '6819241294719274' | ||
- '6829241294719274' | ||
- '6839241294719274' | ||
example: | ||
- '6819241294719274' | ||
- '6829241294719274' | ||
- '6839241294719274' | ||
responses: | ||
'200': | ||
description: OK | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/AddParticipantToGroupResponse' | ||
'400': | ||
description: Bad Request | ||
content: | ||
|
@@ -807,7 +853,7 @@ paths: | |
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/SendResponse' | ||
$ref: '#/components/schemas/GenericResponse' | ||
'400': | ||
description: Bad Request | ||
content: | ||
|
@@ -857,6 +903,44 @@ paths: | |
|
||
components: | ||
schemas: | ||
CreateGroupResponse: | ||
type: object | ||
properties: | ||
code: | ||
type: string | ||
example: SUCCESS | ||
message: | ||
type: string | ||
example: Success get list groups | ||
results: | ||
type: object | ||
properties: | ||
group_id: | ||
type: string | ||
example: [email protected] | ||
AddParticipantToGroupResponse: | ||
type: object | ||
properties: | ||
code: | ||
type: string | ||
example: SUCCESS | ||
message: | ||
type: string | ||
example: Success get list groups | ||
results: | ||
type: array | ||
items: | ||
properties: | ||
participant: | ||
type: string | ||
example: '[email protected]' | ||
status: | ||
type: string | ||
example: success | ||
message: | ||
type: string | ||
example: Participant added | ||
|
||
UserGroupResponse: | ||
type: object | ||
properties: | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -53,17 +53,9 @@ func (service groupService) CreateGroup(ctx context.Context, request domainGroup | |||||||||||||
} | ||||||||||||||
whatsapp.MustLogin(service.WaCli) | ||||||||||||||
|
||||||||||||||
var participantsJID []types.JID | ||||||||||||||
for _, participant := range request.Participants { | ||||||||||||||
formattedParticipant := participant + config.WhatsappTypeUser | ||||||||||||||
|
||||||||||||||
if !whatsapp.IsOnWhatsapp(service.WaCli, formattedParticipant) { | ||||||||||||||
return "", pkgError.ErrUserNotRegistered | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
if participantJID, err := types.ParseJID(formattedParticipant); err == nil { | ||||||||||||||
participantsJID = append(participantsJID, participantJID) | ||||||||||||||
} | ||||||||||||||
participantsJID, err := service.participantToJID(request.Participants) | ||||||||||||||
if err != nil { | ||||||||||||||
return | ||||||||||||||
Comment on lines
+56
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle errors explicitly in - participantsJID, err := service.participantToJID(request.Participants)
- if err != nil {
- return
- }
+ participantsJID, err := service.participantToJID(request.Participants)
+ if err != nil {
+ return nil, err
+ } Ensure that errors returned by Committable suggestion
Suggested change
|
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
groupConfig := whatsmeow.ReqCreateGroup{ | ||||||||||||||
|
@@ -80,3 +72,59 @@ func (service groupService) CreateGroup(ctx context.Context, request domainGroup | |||||||||||||
|
||||||||||||||
return groupInfo.JID.String(), nil | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
func (service groupService) AddParticipant(ctx context.Context, request domainGroup.ParticipantRequest) (result []domainGroup.ParticipantStatus, err error) { | ||||||||||||||
if err = validations.ValidateParticipant(ctx, request); err != nil { | ||||||||||||||
return result, err | ||||||||||||||
} | ||||||||||||||
whatsapp.MustLogin(service.WaCli) | ||||||||||||||
|
||||||||||||||
groupJID, err := whatsapp.ValidateJidWithLogin(service.WaCli, request.GroupID) | ||||||||||||||
if err != nil { | ||||||||||||||
return result, err | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
participantsJID, err := service.participantToJID(request.Participants) | ||||||||||||||
if err != nil { | ||||||||||||||
return result, err | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
participants, err := service.WaCli.UpdateGroupParticipants(groupJID, participantsJID, whatsmeow.ParticipantChangeAdd) | ||||||||||||||
if err != nil { | ||||||||||||||
return result, err | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
for _, participant := range participants { | ||||||||||||||
if participant.Error == 403 && participant.AddRequest != nil { | ||||||||||||||
result = append(result, domainGroup.ParticipantStatus{ | ||||||||||||||
Participant: participant.JID.String(), | ||||||||||||||
Status: "error", | ||||||||||||||
Message: "Failed to add participant", | ||||||||||||||
}) | ||||||||||||||
} else { | ||||||||||||||
result = append(result, domainGroup.ParticipantStatus{ | ||||||||||||||
Participant: participant.JID.String(), | ||||||||||||||
Status: "success", | ||||||||||||||
Message: "Participant added", | ||||||||||||||
}) | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
return result, nil | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
func (service groupService) participantToJID(participants []string) ([]types.JID, error) { | ||||||||||||||
var participantsJID []types.JID | ||||||||||||||
for _, participant := range participants { | ||||||||||||||
formattedParticipant := participant + config.WhatsappTypeUser | ||||||||||||||
|
||||||||||||||
if !whatsapp.IsOnWhatsapp(service.WaCli, formattedParticipant) { | ||||||||||||||
return nil, pkgError.ErrUserNotRegistered | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
if participantJID, err := types.ParseJID(formattedParticipant); err == nil { | ||||||||||||||
participantsJID = append(participantsJID, participantJID) | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
return participantsJID, nil | ||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sanitize
GroupID
to prevent injection attacks.Ensure that
GroupID
is properly sanitized to prevent potential injection attacks. Consider implementing a dedicated sanitization function if not already available.Committable suggestion