1// Copyright 2019 Keybase Inc. All rights reserved.
2// Use of this source code is governed by a BSD
3// license that can be found in the LICENSE file.
4
5package favorites
6
7import (
8	"strings"
9
10	"github.com/keybase/client/go/kbfs/tlf"
11	"github.com/keybase/client/go/protocol/keybase1"
12)
13
14// Folder is a top-level favorited folder name.
15type Folder struct {
16	Name string
17	Type tlf.Type
18}
19
20// NewFolderFromProtocol creates a Folder from a
21// keybase1.Folder.
22func NewFolderFromProtocol(folder keybase1.Folder) *Folder {
23	name := folder.Name
24	if !folder.Private {
25		// Old versions of the client still use an outdated "#public"
26		// suffix for favorited public folders. TODO: remove this once
27		// those old versions of the client are retired.
28		const oldPublicSuffix = tlf.ReaderSep + "public"
29		name = strings.TrimSuffix(folder.Name, oldPublicSuffix)
30	}
31
32	var t tlf.Type
33	if folder.FolderType == keybase1.FolderType_UNKNOWN {
34		// Use deprecated boolean
35		if folder.Private {
36			t = tlf.Private
37		} else {
38			t = tlf.Public
39		}
40	} else {
41		switch folder.FolderType {
42		case keybase1.FolderType_PRIVATE:
43			t = tlf.Private
44		case keybase1.FolderType_PUBLIC:
45			t = tlf.Public
46		case keybase1.FolderType_TEAM:
47			// TODO: if we ever support something other than single
48			// teams in the favorites list, we'll have to figure out
49			// which type the favorite is from its name.
50			t = tlf.SingleTeam
51		default:
52			// This shouldn't happen, but just in case the service
53			// sends us bad info....
54			t = tlf.Private
55		}
56	}
57
58	return &Folder{
59		Name: name,
60		Type: t,
61	}
62}
63
64// ToKBFolderHandle creates a keybase1.FolderHandle from a Folder.
65func (f Folder) ToKBFolderHandle(created bool) keybase1.FolderHandle {
66	return keybase1.FolderHandle{
67		Name:       f.Name,
68		FolderType: f.Type.FolderType(),
69		Created:    created,
70	}
71}
72
73// Data represents the auxiliary data belonging to a KBFS favorite.
74type Data struct {
75	Name         string
76	FolderType   keybase1.FolderType
77	Private      bool
78	TeamID       *keybase1.TeamID
79	ResetMembers []keybase1.User
80	// Mtime is the TLF mtime (i.e. not favorite change time) stored in the
81	// core db. It's based on notifications from the mdserver.
82	TlfMtime *keybase1.Time
83}
84
85// DataFrom returns auxiliary data from a folder sent via the
86// keybase1 protocol.
87func DataFrom(folder keybase1.Folder) Data {
88	return Data{
89		Name:         folder.Name,
90		FolderType:   folder.FolderType,
91		Private:      folder.Private,
92		TeamID:       folder.TeamID,
93		ResetMembers: folder.ResetMembers,
94		TlfMtime:     folder.Mtime,
95	}
96}
97
98// ToAdd contains the data needed to add a new favorite to the
99// favorites list.
100type ToAdd struct {
101	Folder Folder
102	Data   Data
103
104	// Created, if set to true, indicates that this is the first time
105	// the TLF has ever existed. It is only used when adding the TLF
106	// to favorites
107	Created bool
108}
109
110// ToKBFolderHandle converts this data into an object suitable for the
111// keybase1 protocol.
112func (ta ToAdd) ToKBFolderHandle() keybase1.FolderHandle {
113	return ta.Folder.ToKBFolderHandle(ta.Created)
114}
115