-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
191 lines (151 loc) · 3.87 KB
/
message.go
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package templates
import (
"fmt"
"html"
"html/template"
"io"
"log"
"path"
"strconv"
"strings"
"time"
"github.com/diamondburned/arikawa/v2/discord"
"github.com/diamondburned/arikawa/v2/state"
"github.com/nixhub-io/nixhub-io/templates/md"
)
type Messages []Message
func (ms Messages) Render(w io.Writer) error {
return Frontpage.ExecuteTemplate(w, "messages", ms)
}
func (ms Messages) RenderWithTimezone(w io.Writer, location *time.Location) error {
cp := append(Messages(nil), ms...)
for i, msg := range cp {
cp[i].timestamp = msg.timestamp.In(location)
cp[i].Timestamp = fmtTime(msg.timestamp)
}
return Messages(cp).Render(w)
}
type Message struct {
ID discord.MessageID
Author template.HTML
Timestamp template.HTML
Content template.HTML
AvatarURL string
// If the last author is the same
Small bool
Message discord.Message
timestamp time.Time
}
var _ RendererWithTimezone = (*Message)(nil)
func (m Message) Render(w io.Writer) error {
return Frontpage.ExecuteTemplate(w, "message", m)
}
func (m Message) RenderWithTimezone(w io.Writer, location *time.Location) error {
m.timestamp = m.timestamp.In(location)
m.Timestamp = fmtTime(m.timestamp)
return m.Render(w)
}
func RenderMessages(state *state.State, dmsgs []discord.Message) Messages {
var msgs = make([]Message, len(dmsgs))
for i, dm := range dmsgs {
msgs[i] = RenderMessage(state, dm)
}
return msgs
}
func RenderMessage(state *state.State, dm discord.Message) Message {
var m = Message{
ID: dm.ID,
Message: dm,
}
// Parse everything, really
m.Content = md.Parse(state, &dm)
// Parse timestamp
m.timestamp = dm.Timestamp.Time()
m.Timestamp = fmtTime(m.timestamp)
// Parse author
m.Author = parseAuthor(state, dm.Author, dm.GuildID)
// Parse AvatarURL
m.AvatarURL = dm.Author.AvatarURL() + "?size=64"
return m
}
func fmtTime(t time.Time) template.HTML {
if t.Before(time.Now().Add(-24 * time.Hour)) {
// If the message was from yesterday
return template.HTML(t.Format("02/01/2006"))
}
return template.HTML(t.Format(time.Kitchen))
}
func parseAuthor(
state *state.State, u discord.User, guildID discord.GuildID) template.HTML {
var name = u.Username
var color = discord.Color(0xFFFFFF)
if !guildID.IsValid() {
log.Println("GuildID is empty")
return ""
}
// We should try and be conservative, so no session calls
mem, err := state.Member(guildID, u.ID)
if err == nil {
if mem.Nick != "" {
name = mem.Nick
}
c, err := state.MemberColor(guildID, u.ID)
if err == nil {
color = c
}
}
// Wrap the username around color codes
var html = fmt.Sprintf(
`<span style="color: #%x">%s</span>`,
color, html.EscapeString(name),
)
if u.Bot {
html += ` <span class="bot">BOT</span>`
}
return template.HTML(html)
}
var (
ImageFormats = []string{"jpg", "jpeg", "png", "webm", "gif"}
VideoFormats = []string{"mkv", "webm", "mp4", "ogv"}
AudioFormats = []string{"mp3", "flac", "ogg", "opus", "aac"}
)
// Helper functions for messages
var htmlFns = template.FuncMap{
"URLIsImage": func(url string) bool {
return checkURLfiletype(url, ImageFormats)
},
"URLIsVideo": func(url string) bool {
return checkURLfiletype(url, VideoFormats)
},
"URLIsAudio": func(url string) bool {
return checkURLfiletype(url, AudioFormats)
},
"hex": func(color discord.Color) string {
return strconv.FormatUint(uint64(color.Uint32()), 16)
},
"md": func(str string) template.HTML {
return md.ParseString(str)
},
"rfc3339": func(t discord.Timestamp) template.HTML {
if !t.IsValid() {
return ""
}
return fmtTime(t.Time())
},
}
func checkURLfiletype(url string, filetypes []string) bool {
ext := path.Ext(url)
if ext == "" {
return false
}
ext = strings.ToLower(ext)
for _, ft := range filetypes {
if ext == "."+ft {
return true
}
}
return false
}
func escapeHTML(s string) template.HTML {
return template.HTML(template.HTMLEscapeString(s))
}