snapshot.go

  1package bug
  2
  3import (
  4	"fmt"
  5	"time"
  6
  7	"github.com/MichaelMure/git-bug/entities/common"
  8	"github.com/MichaelMure/git-bug/entities/identity"
  9	"github.com/MichaelMure/git-bug/entity"
 10	"github.com/MichaelMure/git-bug/entity/dag"
 11)
 12
 13var _ dag.Snapshot = &Snapshot{}
 14
 15// Snapshot is a compiled form of the Bug data structure used for storage and merge
 16type Snapshot struct {
 17	id entity.Id
 18
 19	Status       common.Status
 20	Title        string
 21	Comments     []Comment
 22	Labels       []Label
 23	Author       identity.Interface
 24	Actors       []identity.Interface
 25	Participants []identity.Interface
 26	CreateTime   time.Time
 27
 28	Timeline []TimelineItem
 29
 30	Operations []dag.Operation
 31}
 32
 33// Id returns the Bug identifier
 34func (snap *Snapshot) Id() entity.Id {
 35	if snap.id == "" {
 36		// simply panic as it would be a coding error (no id provided at construction)
 37		panic("no id")
 38	}
 39	return snap.id
 40}
 41
 42func (snap *Snapshot) AllOperations() []dag.Operation {
 43	return snap.Operations
 44}
 45
 46// EditTime returns the last time a bug was modified
 47func (snap *Snapshot) EditTime() time.Time {
 48	if len(snap.Operations) == 0 {
 49		return time.Unix(0, 0)
 50	}
 51
 52	return snap.Operations[len(snap.Operations)-1].Time()
 53}
 54
 55// GetCreateMetadata return the creation metadata
 56func (snap *Snapshot) GetCreateMetadata(key string) (string, bool) {
 57	return snap.Operations[0].GetMetadata(key)
 58}
 59
 60// SearchTimelineItem will search in the timeline for an item matching the given hash
 61func (snap *Snapshot) SearchTimelineItem(id entity.Id) (TimelineItem, error) {
 62	for i := range snap.Timeline {
 63		if snap.Timeline[i].Id() == id {
 64			return snap.Timeline[i], nil
 65		}
 66	}
 67
 68	return nil, fmt.Errorf("timeline item not found")
 69}
 70
 71// SearchComment will search for a comment matching the given hash
 72func (snap *Snapshot) SearchComment(id entity.Id) (*Comment, error) {
 73	for _, c := range snap.Comments {
 74		if c.id == id {
 75			return &c, nil
 76		}
 77	}
 78
 79	return nil, fmt.Errorf("comment item not found")
 80}
 81
 82// append the operation author to the actors list
 83func (snap *Snapshot) addActor(actor identity.Interface) {
 84	for _, a := range snap.Actors {
 85		if actor.Id() == a.Id() {
 86			return
 87		}
 88	}
 89
 90	snap.Actors = append(snap.Actors, actor)
 91}
 92
 93// append the operation author to the participants list
 94func (snap *Snapshot) addParticipant(participant identity.Interface) {
 95	for _, p := range snap.Participants {
 96		if participant.Id() == p.Id() {
 97			return
 98		}
 99	}
100
101	snap.Participants = append(snap.Participants, participant)
102}
103
104// HasParticipant return true if the id is a participant
105func (snap *Snapshot) HasParticipant(id entity.Id) bool {
106	for _, p := range snap.Participants {
107		if p.Id() == id {
108			return true
109		}
110	}
111	return false
112}
113
114// HasAnyParticipant return true if one of the ids is a participant
115func (snap *Snapshot) HasAnyParticipant(ids ...entity.Id) bool {
116	for _, id := range ids {
117		if snap.HasParticipant(id) {
118			return true
119		}
120	}
121	return false
122}
123
124// HasActor return true if the id is a actor
125func (snap *Snapshot) HasActor(id entity.Id) bool {
126	for _, p := range snap.Actors {
127		if p.Id() == id {
128			return true
129		}
130	}
131	return false
132}
133
134// HasAnyActor return true if one of the ids is a actor
135func (snap *Snapshot) HasAnyActor(ids ...entity.Id) bool {
136	for _, id := range ids {
137		if snap.HasActor(id) {
138			return true
139		}
140	}
141	return false
142}
143
144// IsAuthored is a sign post method for gqlgen
145func (snap *Snapshot) IsAuthored() {}