Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielolivrp committed Oct 22, 2023
1 parent b15b693 commit b57d758
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 210 deletions.
8 changes: 4 additions & 4 deletions controllers/send_audio_message_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func (a *sendAudioMessageController) Handler(c *gin.Context) {

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

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

err = a.messageService.CreateMessage(&message)
Expand Down
8 changes: 4 additions & 4 deletions controllers/send_image_message_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func (i *sendImageMessageController) Handler(c *gin.Context) {

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

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

err = i.messageService.CreateMessage(&message)
Expand Down
171 changes: 0 additions & 171 deletions services/message_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,10 @@ import (
"fmt"
"io/ioutil"
"mime"
"os"
"path/filepath"
"time"
"zapmeow/configs"
"zapmeow/models"
"zapmeow/repositories"
"zapmeow/utils"

waProto "go.mau.fi/whatsmeow/binary/proto"
"go.mau.fi/whatsmeow/types/events"
)

type MessageService interface {
Expand All @@ -23,7 +17,6 @@ type MessageService interface {
GetChatMessages(instanceID string, chatJID string) (*[]models.Message, error)
CountChatMessages(instanceID string, chatJID string) (int64, error)
DeleteMessagesByInstanceID(instanceID string) error
Parse(instance *configs.Instance, msg *events.Message) *models.Message
ToJSON(message models.Message) Message
}

Expand Down Expand Up @@ -72,43 +65,6 @@ func (m *messageService) DeleteMessagesByInstanceID(instanceID string) error {
return m.messageRep.DeleteMessagesByInstanceID(instanceID)
}

func (m *messageService) Parse(instance *configs.Instance, msg *events.Message) *models.Message {
mediaType, path := m.downloadMessageMedia(
instance,
msg.Message,
msg.Info.ID,
)

var body = m.getTextMessage(msg.Message)
if mediaType == "" && body == "" {
return nil
}

if mediaType != "" {
return &models.Message{
InstanceID: instance.ID,
MessageID: msg.Info.ID,
FromMe: msg.Info.MessageSource.IsFromMe,
ChatJID: msg.Info.Chat.User,
SenderJID: msg.Info.Sender.User,
Body: body,
MediaPath: mediaType,
MediaType: path,
Timestamp: msg.Info.Timestamp,
}
}

return &models.Message{
InstanceID: instance.ID,
MessageID: msg.Info.ID,
FromMe: msg.Info.MessageSource.IsFromMe,
ChatJID: msg.Info.Chat.User,
SenderJID: msg.Info.Sender.User,
Body: body,
Timestamp: msg.Info.Timestamp,
}
}

func (m *messageService) ToJSON(message models.Message) Message {
messageJson := Message{
ID: message.ID,
Expand Down Expand Up @@ -140,130 +96,3 @@ func (m *messageService) ToJSON(message models.Message) Message {

return messageJson
}

func (m *messageService) downloadMessageMedia(
instance *configs.Instance,
message *waProto.Message,
fileName string,
) (string, string) {
path := ""
mediaType := ""

dirPath := utils.MakeAccountStoragePath(instance.ID)
err := os.MkdirAll(dirPath, 0751)
if err != nil {
return "", ""
}

document := message.GetDocumentMessage()
if document != nil {
mediaType = "document"

data, err := instance.Client.Download(document)

if err != nil {
// fmt.Println("Failed to download document", err)
return mediaType, ""
}

path, err = utils.SaveMedia(
instance.ID,
data,
fileName,
document.GetMimetype(),
)
if err != nil {
// fmt.Println("Failed to save document", err)
return mediaType, ""
}
// fmt.Println("Document saved")
}

audio := message.GetAudioMessage()
if audio != nil {
mediaType = "audio"

data, err := instance.Client.Download(audio)
if err != nil {
// fmt.Println("Failed to download audio", err)
return mediaType, ""
}

path, err = utils.SaveMedia(
instance.ID,
data,
fileName,
audio.GetMimetype(),
)

if err != nil {
// fmt.Println("Failed to save audio", err)
return mediaType, ""
}
// fmt.Println("Audio saved")
}

image := message.GetImageMessage()
if image != nil {
mediaType = "image"
data, err := instance.Client.Download(image)
if err != nil {
fmt.Println("Failed to download image", err)
return mediaType, ""
}

path, err = utils.SaveMedia(
instance.ID,
data,
fileName,
image.GetMimetype(),
)
if err != nil {
fmt.Println("Failed to save image", err)
return mediaType, ""
}
fmt.Println("Image saved")
}

sticker := message.GetStickerMessage()
if sticker != nil {
mediaType = "image"
data, err := instance.Client.Download(sticker)
if err != nil {
// fmt.Println("Failed to download sticker", err)
return mediaType, ""
}

path, err = utils.SaveMedia(
instance.ID,
data,
fileName,
sticker.GetMimetype(),
)
if err != nil {
// fmt.Println("Failed to download sticker", err)
return mediaType, ""
}

// fmt.Println("Sticker saved")
}

video := message.GetVideoMessage()
if video != nil {
return "video", ""
}

if path != "" && mediaType != "" {
return mediaType, path
}

return "", ""
}

func (m *messageService) getTextMessage(message *waProto.Message) string {
extendedTextMessage := message.GetExtendedTextMessage()
if extendedTextMessage != nil {
return *extendedTextMessage.Text
}
return message.GetConversation()
}
Loading

0 comments on commit b57d758

Please sign in to comment.