Browse Source

update fix jid composing detection for group

pull/28/head v1.3.2
Dimas Restu H 4 years ago
parent
commit
c364acd1bc
  1. 6
      docs/docs.go
  2. 6
      docs/swagger.json
  3. 6
      docs/swagger.yaml
  4. 14
      internal/whatsapp/whatsapp.go
  5. 82
      pkg/whatsapp/whatsapp.go

6
docs/docs.go

@ -149,18 +149,18 @@ const docTemplate = `{
"BearerAuth": []
}
],
"description": "Check WhatsApp Personal ID or Group ID is Registered",
"description": "Check WhatsApp Personal ID is Registered",
"produces": [
"application/json"
],
"tags": [
"WhatsApp Authentication"
],
"summary": "Check If WhatsApp Personal ID or Group ID is Registered",
"summary": "Check If WhatsApp Personal ID is Registered",
"parameters": [
{
"type": "string",
"description": "WhatsApp Personal ID or Group ID to Check",
"description": "WhatsApp Personal ID to Check",
"name": "msisdn",
"in": "query",
"required": true

6
docs/swagger.json

@ -144,18 +144,18 @@
"BearerAuth": []
}
],
"description": "Check WhatsApp Personal ID or Group ID is Registered",
"description": "Check WhatsApp Personal ID is Registered",
"produces": [
"application/json"
],
"tags": [
"WhatsApp Authentication"
],
"summary": "Check If WhatsApp Personal ID or Group ID is Registered",
"summary": "Check If WhatsApp Personal ID is Registered",
"parameters": [
{
"type": "string",
"description": "WhatsApp Personal ID or Group ID to Check",
"description": "WhatsApp Personal ID to Check",
"name": "msisdn",
"in": "query",
"required": true

6
docs/swagger.yaml

@ -85,9 +85,9 @@ paths:
- WhatsApp Authentication
/api/v1/whatsapp/registered:
get:
description: Check WhatsApp Personal ID or Group ID is Registered
description: Check WhatsApp Personal ID is Registered
parameters:
- description: WhatsApp Personal ID or Group ID to Check
- description: WhatsApp Personal ID to Check
in: query
name: msisdn
required: true
@ -99,7 +99,7 @@ paths:
description: ""
security:
- BearerAuth: []
summary: Check If WhatsApp Personal ID or Group ID is Registered
summary: Check If WhatsApp Personal ID is Registered
tags:
- WhatsApp Authentication
/api/v1/whatsapp/send/audio:

14
internal/whatsapp/whatsapp.go

@ -102,11 +102,11 @@ func Login(c echo.Context) error {
}
// Registered
// @Summary Check If WhatsApp Personal ID or Group ID is Registered
// @Description Check WhatsApp Personal ID or Group ID is Registered
// @Summary Check If WhatsApp Personal ID is Registered
// @Description Check WhatsApp Personal ID is Registered
// @Tags WhatsApp Authentication
// @Produce json
// @Param msisdn query string true "WhatsApp Personal ID or Group ID to Check"
// @Param msisdn query string true "WhatsApp Personal ID to Check"
// @Success 200
// @Security BearerAuth
// @Router /api/v1/whatsapp/registered [get]
@ -118,12 +118,12 @@ func Registered(c echo.Context) error {
return router.ResponseInternalError(c, "Missing Query Value MSISDN")
}
_, err := pkgWhatsApp.WhatsAppComposeJID(jid, remoteJID)
if err != nil {
return router.ResponseNotFound(c, err.Error())
jidInfo := pkgWhatsApp.WhatsAppGetJID(jid, remoteJID)
if jidInfo.IsEmpty() {
return router.ResponseNotFound(c, "WhatsApp Personal ID is Not Registered")
}
return router.ResponseSuccess(c, "WhatsApp Personal ID or Group ID is Registered")
return router.ResponseSuccess(c, "WhatsApp Personal ID is Registered")
}
// Logout

82
pkg/whatsapp/whatsapp.go

@ -224,15 +224,6 @@ func WhatsAppIsClientOK(jid string) error {
return nil
}
func WhatsAppIsJIDOK(jid types.JID) error {
// Make Sure JID is not Empty
if jid.IsEmpty() {
return errors.New("WhatsApp ID is not Registered")
}
return nil
}
func WhatsAppGetJID(jid string, id string) types.JID {
if WhatsAppClient[jid] != nil {
var ids []string
@ -252,22 +243,19 @@ func WhatsAppGetJID(jid string, id string) types.JID {
return types.EmptyJID
}
func WhatsAppComposeJID(jid string, id string) (types.JID, error) {
func WhatsAppComposeJID(id string) types.JID {
// Decompose WhatsApp ID First Before Recomposing
id = WhatsAppDecomposeJID(id)
// Get Registered ID Information from WhatsApp
info := WhatsAppGetJID(jid, id)
// Check if ID Information is OK
err := WhatsAppIsJIDOK(info)
if err != nil {
// Return Empty JID
return types.EmptyJID, err
// Check if ID is Group or Not By Detecting '-' for Old Group ID
// Or By ID Length That Should be 18 Digits or More
if strings.ContainsRune(id, '-') || len(id) >= 18 {
// Return New Group User JID
return types.NewJID(id, types.GroupServer)
}
// Return New Composed JID from WhatsApp ID and JID Server Information
return types.NewJID(id, info.Server), nil
// Return New Standard User JID
return types.NewJID(id, types.DefaultUserServer)
}
func WhatsAppDecomposeJID(id string) string {
@ -319,12 +307,8 @@ func WhatsAppSendText(jid string, rjid string, message string) (string, error) {
return "", err
}
// Check if Remote JID is Registered and
// Compose New Remote JID
remoteJID, err := WhatsAppComposeJID(jid, rjid)
if err != nil {
return "", err
}
remoteJID := WhatsAppComposeJID(rjid)
// Set Chat Presence
WhatsAppComposeStatus(jid, remoteJID, true, false)
@ -359,12 +343,8 @@ func WhatsAppSendLocation(jid string, rjid string, latitude float64, longitude f
return "", err
}
// Check if Remote JID is Registered and
// Compose New Remote JID
remoteJID, err := WhatsAppComposeJID(jid, rjid)
if err != nil {
return "", err
}
remoteJID := WhatsAppComposeJID(rjid)
// Set Chat Presence
WhatsAppComposeStatus(jid, remoteJID, true, false)
@ -402,12 +382,8 @@ func WhatsAppSendDocument(jid string, rjid string, fileBytes []byte, fileType st
return "", err
}
// Check if Remote JID is Registered and
// Compose New Remote JID
remoteJID, err := WhatsAppComposeJID(jid, rjid)
if err != nil {
return "", err
}
remoteJID := WhatsAppComposeJID(rjid)
// Set Chat Presence
WhatsAppComposeStatus(jid, remoteJID, true, false)
@ -458,12 +434,8 @@ func WhatsAppSendImage(jid string, rjid string, imageBytes []byte, imageType str
return "", err
}
// Check if Remote JID is Registered and
// Compose New Remote JID
remoteJID, err := WhatsAppComposeJID(jid, rjid)
if err != nil {
return "", err
}
remoteJID := WhatsAppComposeJID(rjid)
// Set Chat Presence
WhatsAppComposeStatus(jid, remoteJID, true, false)
@ -582,12 +554,8 @@ func WhatsAppSendAudio(jid string, rjid string, audioBytes []byte, audioType str
return "", err
}
// Check if Remote JID is Registered and
// Compose New Remote JID
remoteJID, err := WhatsAppComposeJID(jid, rjid)
if err != nil {
return "", err
}
remoteJID := WhatsAppComposeJID(rjid)
// Set Chat Presence
WhatsAppComposeStatus(jid, remoteJID, true, true)
@ -636,12 +604,8 @@ func WhatsAppSendVideo(jid string, rjid string, videoBytes []byte, videoType str
return "", err
}
// Check if Remote JID is Registered and
// Compose New Remote JID
remoteJID, err := WhatsAppComposeJID(jid, rjid)
if err != nil {
return "", err
}
remoteJID := WhatsAppComposeJID(rjid)
// Set Chat Presence
WhatsAppComposeStatus(jid, remoteJID, true, false)
@ -692,12 +656,8 @@ func WhatsAppSendContact(jid string, rjid string, contactName string, contactNum
return "", err
}
// Check if Remote JID is Registered and
// Compose New Remote JID
remoteJID, err := WhatsAppComposeJID(jid, rjid)
if err != nil {
return "", err
}
remoteJID := WhatsAppComposeJID(rjid)
// Set Chat Presence
WhatsAppComposeStatus(jid, remoteJID, true, false)
@ -737,12 +697,8 @@ func WhatsAppSendLink(jid string, rjid string, linkCaption string, linkURL strin
return "", err
}
// Check if Remote JID is Registered and
// Compose New Remote JID
remoteJID, err := WhatsAppComposeJID(jid, rjid)
if err != nil {
return "", err
}
remoteJID := WhatsAppComposeJID(rjid)
// Set Chat Presence
WhatsAppComposeStatus(jid, remoteJID, true, false)
@ -794,12 +750,8 @@ func WhatsAppSendSticker(jid string, rjid string, stickerBytes []byte) (string,
return "", err
}
// Check if Remote JID is Registered and
// Compose New Remote JID
remoteJID, err := WhatsAppComposeJID(jid, rjid)
if err != nil {
return "", err
}
remoteJID := WhatsAppComposeJID(rjid)
// Set Chat Presence
WhatsAppComposeStatus(jid, remoteJID, true, false)

Loading…
Cancel
Save