Browse Source

feat: add middleware JID

pull/28/head
Aldino Kemal 3 years ago
parent
commit
566dff30df
  1. 1
      src/cmd/root.go
  2. 8
      src/config/settings.go
  3. 3
      src/go.mod
  4. 7
      src/go.sum
  5. 8
      src/internal/rest/app_rest.go
  6. 10
      src/internal/rest/send_controller.go
  7. 4
      src/internal/rest/user_controller.go
  8. 21
      src/middleware/select_jid.go
  9. 4
      src/views/index.html

1
src/cmd/root.go

@ -63,6 +63,7 @@ func runRest(cmd *cobra.Command, args []string) {
})
app.Static("/statics", "./statics")
app.Use(middleware.Recovery())
app.Use(middleware.SelectJid())
if config.AppDebug {
app.Use(logger.New())
}

8
src/config/settings.go

@ -11,17 +11,19 @@ var (
AppDebug = false
AppOs = fmt.Sprintf("AldinoKemal")
AppPlatform = waProto.DeviceProps_PlatformType(1)
AppSelectedDeviceKey = "deviceID"
AppDefaultDevice = "default"
AppBasicAuthCredential string
PathQrCode = "statics/qrcode"
PathSendItems = "statics/senditems"
PathStorages = "storages"
DBName = "hydrogenWaCli.db"
DBName = "whatsapp.db"
WhatsappLogLevel = "ERROR"
WhatsappAutoReplyMessage string
WhatsappAutoReplyWebhook string
WhatsappSettingMaxFileSize int64 = 30000000 // 10MB
WhatsappSettingMaxVideoSize int64 = 30000000 // 30MB
WhatsappSettingMaxFileSize int64 = 50000000 // 50MB
WhatsappSettingMaxVideoSize int64 = 100000000 // 100MB
)

3
src/go.mod

@ -7,12 +7,14 @@ require (
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
github.com/gofiber/fiber/v2 v2.40.0
github.com/gofiber/template v1.7.2
github.com/google/uuid v1.1.2
github.com/h2non/bimg v1.1.9
github.com/markbates/pkger v0.17.1
github.com/mattn/go-sqlite3 v1.14.16
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.8.0
github.com/valyala/fasthttp v1.41.0
go.mau.fi/whatsmeow v0.0.0-20221117205735-357841c4ff30
google.golang.org/protobuf v1.28.1
)
@ -33,7 +35,6 @@ require (
github.com/rivo/uniseg v0.4.2 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.41.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
go.mau.fi/libsignal v0.0.0-20221015105917-d970e7c3c9cf // indirect
golang.org/x/crypto v0.1.0 // indirect

7
src/go.sum

@ -140,13 +140,9 @@ github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PL
github.com/gobuffalo/here v0.6.7 h1:hpfhh+kt2y9JLDfhYUxxCRxQol540jsVfKUZzjlbp8o=
github.com/gobuffalo/here v0.6.7/go.mod h1:vuCfanjqckTuRlqAitJz6QC4ABNnS27wLb816UhsPcc=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gofiber/fiber/v2 v2.37.1/go.mod h1:j3UslgQeJQP3mNhBxHnLLE8TPqA1Fd/lrl4gD25rRUY=
github.com/gofiber/fiber/v2 v2.39.0 h1:uhWpYQ6EHN8J7FOPYbI2hrdBD/KNZBC5CjbuOd4QUt4=
github.com/gofiber/fiber/v2 v2.39.0/go.mod h1:Cmuu+elPYGqlvQvdKyjtYsjGMi69PDp8a1AY2I5B2gM=
github.com/gofiber/fiber/v2 v2.40.0 h1:fdU7w5hT6PLL7jiWIhtQ+S/k5WEFYoUZidptlPu8GBo=
github.com/gofiber/fiber/v2 v2.40.0/go.mod h1:Gko04sLksnHbzLSRBFWPFdzM9Ws9pRxvvIaohJK1dsk=
github.com/gofiber/template v1.7.1 h1:QCRChZA6UrLROgMbzCMKm4a1yqM/5S8RTBKYWZ9GfL4=
github.com/gofiber/template v1.7.1/go.mod h1:l3ZOSp8yrMvROzqyh0QTCw7MHet/yLBzaRX+wsiw+gM=
github.com/gofiber/template v1.7.2 h1:sCHY5WcvmLtR7t8ihXCs5HRdYv4W9EzgBIMbUl4a5zw=
github.com/gofiber/template v1.7.2/go.mod h1:uk/mUKTXJMBMoETvo/5CA/QlTFERHMooOm0pJPGDGVA=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
@ -218,6 +214,7 @@ github.com/google/pprof v0.0.0-20210601050228-01bbb1931b22/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20210609004039-a478d1d731e9/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
@ -408,8 +405,6 @@ go.etcd.io/etcd/client/pkg/v3 v3.5.1/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3
go.etcd.io/etcd/client/v2 v2.305.1/go.mod h1:pMEacxZW7o8pg4CrFE7pquyCJJzZvkvdD2RibOCCCGs=
go.mau.fi/libsignal v0.0.0-20221015105917-d970e7c3c9cf h1:mzPxXBgDPHKDHMVV1tIWh7lwCiRpzCsXC0gNRX+K07c=
go.mau.fi/libsignal v0.0.0-20221015105917-d970e7c3c9cf/go.mod h1:XCjaU93vl71YNRPn059jMrK0xRDwVO5gKbxoPxow9mQ=
go.mau.fi/whatsmeow v0.0.0-20221104090956-123ccfa04185 h1:TTya4FuY3E8s+Dl7Srzv9fKbhvJeaWEcpQqa/WadsKo=
go.mau.fi/whatsmeow v0.0.0-20221104090956-123ccfa04185/go.mod h1:elpwNbr2EwG1kadbswg+fKjlWffYUmOxqvx7Q8uX9YE=
go.mau.fi/whatsmeow v0.0.0-20221117205735-357841c4ff30 h1:AVHEkGJSI6NsdbxVs9YEGPFR68uru1Ke/hH72EqzGpk=
go.mau.fi/whatsmeow v0.0.0-20221117205735-357841c4ff30/go.mod h1:2yweL8nczvtlIxkrvCb0y8xiO13rveX9lJPambwYV/E=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=

8
src/internal/rest/app_rest.go

@ -22,7 +22,7 @@ func InitRestApp(app *fiber.App, service domainApp.IAppService) App {
}
func (controller *App) Login(c *fiber.Ctx) error {
response, err := controller.Service.Login(c.Context())
response, err := controller.Service.Login(c.UserContext())
utils.PanicIfNeeded(err)
return c.JSON(utils.ResponseData{
@ -36,7 +36,7 @@ func (controller *App) Login(c *fiber.Ctx) error {
}
func (controller *App) Logout(c *fiber.Ctx) error {
err := controller.Service.Logout(c.Context())
err := controller.Service.Logout(c.UserContext())
utils.PanicIfNeeded(err)
return c.JSON(utils.ResponseData{
@ -47,7 +47,7 @@ func (controller *App) Logout(c *fiber.Ctx) error {
}
func (controller *App) Reconnect(c *fiber.Ctx) error {
err := controller.Service.Reconnect(c.Context())
err := controller.Service.Reconnect(c.UserContext())
utils.PanicIfNeeded(err)
return c.JSON(utils.ResponseData{
@ -58,7 +58,7 @@ func (controller *App) Reconnect(c *fiber.Ctx) error {
}
func (controller *App) Devices(c *fiber.Ctx) error {
devices, err := controller.Service.FetchDevices(c.Context())
devices, err := controller.Service.FetchDevices(c.UserContext())
utils.PanicIfNeeded(err)
return c.JSON(utils.ResponseData{

10
src/internal/rest/send_controller.go

@ -36,7 +36,7 @@ func (controller *Send) SendText(c *fiber.Ctx) error {
request.Phone = request.Phone + "@s.whatsapp.net"
}
response, err := controller.Service.SendText(c.Context(), request)
response, err := controller.Service.SendText(c.UserContext(), request)
utils.PanicIfNeeded(err)
return c.JSON(utils.ResponseData{
@ -67,7 +67,7 @@ func (controller *Send) SendImage(c *fiber.Ctx) error {
request.Phone = request.Phone + "@s.whatsapp.net"
}
response, err := controller.Service.SendImage(c.Context(), request)
response, err := controller.Service.SendImage(c.UserContext(), request)
utils.PanicIfNeeded(err)
return c.JSON(utils.ResponseData{
@ -96,7 +96,7 @@ func (controller *Send) SendFile(c *fiber.Ctx) error {
request.Phone = request.Phone + "@s.whatsapp.net"
}
response, err := controller.Service.SendFile(c.Context(), request)
response, err := controller.Service.SendFile(c.UserContext(), request)
utils.PanicIfNeeded(err)
return c.JSON(utils.ResponseData{
@ -125,7 +125,7 @@ func (controller *Send) SendVideo(c *fiber.Ctx) error {
request.Phone = request.Phone + "@s.whatsapp.net"
}
response, err := controller.Service.SendVideo(c.Context(), request)
response, err := controller.Service.SendVideo(c.UserContext(), request)
utils.PanicIfNeeded(err)
return c.JSON(utils.ResponseData{
@ -149,7 +149,7 @@ func (controller *Send) SendContact(c *fiber.Ctx) error {
request.Phone = request.Phone + "@s.whatsapp.net"
}
response, err := controller.Service.SendContact(c.Context(), request)
response, err := controller.Service.SendContact(c.UserContext(), request)
utils.PanicIfNeeded(err)
return c.JSON(utils.ResponseData{

4
src/internal/rest/user_controller.go

@ -67,7 +67,7 @@ func (controller *User) UserAvatar(c *fiber.Ctx) error {
}
func (controller *User) UserMyPrivacySetting(c *fiber.Ctx) error {
response, err := controller.Service.MyPrivacySetting(c.Context())
response, err := controller.Service.MyPrivacySetting(c.UserContext())
utils.PanicIfNeeded(err)
return c.JSON(utils.ResponseData{
@ -78,7 +78,7 @@ func (controller *User) UserMyPrivacySetting(c *fiber.Ctx) error {
}
func (controller *User) UserMyListGroups(c *fiber.Ctx) error {
response, err := controller.Service.MyListGroups(c.Context())
response, err := controller.Service.MyListGroups(c.UserContext())
utils.PanicIfNeeded(err)
return c.JSON(utils.ResponseData{

21
src/middleware/select_jid.go

@ -0,0 +1,21 @@
package middleware
import (
"context"
"github.com/aldinokemal/go-whatsapp-web-multidevice/config"
"github.com/gofiber/fiber/v2"
)
func SelectJid() fiber.Handler {
return func(c *fiber.Ctx) error {
selectedJid := string(c.Request().Header.Peek("jid"))
if selectedJid == "" {
selectedJid = config.AppDefaultDevice
}
ctx := context.WithValue(c.Context(), config.AppSelectedDeviceKey, selectedJid)
c.SetUserContext(ctx)
return c.Next()
}
}

4
src/views/index.html

@ -108,7 +108,7 @@
</div>
<div class="green card" @click="sendContactModal('user')" style="cursor: pointer">
<div class="content">
<a class="ui blue right ribbon label">Contact</a>
<a class="ui blue right ribbon label">Private</a>
<div class="header">Send Contact</div>
<div class="description">
Send contact to any whatsapp number
@ -166,7 +166,7 @@
</div>
<div class="green card" @click="sendContactModal('group')" style="cursor: pointer">
<div class="content">
<a class="ui teal right ribbon label">Contact</a>
<a class="ui teal right ribbon label">Group</a>
<div class="header">Send Contact</div>
<div class="description">
Send contact to any whatsapp number

Loading…
Cancel
Save