josie / simplegit

package web

import (
	"database/sql"

	"git.josie-c.com/josie/simplegit/internal/db"
)

// commentRow is the projection of an issue or PR comment row onto the
// shared comment machinery; each store type gets a one-line ToRow.
type commentRow struct {
	ID          int64
	Body        string
	Pending     bool
	AuthorID    sql.NullInt64
	AuthorName  string
	AuthorEmail string
	CreatedAt   int64
}

func issueCommentRow(c db.IssueComment) commentRow {
	return commentRow{c.ID, c.Body, c.Pending, c.AuthorID, c.AuthorName, c.AuthorEmail, c.CreatedAt}
}

func pullCommentRow(c db.PullComment) commentRow {
	return commentRow{c.ID, c.Body, c.Pending, c.AuthorID, c.AuthorName, c.AuthorEmail, c.CreatedAt}
}

// commentViews renders comment rows into views. ownerID marks the owner
// badge; ownerView decides whether pending rows are shown at all.
func commentViews[T any](items []T, ownerID int64, ownerView bool, base string, row func(T) commentRow) []commentView {
	var views []commentView
	for _, item := range items {
		c := row(item)
		if c.Pending && !ownerView {
			continue
		}
		views = append(views, commentView{
			ID:            c.ID,
			BodyHTML:      markdownHTML(c.Body),
			Pending:       c.Pending,
			Author:        guestAuthorName(c.AuthorName),
			AuthorIsOwner: c.AuthorID.Valid && c.AuthorID.Int64 == ownerID,
			AuthorEmail:   c.AuthorEmail,
			Created:       issuedAt(c.CreatedAt),
			IsOwner:       ownerView,
			Base:          base,
		})
	}
	return views
}