Initial commit: Open sourcing all of the Maple Open Technologies code.

This commit is contained in:
Bartlomiej Mika 2025-12-02 14:33:08 -05:00
commit 755d54a99d
2010 changed files with 448675 additions and 0 deletions

View file

@ -0,0 +1,40 @@
package inviteemailratelimit
import (
"context"
"time"
"github.com/gocql/gocql"
"go.uber.org/zap"
)
// GetDailyEmailCount returns the number of invitation emails sent by a user on the given date.
// Returns 0 if no record exists (user hasn't sent any invites today).
func (r *repositoryImpl) GetDailyEmailCount(ctx context.Context, userID gocql.UUID, date time.Time) (int, error) {
r.logger.Debug("Getting daily email count",
zap.String("user_id", userID.String()),
zap.Time("date", date))
// Normalize date to midnight UTC
dateOnly := date.UTC().Truncate(24 * time.Hour)
var count int64
query := r.session.Query(`
SELECT emails_sent_today
FROM invite_email_rate_limits_by_user_id_and_date
WHERE user_id = ? AND date = ?
`, userID, dateOnly).WithContext(ctx)
if err := query.Scan(&count); err != nil {
if err == gocql.ErrNotFound {
// No record means no emails sent today
return 0, nil
}
r.logger.Error("Failed to get daily email count",
zap.String("user_id", userID.String()),
zap.Error(err))
return 0, err
}
return int(count), nil
}

View file

@ -0,0 +1,36 @@
// Package inviteemailratelimit provides rate limiting for invitation emails
// using Cassandra counter tables.
package inviteemailratelimit
import (
"context"
"time"
"github.com/gocql/gocql"
"go.uber.org/zap"
"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/config"
)
// Repository defines the interface for invite email rate limiting
type Repository interface {
// GetDailyEmailCount returns the number of invitation emails sent by a user today
GetDailyEmailCount(ctx context.Context, userID gocql.UUID, date time.Time) (int, error)
// IncrementDailyEmailCount increments the counter for emails sent today
IncrementDailyEmailCount(ctx context.Context, userID gocql.UUID, date time.Time) error
}
type repositoryImpl struct {
logger *zap.Logger
session *gocql.Session
}
// NewRepository creates a new invite email rate limit repository
func NewRepository(appCfg *config.Configuration, session *gocql.Session, logger *zap.Logger) Repository {
logger = logger.Named("InviteEmailRateLimitRepository")
return &repositoryImpl{
logger: logger,
session: session,
}
}

View file

@ -0,0 +1,42 @@
package inviteemailratelimit
import (
"context"
"time"
"github.com/gocql/gocql"
"go.uber.org/zap"
)
// IncrementDailyEmailCount increments the counter for emails sent by a user on the given date.
// Uses Cassandra COUNTER type for atomic increment operations.
// TTL of 2 days (172800 seconds) is applied at the UPDATE level since counter tables
// do not support default_time_to_live in Cassandra.
func (r *repositoryImpl) IncrementDailyEmailCount(ctx context.Context, userID gocql.UUID, date time.Time) error {
r.logger.Debug("Incrementing daily email count",
zap.String("user_id", userID.String()),
zap.Time("date", date))
// Normalize date to midnight UTC
dateOnly := date.UTC().Truncate(24 * time.Hour)
// TTL of 172800 seconds = 2 days for automatic cleanup
query := r.session.Query(`
UPDATE invite_email_rate_limits_by_user_id_and_date
USING TTL 172800
SET emails_sent_today = emails_sent_today + 1
WHERE user_id = ? AND date = ?
`, userID, dateOnly).WithContext(ctx)
if err := query.Exec(); err != nil {
r.logger.Error("Failed to increment daily email count",
zap.String("user_id", userID.String()),
zap.Error(err))
return err
}
r.logger.Debug("Successfully incremented daily email count",
zap.String("user_id", userID.String()))
return nil
}

View file

@ -0,0 +1,13 @@
package inviteemailratelimit
import (
"github.com/gocql/gocql"
"go.uber.org/zap"
"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/config"
)
// ProvideRepository provides an invite email rate limit repository for Wire DI
func ProvideRepository(cfg *config.Config, session *gocql.Session, logger *zap.Logger) Repository {
return NewRepository(cfg, session, logger)
}