release: v0.6.1
This commit is contained in:
commit
877eef7f07
118 files changed
+46315
No files matched your search
@@ -0,0 +1,160 @@
|
||||
package community_handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
community_service "github.com/EvolutionAPI/evolution-go/pkg/community/service"
|
||||
instance_model "github.com/EvolutionAPI/evolution-go/pkg/instance/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type CommunityHandler interface {
|
||||
CreateCommunity(ctx *gin.Context)
|
||||
CommunityAdd(ctx *gin.Context)
|
||||
CommunityRemove(ctx *gin.Context)
|
||||
}
|
||||
|
||||
type communityHandler struct {
|
||||
communityService community_service.CommunityService
|
||||
}
|
||||
|
||||
// Create community
|
||||
// @Summary Create community
|
||||
// @Description Create community
|
||||
// @Tags Community
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param message body community_service.CreateCommunityStruct true "Community data"
|
||||
// @Success 200 {object} gin.H "success"
|
||||
// @Failure 400 {object} gin.H "Error on validation"
|
||||
// @Failure 500 {object} gin.H "Internal server error"
|
||||
// @Router /community/create [post]
|
||||
func (c *communityHandler) CreateCommunity(ctx *gin.Context) {
|
||||
getInstance := ctx.MustGet("instance")
|
||||
|
||||
instance, ok := getInstance.(*instance_model.Instance)
|
||||
if !ok {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "instance not found"})
|
||||
return
|
||||
}
|
||||
|
||||
var data *community_service.CreateCommunityStruct
|
||||
err := ctx.ShouldBindBodyWithJSON(&data)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if data.CommunityName == "" {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "community name is required"})
|
||||
return
|
||||
}
|
||||
|
||||
community, err := c.communityService.CreateCommunity(data, instance)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{"message": "success", "data": community})
|
||||
}
|
||||
|
||||
// Add participant to community
|
||||
// @Summary Add participant to community
|
||||
// @Description Add participant to community
|
||||
// @Tags Community
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param message body community_service.AddParticipantStruct true "Participant data"
|
||||
// @Success 200 {object} gin.H "success"
|
||||
// @Failure 400 {object} gin.H "Error on validation"
|
||||
// @Failure 500 {object} gin.H "Internal server error"
|
||||
// @Router /community/add [post]
|
||||
func (c *communityHandler) CommunityAdd(ctx *gin.Context) {
|
||||
getInstance := ctx.MustGet("instance")
|
||||
|
||||
instance, ok := getInstance.(*instance_model.Instance)
|
||||
if !ok {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "instance not found"})
|
||||
return
|
||||
}
|
||||
|
||||
var data *community_service.AddParticipantStruct
|
||||
err := ctx.ShouldBindBodyWithJSON(&data)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if data.CommunityJID == "" {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "community jid is required"})
|
||||
return
|
||||
}
|
||||
|
||||
if len(data.GroupJID) == 0 {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "group jid is required"})
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := c.communityService.CommunityAdd(data, instance)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{"message": "success", "data": resp})
|
||||
}
|
||||
|
||||
// Remove participant from community
|
||||
// @Summary Remove participant from community
|
||||
// @Description Remove participant from community
|
||||
// @Tags Community
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param message body community_service.AddParticipantStruct true "Participant data"
|
||||
// @Success 200 {object} gin.H "success"
|
||||
// @Failure 400 {object} gin.H "Error on validation"
|
||||
// @Failure 500 {object} gin.H "Internal server error"
|
||||
// @Router /community/remove [post]
|
||||
func (c *communityHandler) CommunityRemove(ctx *gin.Context) {
|
||||
getInstance := ctx.MustGet("instance")
|
||||
|
||||
instance, ok := getInstance.(*instance_model.Instance)
|
||||
if !ok {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "instance not found"})
|
||||
return
|
||||
}
|
||||
|
||||
var data *community_service.AddParticipantStruct
|
||||
err := ctx.ShouldBindBodyWithJSON(&data)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if data.CommunityJID == "" {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "community jid is required"})
|
||||
return
|
||||
}
|
||||
|
||||
if len(data.GroupJID) == 0 {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "group jid is required"})
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := c.communityService.CommunityRemove(data, instance)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{"message": "success", "data": resp})
|
||||
}
|
||||
|
||||
func NewCommunityHandler(
|
||||
communityService community_service.CommunityService,
|
||||
) CommunityHandler {
|
||||
return &communityHandler{
|
||||
communityService: communityService,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
package community_service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
instance_model "github.com/EvolutionAPI/evolution-go/pkg/instance/model"
|
||||
logger_wrapper "github.com/EvolutionAPI/evolution-go/pkg/logger"
|
||||
"github.com/EvolutionAPI/evolution-go/pkg/utils"
|
||||
whatsmeow_service "github.com/EvolutionAPI/evolution-go/pkg/whatsmeow/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.mau.fi/whatsmeow"
|
||||
"go.mau.fi/whatsmeow/types"
|
||||
)
|
||||
|
||||
type CommunityService interface {
|
||||
CreateCommunity(data *CreateCommunityStruct, instance *instance_model.Instance) (*types.GroupInfo, error)
|
||||
CommunityAdd(data *AddParticipantStruct, instance *instance_model.Instance) (gin.H, error)
|
||||
CommunityRemove(data *AddParticipantStruct, instance *instance_model.Instance) (gin.H, error)
|
||||
}
|
||||
|
||||
type communityService struct {
|
||||
clientPointer map[string]*whatsmeow.Client
|
||||
whatsmeowService whatsmeow_service.WhatsmeowService
|
||||
loggerWrapper *logger_wrapper.LoggerManager
|
||||
}
|
||||
|
||||
type CreateCommunityStruct struct {
|
||||
CommunityName string `json:"communityName"`
|
||||
}
|
||||
|
||||
type AddParticipantStruct struct {
|
||||
CommunityJID string `json:"communityJid"`
|
||||
GroupJID []string `json:"groupJid"`
|
||||
}
|
||||
|
||||
func (c *communityService) ensureClientConnected(instanceId string) (*whatsmeow.Client, error) {
|
||||
client := c.clientPointer[instanceId]
|
||||
c.loggerWrapper.GetLogger(instanceId).LogInfo("[%s] Checking client connection status - Client exists: %v", instanceId, client != nil)
|
||||
|
||||
if client == nil {
|
||||
c.loggerWrapper.GetLogger(instanceId).LogInfo("[%s] No client found, attempting to start new instance", instanceId)
|
||||
err := c.whatsmeowService.StartInstance(instanceId)
|
||||
if err != nil {
|
||||
c.loggerWrapper.GetLogger(instanceId).LogError("[%s] Failed to start instance: %v", instanceId, err)
|
||||
return nil, errors.New("no active session found")
|
||||
}
|
||||
|
||||
c.loggerWrapper.GetLogger(instanceId).LogInfo("[%s] Instance started, waiting 2 seconds...", instanceId)
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
client = c.clientPointer[instanceId]
|
||||
c.loggerWrapper.GetLogger(instanceId).LogInfo("[%s] Checking new client - Exists: %v, Connected: %v",
|
||||
instanceId,
|
||||
client != nil,
|
||||
client != nil && client.IsConnected())
|
||||
|
||||
if client == nil || !client.IsConnected() {
|
||||
c.loggerWrapper.GetLogger(instanceId).LogError("[%s] New client validation failed - Exists: %v, Connected: %v",
|
||||
instanceId,
|
||||
client != nil,
|
||||
client != nil && client.IsConnected())
|
||||
return nil, errors.New("no active session found")
|
||||
}
|
||||
} else if !client.IsConnected() {
|
||||
c.loggerWrapper.GetLogger(instanceId).LogError("[%s] Existing client is disconnected - Connected status: %v",
|
||||
instanceId,
|
||||
client.IsConnected())
|
||||
return nil, errors.New("client disconnected")
|
||||
}
|
||||
|
||||
c.loggerWrapper.GetLogger(instanceId).LogInfo("[%s] Client successfully validated - Connected: %v", instanceId, client.IsConnected())
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func (c *communityService) CreateCommunity(data *CreateCommunityStruct, instance *instance_model.Instance) (*types.GroupInfo, error) {
|
||||
client, err := c.ensureClientConnected(instance.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := client.CreateGroup(context.Background(), whatsmeow.ReqCreateGroup{
|
||||
Name: data.CommunityName,
|
||||
GroupParent: types.GroupParent{
|
||||
IsParent: true,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
c.loggerWrapper.GetLogger(instance.Id).LogError("[%s] error create community: %v", instance.Id, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *communityService) CommunityAdd(data *AddParticipantStruct, instance *instance_model.Instance) (gin.H, error) {
|
||||
client, err := c.ensureClientConnected(instance.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
communityJID, ok := utils.ParseJID(data.CommunityJID)
|
||||
if !ok {
|
||||
c.loggerWrapper.GetLogger(instance.Id).LogError("[%s] error parse community jid", instance.Id)
|
||||
return nil, errors.New("error parse community jid")
|
||||
}
|
||||
|
||||
var successList []string
|
||||
var failedList []string
|
||||
|
||||
for _, participant := range data.GroupJID {
|
||||
groupJID, _ := utils.ParseJID(participant)
|
||||
err := client.LinkGroup(context.Background(), communityJID, groupJID)
|
||||
if err != nil {
|
||||
c.loggerWrapper.GetLogger(instance.Id).LogError("[%s] error link group: %v", instance.Id, err)
|
||||
failedList = append(failedList, groupJID.String())
|
||||
}
|
||||
successList = append(failedList, groupJID.String())
|
||||
}
|
||||
|
||||
return gin.H{
|
||||
"success": successList,
|
||||
"failed": failedList,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *communityService) CommunityRemove(data *AddParticipantStruct, instance *instance_model.Instance) (gin.H, error) {
|
||||
client, err := c.ensureClientConnected(instance.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
communityJID, ok := utils.ParseJID(data.CommunityJID)
|
||||
if !ok {
|
||||
c.loggerWrapper.GetLogger(instance.Id).LogError("[%s] error parse community jid", instance.Id)
|
||||
return nil, errors.New("error parse community jid")
|
||||
}
|
||||
|
||||
var successList []string
|
||||
var failedList []string
|
||||
|
||||
for _, participant := range data.GroupJID {
|
||||
groupJID, _ := utils.ParseJID(participant)
|
||||
err := client.UnlinkGroup(context.Background(), communityJID, groupJID)
|
||||
if err != nil {
|
||||
c.loggerWrapper.GetLogger(instance.Id).LogError("[%s] error link group: %v", instance.Id, err)
|
||||
failedList = append(failedList, groupJID.String())
|
||||
}
|
||||
successList = append(failedList, groupJID.String())
|
||||
}
|
||||
|
||||
return gin.H{
|
||||
"success": successList,
|
||||
"failed": failedList,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func NewCommunityService(
|
||||
clientPointer map[string]*whatsmeow.Client,
|
||||
whatsmeowService whatsmeow_service.WhatsmeowService,
|
||||
loggerWrapper *logger_wrapper.LoggerManager,
|
||||
) CommunityService {
|
||||
return &communityService{
|
||||
clientPointer: clientPointer,
|
||||
whatsmeowService: whatsmeowService,
|
||||
loggerWrapper: loggerWrapper,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user