Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chat: handle suffixes and readers when adding username to TLF #16067

Merged
merged 2 commits into from Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 28 additions & 8 deletions go/chat/utils/utils.go
Expand Up @@ -1699,16 +1699,36 @@ func AddUserToTLFName(g *globals.Context, tlfName string, vis keybase1.TLFVisibi
switch membersType {
case chat1.ConversationMembersType_IMPTEAMNATIVE, chat1.ConversationMembersType_IMPTEAMUPGRADE,
chat1.ConversationMembersType_KBFS:
username := g.Env.GetUsername().String()
if vis != keybase1.TLFVisibility_PUBLIC {
if len(tlfName) == 0 {
tlfName = username
} else {
tlfName += "," + username
}
if vis == keybase1.TLFVisibility_PUBLIC {
return tlfName
}

username := g.Env.GetUsername().String()
if len(tlfName) == 0 {
return username
}

// KBFS creates TLFs with suffixes (e.g., folder names that
// conflict after an assertion has been resolved) and readers,
// so we need to handle those types of TLF names here so that
// edit history works correctly.
split1 := strings.SplitN(tlfName, " ", 2) // split off suffix
split2 := strings.Split(split1[0], "#") // split off readers
// Add the name to the writers list (assume the current user
// is a writer).
tlfName = split2[0] + "," + username
if len(split2) > 1 {
// Re-append any readers.
tlfName += "#" + split2[1]
}
if len(split1) > 1 {
// Re-append any suffix.
tlfName += " " + split1[1]
}
return tlfName
default:
return tlfName
}
return tlfName
}

func ForceReloadUPAKsForUIDs(ctx context.Context, g *globals.Context, uids []keybase1.UID) error {
Expand Down
38 changes: 38 additions & 0 deletions go/chat/utils/utils_test.go
Expand Up @@ -8,7 +8,9 @@ import (
"testing"
"time"

"github.com/keybase/client/go/chat/globals"
"github.com/keybase/client/go/chat/types"
"github.com/keybase/client/go/externalstest"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/chat1"
"github.com/keybase/client/go/protocol/gregor1"
Expand Down Expand Up @@ -237,3 +239,39 @@ func TestDecorateMentions(t *testing.T) {
require.Equal(t, c.result, res)
}
}

type configUsernamer struct {
libkb.ConfigReader
username libkb.NormalizedUsername
}

func (c configUsernamer) GetUsername() libkb.NormalizedUsername {
return c.username
}

func TestAddUserToTlfName(t *testing.T) {
tc := externalstest.SetupTest(t, "chat-utils", 0)
g := globals.NewContext(tc.G, &globals.ChatContext{})
g.Env.SetConfig(
&configUsernamer{g.Env.GetConfig(), "charlie"}, g.Env.GetConfigWriter())

priv := keybase1.TLFVisibility_PRIVATE
mem := chat1.ConversationMembersType_IMPTEAMNATIVE
s := AddUserToTLFName(g, "alice,bob", priv, mem)
require.Equal(t, "alice,bob,charlie", s)
s = AddUserToTLFName(g, "charlie", priv, mem)
require.Equal(t, "charlie,charlie", s)
s = AddUserToTLFName(
g, "alice,bob (conflicted copy 2019-02-14 #1)", priv, mem)
require.Equal(t, "alice,bob,charlie (conflicted copy 2019-02-14 #1)", s)
s = AddUserToTLFName(
g, "alice#bob", priv, mem)
require.Equal(t, "alice,charlie#bob", s)
s = AddUserToTLFName(
g, "alice#bob (conflicted copy 2019-02-14 #1)", priv, mem)
require.Equal(t, "alice,charlie#bob (conflicted copy 2019-02-14 #1)", s)

pub := keybase1.TLFVisibility_PUBLIC
s = AddUserToTLFName(g, "alice,bob", pub, mem)
require.Equal(t, "alice,bob", s)
}