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

     1  // Copyright (c) 2017-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 legacy
     6  
     7  import (
     8  	"encoding/json"
     9  	"errors"
    10  	"net/http"
    11  
    12  	www "github.com/decred/politeia/politeiawww/api/www/v1"
    13  	"github.com/decred/politeia/politeiawww/legacy/sessions"
    14  	"github.com/decred/politeia/util"
    15  	"github.com/gorilla/mux"
    16  )
    17  
    18  func (p *Politeiawww) handleTokenInventory(w http.ResponseWriter, r *http.Request) {
    19  	log.Tracef("handleTokenInventory")
    20  
    21  	// Get session user. This is a public route so one might not exist.
    22  	user, err := p.sessions.GetSessionUser(w, r)
    23  	if err != nil && !errors.Is(err, sessions.ErrSessionNotFound) {
    24  		RespondWithError(w, r, 0,
    25  			"handleTokenInventory: getSessionUser %v", err)
    26  		return
    27  	}
    28  
    29  	isAdmin := user != nil && user.Admin
    30  	reply, err := p.processTokenInventory(r.Context(), isAdmin)
    31  	if err != nil {
    32  		RespondWithError(w, r, 0,
    33  			"handleTokenInventory: processTokenInventory: %v", err)
    34  		return
    35  	}
    36  	util.RespondWithJSON(w, http.StatusOK, reply)
    37  }
    38  
    39  func (p *Politeiawww) handleAllVetted(w http.ResponseWriter, r *http.Request) {
    40  	log.Tracef("handleAllVetted")
    41  
    42  	var v www.GetAllVetted
    43  	err := util.ParseGetParams(r, &v)
    44  	if err != nil {
    45  		RespondWithError(w, r, 0, "handleAllVetted: ParseGetParams",
    46  			www.UserError{
    47  				ErrorCode: www.ErrorStatusInvalidInput,
    48  			})
    49  		return
    50  	}
    51  
    52  	vr, err := p.processAllVetted(r.Context(), v)
    53  	if err != nil {
    54  		RespondWithError(w, r, 0,
    55  			"handleAllVetted: processAllVetted %v", err)
    56  		return
    57  	}
    58  
    59  	util.RespondWithJSON(w, http.StatusOK, vr)
    60  }
    61  
    62  func (p *Politeiawww) handleProposalDetails(w http.ResponseWriter, r *http.Request) {
    63  	log.Tracef("handleProposalDetails")
    64  
    65  	// Get version from query string parameters
    66  	var pd www.ProposalsDetails
    67  	err := util.ParseGetParams(r, &pd)
    68  	if err != nil {
    69  		RespondWithError(w, r, 0, "handleProposalDetails: ParseGetParams",
    70  			www.UserError{
    71  				ErrorCode: www.ErrorStatusInvalidInput,
    72  			})
    73  		return
    74  	}
    75  
    76  	// Get proposal token from path parameters
    77  	pathParams := mux.Vars(r)
    78  	pd.Token = pathParams["token"]
    79  
    80  	// Get session user. This is a public route so one might not exist.
    81  	user, err := p.sessions.GetSessionUser(w, r)
    82  	if err != nil && err != sessions.ErrSessionNotFound {
    83  		RespondWithError(w, r, 0,
    84  			"handleProposalDetails: getSessionUser %v", err)
    85  		return
    86  	}
    87  
    88  	reply, err := p.processProposalDetails(r.Context(), pd, user)
    89  	if err != nil {
    90  		RespondWithError(w, r, 0,
    91  			"handleProposalDetails: processProposalDetails %v", err)
    92  		return
    93  	}
    94  
    95  	// Reply with the proposal details.
    96  	util.RespondWithJSON(w, http.StatusOK, reply)
    97  }
    98  
    99  func (p *Politeiawww) handleBatchProposals(w http.ResponseWriter, r *http.Request) {
   100  	log.Tracef("handleBatchProposals")
   101  
   102  	var bp www.BatchProposals
   103  	decoder := json.NewDecoder(r.Body)
   104  	if err := decoder.Decode(&bp); err != nil {
   105  		RespondWithError(w, r, 0, "handleBatchProposals: unmarshal",
   106  			www.UserError{
   107  				ErrorCode: www.ErrorStatusInvalidInput,
   108  			})
   109  		return
   110  	}
   111  
   112  	// Get session user. This is a public route so one might not exist.
   113  	user, err := p.sessions.GetSessionUser(w, r)
   114  	if err != nil && err != sessions.ErrSessionNotFound {
   115  		RespondWithError(w, r, 0,
   116  			"handleBatchProposals: getSessionUser %v", err)
   117  		return
   118  	}
   119  
   120  	reply, err := p.processBatchProposals(r.Context(), bp, user)
   121  	if err != nil {
   122  		RespondWithError(w, r, 0,
   123  			"handleBatchProposals: processBatchProposals %v", err)
   124  		return
   125  	}
   126  
   127  	util.RespondWithJSON(w, http.StatusOK, reply)
   128  }
   129  
   130  func (p *Politeiawww) handleBatchVoteSummary(w http.ResponseWriter, r *http.Request) {
   131  	log.Tracef("handleBatchVoteSummary")
   132  
   133  	var bvs www.BatchVoteSummary
   134  	decoder := json.NewDecoder(r.Body)
   135  	if err := decoder.Decode(&bvs); err != nil {
   136  		RespondWithError(w, r, 0, "handleBatchVoteSummary: unmarshal",
   137  			www.UserError{
   138  				ErrorCode: www.ErrorStatusInvalidInput,
   139  			})
   140  		return
   141  	}
   142  
   143  	reply, err := p.processBatchVoteSummary(r.Context(), bvs)
   144  	if err != nil {
   145  		RespondWithError(w, r, 0,
   146  			"handleBatchVoteSummary: processBatchVoteSummary %v", err)
   147  		return
   148  	}
   149  
   150  	util.RespondWithJSON(w, http.StatusOK, reply)
   151  }
   152  
   153  func (p *Politeiawww) handleVoteStatus(w http.ResponseWriter, r *http.Request) {
   154  	log.Tracef("handleVoteStatus")
   155  
   156  	pathParams := mux.Vars(r)
   157  	token := pathParams["token"]
   158  
   159  	reply, err := p.processVoteStatus(r.Context(), token)
   160  	if err != nil {
   161  		RespondWithError(w, r, 0,
   162  			"handleVoteStatus: processVoteStatus %v", err)
   163  		return
   164  	}
   165  
   166  	util.RespondWithJSON(w, http.StatusOK, reply)
   167  }
   168  
   169  func (p *Politeiawww) handleAllVoteStatus(w http.ResponseWriter, r *http.Request) {
   170  	log.Tracef("handleAllVoteStatus")
   171  
   172  	reply, err := p.processAllVoteStatus(r.Context())
   173  	if err != nil {
   174  		RespondWithError(w, r, 0,
   175  			"handleAllVoteStatus: processAllVoteStatus %v", err)
   176  		return
   177  	}
   178  
   179  	util.RespondWithJSON(w, http.StatusOK, reply)
   180  }
   181  
   182  func (p *Politeiawww) handleActiveVote(w http.ResponseWriter, r *http.Request) {
   183  	log.Tracef("handleActiveVote")
   184  
   185  	avr, err := p.processActiveVote(r.Context())
   186  	if err != nil {
   187  		RespondWithError(w, r, 0,
   188  			"handleActiveVote: processActiveVote %v", err)
   189  		return
   190  	}
   191  
   192  	util.RespondWithJSON(w, http.StatusOK, avr)
   193  }
   194  
   195  func (p *Politeiawww) handleCastVotes(w http.ResponseWriter, r *http.Request) {
   196  	log.Tracef("handleCastVotes")
   197  
   198  	var cv www.Ballot
   199  	decoder := json.NewDecoder(r.Body)
   200  	if err := decoder.Decode(&cv); err != nil {
   201  		RespondWithError(w, r, 0, "handleCastVotes: unmarshal", www.UserError{
   202  			ErrorCode: www.ErrorStatusInvalidInput,
   203  		})
   204  		return
   205  	}
   206  
   207  	avr, err := p.processCastVotes(r.Context(), &cv)
   208  	if err != nil {
   209  		RespondWithError(w, r, 0,
   210  			"handleCastVotes: processCastVotes %v", err)
   211  		return
   212  	}
   213  
   214  	util.RespondWithJSON(w, http.StatusOK, avr)
   215  }
   216  
   217  func (p *Politeiawww) handleVoteResults(w http.ResponseWriter, r *http.Request) {
   218  	log.Tracef("handleVoteResults")
   219  
   220  	pathParams := mux.Vars(r)
   221  	token := pathParams["token"]
   222  
   223  	vrr, err := p.processVoteResults(r.Context(), token)
   224  	if err != nil {
   225  		RespondWithError(w, r, 0,
   226  			"handleVoteResults: processVoteResults %v",
   227  			err)
   228  		return
   229  	}
   230  
   231  	util.RespondWithJSON(w, http.StatusOK, vrr)
   232  }