-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
41 lines (32 loc) · 1.16 KB
/
utils.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
package hannibal
import (
"net/http"
"strings"
"time"
"github.com/benpate/hannibal/vocab"
"github.com/benpate/rosetta/list"
)
// TimeFormat returns a string representation of the provided time value,
// using the format designated by the W3C spec: https://www.w3.org/TR/activitystreams-core/#dates
func TimeFormat(value time.Time) string {
return value.UTC().Format(http.TimeFormat)
}
// IsActivityPubContentType returns TRUE if the provided contentType is a valid ActivityPub content type.
// https://www.w3.org/TR/activitystreams-core/#media-type
func IsActivityPubContentType(contentType string) bool {
// If multiple content types are provided, then only check the first one.
contentType = list.First(contentType, ',')
// Strip off any parameters from the content type (like charsets and json-ld profiles)
contentType = list.First(contentType, ';')
// Remove whitespace around the actual value
contentType = strings.TrimSpace(contentType)
// If what remains matches any of these values, then Success!
switch contentType {
case vocab.ContentTypeActivityPub,
vocab.ContentTypeJSON,
vocab.ContentTypeJSONLD:
return true
}
// Failure.
return false
}