...
1 package model
2
3 import (
4 "fmt"
5 "html/template"
6 "strings"
7 "time"
8
9 . "codeberg.org/tslocum/sriracha/util"
10 )
11
12 type News struct {
13 ID int
14 Account *Account
15 Timestamp int64
16 Modified int64
17 Share bool
18 Name string
19 Subject string
20 Message string
21 }
22
23 func (n *News) Validate() error {
24 switch {
25 case strings.TrimSpace(n.Message) == "":
26 return fmt.Errorf("a message is required")
27 case n.Timestamp < 0:
28 return fmt.Errorf("invalid news timestamp")
29 default:
30 return nil
31 }
32 }
33
34 func (n *News) MayUpdate(a *Account) bool {
35 if a == nil {
36 return false
37 }
38 return n.Share || (n.Account != nil && n.Account.ID == a.ID) || (n.Account == nil && (a.Role == RoleSuperAdmin || a.Role == RoleAdmin))
39 }
40
41 func (n *News) MayDelete(a *Account) bool {
42 if a == nil {
43 return false
44 }
45 return a.Role == RoleSuperAdmin || a.Role == RoleAdmin || n.MayUpdate(a)
46 }
47
48 func (n *News) DateInput() string {
49 if n.Timestamp == 0 {
50 return ""
51 }
52 return time.Unix(n.Timestamp, 0).Format("2006/01/02 15:04")
53 }
54
55 func (n *News) DateLabel() template.HTML {
56 switch {
57 case n.Timestamp == 0:
58 return "Draft"
59 case n.Timestamp > time.Now().Unix():
60 return "Hidden until " + FormatTimestamp(n.Timestamp)
61 default:
62 return FormatTimestamp(n.Timestamp)
63 }
64 }
65
View as plain text