2020-12-08 05:28:54 -05:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2023-09-07 21:40:02 -04:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-08 05:28:54 -05:00
|
|
|
|
|
|
|
package print
|
|
|
|
|
|
|
|
import (
|
2021-09-05 13:11:17 -04:00
|
|
|
"fmt"
|
2020-12-08 05:28:54 -05:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NotificationsList prints a listing of notification threads
|
2022-09-13 15:08:18 -04:00
|
|
|
func NotificationsList(news []*gitea.NotificationThread, output string, fields []string) {
|
|
|
|
var printables = make([]printable, len(news))
|
|
|
|
for i, x := range news {
|
|
|
|
printables[i] = &printableNotification{x}
|
2020-12-08 05:28:54 -05:00
|
|
|
}
|
2022-09-13 15:08:18 -04:00
|
|
|
t := tableFromItems(fields, printables, isMachineReadable(output))
|
|
|
|
t.print(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotificationFields are all available fields to print with NotificationsList
|
|
|
|
var NotificationFields = []string{
|
|
|
|
"id",
|
|
|
|
"status",
|
|
|
|
"updated",
|
|
|
|
|
|
|
|
// these are about the notification subject
|
|
|
|
"index",
|
|
|
|
"type",
|
|
|
|
"state",
|
|
|
|
"title",
|
|
|
|
"repository",
|
|
|
|
}
|
|
|
|
|
|
|
|
type printableNotification struct {
|
|
|
|
*gitea.NotificationThread
|
|
|
|
}
|
2020-12-08 05:28:54 -05:00
|
|
|
|
2022-09-13 15:08:18 -04:00
|
|
|
func (n printableNotification) FormatField(field string, machineReadable bool) string {
|
|
|
|
switch field {
|
|
|
|
case "id":
|
|
|
|
return fmt.Sprintf("%d", n.ID)
|
2020-12-09 17:04:36 -05:00
|
|
|
|
2022-09-13 15:08:18 -04:00
|
|
|
case "status":
|
|
|
|
status := "read"
|
|
|
|
if n.Pinned {
|
|
|
|
status = "pinned"
|
|
|
|
} else if n.Unread {
|
|
|
|
status = "unread"
|
2020-12-08 05:28:54 -05:00
|
|
|
}
|
2022-09-13 15:08:18 -04:00
|
|
|
return status
|
|
|
|
|
|
|
|
case "updated":
|
|
|
|
return FormatTime(n.UpdatedAt, machineReadable)
|
|
|
|
|
|
|
|
case "index":
|
2020-12-08 05:28:54 -05:00
|
|
|
var index string
|
|
|
|
if n.Subject.Type == "Issue" || n.Subject.Type == "Pull" {
|
|
|
|
index = n.Subject.URL
|
|
|
|
urlParts := strings.Split(n.Subject.URL, "/")
|
|
|
|
if len(urlParts) != 0 {
|
|
|
|
index = urlParts[len(urlParts)-1]
|
|
|
|
}
|
|
|
|
}
|
2022-09-13 15:08:18 -04:00
|
|
|
return index
|
2020-12-08 05:28:54 -05:00
|
|
|
|
2022-09-13 15:08:18 -04:00
|
|
|
case "type":
|
|
|
|
return string(n.Subject.Type)
|
2021-09-05 13:11:17 -04:00
|
|
|
|
2022-09-13 15:08:18 -04:00
|
|
|
case "state":
|
|
|
|
return string(n.Subject.State)
|
|
|
|
|
|
|
|
case "title":
|
|
|
|
return n.Subject.Title
|
2020-12-08 05:28:54 -05:00
|
|
|
|
2022-09-13 15:08:18 -04:00
|
|
|
case "repo", "repository":
|
|
|
|
return n.Repository.FullName
|
2020-12-08 05:28:54 -05:00
|
|
|
}
|
2022-09-13 15:08:18 -04:00
|
|
|
return ""
|
2020-12-08 05:28:54 -05:00
|
|
|
}
|