Skip to content

Commit

Permalink
Merge pull request #1 from capsulbrasil/develop
Browse files Browse the repository at this point in the history
improvements and refactoring
  • Loading branch information
gabrielolivrp authored Oct 31, 2023
2 parents 2828a85 + 09e8205 commit 55a4b54
Show file tree
Hide file tree
Showing 8 changed files with 395 additions and 313 deletions.
47 changes: 7 additions & 40 deletions controllers/send_audio_message_controller.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package controllers

import (
"context"
"zapmeow/models"
"zapmeow/services"
"zapmeow/utils"

"github.com/gin-gonic/gin"
"github.com/vincent-petithory/dataurl"
"go.mau.fi/whatsmeow"
waProto "go.mau.fi/whatsmeow/binary/proto"
"google.golang.org/protobuf/proto"
)

type audioMessageBody struct {
Expand Down Expand Up @@ -67,51 +63,22 @@ func (a *sendAudioMessageController) Handler(c *gin.Context) {
return
}

instance, err := a.wppService.GetAuthenticatedInstance(instanceID)
if err != nil {
utils.RespondInternalServerError(c, err.Error())
return
}

audioURL, err := dataurl.DecodeString(body.Base64)
if err != nil {
utils.RespondInternalServerError(c, err.Error())
return
}

uploaded, err := instance.Client.Upload(
context.Background(),
audioURL.Data,
whatsmeow.MediaAudio,
)
if err != nil {
utils.RespondInternalServerError(c, err.Error())
return
}

msg := &waProto.Message{
AudioMessage: &waProto.AudioMessage{
Ptt: proto.Bool(true),
Url: proto.String(uploaded.URL),
DirectPath: proto.String(uploaded.DirectPath),
MediaKey: uploaded.MediaKey,
Mimetype: proto.String(mimitype),
FileEncSha256: uploaded.FileEncSHA256,
FileSha256: uploaded.FileSHA256,
FileLength: proto.Uint64(uint64(len(audioURL.Data))),
},
}

resp, err := instance.Client.SendMessage(context.Background(), jid, msg)
resp, err := a.wppService.SendImageMessage(instanceID, jid, audioURL, mimitype)
if err != nil {
utils.RespondInternalServerError(c, err.Error())
return
}

path, err := utils.SaveMedia(
instanceID,
audioURL.Data,
resp.ID,
audioURL.Data,
mimitype,
)
if err != nil {
Expand All @@ -120,14 +87,14 @@ func (a *sendAudioMessageController) Handler(c *gin.Context) {
}

message := models.Message{
FromMe: true,
ChatJID: jid.User,
SenderJID: instance.Client.Store.ID.User,
InstanceID: instance.ID,
MediaType: "audio",
MediaPath: path,
SenderJID: resp.Sender.User,
InstanceID: instanceID,
Timestamp: resp.Timestamp,
FromMe: true,
MessageID: resp.ID,
MediaType: "audio",
MediaPath: path,
}

err = a.messageService.CreateMessage(&message)
Expand Down
46 changes: 7 additions & 39 deletions controllers/send_image_message_controller.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package controllers

import (
"context"
"zapmeow/models"
"zapmeow/services"
"zapmeow/utils"

"github.com/gin-gonic/gin"
"github.com/vincent-petithory/dataurl"
"go.mau.fi/whatsmeow"
waProto "go.mau.fi/whatsmeow/binary/proto"
"google.golang.org/protobuf/proto"
)

type imageMessageBody struct {
Expand Down Expand Up @@ -67,50 +63,22 @@ func (i *sendImageMessageController) Handler(c *gin.Context) {
return
}

instance, err := i.wppService.GetAuthenticatedInstance(instanceID)
if err != nil {
utils.RespondInternalServerError(c, err.Error())
return
}

imageURL, err := dataurl.DecodeString(body.Base64)
if err != nil {
utils.RespondInternalServerError(c, err.Error())
return
}

uploaded, err := instance.Client.Upload(
context.Background(),
imageURL.Data,
whatsmeow.MediaImage,
)
if err != nil {
utils.RespondInternalServerError(c, err.Error())
return
}

msg := &waProto.Message{
ImageMessage: &waProto.ImageMessage{
Url: proto.String(uploaded.URL),
DirectPath: proto.String(uploaded.DirectPath),
MediaKey: uploaded.MediaKey,
Mimetype: proto.String(mimitype),
FileEncSha256: uploaded.FileEncSHA256,
FileSha256: uploaded.FileSHA256,
FileLength: proto.Uint64(uint64(len(imageURL.Data))),
},
}

resp, err := instance.Client.SendMessage(context.Background(), jid, msg)
resp, err := i.wppService.SendImageMessage(instanceID, jid, imageURL, mimitype)
if err != nil {
utils.RespondInternalServerError(c, err.Error())
return
}

path, err := utils.SaveMedia(
instanceID,
imageURL.Data,
resp.ID,
imageURL.Data,
mimitype,
)
if err != nil {
Expand All @@ -119,14 +87,14 @@ func (i *sendImageMessageController) Handler(c *gin.Context) {
}

message := models.Message{
FromMe: true,
ChatJID: jid.User,
SenderJID: instance.Client.Store.ID.User,
InstanceID: instance.ID,
MediaType: "image",
MediaPath: path,
SenderJID: resp.Sender.User,
InstanceID: instanceID,
Timestamp: resp.Timestamp,
FromMe: true,
MessageID: resp.ID,
MediaType: "image",
MediaPath: path,
}

err = i.messageService.CreateMessage(&message)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package controllers

import (
"context"
"zapmeow/models"
"zapmeow/services"
"zapmeow/utils"

"github.com/gin-gonic/gin"
waProto "go.mau.fi/whatsmeow/binary/proto"
)

type textMessageBody struct {
Expand Down Expand Up @@ -56,30 +54,18 @@ func (t *sendTextMessageController) Handler(c *gin.Context) {
utils.RespondBadRequest(c, "Invalid phone")
return
}
instanceId := c.Param("instanceId")
instanceID := c.Param("instanceId")

instance, err := t.wppService.GetAuthenticatedInstance(instanceId)
if err != nil {
utils.RespondInternalServerError(c, err.Error())
return
}

msg := &waProto.Message{
ExtendedTextMessage: &waProto.ExtendedTextMessage{
Text: &body.Text,
},
}

resp, err := instance.Client.SendMessage(context.Background(), jid, msg)
resp, err := t.wppService.SendTextMessage(instanceID, jid, body.Text)
if err != nil {
utils.RespondInternalServerError(c, err.Error())
return
}

message := models.Message{
ChatJID: jid.User,
SenderJID: instance.Client.Store.ID.User,
InstanceID: instance.ID,
SenderJID: resp.Sender.User,
InstanceID: instanceID,
Body: body.Text,
Timestamp: resp.Timestamp,
FromMe: true,
Expand Down
32 changes: 31 additions & 1 deletion services/account_service.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package services

import (
"fmt"
"os"
"path/filepath"
"zapmeow/models"
"zapmeow/repositories"
"zapmeow/utils"
)

type AccountService interface {
CreateAccount(account *models.Account) error
GetConnectedAccounts() ([]models.Account, error)
GetAccountByInstanceID(instanceID string) (*models.Account, error)
UpdateAccount(instanceID string, data map[string]interface{}) error
// DeleteAccountInfos(instanceID string) error
DeleteAccountMessages(instanceID string) error
}

type accountService struct {
Expand Down Expand Up @@ -40,3 +44,29 @@ func (a *accountService) GetAccountByInstanceID(instanceID string) (*models.Acco
func (a *accountService) UpdateAccount(instanceID string, data map[string]interface{}) error {
return a.accountRepo.UpdateAccount(instanceID, data)
}

func (a *accountService) DeleteAccountMessages(instanceID string) error {
err := a.messageService.DeleteMessagesByInstanceID(instanceID)
if err != nil {
return err
}
return a.deleteAccountDirectory(instanceID)
}

func (a *accountService) deleteAccountDirectory(instanceID string) error {
dirPath := utils.MakeAccountStoragePath(instanceID)
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
err = os.Remove(path)
if err != nil {
return err
}
fmt.Printf("File removed: %s\n", path)
}
return nil
})
return err
}
Loading

0 comments on commit 55a4b54

Please sign in to comment.