github.com/decred/politeia@v1.4.0/politeiawww/legacy/emailcache.go (about)

     1  // Copyright (c) 2017-2020 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package legacy
     6  
     7  import (
     8  	"github.com/decred/politeia/politeiawww/legacy/user"
     9  	"github.com/google/uuid"
    10  )
    11  
    12  // initUserEmailsCache initializes the userEmails cache by iterating through
    13  // all the users in the database and adding a email-userID mapping for them.
    14  //
    15  // This function must be called WITHOUT the lock held.
    16  func (p *Politeiawww) initUserEmailsCache() error {
    17  	p.Lock()
    18  	defer p.Unlock()
    19  
    20  	return p.db.AllUsers(func(u *user.User) {
    21  		p.userEmails[u.Email] = u.ID
    22  	})
    23  }
    24  
    25  // setUserEmailsCache sets a email-userID mapping in the user emails cache.
    26  //
    27  // This function must be called WITHOUT the lock held.
    28  func (p *Politeiawww) setUserEmailsCache(email string, id uuid.UUID) {
    29  	p.Lock()
    30  	defer p.Unlock()
    31  	p.userEmails[email] = id
    32  }
    33  
    34  // userIDByEmail returns a userID given their email address.
    35  //
    36  // This function must be called WITHOUT the lock held.
    37  func (p *Politeiawww) userIDByEmail(email string) (uuid.UUID, bool) {
    38  	p.RLock()
    39  	defer p.RUnlock()
    40  	id, ok := p.userEmails[email]
    41  	return id, ok
    42  }
    43  
    44  // userByEmail returns a User object given their email address.
    45  //
    46  // This function must be called WITHOUT the lock held.
    47  func (p *Politeiawww) userByEmail(email string) (*user.User, error) {
    48  	id, ok := p.userIDByEmail(email)
    49  	if !ok {
    50  		log.Debugf("userByEmail: email lookup failed for '%v'", email)
    51  		return nil, user.ErrUserNotFound
    52  	}
    53  	return p.db.UserGetById(id)
    54  }