aboutsummaryrefslogtreecommitdiffstats
path: root/graphql/models/lazy_identity.go
blob: 344bb5f09190f14830c3ad829dd5340621ac5b89 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package models

import (
	"fmt"
	"sync"

	"github.com/MichaelMure/git-bug/cache"
	"github.com/MichaelMure/git-bug/entity"
	"github.com/MichaelMure/git-bug/identity"
	"github.com/MichaelMure/git-bug/util/lamport"
	"github.com/MichaelMure/git-bug/util/timestamp"
)

// IdentityWrapper is an interface used by the GraphQL resolvers to handle an identity.
// Depending on the situation, an Identity can already be fully loaded in memory or not.
// This interface is used to wrap either a lazyIdentity or a loadedIdentity depending on the situation.
type IdentityWrapper interface {
	Id() entity.Id
	Name() string
	Email() (string, error)
	Login() (string, error)
	AvatarUrl() (string, error)
	Keys() ([]*identity.Key, error)
	ValidKeysAtTime(time lamport.Time) ([]*identity.Key, error)
	DisplayName() string
	IsProtected() (bool, error)
	LastModificationLamport() (lamport.Time, error)
	LastModification() (timestamp.Timestamp, error)
}

var _ IdentityWrapper = &lazyIdentity{}

type lazyIdentity struct {
	cache   *cache.RepoCache
	excerpt *cache.IdentityExcerpt

	mu sync.Mutex
	id *cache.IdentityCache
}

func NewLazyIdentity(cache *cache.RepoCache, excerpt *cache.IdentityExcerpt) *lazyIdentity {
	return &lazyIdentity{
		cache:   cache,
		excerpt: excerpt,
	}
}

func (li *lazyIdentity) load() (*cache.IdentityCache, error) {
	if li.id != nil {
		return li.id, nil
	}

	li.mu.Lock()
	defer li.mu.Unlock()

	id, err := li.cache.ResolveIdentity(li.excerpt.Id)
	if err != nil {
		return nil, fmt.Errorf("cache: missing identity %v", li.excerpt.Id)
	}
	li.id = id
	return id, nil
}

func (li *lazyIdentity) Id() entity.Id {
	return li.excerpt.Id
}

func (li *lazyIdentity) Name() string {
	return li.excerpt.Name
}

func (li *lazyIdentity) Email() (string, error) {
	id, err := li.load()
	if err != nil {
		return "", err
	}
	return id.Email(), nil
}

func (li *lazyIdentity) Login() (string, error) {
	id, err := li.load()
	if err != nil {
		return "", err
	}
	return id.Login(), nil
}

func (li *lazyIdentity) AvatarUrl() (string, error) {
	id, err := li.load()
	if err != nil {
		return "", err
	}
	return id.AvatarUrl(), nil
}

func (li *lazyIdentity) Keys() ([]*identity.Key, error) {
	id, err := li.load()
	if err != nil {
		return nil, err
	}
	return id.Keys(), nil
}

func (li *lazyIdentity) ValidKeysAtTime(time lamport.Time) ([]*identity.Key, error) {
	id, err := li.load()
	if err != nil {
		return nil, err
	}
	return id.ValidKeysAtTime(time), nil
}

func (li *lazyIdentity) DisplayName() string {
	return li.excerpt.DisplayName()
}

func (li *lazyIdentity) IsProtected() (bool, error) {
	id, err := li.load()
	if err != nil {
		return false, err
	}
	return id.IsProtected(), nil
}

func (li *lazyIdentity) LastModificationLamport() (lamport.Time, error) {
	id, err := li.load()
	if err != nil {
		return 0, err
	}
	return id.LastModificationLamport(), nil
}

func (li *lazyIdentity) LastModification() (timestamp.Timestamp, error) {
	id, err := li.load()
	if err != nil {
		return 0, err
	}
	return id.LastModification(), nil
}

var _ IdentityWrapper = &loadedIdentity{}

type loadedIdentity struct {
	identity.Interface
}

func NewLoadedIdentity(id identity.Interface) *loadedIdentity {
	return &loadedIdentity{Interface: id}
}

func (l loadedIdentity) Email() (string, error) {
	return l.Interface.Email(), nil
}

func (l loadedIdentity) Login() (string, error) {
	return l.Interface.Login(), nil
}

func (l loadedIdentity) AvatarUrl() (string, error) {
	return l.Interface.AvatarUrl(), nil
}

func (l loadedIdentity) Keys() ([]*identity.Key, error) {
	return l.Interface.Keys(), nil
}

func (l loadedIdentity) ValidKeysAtTime(time lamport.Time) ([]*identity.Key, error) {
	return l.Interface.ValidKeysAtTime(time), nil
}

func (l loadedIdentity) IsProtected() (bool, error) {
	return l.Interface.IsProtected(), nil
}

func (l loadedIdentity) LastModificationLamport() (lamport.Time, error) {
	return l.Interface.LastModificationLamport(), nil
}

func (l loadedIdentity) LastModification() (timestamp.Timestamp, error) {
	return l.Interface.LastModification(), nil
}