File: notification.go

package info (click to toggle)
tea-cli 0.11.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,648 kB
  • sloc: makefile: 116; sh: 17
file content (82 lines) | stat: -rw-r--r-- 1,664 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package print

import (
	"fmt"
	"strings"

	"code.gitea.io/sdk/gitea"
)

// NotificationsList prints a listing of notification threads
func NotificationsList(news []*gitea.NotificationThread, output string, fields []string) {
	var printables = make([]printable, len(news))
	for i, x := range news {
		printables[i] = &printableNotification{x}
	}
	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
}

func (n printableNotification) FormatField(field string, machineReadable bool) string {
	switch field {
	case "id":
		return fmt.Sprintf("%d", n.ID)

	case "status":
		status := "read"
		if n.Pinned {
			status = "pinned"
		} else if n.Unread {
			status = "unread"
		}
		return status

	case "updated":
		return FormatTime(n.UpdatedAt, machineReadable)

	case "index":
		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]
			}
		}
		return index

	case "type":
		return string(n.Subject.Type)

	case "state":
		return string(n.Subject.State)

	case "title":
		return n.Subject.Title

	case "repo", "repository":
		return n.Repository.FullName
	}
	return ""
}