|
1 | 1 | package antispam |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "github.com/sirupsen/logrus" |
| 4 | + "context" |
5 | 5 |
|
6 | | - code_data "github.com/code-payments/code-server/pkg/code/data" |
| 6 | + "github.com/code-payments/code-server/pkg/code/common" |
| 7 | + "github.com/code-payments/code-server/pkg/metrics" |
7 | 8 | ) |
8 | 9 |
|
9 | | -// Guard is an antispam guard that checks whether operations of interest are |
10 | | -// allowed to be performed. |
11 | | -// |
12 | | -// Note: Implementation assumes distributed locking has already occurred for |
13 | | -// all methods. |
14 | 10 | type Guard struct { |
15 | | - log *logrus.Entry |
16 | | - data code_data.Provider |
| 11 | + integration Integration |
17 | 12 | } |
18 | 13 |
|
19 | | -func NewGuard( |
20 | | - data code_data.Provider, |
21 | | -) *Guard { |
22 | | - return &Guard{ |
23 | | - log: logrus.StandardLogger().WithField("type", "antispam/guard"), |
24 | | - data: data, |
| 14 | +func NewGuard(integration Integration) *Guard { |
| 15 | + return &Guard{integration: integration} |
| 16 | +} |
| 17 | + |
| 18 | +func (g *Guard) AllowOpenAccounts(ctx context.Context, owner *common.Account) (bool, error) { |
| 19 | + tracer := metrics.TraceMethodCall(ctx, metricsStructName, "AllowOpenAccounts") |
| 20 | + defer tracer.End() |
| 21 | + |
| 22 | + allow, reason, err := g.integration.AllowOpenAccounts(ctx, owner) |
| 23 | + if err != nil { |
| 24 | + return false, err |
| 25 | + } |
| 26 | + if !allow { |
| 27 | + recordDenialEvent(ctx, actionOpenAccounts, reason) |
| 28 | + } |
| 29 | + return allow, nil |
| 30 | +} |
| 31 | + |
| 32 | +func (g *Guard) AllowWelcomeBonus(ctx context.Context, owner *common.Account) (bool, error) { |
| 33 | + tracer := metrics.TraceMethodCall(ctx, metricsStructName, "AllowWelcomeBonus") |
| 34 | + defer tracer.End() |
| 35 | + |
| 36 | + allow, reason, err := g.integration.AllowWelcomeBonus(ctx, owner) |
| 37 | + if err != nil { |
| 38 | + return false, err |
| 39 | + } |
| 40 | + if !allow { |
| 41 | + recordDenialEvent(ctx, actionWelcomeBonus, reason) |
| 42 | + } |
| 43 | + return allow, nil |
| 44 | +} |
| 45 | + |
| 46 | +func (g *Guard) AllowSendPayment(ctx context.Context, owner, destination *common.Account, isPublic bool) (bool, error) { |
| 47 | + tracer := metrics.TraceMethodCall(ctx, metricsStructName, "AllowSendPayment") |
| 48 | + defer tracer.End() |
| 49 | + |
| 50 | + allow, reason, err := g.integration.AllowSendPayment(ctx, owner, destination, isPublic) |
| 51 | + if err != nil { |
| 52 | + return false, err |
| 53 | + } |
| 54 | + if !allow { |
| 55 | + recordDenialEvent(ctx, actionSendPayment, reason) |
| 56 | + } |
| 57 | + return allow, nil |
| 58 | +} |
| 59 | + |
| 60 | +func (g *Guard) AllowReceivePayments(ctx context.Context, owner *common.Account, isPublic bool) (bool, error) { |
| 61 | + tracer := metrics.TraceMethodCall(ctx, metricsStructName, "AllowReceivePayments") |
| 62 | + defer tracer.End() |
| 63 | + |
| 64 | + allow, reason, err := g.integration.AllowReceivePayments(ctx, owner, isPublic) |
| 65 | + if err != nil { |
| 66 | + return false, err |
| 67 | + } |
| 68 | + if !allow { |
| 69 | + recordDenialEvent(ctx, actionReceivePayments, reason) |
| 70 | + } |
| 71 | + return allow, nil |
| 72 | +} |
| 73 | + |
| 74 | +func (g *Guard) AllowSwap(ctx context.Context, owner *common.Account) (bool, error) { |
| 75 | + tracer := metrics.TraceMethodCall(ctx, metricsStructName, "AllowSwap") |
| 76 | + defer tracer.End() |
| 77 | + |
| 78 | + allow, reason, err := g.integration.AllowSwap(ctx, owner) |
| 79 | + if err != nil { |
| 80 | + return false, err |
| 81 | + } |
| 82 | + if !allow { |
| 83 | + recordDenialEvent(ctx, actionSwap, reason) |
25 | 84 | } |
| 85 | + return allow, nil |
26 | 86 | } |
0 commit comments