aboutsummaryrefslogtreecommitdiff
path: root/internal/db/email_addresses.go
blob: 4f30a8981f894db615e5adadc5ce3cb0d2864d90 (plain)
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
// Copyright 2022 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package db

import (
	"context"
	"fmt"

	"github.com/pkg/errors"
	"gorm.io/gorm"

	"gogs.io/gogs/internal/errutil"
)

// EmailAddressesStore is the persistent interface for email addresses.
//
// NOTE: All methods are sorted in alphabetical order.
type EmailAddressesStore interface {
	// GetByEmail returns the email address with given email. If `needsActivated` is
	// true, only activated email will be returned, otherwise, it may return
	// inactivated email addresses. It returns ErrEmailNotExist when no qualified
	// email is not found.
	GetByEmail(ctx context.Context, email string, needsActivated bool) (*EmailAddress, error)
}

var EmailAddresses EmailAddressesStore

var _ EmailAddressesStore = (*emailAddresses)(nil)

type emailAddresses struct {
	*gorm.DB
}

// NewEmailAddressesStore returns a persistent interface for email addresses
// with given database connection.
func NewEmailAddressesStore(db *gorm.DB) EmailAddressesStore {
	return &emailAddresses{DB: db}
}

var _ errutil.NotFound = (*ErrEmailNotExist)(nil)

type ErrEmailNotExist struct {
	args errutil.Args
}

// IsErrEmailAddressNotExist returns true if the underlying error has the type
// ErrEmailNotExist.
func IsErrEmailAddressNotExist(err error) bool {
	_, ok := errors.Cause(err).(ErrEmailNotExist)
	return ok
}

func (err ErrEmailNotExist) Error() string {
	return fmt.Sprintf("email address does not exist: %v", err.args)
}

func (ErrEmailNotExist) NotFound() bool {
	return true
}

func (db *emailAddresses) GetByEmail(ctx context.Context, email string, needsActivated bool) (*EmailAddress, error) {
	tx := db.WithContext(ctx).Where("email = ?", email)
	if needsActivated {
		tx = tx.Where("is_activated = ?", true)
	}

	emailAddress := new(EmailAddress)
	err := tx.First(emailAddress).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, ErrEmailNotExist{
				args: errutil.Args{
					"email": email,
				},
			}
		}
		return nil, err
	}
	return emailAddress, nil
}