github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/teams/chatmessaging.go (about)

     1  package teams
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/keybase/client/go/chat/globals"
     8  	"github.com/keybase/client/go/libkb"
     9  	"github.com/keybase/client/go/protocol/chat1"
    10  	"github.com/keybase/client/go/protocol/keybase1"
    11  )
    12  
    13  func SendTeamChatWelcomeMessage(ctx context.Context, g *libkb.GlobalContext, teamID keybase1.TeamID, team,
    14  	user string, membersType chat1.ConversationMembersType, role keybase1.TeamRole) (err error) {
    15  	if team == "" && !teamID.IsNil() {
    16  		teamname, err := ResolveIDToName(ctx, g, teamID)
    17  		if err != nil {
    18  			return fmt.Errorf("getting team name: %v", err)
    19  		}
    20  		team = teamname.String()
    21  	}
    22  	username := g.Env.GetUsername()
    23  	subBody := chat1.NewMessageSystemWithAddedtoteam(chat1.MessageSystemAddedToTeam{
    24  		Adder: username.String(),
    25  		Addee: user,
    26  		Role:  role,
    27  		Team:  team,
    28  	})
    29  	body := chat1.NewMessageBodyWithSystem(subBody)
    30  
    31  	// Ensure we have chat available, since TeamAddMember may also be
    32  	// coming from a standalone launch.
    33  	g.StartStandaloneChat()
    34  	var topicName *string
    35  	if membersType == chat1.ConversationMembersType_TEAM {
    36  		topicName = &globals.DefaultTeamTopic
    37  	}
    38  
    39  	_, err = g.ChatHelper.SendMsgByNameNonblock(ctx, team,
    40  		topicName, membersType, keybase1.TLFIdentifyBehavior_CHAT_CLI, body,
    41  		chat1.MessageType_SYSTEM, nil)
    42  	return err
    43  }
    44  
    45  func SendChatInviteWelcomeMessage(ctx context.Context, g *libkb.GlobalContext, team string,
    46  	category keybase1.TeamInviteCategory, inviter, invitee keybase1.UID, role keybase1.TeamRole) (res bool) {
    47  
    48  	if !g.Env.SendSystemChatMessages() {
    49  		g.Log.CDebugf(ctx, "Skipping SentChatInviteWelcomeMessage via environment flag")
    50  		return false
    51  	}
    52  
    53  	inviterName, err := g.GetUPAKLoader().LookupUsername(ctx, inviter)
    54  	if err != nil {
    55  		g.Log.CDebugf(ctx, "sendChatInviteWelcomeMessage: failed to lookup inviter username: %s", err)
    56  		return false
    57  	}
    58  	inviteeName, err := g.GetUPAKLoader().LookupUsername(ctx, invitee)
    59  	if err != nil {
    60  		g.Log.CDebugf(ctx, "sendChatInviteWelcomeMessage: failed to lookup invitee username: %s", err)
    61  		return false
    62  	}
    63  
    64  	username := g.Env.GetUsername()
    65  	subBody := chat1.NewMessageSystemWithInviteaddedtoteam(chat1.MessageSystemInviteAddedToTeam{
    66  		Team:       team,
    67  		Inviter:    inviterName.String(),
    68  		Invitee:    inviteeName.String(),
    69  		Adder:      username.String(),
    70  		Role:       role,
    71  		InviteType: category,
    72  	})
    73  	body := chat1.NewMessageBodyWithSystem(subBody)
    74  
    75  	if _, err = g.ChatHelper.SendMsgByNameNonblock(ctx, team,
    76  		&globals.DefaultTeamTopic,
    77  		chat1.ConversationMembersType_TEAM, keybase1.TLFIdentifyBehavior_CHAT_CLI, body,
    78  		chat1.MessageType_SYSTEM, nil); err != nil {
    79  		g.Log.CDebugf(ctx, "sendChatInviteWelcomeMessage: failed to send message: %s", err)
    80  		return false
    81  	}
    82  	return true
    83  }
    84  
    85  func SendChatSBSResolutionMessage(ctx context.Context, g *libkb.GlobalContext,
    86  	team, assertionUser, assertionService string,
    87  	prover keybase1.UID) (res bool) {
    88  
    89  	if !g.Env.SendSystemChatMessages() {
    90  		g.Log.CDebugf(ctx, "Skipping SentChatInviteWelcomeMessage via environment flag")
    91  		return false
    92  	}
    93  
    94  	proverName, err := g.GetUPAKLoader().LookupUsername(ctx, prover)
    95  	if err != nil {
    96  		g.Log.CDebugf(ctx, "SendChatSBSResolutionMessage: failed to lookup invitee username: %s", err)
    97  		return false
    98  	}
    99  
   100  	subBody := chat1.NewMessageSystemWithSbsresolve(chat1.
   101  		MessageSystemSbsResolve{
   102  		Prover:            proverName.String(),
   103  		AssertionUsername: assertionUser,
   104  		AssertionService:  assertionService,
   105  	})
   106  	body := chat1.NewMessageBodyWithSystem(subBody)
   107  
   108  	g.StartStandaloneChat()
   109  
   110  	if _, err = g.ChatHelper.SendMsgByNameNonblock(ctx, team, nil,
   111  		chat1.ConversationMembersType_IMPTEAMNATIVE,
   112  		keybase1.TLFIdentifyBehavior_CHAT_CLI, body,
   113  		chat1.MessageType_SYSTEM, nil); err != nil {
   114  		g.Log.CDebugf(ctx, "SendChatSBSResolutionMessage: failed to send message: %s", err)
   115  		return false
   116  	}
   117  	return true
   118  }
   119  
   120  func SendTeamChatCreateMessage(ctx context.Context, g *libkb.GlobalContext, team, creator string) bool {
   121  	var err error
   122  
   123  	if !g.Env.SendSystemChatMessages() {
   124  		g.Log.CDebugf(ctx, "Skipping SendTeamChatCreateMessage via environment flag")
   125  		return false
   126  	}
   127  
   128  	defer func() {
   129  		if err != nil {
   130  			g.Log.CWarningf(ctx, "failed to send team create message: %s", err.Error())
   131  		}
   132  	}()
   133  
   134  	subBody := chat1.NewMessageSystemWithCreateteam(chat1.MessageSystemCreateTeam{
   135  		Team:    team,
   136  		Creator: creator,
   137  	})
   138  	body := chat1.NewMessageBodyWithSystem(subBody)
   139  
   140  	// Ensure we have chat available
   141  	g.StartStandaloneChat()
   142  
   143  	if _, err = g.ChatHelper.SendMsgByNameNonblock(ctx, team,
   144  		&globals.DefaultTeamTopic,
   145  		chat1.ConversationMembersType_TEAM, keybase1.TLFIdentifyBehavior_CHAT_CLI, body,
   146  		chat1.MessageType_SYSTEM, nil); err != nil {
   147  		return false
   148  	}
   149  
   150  	return true
   151  }
   152  
   153  func SendTeamChatChangeAvatar(mctx libkb.MetaContext, team, username string) bool {
   154  	var err error
   155  
   156  	if !mctx.G().Env.SendSystemChatMessages() {
   157  		mctx.Debug("Skipping SendTeamChatChangeAvatar via environment flag")
   158  		return false
   159  	}
   160  
   161  	defer func() {
   162  		if err != nil {
   163  			mctx.Warning("failed to send team change avatar message: %s", err.Error())
   164  		}
   165  	}()
   166  
   167  	subBody := chat1.NewMessageSystemWithChangeavatar(chat1.MessageSystemChangeAvatar{
   168  		Team: team,
   169  		User: username,
   170  	})
   171  	body := chat1.NewMessageBodyWithSystem(subBody)
   172  
   173  	// Ensure we have chat available
   174  	mctx.G().StartStandaloneChat()
   175  
   176  	if _, err = mctx.G().ChatHelper.SendMsgByNameNonblock(mctx.Ctx(), team,
   177  		&globals.DefaultTeamTopic,
   178  		chat1.ConversationMembersType_TEAM, keybase1.TLFIdentifyBehavior_CHAT_CLI, body,
   179  		chat1.MessageType_SYSTEM, nil); err != nil {
   180  		return false
   181  	}
   182  
   183  	return true
   184  }