More API progress
This time mainly helper functions for converting an account and associated types into their API representation
This commit is contained in:
parent
873f52d64f
commit
a653477e7f
8 changed files with 201 additions and 7 deletions
109
server/apiLinstromTypeHelpers.go
Normal file
109
server/apiLinstromTypeHelpers.go
Normal file
|
@ -0,0 +1,109 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"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, err
|
||||
}
|
||||
apiServer, err := convertServerStorageToLinstrom(storageServer, store)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
storageIcon, err := store.GetMediaMetadataById(acc.Icon)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
storageBanner, err := store.GetMediaMetadataById(acc.Banner)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
storageFields, err := store.FindMultipleUserFieldsById(acc.CustomFields)
|
||||
if err != nil {
|
||||
return nil, 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, 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,
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue