aboutsummaryrefslogtreecommitdiffstats
path: root/api/graphql/models/lazy_bug.go
blob: 7570b4eaa783698862270b750544d51990e792cd (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package models

import (
	"sync"
	"time"

	"github.com/git-bug/git-bug/cache"
	"github.com/git-bug/git-bug/entities/bug"
	"github.com/git-bug/git-bug/entities/common"
	"github.com/git-bug/git-bug/entity"
	"github.com/git-bug/git-bug/entity/dag"
)

// BugWrapper is an interface used by the GraphQL resolvers to handle a bug.
// Depending on the situation, a Bug can already be fully loaded in memory or not.
// This interface is used to wrap either a lazyBug or a loadedBug depending on the situation.
type BugWrapper interface {
	Id() entity.Id
	LastEdit() time.Time
	Status() common.Status
	Title() string
	Comments() ([]bug.Comment, error)
	Labels() []common.Label
	Author() (IdentityWrapper, error)
	Actors() ([]IdentityWrapper, error)
	Participants() ([]IdentityWrapper, error)
	CreatedAt() time.Time
	Timeline() ([]bug.TimelineItem, error)
	Operations() ([]dag.Operation, error)

	IsAuthored()
}

var _ BugWrapper = &lazyBug{}

// lazyBug is a lazy-loading wrapper that fetch data from the cache (BugExcerpt) in priority,
// and load the complete bug and snapshot only when necessary.
type lazyBug struct {
	cache   *cache.RepoCache
	excerpt *cache.BugExcerpt

	mu   sync.Mutex
	snap *bug.Snapshot
}

func NewLazyBug(cache *cache.RepoCache, excerpt *cache.BugExcerpt) *lazyBug {
	return &lazyBug{
		cache:   cache,
		excerpt: excerpt,
	}
}

func (lb *lazyBug) load() error {
	lb.mu.Lock()
	defer lb.mu.Unlock()

	if lb.snap != nil {
		return nil
	}

	b, err := lb.cache.Bugs().Resolve(lb.excerpt.Id())
	if err != nil {
		return err
	}

	lb.snap = b.Snapshot()
	return nil
}

func (lb *lazyBug) identity(id entity.Id) (IdentityWrapper, error) {
	i, err := lb.cache.Identities().ResolveExcerpt(id)
	if err != nil {
		return nil, err
	}
	return &lazyIdentity{cache: lb.cache, excerpt: i}, nil
}

// Sign post method for gqlgen
func (lb *lazyBug) IsAuthored() {}

func (lb *lazyBug) Id() entity.Id {
	return lb.excerpt.Id()
}

func (lb *lazyBug) LastEdit() time.Time {
	return lb.excerpt.EditTime()
}

func (lb *lazyBug) Status() common.Status {
	return lb.excerpt.Status
}

func (lb *lazyBug) Title() string {
	return lb.excerpt.Title
}

func (lb *lazyBug) Comments() ([]bug.Comment, error) {
	err := lb.load()
	if err != nil {
		return nil, err
	}
	return lb.snap.Comments, nil
}

func (lb *lazyBug) Labels() []common.Label {
	return lb.excerpt.Labels
}

func (lb *lazyBug) Author() (IdentityWrapper, error) {
	return lb.identity(lb.excerpt.AuthorId)
}

func (lb *lazyBug) Actors() ([]IdentityWrapper, error) {
	result := make([]IdentityWrapper, len(lb.excerpt.Actors))
	for i, actorId := range lb.excerpt.Actors {
		actor, err := lb.identity(actorId)
		if err != nil {
			return nil, err
		}
		result[i] = actor
	}
	return result, nil
}

func (lb *lazyBug) Participants() ([]IdentityWrapper, error) {
	result := make([]IdentityWrapper, len(lb.excerpt.Participants))
	for i, participantId := range lb.excerpt.Participants {
		participant, err := lb.identity(participantId)
		if err != nil {
			return nil, err
		}
		result[i] = participant
	}
	return result, nil
}

func (lb *lazyBug) CreatedAt() time.Time {
	return lb.excerpt.CreateTime()
}

func (lb *lazyBug) Timeline() ([]bug.TimelineItem, error) {
	err := lb.load()
	if err != nil {
		return nil, err
	}
	return lb.snap.Timeline, nil
}

func (lb *lazyBug) Operations() ([]dag.Operation, error) {
	err := lb.load()
	if err != nil {
		return nil, err
	}
	return lb.snap.Operations, nil
}

var _ BugWrapper = &loadedBug{}

type loadedBug struct {
	*bug.Snapshot
}

func NewLoadedBug(snap *bug.Snapshot) *loadedBug {
	return &loadedBug{Snapshot: snap}
}

func (l *loadedBug) LastEdit() time.Time {
	return l.Snapshot.EditTime()
}

func (l *loadedBug) Status() common.Status {
	return l.Snapshot.Status
}

func (l *loadedBug) Title() string {
	return l.Snapshot.Title
}

func (l *loadedBug) Comments() ([]bug.Comment, error) {
	return l.Snapshot.Comments, nil
}

func (l *loadedBug) Labels() []common.Label {
	return l.Snapshot.Labels
}

func (l *loadedBug) Author() (IdentityWrapper, error) {
	return NewLoadedIdentity(l.Snapshot.Author), nil
}

func (l *loadedBug) Actors() ([]IdentityWrapper, error) {
	res := make([]IdentityWrapper, len(l.Snapshot.Actors))
	for i, actor := range l.Snapshot.Actors {
		res[i] = NewLoadedIdentity(actor)
	}
	return res, nil
}

func (l *loadedBug) Participants() ([]IdentityWrapper, error) {
	res := make([]IdentityWrapper, len(l.Snapshot.Participants))
	for i, participant := range l.Snapshot.Participants {
		res[i] = NewLoadedIdentity(participant)
	}
	return res, nil
}

func (l *loadedBug) CreatedAt() time.Time {
	return l.Snapshot.CreateTime
}

func (l *loadedBug) Timeline() ([]bug.TimelineItem, error) {
	return l.Snapshot.Timeline, nil
}

func (l *loadedBug) Operations() ([]dag.Operation, error) {
	return l.Snapshot.Operations, nil
}