89 lines
2.3 KiB
Go
89 lines
2.3 KiB
Go
package utils
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"git.nutfactory.org/hoernschen/ActivityPub/config"
|
|
)
|
|
|
|
type ErrorResponse struct {
|
|
ErrorCode string `json:"errcode,omitempty"`
|
|
ErrorMessage string `json:"error,omitempty"`
|
|
RetryTime int `json:"retry_after_ms,omitempty"`
|
|
}
|
|
|
|
func CheckRequest(r *http.Request) (err error) {
|
|
if !strings.Contains(r.Header.Get("Content-Type"), "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"") {
|
|
err = errors.New("Content Type not JSON")
|
|
}
|
|
return
|
|
}
|
|
|
|
func GetContentTypeString() string {
|
|
return "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
|
|
}
|
|
|
|
func GetDefaultContext() string {
|
|
return "https://www.w3.org/ns/activitystreams"
|
|
}
|
|
|
|
func GenerateProfileUrl(userId string) string {
|
|
return fmt.Sprintf("%s://%s/%s/", config.HttpString, config.Homeserver, userId)
|
|
}
|
|
|
|
func GenerateInboxUrl(userId string) string {
|
|
return fmt.Sprintf("%s://%s/%s/inbox/", config.HttpString, config.Homeserver, userId)
|
|
}
|
|
|
|
func GenerateOutboxUrl(userId string) string {
|
|
return fmt.Sprintf("%s://%s/%s/outbox/", config.HttpString, config.Homeserver, userId)
|
|
}
|
|
|
|
func GenerateFollowersUrl(userId string) string {
|
|
return fmt.Sprintf("%s://%s/%s/followers/", config.HttpString, config.Homeserver, userId)
|
|
}
|
|
|
|
func GenerateFollowingUrl(userId string) string {
|
|
return fmt.Sprintf("%s://%s/%s/following/", config.HttpString, config.Homeserver, userId)
|
|
}
|
|
|
|
func GenerateLikedUrl(userId string) string {
|
|
return fmt.Sprintf("%s://%s/%s/liked/", config.HttpString, config.Homeserver, userId)
|
|
}
|
|
|
|
func GetAccessToken(r *http.Request) (token string, err error) {
|
|
token = r.URL.Query().Get("access_token")
|
|
if token == "" {
|
|
token = r.Header.Get("Authorization")
|
|
if token == "" || !strings.Contains(token, "Bearer") {
|
|
err = errors.New("Missing Token")
|
|
} else {
|
|
token = strings.Split(token, " ")[1]
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func HandleHTTPError(res *http.Response) (err error) {
|
|
buf := new(bytes.Buffer)
|
|
buf.ReadFrom(res.Body)
|
|
err = errors.New(fmt.Sprintf("%s (%s)", buf.String(), res.Status))
|
|
return
|
|
}
|
|
|
|
func RemoveDuplicates(array []string) []string {
|
|
keys := make(map[string]bool)
|
|
list := []string{}
|
|
|
|
for _, entry := range array {
|
|
if _, value := keys[entry]; !value {
|
|
keys[entry] = true
|
|
list = append(list, entry)
|
|
}
|
|
}
|
|
return list
|
|
}
|