-
-
Notifications
You must be signed in to change notification settings - Fork 190
/
site.go
751 lines (643 loc) · 20.7 KB
/
site.go
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
// Copyright © Martin Tournoij – This file is part of GoatCounter and published
// under the terms of a slightly modified EUPL v1.2 license, which can be found
// in the LICENSE file or at https://license.goatcounter.com
package goatcounter
import (
"context"
"fmt"
"path"
"strconv"
"strings"
"time"
"zgo.at/errors"
"zgo.at/guru"
"zgo.at/zdb"
"zgo.at/zstd/zcrypto"
"zgo.at/zstd/znet"
"zgo.at/zstd/zslice"
"zgo.at/zstd/zstring"
"zgo.at/zstd/ztime"
)
var reserved = []string{
"www", "mail", "smtp", "imap", "static",
"admin", "ns1", "ns2", "m", "mobile", "api",
"dev", "test", "beta", "new", "staging", "debug", "pprof",
"chat", "example", "yoursite", "test", "sql", "license",
"stat", "stats",
}
var statTables = []string{"hit_stats", "system_stats", "browser_stats",
"location_stats", "language_stats", "size_stats"}
type Site struct {
ID int64 `db:"site_id" json:"id,readonly"`
Parent *int64 `db:"parent" json:"parent,readonly"`
// Custom domain, e.g. "stats.example.com".
//
// When self-hosting this is the domain/vhost your site is accessible at.
Cname *string `db:"cname" json:"cname"`
// When the CNAME was verified.
CnameSetupAt *time.Time `db:"cname_setup_at" json:"cname_setup_at,readonly"`
// Domain code (e.g. "arp242", which makes arp242.goatcounter.com). Only
// used for goatcounter.com and not when self-hosting.
Code string `db:"code" json:"code"`
// Site domain for linking (www.arp242.net). Note this can be a full URL and
// is a bit misnamed.
LinkDomain string `db:"link_domain" json:"link_domain"`
Settings SiteSettings `db:"settings" json:"setttings"`
UserDefaults UserSettings `db:"user_defaults" json:"user_defaults"`
// Whether this site has received any data; will be true after the first
// pageview.
ReceivedData bool `db:"received_data" json:"received_data"`
// {omitdoc}
Notes string `db:"notes" json:"-"`
State string `db:"state" json:"state"`
CreatedAt time.Time `db:"created_at" json:"created_at"`
UpdatedAt *time.Time `db:"updated_at" json:"updated_at"`
FirstHitAt time.Time `db:"first_hit_at" json:"first_hit_at"`
}
// ClearCache clears the cache for this site.
func (s Site) ClearCache(ctx context.Context, full bool) {
cacheSites(ctx).Delete(strconv.FormatInt(s.ID, 10))
// TODO: be more selective about this.
if full {
cachePaths(ctx).Flush()
cacheChangedTitles(ctx).Flush()
}
}
// Defaults sets fields to default values, unless they're already set.
func (s *Site) Defaults(ctx context.Context) {
if s.State == "" {
s.State = StateActive
}
n := ztime.Now()
if !Config(ctx).GoatcounterCom {
s.Code = "serve-" + zcrypto.Secret64()
s.CnameSetupAt = &n
}
s.Code = strings.ToLower(s.Code)
if s.CreatedAt.IsZero() {
s.CreatedAt = n
} else {
s.UpdatedAt = &n
}
if s.FirstHitAt.IsZero() {
s.FirstHitAt = n
}
s.LinkDomain = strings.TrimRight(s.LinkDomain, "/")
s.Settings.Defaults(ctx)
s.UserDefaults.Defaults(ctx)
}
var noUnderscore = time.Date(2020, 03, 20, 0, 0, 0, 0, time.UTC)
// Validate the object.
func (s *Site) Validate(ctx context.Context) error {
v := NewValidate(ctx)
if Config(ctx).GoatcounterCom {
v.Required("code", s.Code)
v.Len("code", s.Code, 2, 50)
v.Exclude("code", s.Code, reserved)
} else {
v.Required("cname", s.Cname)
}
v.Required("state", s.State)
v.Include("state", s.State, States)
v.URL("link_domain", s.LinkDomain)
v.Sub("settings", "", s.Settings.Validate(ctx))
v.Sub("user_defaults", "", s.UserDefaults.Validate(ctx))
// TODO: compat with older requirements, otherwise various update functions
// will error out.
if !s.CreatedAt.IsZero() && s.CreatedAt.Before(noUnderscore) {
for _, c := range s.Code {
if !(c == '-' || c == '_' || (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')) {
v.Append("code", fmt.Sprintf("%q not allowed; characters are limited to '_', '-', a to z, and numbers", c))
break
}
}
if len(s.Code) > 0 && (s.Code[0] == '_' || s.Code[0] == '-') { // Special domains, like _acme-challenge.
v.Append("code", "cannot start with underscore or dash (_, -)")
}
} else {
labels := v.Hostname("code", s.Code)
if len(labels) > 1 {
v.Append("code", "cannot contain '.'")
}
}
if s.Cname != nil {
v.Len("cname", *s.Cname, 4, 255)
v.Domain("cname", *s.Cname)
if Config(ctx).GoatcounterCom && strings.HasSuffix(*s.Cname, Config(ctx).Domain) {
v.Append("cname", "cannot end with %q", Config(ctx).Domain)
}
}
if !v.HasErrors() {
exists, err := s.Exists(ctx)
if err != nil {
return err
}
if exists > 0 {
field := "code"
if s.Cname != nil {
field = "cname"
}
v.Append(field, "already exists")
}
}
return v.ErrorOrNil()
}
// Insert a new row.
func (s *Site) Insert(ctx context.Context) error {
if s.ID > 0 {
return errors.New("ID > 0")
}
s.Defaults(ctx)
err := s.Validate(ctx)
if err != nil {
return err
}
s.ID, err = zdb.InsertID(ctx, "site_id", `insert into sites (
parent, code, cname, link_domain, settings, user_defaults, created_at, first_hit_at, cname_setup_at) values (?)`,
[]any{s.Parent, s.Code, s.Cname, s.LinkDomain, s.Settings, s.UserDefaults, s.CreatedAt, s.CreatedAt, s.CnameSetupAt})
if err != nil && zdb.ErrUnique(err) {
return guru.New(400, "this site already exists: code or domain must be unique")
}
return errors.Wrap(err, "Site.Insert")
}
// Update existing site. Sets settings, cname, link_domain.
func (s *Site) Update(ctx context.Context) error {
if s.ID == 0 {
return errors.New("ID == 0")
}
s.Defaults(ctx)
err := s.Validate(ctx)
if err != nil {
return err
}
err = zdb.Exec(ctx,
`update sites set settings=?, user_defaults=?, cname=?, link_domain=?, updated_at=? where site_id=?`,
s.Settings, s.UserDefaults, s.Cname, s.LinkDomain, s.UpdatedAt, s.ID)
if err != nil {
return errors.Wrap(err, "Site.Update")
}
s.ClearCache(ctx, false)
return nil
}
func (s *Site) UpdateParent(ctx context.Context, newParent *int64) error {
if s.ID == 0 {
return errors.New("ID == 0")
}
s.Parent = newParent
s.Defaults(ctx)
err := s.Validate(ctx)
if err != nil {
return err
}
err = zdb.TX(ctx, func(ctx context.Context) error {
var users Users
err := users.BySite(ctx, s.ID)
if err != nil {
return err
}
for _, u := range users {
u.Site = *newParent
err := u.UpdateSite(ctx)
if err != nil {
return err
}
}
return zdb.Exec(ctx,
`update sites set parent=?, updated_at=? where site_id=?`,
s.Parent, s.UpdatedAt, s.ID)
})
if err != nil {
return errors.Wrap(err, "Site.UpdateParent")
}
s.ClearCache(ctx, false)
return nil
}
// UpdateCode changes the site's domain code (e.g. "test" in
// "test.goatcounter.com").
func (s *Site) UpdateCode(ctx context.Context, code string) error {
if s.ID == 0 {
return errors.New("ID == 0")
}
s.Code = code
s.Defaults(ctx)
err := s.Validate(ctx)
if err != nil {
return err
}
err = zdb.Exec(ctx,
`update sites set code=$1, updated_at=$2 where site_id=$3`,
s.Code, s.UpdatedAt, s.ID)
if err != nil {
return errors.Wrap(err, "Site.UpdateCode")
}
cacheSites(ctx).Delete(strconv.FormatInt(s.ID, 10))
cacheSitesHost(ctx).Flush()
return nil
}
func (s *Site) UpdateReceivedData(ctx context.Context) error {
err := zdb.Exec(ctx, `update sites set received_data=1 where site_id=$1`, s.ID)
s.ClearCache(ctx, false)
return errors.Wrap(err, "Site.UpdateReceivedData")
}
func (s *Site) UpdateFirstHitAt(ctx context.Context, f time.Time) error {
f = f.UTC().Add(-12 * time.Hour)
s.FirstHitAt = f
err := zdb.Exec(ctx,
`update sites set first_hit_at=$1 where site_id=$2`,
s.FirstHitAt, s.ID)
s.ClearCache(ctx, false)
return errors.Wrap(err, "Site.UpdateFirstHitAt")
}
// UpdateCnameSetupAt confirms the custom domain was setup correct.
func (s *Site) UpdateCnameSetupAt(ctx context.Context) error {
if s.ID == 0 {
return errors.New("ID == 0")
}
n := ztime.Now()
s.CnameSetupAt = &n
err := zdb.Exec(ctx,
`update sites set cname_setup_at=$1 where site_id=$2`,
s.CnameSetupAt, s.ID)
if err != nil {
return errors.Wrap(err, "Site.UpdateCnameSetupAt")
}
s.ClearCache(ctx, false)
return nil
}
// Delete a site and all child sites.
func (s *Site) Delete(ctx context.Context, deleteChildren bool) error {
if s.ID == 0 {
return errors.New("ID == 0")
}
return zdb.TX(ctx, func(ctx context.Context) error {
if !deleteChildren {
var n int
err := zdb.Get(ctx, &n, `select site_id from sites where parent = ? order by site_id limit 1`, s.ID)
if err != nil && !zdb.ErrNoRows(err) {
return errors.Wrap(err, "Site.Delete")
}
// Roll over the "parent" to the next site.
if n > 0 {
err = zdb.Exec(ctx, `update sites set parent = ? where parent = ?`, n, s.ID)
if err != nil {
return errors.Wrap(err, "Site.Delete")
}
err = zdb.Exec(ctx, `update sites set parent = null where site_id = ?`, n)
if err != nil {
return errors.Wrap(err, "Site.Delete")
}
err = zdb.Exec(ctx, `update users set site_id = ? where site_id = ?`, n, s.ID)
if err != nil {
return errors.Wrap(err, "Site.Delete")
}
}
// Just clear the entire sites cache; this operation is rare enough
// that it doesn't really matter.
cacheSites(ctx).Flush()
}
// Update the site code so people can delete a site and then immediately
// re-create a new site with the same name.
q := `update sites set state=$1, updated_at=$2, code=random(), cname=null where site_id=$3 or parent=$3`
if zdb.SQLDialect(ctx) == zdb.DialectPostgreSQL {
q = `update sites set state=$1, updated_at=$2, code=gen_random_uuid(), cname=null where site_id=$3 or parent=$3`
}
t := ztime.Now()
err := zdb.Exec(ctx, q, StateDeleted, t, s.ID)
if err != nil {
return errors.Wrap(err, "Site.Delete")
}
s.ClearCache(ctx, true)
s.ID = 0
s.UpdatedAt = &t
s.State = StateDeleted
return nil
})
}
func (s Site) Undelete(ctx context.Context, id int64) error {
s.State = StateActive
s.ID = id
err := zdb.Exec(ctx, `update sites set state = ? where site_id = ?`, StateActive, id)
if err != nil {
return fmt.Errorf("Site.Undelete %d: %w", id, err)
}
s.ClearCache(ctx, false)
return errors.Wrap(s.ByID(ctx, id), "Site.Undelete")
}
// Exists checks if this site already exists, based on either the Cname or Code
// field.
func (s Site) Exists(ctx context.Context) (int64, error) {
var (
id int64
query = `select site_id from sites where lower(code) = lower($1) and site_id != $2 limit 1`
params = []any{s.Code, s.ID}
)
if s.Cname != nil {
query = `select site_id from sites where lower(cname) = lower($1) and site_id != $2 limit 1`
params = []any{s.Cname, s.ID}
}
err := zdb.Get(ctx, &id, query, params...)
if err != nil && !zdb.ErrNoRows(err) {
return 0, fmt.Errorf("Site.Exists: %w", err)
}
return id, nil
}
// ByID gets a site by ID.
func (s *Site) ByID(ctx context.Context, id int64) error {
err := s.ByIDState(ctx, id, StateActive)
if err != nil {
return fmt.Errorf("Site.ByID: %w", errors.Unwrap(err))
}
return nil
}
// ByIDState gets a site by ID and state. This may return deleted sites.
func (s *Site) ByIDState(ctx context.Context, id int64, state string) error {
k := strconv.FormatInt(id, 10)
ss, ok := cacheSites(ctx).Get(k)
if ok {
*s = *ss.(*Site)
return nil
}
err := zdb.Get(ctx, s,
`/* Site.ByID */ select * from sites where site_id=$1 and state=$2`,
id, state)
if err != nil {
return errors.Wrapf(err, "Site.ByIDState %d", id)
}
cacheSites(ctx).SetDefault(k, s)
return nil
}
// ByCode gets a site by code.
func (s *Site) ByCode(ctx context.Context, code string) error {
return errors.Wrapf(zdb.Get(ctx, s,
`/* Site.ByCode */ select * from sites where code=$1 and state=$2`,
code, StateActive), "Site.ByCode %s", code)
}
// ByHost gets a site by host name.
func (s *Site) ByHost(ctx context.Context, host string) error {
ss, ok := cacheSitesHost(ctx).Get(host)
if ok {
*s = *ss.(*Site)
return nil
}
// Custom domain or serve.
if !Config(ctx).GoatcounterCom || !strings.HasSuffix(host, Config(ctx).Domain) {
err := zdb.Get(ctx, s,
`/* Site.ByHost */ select * from sites where lower(cname)=lower($1) and state=$2`,
znet.RemovePort(host), StateActive)
if err != nil {
return errors.Wrap(err, "site.ByHost: from custom domain")
}
cacheSitesHost(ctx).Set(strconv.FormatInt(s.ID, 10), host, s)
return nil
}
// Get from code (e.g. "arp242" in "arp242.goatcounter.com").
p := strings.Index(host, ".")
if p == -1 {
return errors.Errorf("Site.ByHost: no subdomain in host %q", host)
}
err := zdb.Get(ctx, s,
`/* Site.ByHost */ select * from sites where lower(code)=lower($1) and state=$2`,
host[:p], StateActive)
if err != nil {
return errors.Wrap(err, "site.ByHost: from code")
}
cacheSitesHost(ctx).Set(strconv.FormatInt(s.ID, 10), host, s)
return nil
}
// Find a site: by ID if ident is a number, or by host if it's not.
func (s *Site) Find(ctx context.Context, ident string) error {
id, err := strconv.ParseInt(ident, 10, 64)
if err == nil {
return errors.Wrap(s.ByID(ctx, id), "Site.Find")
}
return errors.Wrap(s.ByHost(ctx, ident), "Site.Find")
}
// ListSubs lists all subsites, including the current site and parent.
func (s *Site) ListSubs(ctx context.Context) ([]string, error) {
col := "cname"
if Config(ctx).GoatcounterCom {
col = "code"
}
var codes []string
err := zdb.Select(ctx, &codes, `/* Site.ListSubs */
select `+col+` from sites
where state=$1 and (parent=$2 or site_id=$2) or (
parent = (select parent from sites where site_id=$2) or
site_id = (select parent from sites where site_id=$2)
) and state=$1
order by code
`, StateActive, s.ID)
return codes, errors.Wrap(err, "Site.ListSubs")
}
// Domain gets the global default domain, or this site's configured custom
// domain.
func (s Site) Domain(ctx context.Context) string {
if s.Cname != nil && s.CnameSetupAt != nil {
return *s.Cname
}
return Config(ctx).Domain
}
// Display format: just the domain (cname or code+domain).
func (s Site) Display(ctx context.Context) string {
if s.Cname != nil && s.CnameSetupAt != nil {
return *s.Cname
}
return fmt.Sprintf("%s.%s", s.Code, znet.RemovePort(Config(ctx).Domain))
}
// URL to this site, without the scheme.
func (s Site) SchemelessURL(ctx context.Context) string {
if s.Cname != nil && s.CnameSetupAt != nil {
return *s.Cname + Config(ctx).Port + Config(ctx).BasePath
}
return fmt.Sprintf("%s.%s%s%s",
s.Code, Config(ctx).Domain, Config(ctx).Port, Config(ctx).BasePath)
}
// URL to this site.
func (s Site) URL(ctx context.Context) string {
if Config(ctx).Dev {
return "http://" + s.SchemelessURL(ctx)
}
return "https://" + s.SchemelessURL(ctx)
}
// LinkDomainURL creates a valid url to the configured LinkDomain.
func (s Site) LinkDomainURL(withProto bool, paths ...string) string {
if s.LinkDomain == "" {
return ""
}
if withProto && !zstring.HasPrefixes(s.LinkDomain, "http://", "https://") {
s.LinkDomain = "http://" + s.LinkDomain
} else if !withProto {
s.LinkDomain = zstring.TrimPrefixes(s.LinkDomain, "http://", "https://")
}
return strings.TrimRight(s.LinkDomain, "/") + path.Join(paths...)
}
// IDOrParent gets this site's ID or the parent ID if that's set.
func (s Site) IDOrParent() int64 {
if s.Parent != nil {
return *s.Parent
}
return s.ID
}
// DeleteAll deletes all pageviews for this site, keeping the site itself and
// user intact.
func (s Site) DeleteAll(ctx context.Context) error {
return zdb.TX(ctx, func(ctx context.Context) error {
for _, t := range append(statTables, "campaign_stats", "hit_counts", "ref_counts", "hits", "paths") {
err := zdb.Exec(ctx, `delete from `+t+` where site_id=:id`, map[string]any{"id": s.ID})
if err != nil {
return errors.Wrap(err, "Site.DeleteAll: delete "+t)
}
}
s.ClearCache(ctx, true)
return nil
})
}
func (s Site) DeleteOlderThan(ctx context.Context, days int) error {
if days < 14 {
return errors.Errorf("days must be at least 14: %d", days)
}
return zdb.TX(ctx, func(ctx context.Context) error {
ival := Interval(ctx, days)
var pathIDs []int64
err := zdb.Select(ctx, &pathIDs, `/* Site.DeleteOlderThan */
select path_id from hit_counts where site_id=$1 and hour < `+ival+` group by path_id`, s.ID)
if err != nil {
return errors.Wrap(err, "Site.DeleteOlderThan: get paths")
}
for _, t := range append(statTables, "campaign_stats") {
err := zdb.Exec(ctx, `delete from `+t+` where site_id=$1 and day < `+ival, s.ID)
if err != nil {
return errors.Wrap(err, "Site.DeleteOlderThan: delete "+t)
}
}
err = zdb.Exec(ctx, `delete from hit_counts where site_id=$1 and hour < `+ival, s.ID)
if err != nil {
return errors.Wrap(err, "Site.DeleteOlderThan: delete hit_counts")
}
err = zdb.Exec(ctx, `delete from ref_counts where site_id=$1 and hour < `+ival, s.ID)
if err != nil {
return errors.Wrap(err, "Site.DeleteOlderThan: delete ref_counts")
}
err = zdb.Exec(ctx, `delete from hits where site_id=$1 and created_at < `+ival, s.ID)
if err != nil {
return errors.Wrap(err, "Site.DeleteOlderThan: delete hits")
}
if len(pathIDs) > 0 {
var remainPath []int64
err := zdb.Select(ctx, &remainPath, `/* Site.DeleteOlderThan */
select path_id from hit_counts where site_id=? and path_id in (?)`,
s.ID, pathIDs)
if err != nil {
return errors.Wrap(err, "Site.DeleteOlderThan")
}
diff := zslice.Difference(pathIDs, remainPath)
if len(diff) > 0 {
err = zdb.Exec(ctx, `delete from paths where site_id=? and path_id in (?)`, s.ID, diff)
if err != nil {
return errors.Wrap(err, "Site.DeleteOlderThan")
}
}
}
return nil
})
}
// Sites is a list of sites.
type Sites []Site
// UnscopedList lists all sites, not scoped to the current user.
func (s *Sites) UnscopedList(ctx context.Context) error {
return errors.Wrap(zdb.Select(ctx, s,
`/* Sites.List */ select * from sites where state=$1`,
StateActive), "Sites.List")
}
// UnscopedListCnames all sites that have CNAME set, not scoped to the current
// user.
func (s *Sites) UnscopedListCnames(ctx context.Context) error {
return errors.Wrap(zdb.Select(ctx, s, `/* Sites.ListCnames */
select * from sites where state=$1 and cname is not null`,
StateActive), "Sites.List")
}
// ListSubs lists all subsites for the current site.
func (s *Sites) ListSubs(ctx context.Context) error {
return errors.Wrap(zdb.Select(ctx, s, `/* Sites.ListSubs */
select * from sites where parent=$1 and state=$2 order by code`,
MustGetSite(ctx).ID, StateActive), "Sites.ListSubs")
}
// ForThisAccount gets all sites associated with this account.
func (s *Sites) ForThisAccount(ctx context.Context, excludeCurrent bool) error {
site := MustGetSite(ctx)
err := zdb.Select(ctx, s, `/* Sites.ForThisAccount */
select * from sites
where state=$1 and (parent=$2 or site_id=$2) or (
parent = (select parent from sites where site_id=$2) or
site_id = (select parent from sites where site_id=$2)
) and state=$1
order by code
`, StateActive, site.ID)
if err != nil {
return errors.Wrap(err, "Sites.ForThisAccount")
}
if excludeCurrent {
ss := *s
for i := range ss {
if ss[i].ID == site.ID {
*s = append(ss[:i], ss[i+1:]...)
break
}
}
}
return nil
}
// ContainsCNAME reports if there is a site with this CNAME set.
func (s *Sites) ContainsCNAME(ctx context.Context, cname string) (bool, error) {
var ok bool
err := zdb.Get(ctx, &ok, `/* Sites.ContainsCNAME */
select 1 from sites where lower(cname)=lower($1) limit 1`, cname)
return ok, errors.Wrapf(err, "Sites.ContainsCNAME for %q", cname)
}
// OldSoftDeleted finds all sites which have been soft-deleted more than a week
// ago.
func (s *Sites) OldSoftDeleted(ctx context.Context) error {
err := zdb.Select(ctx, s, `select * from sites where state=$1`, StateDeleted)
return errors.Wrap(err, "Sites.OldSoftDeleted")
}
// Find sites: by ID if ident is a number, or by host if it's not.
func (s *Sites) Find(ctx context.Context, ident []string) error {
ids, strs := splitIntStr(ident)
err := zdb.Select(ctx, s, `select * from sites where
{{:ids site_id in (:ids) or}}
{{:strs! 0=1}}
{{:strs cname in (:strs)}}`,
map[string]any{"ids": ids, "strs": strs})
return errors.Wrap(err, "Sites.Find")
}
// IDs gets a list of all IDs for these sites.
func (s *Sites) IDs() []int64 {
ids := make([]int64, 0, len(*s))
for _, ss := range *s {
ids = append(ids, ss.ID)
}
return ids
}
// Delete all sites in this selection.
func (s *Sites) Delete(ctx context.Context, deleteChildren bool) error {
err := zdb.TX(ctx, func(ctx context.Context) error {
for _, ss := range *s {
err := ss.Delete(ctx, deleteChildren)
if err != nil {
return err
}
}
return nil
})
return errors.Wrap(err, "Sites.Delete")
}
// ListIDs lists all sites with the given IDs.
func (s *Sites) ListIDs(ctx context.Context, ids ...int64) error {
if len(ids) == 0 {
return nil
}
err := zdb.Select(ctx, s,
`select * from sites where state=? and site_id in (?) order by created_at desc`,
StateActive, ids)
return errors.Wrap(err, "Sites.ListIDs")
}