linstrom/server/apiLinstromTypeHelpers.go

112 lines
3.2 KiB
Go
Raw Normal View History

package server
import (
"fmt"
"gitlab.com/mstarongitlab/goutils/sliceutils"
"gitlab.com/mstarongitlab/linstrom/storage"
)
func convertAccountStorageToLinstrom(
acc *storage.Account,
store *storage.Storage,
) (*linstromAccount, error) {
storageServer, err := store.FindRemoteServerById(acc.ServerId)
if err != nil {
return nil, fmt.Errorf("remote server: %w", err)
}
apiServer, err := convertServerStorageToLinstrom(storageServer, store)
if err != nil {
return nil, fmt.Errorf("remote server conversion: %w", err)
}
storageIcon, err := store.GetMediaMetadataById(acc.Icon)
if err != nil {
return nil, fmt.Errorf("icon: %w", err)
}
storageBanner, err := store.GetMediaMetadataById(acc.Banner)
if err != nil {
return nil, fmt.Errorf("banner: %w", err)
}
storageFields, err := store.FindMultipleUserFieldsById(acc.CustomFields)
if err != nil {
return nil, fmt.Errorf("customFields: %w", err)
}
return &linstromAccount{
Id: acc.ID,
CreatedAt: acc.CreatedAt,
UpdatedAt: &acc.UpdatedAt,
Username: acc.Username,
OriginServer: apiServer,
OriginServerId: int(acc.ServerId),
DisplayName: acc.DisplayName,
CustomFields: sliceutils.Map(
storageFields,
func(t storage.UserInfoField) *linstromCustomAccountField {
return convertInfoFieldStorageToLinstrom(t)
},
),
CustomFieldIds: acc.CustomFields,
IsBot: acc.IsBot,
Description: acc.Description,
Icon: convertMediaMetadataStorageToLinstrom(storageIcon),
Banner: convertMediaMetadataStorageToLinstrom(storageBanner),
FollowerIds: acc.Followers,
FollowingIds: acc.Follows,
Indexable: acc.Indexable,
RestrictedFollow: acc.RestrictedFollow,
IdentifiesAs: sliceutils.Map(
acc.IdentifiesAs,
func(t storage.Being) string { return string(t) },
),
Pronouns: acc.Gender,
Roles: acc.Roles,
}, nil
}
func convertServerStorageToLinstrom(
server *storage.RemoteServer,
store *storage.Storage,
) (*linstromOriginServer, error) {
storageMeta, err := store.GetMediaMetadataById(server.Icon)
if err != nil {
return nil, fmt.Errorf("icon metadata: %w", err)
}
return &linstromOriginServer{
Id: server.ID,
CreatedAt: server.CreatedAt,
UpdatedAt: &server.UpdatedAt,
ServerType: string(server.ServerType),
Domain: server.Domain,
DisplayName: server.Name,
Icon: convertMediaMetadataStorageToLinstrom(storageMeta),
IsSelf: server.IsSelf,
}, nil
}
func convertMediaMetadataStorageToLinstrom(metadata *storage.MediaMetadata) *linstromMediaMetadata {
return &linstromMediaMetadata{
Id: metadata.ID,
CreatedAt: metadata.CreatedAt,
UpdatedAt: &metadata.UpdatedAt,
IsRemote: metadata.Remote,
Url: metadata.Location,
MimeType: metadata.Type,
Name: metadata.Name,
AltText: metadata.AltText,
Blurred: metadata.Blurred,
}
}
func convertInfoFieldStorageToLinstrom(field storage.UserInfoField) *linstromCustomAccountField {
return &linstromCustomAccountField{
Id: field.ID,
CreatedAt: field.CreatedAt,
UpdatedAt: &field.UpdatedAt,
Key: field.Name,
Value: field.Value,
Verified: &field.Confirmed,
BelongsToId: field.BelongsTo,
}
}