github.com/decred/politeia@v1.4.0/politeiad/client/comments.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 client
     6  
     7  import (
     8  	"context"
     9  	"encoding/json"
    10  	"fmt"
    11  
    12  	pdv2 "github.com/decred/politeia/politeiad/api/v2"
    13  	"github.com/decred/politeia/politeiad/plugins/comments"
    14  )
    15  
    16  // CommentNew sends the comments plugin New command to the politeiad v2 API.
    17  func (c *Client) CommentNew(ctx context.Context, n comments.New) (*comments.Comment, error) {
    18  	// Setup request
    19  	b, err := json.Marshal(n)
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  	cmd := pdv2.PluginCmd{
    24  		Token:   n.Token,
    25  		ID:      comments.PluginID,
    26  		Command: comments.CmdNew,
    27  		Payload: string(b),
    28  	}
    29  
    30  	// Send request
    31  	reply, err := c.PluginWrite(ctx, cmd)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	// Decode reply
    37  	var nr comments.NewReply
    38  	err = json.Unmarshal([]byte(reply), &nr)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	return &nr.Comment, nil
    44  }
    45  
    46  // CommentEdit sends the comments plugin Edit command to the politeiad v2 API.
    47  func (c *Client) CommentEdit(ctx context.Context, e comments.Edit) (*comments.Comment, error) {
    48  	// Setup request
    49  	b, err := json.Marshal(e)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	cmd := pdv2.PluginCmd{
    54  		Token:   e.Token,
    55  		ID:      comments.PluginID,
    56  		Command: comments.CmdEdit,
    57  		Payload: string(b),
    58  	}
    59  
    60  	// Send request
    61  	reply, err := c.PluginWrite(ctx, cmd)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  
    66  	// Decode reply
    67  	var er comments.EditReply
    68  	err = json.Unmarshal([]byte(reply), &er)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	return &er.Comment, nil
    74  }
    75  
    76  // CommentVote sends the comments plugin Vote command to the politeiad v2 API.
    77  func (c *Client) CommentVote(ctx context.Context, v comments.Vote) (*comments.VoteReply, error) {
    78  	// Setup request
    79  	b, err := json.Marshal(v)
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  	cmd := pdv2.PluginCmd{
    84  		Token:   v.Token,
    85  		ID:      comments.PluginID,
    86  		Command: comments.CmdVote,
    87  		Payload: string(b),
    88  	}
    89  
    90  	// Send request
    91  	reply, err := c.PluginWrite(ctx, cmd)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  
    96  	// Decode reply
    97  	var vr comments.VoteReply
    98  	err = json.Unmarshal([]byte(reply), &vr)
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  
   103  	return &vr, nil
   104  }
   105  
   106  // CommentDel sends the comments plugin Del command to the politeiad v2 API.
   107  func (c *Client) CommentDel(ctx context.Context, d comments.Del) (*comments.DelReply, error) {
   108  	// Setup request
   109  	b, err := json.Marshal(d)
   110  	if err != nil {
   111  		return nil, err
   112  	}
   113  	cmd := pdv2.PluginCmd{
   114  		Token:   d.Token,
   115  		ID:      comments.PluginID,
   116  		Command: comments.CmdDel,
   117  		Payload: string(b),
   118  	}
   119  
   120  	// Send request
   121  	reply, err := c.PluginWrite(ctx, cmd)
   122  	if err != nil {
   123  		return nil, err
   124  	}
   125  
   126  	// Decode reply
   127  	var dr comments.DelReply
   128  	err = json.Unmarshal([]byte(reply), &dr)
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  
   133  	return &dr, nil
   134  }
   135  
   136  // CommentCount sends a batch of comment plugin Count commands to the
   137  // politeiad v2 API and returns a map[token]count with the results. If a
   138  // record is not found for a token or any other error occurs, that token
   139  // will not be included in the reply.
   140  func (c *Client) CommentCount(ctx context.Context, tokens []string) (map[string]uint32, error) {
   141  	// Setup request
   142  	cmds := make([]pdv2.PluginCmd, 0, len(tokens))
   143  	for _, v := range tokens {
   144  		cmds = append(cmds, pdv2.PluginCmd{
   145  			Token:   v,
   146  			ID:      comments.PluginID,
   147  			Command: comments.CmdCount,
   148  		})
   149  	}
   150  
   151  	// Send request
   152  	replies, err := c.PluginReads(ctx, cmds)
   153  	if err != nil {
   154  		return nil, err
   155  	}
   156  	if len(replies) != len(cmds) {
   157  		return nil, fmt.Errorf("replies missing")
   158  	}
   159  
   160  	// Decode replies
   161  	counts := make(map[string]uint32, len(replies))
   162  	for _, v := range replies {
   163  		// This command swallows individual errors. The token of the
   164  		// command that errored will not be included in the reply.
   165  		err = extractPluginCmdError(v)
   166  		if err != nil {
   167  			continue
   168  		}
   169  
   170  		var cr comments.CountReply
   171  		err = json.Unmarshal([]byte(v.Payload), &cr)
   172  		if err != nil {
   173  			continue
   174  		}
   175  		counts[v.Token] = cr.Count
   176  	}
   177  
   178  	return counts, nil
   179  }
   180  
   181  // CommentsGet sends the comments plugin Get command to the politeiad v2 API.
   182  func (c *Client) CommentsGet(ctx context.Context, token string, g comments.Get) (map[uint32]comments.Comment, error) {
   183  	// Setup request
   184  	b, err := json.Marshal(g)
   185  	if err != nil {
   186  		return nil, err
   187  	}
   188  	cmds := []pdv2.PluginCmd{
   189  		{
   190  			Token:   token,
   191  			ID:      comments.PluginID,
   192  			Command: comments.CmdGet,
   193  			Payload: string(b),
   194  		},
   195  	}
   196  
   197  	// Send request
   198  	replies, err := c.PluginReads(ctx, cmds)
   199  	if err != nil {
   200  		return nil, err
   201  	}
   202  	if len(replies) == 0 {
   203  		return nil, fmt.Errorf("no replies found")
   204  	}
   205  	pcr := replies[0]
   206  	err = extractPluginCmdError(pcr)
   207  	if err != nil {
   208  		return nil, err
   209  	}
   210  
   211  	// Decode reply
   212  	var gr comments.GetReply
   213  	err = json.Unmarshal([]byte(pcr.Payload), &gr)
   214  	if err != nil {
   215  		return nil, err
   216  	}
   217  
   218  	return gr.Comments, nil
   219  }
   220  
   221  // CommentsGetAll sends the comments plugin GetAll command to the politeiad v2
   222  // API.
   223  func (c *Client) CommentsGetAll(ctx context.Context, token string) ([]comments.Comment, error) {
   224  	// Setup request
   225  	cmds := []pdv2.PluginCmd{
   226  		{
   227  			Token:   token,
   228  			ID:      comments.PluginID,
   229  			Command: comments.CmdGetAll,
   230  		},
   231  	}
   232  
   233  	// Send request
   234  	replies, err := c.PluginReads(ctx, cmds)
   235  	if err != nil {
   236  		return nil, err
   237  	}
   238  	if len(replies) == 0 {
   239  		return nil, fmt.Errorf("no replies found")
   240  	}
   241  	pcr := replies[0]
   242  	err = extractPluginCmdError(pcr)
   243  	if err != nil {
   244  		return nil, err
   245  	}
   246  
   247  	// Decode reply
   248  	var gar comments.GetAllReply
   249  	err = json.Unmarshal([]byte(pcr.Payload), &gar)
   250  	if err != nil {
   251  		return nil, err
   252  	}
   253  
   254  	return gar.Comments, nil
   255  }
   256  
   257  // CommentVotes sends the comments plugin Votes command to the politeiad v2
   258  // API.
   259  func (c *Client) CommentVotes(ctx context.Context, token string, v comments.Votes) ([]comments.CommentVote, error) {
   260  	// Setup request
   261  	b, err := json.Marshal(v)
   262  	if err != nil {
   263  		return nil, err
   264  	}
   265  	cmds := []pdv2.PluginCmd{
   266  		{
   267  			Token:   token,
   268  			ID:      comments.PluginID,
   269  			Command: comments.CmdVotes,
   270  			Payload: string(b),
   271  		},
   272  	}
   273  
   274  	// Send request
   275  	replies, err := c.PluginReads(ctx, cmds)
   276  	if err != nil {
   277  		return nil, err
   278  	}
   279  	if len(replies) == 0 {
   280  		return nil, fmt.Errorf("no replies found")
   281  	}
   282  	pcr := replies[0]
   283  	err = extractPluginCmdError(pcr)
   284  	if err != nil {
   285  		return nil, err
   286  	}
   287  
   288  	// Decode reply
   289  	var vr comments.VotesReply
   290  	err = json.Unmarshal([]byte(pcr.Payload), &vr)
   291  	if err != nil {
   292  		return nil, err
   293  	}
   294  
   295  	return vr.Votes, nil
   296  }
   297  
   298  // CommentTimestamps sends the comments plugin Timestamps command to the
   299  // politeiad v2 API.
   300  func (c *Client) CommentTimestamps(ctx context.Context, token string, t comments.Timestamps) (*comments.TimestampsReply, error) {
   301  	// Setup request
   302  	b, err := json.Marshal(t)
   303  	if err != nil {
   304  		return nil, err
   305  	}
   306  	cmds := []pdv2.PluginCmd{
   307  		{
   308  			Token:   token,
   309  			ID:      comments.PluginID,
   310  			Command: comments.CmdTimestamps,
   311  			Payload: string(b),
   312  		},
   313  	}
   314  
   315  	// Send request
   316  	replies, err := c.PluginReads(ctx, cmds)
   317  	if err != nil {
   318  		return nil, err
   319  	}
   320  	if len(replies) == 0 {
   321  		return nil, fmt.Errorf("no replies found")
   322  	}
   323  	pcr := replies[0]
   324  	err = extractPluginCmdError(pcr)
   325  	if err != nil {
   326  		return nil, err
   327  	}
   328  
   329  	// Decode reply
   330  	var tr comments.TimestampsReply
   331  	err = json.Unmarshal([]byte(pcr.Payload), &tr)
   332  	if err != nil {
   333  		return nil, err
   334  	}
   335  
   336  	return &tr, nil
   337  }