aboutsummaryrefslogblamecommitdiffstats
path: root/bug/timeline.go
blob: 247c2927805ab91e9415ab0347ac87a76d4b7e75 (plain) (tree)
1
2
3
4
5
6
7
8
9

           
        

                 
                                                 
                                                 
                                                       
 

                             

                                               

 
                                                                  
                                

                                                                                   
                                 
                          
                       
                                    

 
                                                                                     
                                 
                        
                                    

                            

                                     
                                      

 
                                                                             
                                   
                              









                                                           



                  

                                           

 





                                                                              
                                         



                                           
 


                                             
 




                                                                            
package bug

import (
	"strings"

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

type TimelineItem interface {
	// ID return the identifier of the item
	ID() string
}

// CommentHistoryStep hold one version of a message in the history
type CommentHistoryStep struct {
	// The author of the edition, not necessarily the same as the author of the
	// original comment
	Author identity.Interface
	// The new message
	Message  string
	UnixTime timestamp.Timestamp
}

// CommentTimelineItem is a TimelineItem that holds a Comment and its edition history
type CommentTimelineItem struct {
	id        string
	Author    identity.Interface
	Message   string
	Files     []git.Hash
	CreatedAt timestamp.Timestamp
	LastEdit  timestamp.Timestamp
	History   []CommentHistoryStep
}

func NewCommentTimelineItem(ID string, comment Comment) CommentTimelineItem {
	return CommentTimelineItem{
		id:        ID,
		Author:    comment.Author,
		Message:   comment.Message,
		Files:     comment.Files,
		CreatedAt: comment.UnixTime,
		LastEdit:  comment.UnixTime,
		History: []CommentHistoryStep{
			{
				Message:  comment.Message,
				UnixTime: comment.UnixTime,
			},
		},
	}
}

func (c *CommentTimelineItem) ID() string {
	return c.id
}

// Append will append a new comment in the history and update the other values
func (c *CommentTimelineItem) Append(comment Comment) {
	c.Message = comment.Message
	c.Files = comment.Files
	c.LastEdit = comment.UnixTime
	c.History = append(c.History, CommentHistoryStep{
		Author:   comment.Author,
		Message:  comment.Message,
		UnixTime: comment.UnixTime,
	})
}

// Edited say if the comment was edited
func (c *CommentTimelineItem) Edited() bool {
	return len(c.History) > 1
}

// MessageIsEmpty return true is the message is empty or only made of spaces
func (c *CommentTimelineItem) MessageIsEmpty() bool {
	return len(strings.TrimSpace(c.Message)) == 0
}