github.com/decred/politeia@v1.4.0/politeiawww/legacy/records/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 records 6 7 import ( 8 "fmt" 9 10 v1 "github.com/decred/politeia/politeiawww/api/records/v1" 11 "github.com/decred/politeia/politeiawww/legacy/user" 12 ) 13 14 // paywallIsEnabled returns whether the user paywall is enabled. 15 // 16 // This function is a temporary function that will be removed once user plugins 17 // have been implemented. 18 func (r *Records) paywallIsEnabled() bool { 19 return r.cfg.PaywallAmount != 0 && r.cfg.PaywallXpub != "" 20 } 21 22 // userHasPaid returns whether the user has paid their user registration fee. 23 // 24 // This function is a temporary function that will be removed once user plugins 25 // have been implemented. 26 func userHasPaid(u user.User) bool { 27 return u.NewUserPaywallTx != "" 28 } 29 30 // userHashProposalCredits returns whether the user has any unspent proposal 31 // credits. 32 // 33 // This function is a temporary function that will be removed once user plugins 34 // have been implemented. 35 func userHasProposalCredits(u user.User) bool { 36 return len(u.UnspentProposalCredits) > 0 37 } 38 39 // spendProposalCredit moves a unspent credit to the spent credit list and 40 // updates the user in the database. 41 // 42 // This function is a temporary function that will be removed once user plugins 43 // have been implemented. 44 func (r *Records) spendProposalCredit(u user.User, token string) error { 45 // Verify there are credits to be spent 46 if !userHasProposalCredits(u) { 47 return fmt.Errorf("no proposal credits found") 48 } 49 50 // Credits are spent FIFO 51 c := u.UnspentProposalCredits[0] 52 c.CensorshipToken = token 53 u.SpentProposalCredits = append(u.SpentProposalCredits, c) 54 u.UnspentProposalCredits = u.UnspentProposalCredits[1:] 55 56 return r.userdb.UserUpdate(u) 57 } 58 59 // piHookNewRecordpre executes the new record pre hook for pi. 60 // 61 // This function is a temporary function that will be removed once user plugins 62 // have been implemented. 63 func (r *Records) piHookNewRecordPre(u user.User) error { 64 if !r.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 // Verify user has a proposal credit 77 if !userHasProposalCredits(u) { 78 return v1.PluginErrorReply{ 79 PluginID: user.PiUserPluginID, 80 ErrorCode: user.ErrorCodeUserBalanceInsufficient, 81 } 82 } 83 return nil 84 } 85 86 // piHoonNewRecordPost executes the new record post hook for pi. 87 // 88 // This function is a temporary function that will be removed once user plugins 89 // have been implemented. 90 func (r *Records) piHookNewRecordPost(u user.User, token string) error { 91 if !r.paywallIsEnabled() { 92 return nil 93 } 94 return r.spendProposalCredit(u, token) 95 }