github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/service/user_report.go (about)

     1  // Copyright 2019 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package service
     5  
     6  import (
     7  	"encoding/json"
     8  	"fmt"
     9  
    10  	"github.com/keybase/client/go/chat"
    11  	"github.com/keybase/client/go/chat/types"
    12  	"github.com/keybase/client/go/kbun"
    13  
    14  	"github.com/keybase/client/go/libkb"
    15  	"github.com/keybase/client/go/protocol/chat1"
    16  	keybase1 "github.com/keybase/client/go/protocol/keybase1"
    17  	"golang.org/x/net/context"
    18  )
    19  
    20  // pullTranscript uses chat transcript functions to pull transcript and encode
    21  // it to postArgs.
    22  func pullTranscript(mctx libkb.MetaContext, postArgs libkb.HTTPArgs, convSource types.ConversationSource,
    23  	convID chat1.ConvIDStr, usernames []kbun.NormalizedUsername) (err error) {
    24  
    25  	config := chat.PullTranscriptConfigDefault()
    26  	transcript, err := chat.PullTranscript(mctx, convSource, convID, usernames, config)
    27  	if err != nil {
    28  		return err
    29  	}
    30  	transcriptStr, err := json.Marshal(transcript)
    31  	if err != nil {
    32  		return err
    33  	}
    34  	postArgs["transcript"] = libkb.S{Val: string(transcriptStr)}
    35  	mctx.Debug("Got transcript for %s, %d messages, JSON size: %d", convID,
    36  		len(transcript.Messages), len(transcriptStr))
    37  	return nil
    38  }
    39  
    40  func (h *UserHandler) ReportUser(ctx context.Context, arg keybase1.ReportUserArg) (err error) {
    41  	mctx := libkb.NewMetaContext(ctx, h.G()).WithLogTag("REPORT")
    42  	defer mctx.Trace(fmt.Sprintf(
    43  		"UserHandler#ReportUser(username=%q,transcript=%t,convId=%v)",
    44  		arg.Username, arg.IncludeTranscript, arg.ConvID),
    45  		&err)()
    46  
    47  	postArgs := libkb.HTTPArgs{
    48  		"username": libkb.S{Val: arg.Username},
    49  		"reason":   libkb.S{Val: arg.Reason},
    50  		"comment":  libkb.S{Val: arg.Comment},
    51  	}
    52  	if arg.ConvID != nil {
    53  		postArgs["conv_id"] = libkb.S{Val: *arg.ConvID}
    54  	}
    55  	if arg.IncludeTranscript && arg.ConvID != nil {
    56  		convID := *arg.ConvID
    57  		// Pull transcripts with messages from curent user and the reported user.
    58  		usernames := []kbun.NormalizedUsername{
    59  			kbun.NewNormalizedUsername(arg.Username),
    60  			mctx.CurrentUsername(),
    61  		}
    62  		err = pullTranscript(mctx, postArgs, h.ChatG().ConvSource, chat1.ConvIDStr(convID), usernames)
    63  		if err != nil {
    64  			// This is not a failure of entire RPC, just warn about the error.
    65  			// Report can still go through without the transcript.
    66  			mctx.Warning("Could not load conversation transcript: %s", err)
    67  		}
    68  	}
    69  
    70  	apiArg := libkb.APIArg{
    71  		Endpoint:    "report/conversation",
    72  		SessionType: libkb.APISessionTypeREQUIRED,
    73  		Args:        postArgs,
    74  	}
    75  	_, err = mctx.G().API.Post(mctx, apiArg)
    76  	return err
    77  }