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

     1  // Copyright (c) 2017-2019 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  	"context"
     9  	"fmt"
    10  
    11  	"github.com/decred/politeia/politeiad/backend/gitbe/decredplugin"
    12  )
    13  
    14  // decredGetComments sends the decred plugin getcomments command to the cache
    15  // and returns all of the comments for the passed in proposal token.
    16  func (p *Politeiawww) decredGetComments(ctx context.Context, token string) ([]decredplugin.Comment, error) {
    17  	// Setup plugin command
    18  	gc := decredplugin.GetComments{
    19  		Token: token,
    20  	}
    21  	payload, err := decredplugin.EncodeGetComments(gc)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  
    26  	// Execute plugin command
    27  	reply, err := p.politeiad.PluginCommand(ctx, decredplugin.ID,
    28  		decredplugin.CmdGetComments, string(payload))
    29  	if err != nil {
    30  		return nil, fmt.Errorf("PluginCommand %v %v: %v",
    31  			decredplugin.ID, decredplugin.CmdGetComments, err)
    32  	}
    33  
    34  	// Receive plugin command reply
    35  	gcr, err := decredplugin.DecodeGetCommentsReply([]byte(reply))
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	return gcr.Comments, nil
    41  }
    42  
    43  func (p *Politeiawww) decredBestBlock(ctx context.Context) (uint32, error) {
    44  	// Setup plugin command
    45  	payload, err := decredplugin.EncodeBestBlock(decredplugin.BestBlock{})
    46  	if err != nil {
    47  		return 0, err
    48  	}
    49  
    50  	// Execute plugin command
    51  	reply, err := p.politeiad.PluginCommand(ctx, decredplugin.ID,
    52  		decredplugin.CmdBestBlock, string(payload))
    53  	if err != nil {
    54  		return 0, fmt.Errorf("PluginCommand %v %v: %v",
    55  			decredplugin.ID, decredplugin.CmdBestBlock, err)
    56  	}
    57  
    58  	// Receive plugin command reply
    59  	bbr, err := decredplugin.DecodeBestBlockReply([]byte(reply))
    60  	if err != nil {
    61  		return 0, err
    62  	}
    63  
    64  	return bbr.Height, nil
    65  }