Skip to content

Commit

Permalink
feat(underattack): custom datastore (#49)
Browse files Browse the repository at this point in the history
* feat(underattack): custom datastore

as part of migrating captcha-lite functionality to captcha

* fix: mismatched swarm group id comparator

* chore: rename DATABASE_URL to POSTGRES_URL for testing

* test: pre set data for under attack
  • Loading branch information
aldy505 authored Dec 25, 2023
1 parent c8f1c01 commit 6cb9adf
Show file tree
Hide file tree
Showing 32 changed files with 1,110 additions and 647 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ ENVIRONMENT=
BOT_TOKEN=
SENTRY_DSN=
REDIS_URL=
DATABASE_URL=
POSTGRES_URL=
MONGO_URL=
TZ=
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
run: go test -v -coverprofile=coverage.out -covermode=atomic ./...
env:
ENVIRONMENT: development
DATABASE_URL: postgres://postgres:password@db:5432/captcha?sslmode=disable
POSTGRES_URL: postgres://postgres:password@db:5432/captcha?sslmode=disable
REDIS_URL: redis://@cache:6379/
MONGO_URL: mongodb://root:password@mongo:27017/captcha?useNewUrlParser=true&useUnifiedTopology=true&authSource=admin
MONGO_DBNAME: captcha
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
run: go test -v -coverprofile=coverage.out -covermode=atomic ./...
env:
ENVIRONMENT: development
DATABASE_URL: postgres://postgres:password@db:5432/captcha?sslmode=disable
POSTGRES_URL: postgres://postgres:password@db:5432/captcha?sslmode=disable
REDIS_URL: redis://@cache:6379/
MONGO_URL: mongodb://root:password@mongo:27017/captcha?useNewUrlParser=true&useUnifiedTopology=true&authSource=admin
MONGO_DBNAME: captcha
Expand Down
8 changes: 4 additions & 4 deletions analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
// Dependency is the dependency injection struct
// for the analytics package.
type Dependency struct {
Memory *bigcache.BigCache
Bot *tb.Bot
DB *sqlx.DB
TeknumID string
Memory *bigcache.BigCache
Bot *tb.Bot
DB *sqlx.DB
HomeGroupID int64
}

// HourMapper is meant to use for mapping a time.Hour() to a string
Expand Down
8 changes: 4 additions & 4 deletions analytics/analytics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
var dependency *analytics.Dependency

func TestMain(m *testing.M) {
databaseUrl, ok := os.LookupEnv("DATABASE_URL")
databaseUrl, ok := os.LookupEnv("POSTGRES_URL")
if !ok {
databaseUrl = "postgresql://postgres:password@localhost:5432/captcha?sslmode=disable"
}
Expand Down Expand Up @@ -48,9 +48,9 @@ func TestMain(m *testing.M) {
}

dependency = &analytics.Dependency{
DB: db,
Memory: memory,
TeknumID: "123456789",
DB: db,
Memory: memory,
HomeGroupID: 123456789,
}

setupCtx, setupCancel := context.WithTimeout(context.Background(), time.Second*30)
Expand Down
3 changes: 1 addition & 2 deletions analytics/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package analytics
import (
"context"
"database/sql"
"strconv"
"time"

"github.com/teknologi-umum/captcha/shared"
Expand All @@ -21,7 +20,7 @@ import (
// reason, their data should still be here. But, their joined date
// will be updated to their newest join date.
func (d *Dependency) NewUser(ctx context.Context, m *tb.Message, user *tb.User) {
if !m.FromGroup() || strconv.FormatInt(m.Chat.ID, 10) != d.TeknumID {
if !m.FromGroup() || m.Chat.ID != d.HomeGroupID {
return
}

Expand Down
3 changes: 1 addition & 2 deletions analytics/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package analytics

import (
"context"
"strconv"
"time"

tb "gopkg.in/telebot.v3"
Expand All @@ -16,7 +15,7 @@ func (d *Dependency) NewMessage(m *tb.Message) error {
return nil
}

if strconv.FormatInt(m.Chat.ID, 10) != d.TeknumID {
if m.Chat.ID != d.HomeGroupID {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion analytics/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestMain(m *testing.M) {
mongoDbName = "captcha"
}

databaseUrl, ok := os.LookupEnv("DATABASE_URL")
databaseUrl, ok := os.LookupEnv("POSTGRES_URL")
if !ok {
databaseUrl = "postgresql://postgres:password@localhost:5432/captcha?sslmode=disable"
}
Expand Down
5 changes: 2 additions & 3 deletions analytics/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"database/sql"
"fmt"
"strconv"
"time"

"github.com/teknologi-umum/captcha/shared"
Expand All @@ -14,7 +13,7 @@ import (
)

func (d *Dependency) SwarmLog(user *tb.User, groupID int64, finishedCaptcha bool) {
if strconv.FormatInt(groupID, 10) != d.TeknumID {
if groupID != d.HomeGroupID {
return
}

Expand Down Expand Up @@ -70,7 +69,7 @@ func (d *Dependency) SwarmLog(user *tb.User, groupID int64, finishedCaptcha bool
}

func (d *Dependency) UpdateSwarm(user *tb.User, groupID int64, finishedCaptcha bool) {
if strconv.FormatInt(groupID, 10) != d.TeknumID {
if groupID != d.HomeGroupID {
return
}

Expand Down
9 changes: 3 additions & 6 deletions captcha/captcha.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package captcha

import (
"github.com/teknologi-umum/captcha/analytics"

"github.com/allegro/bigcache/v3"
tb "gopkg.in/telebot.v3"
)

// Dependencies contains the dependency injection struct for
// methods in the captcha package.
type Dependencies struct {
Memory *bigcache.BigCache
Bot *tb.Bot
Analytics *analytics.Dependency
TeknumID string
Memory *bigcache.BigCache
Bot *tb.Bot
TeknumGroupID int64
}
2 changes: 1 addition & 1 deletion captcha/welcome.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (d *Dependencies) sendWelcomeMessage(ctx context.Context, m *tb.Message) er

var msgToSend string = regularWelcomeMessage

if strconv.FormatInt(m.Chat.ID, 10) == d.TeknumID {
if m.Chat.ID == d.TeknumGroupID {
msgToSend = currentWelcomeMessages[randomNum()]
}

Expand Down
Loading

0 comments on commit 6cb9adf

Please sign in to comment.