chore: init

This commit is contained in:
DevMiner 2024-08-11 03:51:22 +02:00
commit 320715f3e7
174 changed files with 42083 additions and 0 deletions

52
internal/utils/mapper.go Normal file
View file

@ -0,0 +1,52 @@
package utils
import "strings"
func MapSlice[T any, V any](obj []T, transform func(T) V) []V {
vs := make([]V, 0, len(obj))
for _, o := range obj {
vs = append(vs, transform(o))
}
return vs
}
type CombinedError struct {
Errors []error
}
func (e CombinedError) Error() string {
sb := strings.Builder{}
for i, err := range e.Errors {
sb.WriteString(err.Error())
if i < len(e.Errors)-1 {
sb.WriteString("\n")
}
}
return sb.String()
}
func MapErrorSlice[T any, V any](obj []T, transform func(T) (V, error)) ([]V, error) {
vs := make([]V, 0, len(obj))
errs := make([]error, 0, len(obj))
for _, o := range obj {
v, err := transform(o)
if err != nil {
errs = append(errs, err)
continue
}
vs = append(vs, v)
}
if len(errs) > 0 {
return nil, CombinedError{Errors: errs}
}
return vs, nil
}

75
internal/utils/urls.go Normal file
View file

@ -0,0 +1,75 @@
package utils
import (
"fmt"
"net/url"
"github.com/google/uuid"
"github.com/lysand-org/versia-go/config"
"github.com/lysand-org/versia-go/pkg/lysand"
)
var dicebearURL = &url.URL{
Scheme: "https",
Host: "api.dicebear.com",
Path: "9.x/adventurer/svg",
}
func UserAPIURL(uuid uuid.UUID) *lysand.URL {
newPath := &url.URL{Path: fmt.Sprintf("/api/users/%s/", uuid.String())}
return lysand.URLFromStd(config.C.PublicAddress.ResolveReference(newPath))
}
func DefaultAvatarURL(uuid uuid.UUID) *lysand.URL {
u := &url.URL{}
q := u.Query()
q.Set("seed", uuid.String())
u.RawQuery = q.Encode()
return lysand.URLFromStd(dicebearURL.ResolveReference(u))
}
func UserInboxAPIURL(uuid uuid.UUID) *lysand.URL {
newPath := &url.URL{Path: "./inbox"}
return UserAPIURL(uuid).ResolveReference(newPath)
}
func UserOutboxAPIURL(uuid uuid.UUID) *lysand.URL {
newPath := &url.URL{Path: "./outbox"}
return UserAPIURL(uuid).ResolveReference(newPath)
}
func UserFollowersAPIURL(uuid uuid.UUID) *lysand.URL {
newPath := &url.URL{Path: "./followers"}
return UserAPIURL(uuid).ResolveReference(newPath)
}
func UserFollowingAPIURL(uuid uuid.UUID) *lysand.URL {
newPath := &url.URL{Path: "./following"}
return UserAPIURL(uuid).ResolveReference(newPath)
}
func UserFeaturedAPIURL(uuid uuid.UUID) *lysand.URL {
newPath := &url.URL{Path: "./featured"}
return UserAPIURL(uuid).ResolveReference(newPath)
}
func UserLikesAPIURL(uuid uuid.UUID) *lysand.URL {
newPath := &url.URL{Path: "./likes"}
return UserAPIURL(uuid).ResolveReference(newPath)
}
func UserDislikesAPIURL(uuid uuid.UUID) *lysand.URL {
newPath := &url.URL{Path: "./dislikes"}
return UserAPIURL(uuid).ResolveReference(newPath)
}
func FollowAPIURL(uuid uuid.UUID) *lysand.URL {
newPath := &url.URL{Path: fmt.Sprintf("/api/follows/%s/", uuid.String())}
return lysand.URLFromStd(config.C.PublicAddress.ResolveReference(newPath))
}
func NoteAPIURL(uuid uuid.UUID) *lysand.URL {
newPath := &url.URL{Path: fmt.Sprintf("/api/notes/%s/", uuid.String())}
return lysand.URLFromStd(config.C.PublicAddress.ResolveReference(newPath))
}