Skip to content
This repository was archived by the owner on Dec 17, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions data/model/emote-set.model.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type EmoteSetModel struct {
Emotes []ActiveEmoteModel `json:"emotes"`
Capacity int32 `json:"capacity"`
ParentID *primitive.ObjectID `json:"parent_id,omitempty"`
Owner *UserModel `json:"owner" extensions:"x-nullable"`
Owner *UserPartialModel `json:"owner" extensions:"x-nullable"`
}

type ActiveEmoteModel struct {
Expand All @@ -25,6 +25,7 @@ type ActiveEmoteModel struct {
Flags ActiveEmoteFlagModel `json:"flags"`
Timestamp time.Time
ActorID primitive.ObjectID `json:"actor_id,omitempty"`
Data *EmotePartialModel `json:"data,omitempty" extensions:"x-nullable"`
}

type ActiveEmoteFlagModel int32
Expand All @@ -42,13 +43,17 @@ func (x *modelizer) EmoteSet(v structures.EmoteSet) EmoteSetModel {
emotes[i] = x.ActiveEmote(e)
}

var owner *UserModel
var owner *UserPartialModel

if v.Owner != nil {
u := x.User(*v.Owner)
u := x.User(*v.Owner).ToPartial()
owner = &u
}

if v.Tags == nil {
v.Tags = make([]string, 0)
}

return EmoteSetModel{
ID: v.ID,
Name: v.Name,
Expand All @@ -63,11 +68,19 @@ func (x *modelizer) EmoteSet(v structures.EmoteSet) EmoteSetModel {
}

func (x *modelizer) ActiveEmote(v structures.ActiveEmote) ActiveEmoteModel {
var data *EmotePartialModel

if v.Emote != nil {
e := x.Emote(*v.Emote).ToPartial()
data = &e
}

return ActiveEmoteModel{
ID: v.ID,
Name: v.Name,
Flags: ActiveEmoteFlagModel(v.Flags),
Timestamp: v.Timestamp,
ActorID: v.ActorID,
Data: data,
}
}
75 changes: 60 additions & 15 deletions data/model/emote.model.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package model

import (
"fmt"
"sort"

"github.com/seventv/common/structures/v3"
Expand All @@ -15,19 +16,31 @@ type EmoteModel struct {
Lifecycle EmoteLifecycleModel `json:"lifecycle"`
Listed bool `json:"listed"`
Animated bool `json:"animated"`
Owner *UserModel `json:"owner,omitempty" extensions:"x-omitempty"`
Images []Image `json:"images"`
Owner *UserPartialModel `json:"owner,omitempty" extensions:"x-omitempty"`
Host ImageHost `json:"host"`
Versions []EmoteVersionModel `json:"versions"`
}

type EmotePartialModel struct {
ID primitive.ObjectID `json:"id"`
Name string `json:"name"`
Flags EmoteFlagsModel `json:"flags"`
Tags []string `json:"tags"`
Lifecycle EmoteLifecycleModel `json:"lifecycle"`
Listed bool `json:"listed"`
Animated bool `json:"animated"`
Owner *UserPartialModel `json:"owner,omitempty" extensions:"x-omitempty"`
Host ImageHost `json:"host"`
}

type EmoteVersionModel struct {
ID primitive.ObjectID `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Lifecycle EmoteLifecycleModel `json:"lifecycle"`
Listed bool `json:"listed"`
Animated bool `json:"animated"`
Images []Image `json:"images"`
Host ImageHost `json:"host"`
}

type EmoteLifecycleModel int32
Expand Down Expand Up @@ -57,20 +70,20 @@ const (
)

func (x *modelizer) Emote(v structures.Emote) EmoteModel {
images := make([]Image, 0)
images := make([]ImageFile, 0)
lifecycle := EmoteLifecycleDisabled
listed := false
animated := false

versions := make([]EmoteVersionModel, len(v.Versions))

for i, ver := range v.Versions {
files := ver.GetFiles("", true)
files := append(ver.GetFiles("image/avif", true), ver.GetFiles("image/webp", true)...)
sort.Slice(files, func(i, j int) bool {
return files[i].Width > files[j].Width
return files[i].Width < files[j].Width
})

vimages := make([]Image, len(files))
vimages := make([]ImageFile, len(files))

for i, fi := range files {
vimages[i] = x.Image(fi)
Expand All @@ -86,13 +99,21 @@ func (x *modelizer) Emote(v structures.Emote) EmoteModel {
versions[i] = x.EmoteVersion(ver)
}

var owner *UserModel
var owner *UserPartialModel

if v.Owner != nil {
u := x.User(*v.Owner)
u := x.User(*v.Owner).ToPartial()
owner = &u
}

sort.Slice(versions, func(i, j int) bool {
return versions[i].ID == v.ID || versions[j].ID.Timestamp().After(versions[i].ID.Timestamp())
})

if v.Tags == nil {
v.Tags = make([]string, 0)
}

return EmoteModel{
ID: v.ID,
Name: v.Name,
Expand All @@ -102,25 +123,49 @@ func (x *modelizer) Emote(v structures.Emote) EmoteModel {
Listed: listed,
Animated: animated,
Owner: owner,
Images: images,
Versions: versions,
Host: ImageHost{
URL: fmt.Sprintf("//%s/emote/%s", x.cdnURL, v.ID.Hex()),
Files: images,
},
Versions: versions,
}
}

func (em EmoteModel) ToPartial() EmotePartialModel {
return EmotePartialModel{
ID: em.ID,
Name: em.Name,
Flags: em.Flags,
Tags: em.Tags,
Lifecycle: em.Lifecycle,
Listed: em.Listed,
Animated: em.Animated,
Owner: em.Owner,
Host: em.Host,
}
}

func (x *modelizer) EmoteVersion(v structures.EmoteVersion) EmoteVersionModel {
var images []Image
var files []ImageFile

for _, fi := range v.GetFiles("", true) {
images = append(images, x.Image(fi))
for _, fi := range append(v.GetFiles("image/avif", true), v.GetFiles("image/webp", true)...) {
files = append(files, x.Image(fi))
}

sort.Slice(files, func(i, j int) bool {
return files[i].Width < files[j].Width
})

return EmoteVersionModel{
ID: v.ID,
Name: v.Name,
Description: v.Description,
Lifecycle: EmoteLifecycleModel(v.State.Lifecycle),
Listed: v.State.Listed,
Animated: v.Animated,
Images: images,
Host: ImageHost{
URL: fmt.Sprintf("//%s/emote/%s", x.cdnURL, v.ID.Hex()),
Files: files,
},
}
}
53 changes: 35 additions & 18 deletions data/model/model.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package model

import (
"fmt"
"strings"

"github.com/seventv/common/structures/v3"
"go.mongodb.org/mongo-driver/bson"
)

type Modelizer interface {
Emote(v structures.Emote) EmoteModel
User(v structures.User) UserModel
UserEditor(v structures.UserEditor) UserEditorModel
UserConnection(v structures.UserConnection[bson.Raw]) UserConnectionModel
EmoteSet(v structures.EmoteSet) EmoteSetModel
}

Expand All @@ -28,24 +32,37 @@ type ModelInstanceOptions struct {
Website string
}

type Image struct {
Name string `json:"name"`
Width int32 `json:"width"`
Height int32 `json:"height"`
FrameCount int32 `json:"frame_count"`
Size int64 `json:"size"`
ContentType string `json:"content_type"`
URL string `json:"url"`
type ImageHost struct {
URL string `json:"url"`
Files []ImageFile `json:"files"`
}

func (x *modelizer) Image(v structures.EmoteFile) Image {
return Image{
Name: v.Name,
Width: v.Width,
Height: v.Height,
FrameCount: v.FrameCount,
Size: v.Size,
ContentType: v.ContentType,
URL: fmt.Sprintf("//%s/%s", x.cdnURL, v.Key),
type ImageFile struct {
Name string `json:"name"`
Width int32 `json:"width"`
Height int32 `json:"height"`
FrameCount int32 `json:"frame_count"`
Size int64 `json:"size"`
Format ImageFormat `json:"format"`
}

type ImageFormat string

const (
ImageFormatAVIF ImageFormat = "AVIF"
ImageFormatWEBP ImageFormat = "WEBP"
)

func (x *modelizer) Image(v structures.EmoteFile) ImageFile {
format := strings.Split(v.ContentType, "/")[1]
format = strings.ToUpper(format)

return ImageFile{
Name: v.Name,
Format: ImageFormat(format),
Width: v.Width,
Height: v.Height,
FrameCount: v.FrameCount,
Size: v.Size,
}
}
Loading