~evanj/cms

ref: 9d7fbcbdf7e7332ea56493092efe5cffbadccdd6 cms/internal/s/rl/rl.go -rw-r--r-- 5.2 KiB
9d7fbcbdEvan M Jones Fix(go.mod/vendor): Attempting to fix vendoring for deploys. 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Rate limiter.
package rl

import (
	"context"
	"errors"
	"fmt"
	"io"
	"log"
	"time"

	"git.sr.ht/~evanj/cms/internal/m/contenttype"
	"git.sr.ht/~evanj/cms/internal/m/invite"
	"git.sr.ht/~evanj/cms/internal/m/org"
	"git.sr.ht/~evanj/cms/internal/m/space"
	"git.sr.ht/~evanj/cms/internal/m/tier"
	"git.sr.ht/~evanj/cms/internal/m/user"
	"git.sr.ht/~evanj/cms/internal/m/valuetype"
	"git.sr.ht/~evanj/cms/internal/s/cache"
	"git.sr.ht/~evanj/cms/internal/s/db"
	"git.sr.ht/~evanj/cms/pkg/e3"
)

var (
	requestLimits = map[string]int{
		// If not in map, unlimited.
		tier.Free.Name:     60,
		tier.Business.Name: 60,
	}

	spaceLimits = map[string]int{
		// If not in map, unlimited.
		tier.Free.Name:     1,
		tier.Business.Name: 5,
	}

	userLimits = map[string]int{
		// If not in map, unlimited.
		tier.Free.Name:     1,
		tier.Business.Name: 15,
	}

	ErrHitLimit = errors.New("you have surpassed your usage limit: consider upgrading")
	ErrNoAccess = errors.New("you don't have access to that feature")
)

type RL struct {
	*cache.Cache
	e3.E3

	log *log.Logger
	db  *cache.Cache // Or *db.DB depending on nest order.
	e3  e3.E3
}

func New(l *log.Logger, db *cache.Cache, e3 e3.E3) RL {
	return RL{db, e3, l, db, e3}
}

// Limit requests made.

func (rl RL) requestLimit(o org.Org) error {
	now := time.Now().UTC()

	limit, ok := requestLimits[o.Tier().Name]
	if !ok {
		// If not in map, unlimited.
		return rl.db.ActionNew(o, now)
	}

	c, err := rl.db.ActionGetCount(o, now.Add(-1*time.Minute), now)
	if err != nil {
		return err
	}

	if c >= limit {
		return ErrHitLimit
	}

	return rl.db.ActionNew(o, now)
}

func (rl RL) UserGet(username, password string) (user.User, error) {
	u, e := rl.db.UserGet(username, password)
	if e != nil {
		return nil, e
	}
	return u, rl.requestLimit(u.Org())
}

func (rl RL) UserGetFromToken(token string) (user.User, error) {
	u, e := rl.db.UserGetFromToken(token)
	if e != nil {
		return nil, e
	}
	return u, rl.requestLimit(u.Org())
}

// Limit spaces created.

func (rl RL) spaceLimit(o org.Org, getter func() (space.Space, error)) (space.Space, error) {
	limit, ok := spaceLimits[o.Tier().Name]
	if !ok {
		// If not in map, unlimited.
		return getter()
	}

	c, err := rl.db.OrgGetSpaceCount(o)
	if err != nil {
		return nil, err
	}

	if c >= limit {
		return nil, fmt.Errorf("can't create new space: %w", ErrHitLimit)
	}

	return getter()
}

func (rl RL) SpaceNew(user user.User, name, desc string) (space.Space, error) {
	return rl.spaceLimit(user.Org(), func() (space.Space, error) {
		return rl.db.SpaceNew(user, name, desc)
	})
}

func (rl RL) SpaceCopy(user user.User, prevS space.Space, name, desc string) (space.Space, error) {
	return rl.spaceLimit(user.Org(), func() (space.Space, error) {
		return rl.db.SpaceCopy(user, prevS, name, desc)
	})
}

// TODO: Limit users created (and associated with org). Currently, no support
// for adding more users to an org.

// Limit file uploads.

func (rl RL) Upload(ctx context.Context, public bool, filename string, file io.Reader, u user.User) (string, error) {
	if u.Org().Tier().Is(tier.Free) {
		return "", fmt.Errorf("can't upload file: %w", ErrNoAccess)
	}

	return rl.e3.Upload(ctx, public, filename, file)
}

func newParamHasFile(params []db.ContentTypeNewParam) (r bool) {
	for _, p := range params {
		if p.Type == valuetype.File {
			r = true
		}
	}
	return
}

func updateParamHasFile(params []db.ContentTypeUpdateParam) (r bool) {
	for _, p := range params {
		if p.Type == valuetype.File {
			r = true
		}
	}
	return
}

func (rl RL) ContentTypeNew(u user.User, space space.Space, name string, params []db.ContentTypeNewParam) (contenttype.ContentType, error) {
	if space.Org().Tier().Is(tier.Free) && newParamHasFile(params) {
		return nil, fmt.Errorf("can't create content type with field type of file: %w", ErrNoAccess)
	}
	return rl.db.ContentTypeNew(u, space, name, params)
}

func (rl RL) ContentTypeUpdate(u user.User, space space.Space, contenttype contenttype.ContentType, name string, newParams []db.ContentTypeNewParam, updateParams []db.ContentTypeUpdateParam) (contenttype.ContentType, error) {
	if space.Org().Tier().Is(tier.Free) && (newParamHasFile(newParams) || updateParamHasFile(updateParams)) {
		return nil, fmt.Errorf("can't create content type with field type of file: %w", ErrNoAccess)
	}
	return rl.db.ContentTypeUpdate(u, space, contenttype, name, newParams, updateParams)
}

// Rate limit users to org.

func (rl RL) InviteNew(u user.User, o org.Org) (invite.Invite, error) {
	limit, ok := userLimits[o.Tier().Name]
	if !ok {
		// If not in map, unlimited.
		return rl.db.InviteNew(u, o)
	}

	c, err := rl.db.OrgGetUserCount(o)
	if err != nil {
		return nil, err
	}

	if c >= limit {
		return nil, fmt.Errorf("can't invite new users: %w", ErrHitLimit)
	}

	return rl.db.InviteNew(u, o)
}

func (rl RL) InviteAccept(i invite.Invite, u, p, v string) (user.User, invite.Invite, error) {
	limit, ok := userLimits[i.Org().Tier().Name]
	if !ok {
		// If not in map, unlimited.
		return rl.db.InviteAccept(i, u, p, v)
	}

	c, err := rl.db.OrgGetUserCount(i.Org())
	if err != nil {
		return nil, nil, err
	}

	if c >= limit {
		return nil, nil, fmt.Errorf("cannot join this organization: too many users: %w", ErrHitLimit)
	}

	return rl.db.InviteAccept(i, u, p, v)
}