snapshot.go

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