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

     1  // Copyright (c) 2020-2021 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 comments
     6  
     7  import (
     8  	v1 "github.com/decred/politeia/politeiawww/api/comments/v1"
     9  	"github.com/decred/politeia/politeiawww/legacy/user"
    10  )
    11  
    12  // paywallIsEnabled returns whether the user paywall is enabled.
    13  //
    14  // This function is a temporary function that will be removed once user plugins
    15  // have been implemented.
    16  func (c *Comments) paywallIsEnabled() bool {
    17  	return c.cfg.PaywallAmount != 0 && c.cfg.PaywallXpub != ""
    18  }
    19  
    20  // userHasPaid returns whether the user has paid their user registration fee.
    21  //
    22  // This function is a temporary function that will be removed once user plugins
    23  // have been implemented.
    24  func userHasPaid(u user.User) bool {
    25  	return u.NewUserPaywallTx != ""
    26  }
    27  
    28  // piHookNewPre hook runs before each New command.
    29  func (c *Comments) piHookNewPre(u user.User) error {
    30  	if !c.paywallIsEnabled() {
    31  		return nil
    32  	}
    33  
    34  	// Verify user has paid registration paywall
    35  	if !userHasPaid(u) {
    36  		return v1.PluginErrorReply{
    37  			PluginID:  user.PiUserPluginID,
    38  			ErrorCode: user.ErrorCodeUserRegistrationNotPaid,
    39  		}
    40  	}
    41  
    42  	return nil
    43  }
    44  
    45  // piHookEditPre hook runs before each Edit command.
    46  func (c *Comments) piHookEditPre(u user.User) error {
    47  	if !c.paywallIsEnabled() {
    48  		return nil
    49  	}
    50  
    51  	// Verify user has paid registration paywall
    52  	if !userHasPaid(u) {
    53  		return v1.PluginErrorReply{
    54  			PluginID:  user.PiUserPluginID,
    55  			ErrorCode: user.ErrorCodeUserRegistrationNotPaid,
    56  		}
    57  	}
    58  
    59  	return nil
    60  }
    61  
    62  // piHookVotePre hook runs before each Vote command.
    63  func (c *Comments) piHookVotePre(u user.User) error {
    64  	if !c.paywallIsEnabled() {
    65  		return nil
    66  	}
    67  
    68  	// Verify user has paid registration paywall
    69  	if !userHasPaid(u) {
    70  		return v1.PluginErrorReply{
    71  			PluginID:  user.PiUserPluginID,
    72  			ErrorCode: user.ErrorCodeUserRegistrationNotPaid,
    73  		}
    74  	}
    75  
    76  	return nil
    77  }