identity_subcache.go

  1package cache
  2
  3import (
  4	"fmt"
  5
  6	"github.com/MichaelMure/git-bug/entities/identity"
  7	"github.com/MichaelMure/git-bug/entity"
  8	"github.com/MichaelMure/git-bug/repository"
  9)
 10
 11type RepoCacheIdentity struct {
 12	*SubCache[*identity.Identity, *IdentityExcerpt, *IdentityCache]
 13}
 14
 15func NewRepoCacheIdentity(repo repository.ClockedRepo,
 16	resolvers func() entity.Resolvers,
 17	getUserIdentity getUserIdentityFunc) *RepoCacheIdentity {
 18
 19	makeCached := func(i *identity.Identity, entityUpdated func(id entity.Id) error) *IdentityCache {
 20		return NewIdentityCache(i, repo, entityUpdated)
 21	}
 22
 23	makeIndex := func(i *IdentityCache) []string {
 24		// no indexing
 25		return nil
 26	}
 27
 28	// TODO: this is terribly ugly, but we are currently stuck with the fact that identities are NOT using the fancy dag framework.
 29	//   This lead to various complication here and there to handle entities generically, and avoid large code duplication.
 30	//   TL;DR: something has to give, and this is the less ugly solution I found. This "normalize" identities as just another "dag framework"
 31	//   entity. Ideally identities would be converted to the dag framework, but right now that could lead to potential attack: if an old
 32	//   private key is leaked, it would be possible to craft a legal identity update that take over the most recent version. While this is
 33	//   meaningless in the case of a normal entity, it's really an issues for identities.
 34
 35	actions := Actions[*identity.Identity]{
 36		ReadWithResolver: func(repo repository.ClockedRepo, resolvers entity.Resolvers, id entity.Id) (*identity.Identity, error) {
 37			return identity.ReadLocal(repo, id)
 38		},
 39		ReadAllWithResolver: func(repo repository.ClockedRepo, resolvers entity.Resolvers) <-chan entity.StreamedEntity[*identity.Identity] {
 40			return identity.ReadAllLocal(repo)
 41		},
 42		Remove: identity.RemoveIdentity,
 43		MergeAll: func(repo repository.ClockedRepo, resolvers entity.Resolvers, remote string, mergeAuthor identity.Interface) <-chan entity.MergeResult {
 44			return identity.MergeAll(repo, remote)
 45		},
 46	}
 47
 48	sc := NewSubCache[*identity.Identity, *IdentityExcerpt, *IdentityCache](
 49		repo, resolvers, getUserIdentity,
 50		makeCached, NewIdentityExcerpt, makeIndex, actions,
 51		"identity", "identities",
 52		formatVersion, defaultMaxLoadedBugs,
 53	)
 54
 55	return &RepoCacheIdentity{SubCache: sc}
 56}
 57
 58// ResolveIdentityImmutableMetadata retrieve an Identity that has the exact given metadata on
 59// one of its version. If multiple version have the same key, the first defined take precedence.
 60func (c *RepoCacheIdentity) ResolveIdentityImmutableMetadata(key string, value string) (*IdentityCache, error) {
 61	return c.ResolveMatcher(func(excerpt *IdentityExcerpt) bool {
 62		return excerpt.ImmutableMetadata[key] == value
 63	})
 64}
 65
 66// New create a new identity
 67// The new identity is written in the repository (commit)
 68func (c *RepoCacheIdentity) New(name string, email string) (*IdentityCache, error) {
 69	return c.NewRaw(name, email, "", "", nil, nil)
 70}
 71
 72// NewFull create a new identity
 73// The new identity is written in the repository (commit)
 74func (c *RepoCacheIdentity) NewFull(name string, email string, login string, avatarUrl string, keys []*identity.Key) (*IdentityCache, error) {
 75	return c.NewRaw(name, email, login, avatarUrl, keys, nil)
 76}
 77
 78func (c *RepoCacheIdentity) NewRaw(name string, email string, login string, avatarUrl string, keys []*identity.Key, metadata map[string]string) (*IdentityCache, error) {
 79	i, err := identity.NewIdentityFull(c.repo, name, email, login, avatarUrl, keys)
 80	if err != nil {
 81		return nil, err
 82	}
 83	return c.finishIdentity(i, metadata)
 84}
 85
 86func (c *RepoCacheIdentity) NewFromGitUser() (*IdentityCache, error) {
 87	return c.NewFromGitUserRaw(nil)
 88}
 89
 90func (c *RepoCacheIdentity) NewFromGitUserRaw(metadata map[string]string) (*IdentityCache, error) {
 91	i, err := identity.NewFromGitUser(c.repo)
 92	if err != nil {
 93		return nil, err
 94	}
 95	return c.finishIdentity(i, metadata)
 96}
 97
 98func (c *RepoCacheIdentity) finishIdentity(i *identity.Identity, metadata map[string]string) (*IdentityCache, error) {
 99	for key, value := range metadata {
100		i.SetMetadata(key, value)
101	}
102
103	err := i.Commit(c.repo)
104	if err != nil {
105		return nil, err
106	}
107
108	c.mu.Lock()
109	if _, has := c.cached[i.Id()]; has {
110		return nil, fmt.Errorf("identity %s already exist in the cache", i.Id())
111	}
112
113	cached := NewIdentityCache(i, c.repo, c.entityUpdated)
114	c.cached[i.Id()] = cached
115	c.mu.Unlock()
116
117	// force the write of the excerpt
118	err = c.entityUpdated(i.Id())
119	if err != nil {
120		return nil, err
121	}
122
123	return cached, nil
124}