Skip to content
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: manage participant #169

Merged
merged 3 commits into from
Jul 13, 2024
Merged
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
128 changes: 108 additions & 20 deletions docs/openapi.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
openapi: 3.0.0
info:
title: WhatsApp API MultiDevice
version: 4.0.0
version: 4.1.0
description: This API is used for sending whatsapp via API
servers:
- url: http://localhost:3000
Expand Down Expand Up @@ -838,30 +838,104 @@ paths:
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'
$ref: '#/components/schemas/ManageParticipantRequest'
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/ManageParticipantResponse'
'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/remove:
post:
operationId: removeParticipantFromGroup
tags:
- group
summary: Remove participants from group
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/ManageParticipantRequest'
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/ManageParticipantResponse'
'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/promote:
post:
operationId: promoteParticipantToAdmin
tags:
- group
summary: Promote participants to admin
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/ManageParticipantRequest'
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/AddParticipantToGroupResponse'
$ref: '#/components/schemas/ManageParticipantResponse'
'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/demote:
post:
operationId: demoteParticipantToMember
tags:
- group
summary: Demote participants to member
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/ManageParticipantRequest'
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/ManageParticipantResponse'
'400':
description: Bad Request
content:
Expand Down Expand Up @@ -960,7 +1034,21 @@ components:
group_id:
type: string
example: [email protected]
AddParticipantToGroupResponse:
ManageParticipantRequest:
type: object
properties:
group_id:
type: string
example: [email protected]
participants:
type: array
items:
type: string
example:
- '6819241294719274'
- '6829241294719274'
- '6839241294719274'
ManageParticipantResponse:
type: object
properties:
code:
Expand Down
9 changes: 5 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,14 @@ You can fork or edit this source code !
| ✅ | React Message | POST | /message/:message_id/reaction |
| ✅ | Delete Message | POST | /message/:message_id/delete |
| ✅ | Edit Message | POST | /message/:message_id/update |
| ❌ | Star message | POST | /message/:message_id/star |
| ✅ | Join Group With Link | POST | /group/join-with-link |
| ✅ | Leave Group | POST | /group/leave |
| ✅ | Create Group | POST | /group |
| ✅ | Add Participants in Group | POST | /group/participants |
| | Remove Participant in Group | DELETE | /group/participants |
| | Promote Participant in Group | POST | /group/participants/promote |
| | Demote Participant in Group | POST | /group/participants/demote |
| | Remove Participant in Group | DELETE | /group/participants |
| | Promote Participant in Group | PATCH | /group/participants/promote |
| | Demote Participant in Group | PATCH | /group/participants/demote |

```
✅ = Available
Expand All @@ -136,7 +137,7 @@ You can fork or edit this source code !

### App User Interface

1. Homepage ![Homepage](https://i.ibb.co.com/681JTHK/image.png)
1. Homepage ![Homepage](https://i.ibb.co.com/d05L4VX/homepage.png)
2. Login ![Login](https://i.ibb.co.com/jkcB15R/login.png?v=1)
3. Send Message ![Send Message](https://i.ibb.co.com/rc3NXMX/send-message.png?v1)
4. Send Image ![Send Image](https://i.ibb.co.com/BcFL3SD/send-image.png?v1)
Expand Down
2 changes: 1 addition & 1 deletion src/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

var (
AppVersion = "v4.14.2"
AppVersion = "v4.15.0"
AppPort = "3000"
AppDebug = false
AppOs = "AldinoKemal"
Expand Down
12 changes: 8 additions & 4 deletions src/domains/group/group.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package group

import "context"
import (
"context"
"go.mau.fi/whatsmeow"
)

type IGroupService interface {
JoinGroupWithLink(ctx context.Context, request JoinGroupWithLinkRequest) (groupID string, err error)
LeaveGroup(ctx context.Context, request LeaveGroupRequest) (err error)
CreateGroup(ctx context.Context, request CreateGroupRequest) (groupID string, err error)
AddParticipant(ctx context.Context, request ParticipantRequest) (result []ParticipantStatus, err error)
ManageParticipant(ctx context.Context, request ParticipantRequest) (result []ParticipantStatus, err error)
}

type JoinGroupWithLinkRequest struct {
Expand All @@ -23,8 +26,9 @@ type CreateGroupRequest struct {
}

type ParticipantRequest struct {
GroupID string `json:"group_id" form:"group_id"`
Participants []string `json:"participants" form:"participants"`
GroupID string `json:"group_id" form:"group_id"`
Participants []string `json:"participants" form:"participants"`
Action whatsmeow.ParticipantChange `json:"action" form:"action"`
}

type ParticipantStatus struct {
Expand Down
68 changes: 67 additions & 1 deletion src/internal/rest/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/utils"
"github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/whatsapp"
"github.com/gofiber/fiber/v2"
"go.mau.fi/whatsmeow"
)

type Group struct {
Expand All @@ -18,6 +19,9 @@ func InitRestGroup(app *fiber.App, service domainGroup.IGroupService) Group {
app.Post("/group/join-with-link", rest.JoinGroupWithLink)
app.Post("/group/leave", rest.LeaveGroup)
app.Post("/group/participants", rest.AddParticipants)
app.Post("/group/participants/remove", rest.DeleteParticipants)
app.Post("/group/participants/promote", rest.PromoteParticipants)
app.Post("/group/participants/demote", rest.DemoteParticipants)
return rest
}

Expand Down Expand Up @@ -81,7 +85,9 @@ func (controller *Group) AddParticipants(c *fiber.Ctx) error {

whatsapp.SanitizePhone(&request.GroupID)

result, err := controller.Service.AddParticipant(c.UserContext(), request)
request.Action = whatsmeow.ParticipantChangeAdd

result, err := controller.Service.ManageParticipant(c.UserContext(), request)
utils.PanicIfNeeded(err)

return c.JSON(utils.ResponseData{
Expand All @@ -91,3 +97,63 @@ func (controller *Group) AddParticipants(c *fiber.Ctx) error {
Results: result,
})
}

func (controller *Group) DeleteParticipants(c *fiber.Ctx) error {
var request domainGroup.ParticipantRequest
err := c.BodyParser(&request)
utils.PanicIfNeeded(err)

whatsapp.SanitizePhone(&request.GroupID)

request.Action = whatsmeow.ParticipantChangeRemove

result, err := controller.Service.ManageParticipant(c.UserContext(), request)
utils.PanicIfNeeded(err)

return c.JSON(utils.ResponseData{
Status: 200,
Code: "SUCCESS",
Message: "Success delete participants",
Results: result,
})
}

func (controller *Group) PromoteParticipants(c *fiber.Ctx) error {
var request domainGroup.ParticipantRequest
err := c.BodyParser(&request)
utils.PanicIfNeeded(err)

whatsapp.SanitizePhone(&request.GroupID)

request.Action = whatsmeow.ParticipantChangePromote

result, err := controller.Service.ManageParticipant(c.UserContext(), request)
utils.PanicIfNeeded(err)

return c.JSON(utils.ResponseData{
Status: 200,
Code: "SUCCESS",
Message: "Success promote participants",
Results: result,
})
}

func (controller *Group) DemoteParticipants(c *fiber.Ctx) error {
var request domainGroup.ParticipantRequest
err := c.BodyParser(&request)
utils.PanicIfNeeded(err)

whatsapp.SanitizePhone(&request.GroupID)

request.Action = whatsmeow.ParticipantChangeDemote

result, err := controller.Service.ManageParticipant(c.UserContext(), request)
utils.PanicIfNeeded(err)

return c.JSON(utils.ResponseData{
Status: 200,
Code: "SUCCESS",
Message: "Success demote participants",
Results: result,
})
}
11 changes: 7 additions & 4 deletions src/pkg/whatsapp/whatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,14 @@ func ExtractMedia(storageLocation string, mediaFile whatsmeow.DownloadableMessag
extractedMedia.Caption = media.GetCaption()
}

extensions, err := mime.ExtensionsByType(extractedMedia.MimeType)
if err != nil {
return extractedMedia, err
var extension string
if ext, err := mime.ExtensionsByType(extractedMedia.MimeType); err != nil && len(ext) > 0 {
extension = ext[0]
} else if parts := strings.Split(extractedMedia.MimeType, "/"); len(parts) > 1 {
extension = "." + parts[len(parts)-1]
}
extractedMedia.MediaPath = fmt.Sprintf("%s/%d-%s%s", storageLocation, time.Now().Unix(), uuid.NewString(), extensions[0])

extractedMedia.MediaPath = fmt.Sprintf("%s/%d-%s%s", storageLocation, time.Now().Unix(), uuid.NewString(), extension)
err = os.WriteFile(extractedMedia.MediaPath, data, 0600)
if err != nil {
return extractedMedia, err
Expand Down
6 changes: 3 additions & 3 deletions src/services/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ 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) {
func (service groupService) ManageParticipant(ctx context.Context, request domainGroup.ParticipantRequest) (result []domainGroup.ParticipantStatus, err error) {
if err = validations.ValidateParticipant(ctx, request); err != nil {
return result, err
}
Expand All @@ -89,7 +89,7 @@ func (service groupService) AddParticipant(ctx context.Context, request domainGr
return result, err
}

participants, err := service.WaCli.UpdateGroupParticipants(groupJID, participantsJID, whatsmeow.ParticipantChangeAdd)
participants, err := service.WaCli.UpdateGroupParticipants(groupJID, participantsJID, request.Action)
if err != nil {
return result, err
}
Expand All @@ -105,7 +105,7 @@ func (service groupService) AddParticipant(ctx context.Context, request domainGr
result = append(result, domainGroup.ParticipantStatus{
Participant: participant.JID.String(),
Status: "success",
Message: "Participant added",
Message: "Action success",
})
}
}
Expand Down
Loading