Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

define a more robust asset schema and expose media assets on thread/post APIs #20

Merged
merged 5 commits into from
Jul 23, 2023
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
59 changes: 36 additions & 23 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -576,14 +576,6 @@ paths:
#

/v1/assets:
get:
operationId: AssetGetUploadURL
description: Get an upload URL for a new asset.
tags: [assets]
responses:
default: { $ref: "#/components/responses/InternalServerError" }
"401": { $ref: "#/components/responses/Unauthorised" }
"200": { $ref: "#/components/responses/AssetGetUploadURLOK" }
post:
operationId: AssetUpload
description: |
Expand Down Expand Up @@ -924,13 +916,6 @@ components:
schema:
$ref: "#/components/schemas/React"

AssetGetUploadURLOK:
description: A pre-authorised upload URL.
content:
application/json:
schema:
$ref: "#/components/schemas/AssetUploadURL"

AssetUploadOK:
description: The new URL of an uploaded file.
content:
Expand Down Expand Up @@ -1605,6 +1590,7 @@ components:
meta: { $ref: "#/components/schemas/Metadata" }
category: { $ref: "#/components/schemas/Identifier" }
status: { $ref: "#/components/schemas/ThreadStatus" }
assets: { $ref: "#/components/schemas/AssetReferenceList" }

ThreadMutableProps:
type: object
Expand All @@ -1615,6 +1601,7 @@ components:
meta: { $ref: "#/components/schemas/Metadata" }
category: { $ref: "#/components/schemas/Identifier" }
status: { $ref: "#/components/schemas/ThreadStatus" }
assets: { $ref: "#/components/schemas/AssetReferenceList" }

ThreadReference:
description: |
Expand All @@ -1635,6 +1622,7 @@ components:
- category
- reacts
- meta
- assets
properties:
title:
type: string
Expand Down Expand Up @@ -1663,6 +1651,7 @@ components:
reacts:
$ref: "#/components/schemas/ReactList"
meta: { $ref: "#/components/schemas/Metadata" }
assets: { $ref: "#/components/schemas/AssetList" }

ThreadList:
type: array
Expand Down Expand Up @@ -1708,7 +1697,7 @@ components:

PostCommonProps:
type: object
required: [root_id, root_slug, body, author, reacts]
required: [root_id, root_slug, body, author, reacts, assets]
properties:
root_id: { $ref: "#/components/schemas/Identifier" }
root_slug: { $ref: "#/components/schemas/ThreadMark" }
Expand All @@ -1717,6 +1706,7 @@ components:
meta: { $ref: "#/components/schemas/Metadata" }
reacts: { $ref: "#/components/schemas/ReactList" }
reply_to: { $ref: "#/components/schemas/Identifier" }
assets: { $ref: "#/components/schemas/AssetList" }

PostInitialProps:
type: object
Expand Down Expand Up @@ -1763,19 +1753,42 @@ components:
items:
$ref: "#/components/schemas/PostProps"

AssetUploadURL:
type: object
required: [url]
properties:
url:
type: string
#
# 888b d888 888 d8b
# 8888b d8888 888 Y8P
# 88888b.d88888 888
# 888Y88888P888 .d88b. .d88888 888 8888b.
# 888 Y888P 888 d8P Y8b d88" 888 888 "88b
# 888 Y8P 888 88888888 888 888 888 .d888888
# 888 " 888 Y8b. Y88b 888 888 888 888
# 888 888 "Y8888 "Y88888 888 "Y888888
#

AssetID:
$ref: "#/components/schemas/Identifier"

AssetReferenceList:
type: array
items: { $ref: "#/components/schemas/AssetID" }

AssetList:
type: array
items: { $ref: "#/components/schemas/Asset" }

Asset:
type: object
required: [url]
required: [id, url, mime_type, width, height]
properties:
id:
$ref: "#/components/schemas/AssetID"
url:
type: string
mime_type:
type: string
width:
type: number
height:
type: number

securitySchemes:
browser:
Expand Down
72 changes: 72 additions & 0 deletions app/resources/asset/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package asset

import (
"context"

"github.com/Southclaws/fault"
"github.com/Southclaws/fault/fctx"
"github.com/Southclaws/fault/ftag"
"github.com/rs/xid"

"github.com/Southclaws/storyden/app/resources/account"
"github.com/Southclaws/storyden/internal/ent"
)

type database struct {
db *ent.Client
}

func New(db *ent.Client) Repository {
return &database{db}
}

func (d *database) Add(ctx context.Context,
accountID account.AccountID,
id, url, mt string,
width, height int,
) (*Asset, error) {
asset, err := d.db.Asset.Get(ctx, id)
if err != nil && !ent.IsNotFound(err) {
return nil, fault.Wrap(err, fctx.With(ctx))
}

if asset == nil {
asset, err = d.db.Asset.
Create().
SetID(id).
SetURL(url).
SetWidth(width).
SetHeight(height).
SetMimetype(mt).
SetAccountID(xid.ID(accountID)).
Save(ctx)
if err != nil {
return nil, fault.Wrap(err, fctx.With(ctx), ftag.With(ftag.Internal))
}
}

return FromModel(asset), nil
}

func (d *database) Get(ctx context.Context, id string) (*Asset, error) {
asset, err := d.db.Asset.Get(ctx, id)
if err != nil {
if ent.IsNotFound(err) {
return nil, fault.Wrap(err, fctx.With(ctx), ftag.With(ftag.NotFound))
}
return nil, fault.Wrap(err, fctx.With(ctx))
}

return FromModel(asset), nil
}

func (d *database) Remove(ctx context.Context, accountID account.AccountID, id AssetID) error {
q := d.db.Asset.
DeleteOneID(string(id))

if err := q.Exec(ctx); err != nil {
return fault.Wrap(err, fctx.With(ctx), ftag.With(ftag.Internal))
}

return nil
}
25 changes: 25 additions & 0 deletions app/resources/asset/dto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package asset

import (
"github.com/Southclaws/storyden/internal/ent"
)

type AssetID string

type Asset struct {
ID AssetID
URL string
MIMEType string
Width int
Height int
}

func FromModel(a *ent.Asset) *Asset {
return &Asset{
ID: AssetID(a.ID),
URL: a.URL,
MIMEType: a.Mimetype,
Width: a.Width,
Height: a.Height,
}
}
19 changes: 19 additions & 0 deletions app/resources/asset/repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package asset

import (
"context"

"github.com/Southclaws/storyden/app/resources/account"
)

type Repository interface {
Add(ctx context.Context,
owner account.AccountID,
id, url, mt string,
width, height int,
) (*Asset, error)

Get(ctx context.Context, id string) (*Asset, error)

Remove(ctx context.Context, owner account.AccountID, id AssetID) error
}
3 changes: 3 additions & 0 deletions app/resources/post/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (d *database) Create(
WithRoot(func(pq *ent.PostQuery) {
pq.WithAuthor()
}).
WithAssets().
Only(ctx)
if err != nil {
if ent.IsNotFound(err) {
Expand All @@ -99,6 +100,7 @@ func (d *database) Get(ctx context.Context, id PostID) (*Post, error) {
WithRoot(func(pq *ent.PostQuery) {
pq.WithAuthor()
}).
WithAssets().
Only(ctx)
if err != nil {
return nil, fault.Wrap(err, fctx.With(ctx), ftag.With(ftag.Internal))
Expand Down Expand Up @@ -127,6 +129,7 @@ func (d *database) Update(ctx context.Context, id PostID, opts ...Option) (*Post
WithRoot(func(pq *ent.PostQuery) {
pq.WithAuthor()
}).
WithAssets().
Only(ctx)
if err != nil {
return nil, fault.Wrap(err, fctx.With(ctx), ftag.With(ftag.Internal))
Expand Down
3 changes: 3 additions & 0 deletions app/resources/post/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/rs/xid"

"github.com/Southclaws/storyden/app/resources/account"
"github.com/Southclaws/storyden/app/resources/asset"
"github.com/Southclaws/storyden/app/resources/react"
"github.com/Southclaws/storyden/internal/ent"
)
Expand All @@ -28,6 +29,7 @@ type Post struct {
ReplyTo opt.Optional[PostID]
Reacts []*react.React
Meta map[string]any
Assets []*asset.Asset

CreatedAt time.Time
UpdatedAt time.Time
Expand Down Expand Up @@ -74,6 +76,7 @@ func FromModel(m *ent.Post) (w *Post) {
ReplyTo: replyTo,
Reacts: dt.Map(m.Edges.Reacts, react.FromModel),
Meta: m.Metadata,
Assets: dt.Map(m.Edges.Assets, asset.FromModel),

CreatedAt: m.CreatedAt,
UpdatedAt: m.UpdatedAt,
Expand Down
6 changes: 6 additions & 0 deletions app/resources/post/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,9 @@ func WithMeta(meta map[string]any) Option {
m.SetMetadata(meta)
}
}

func WithAssets(ids ...string) Option {
return func(m *ent.PostMutation) {
m.AddAssetIDs(ids...)
}
}
2 changes: 2 additions & 0 deletions app/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"go.uber.org/fx"

"github.com/Southclaws/storyden/app/resources/account"
"github.com/Southclaws/storyden/app/resources/asset"
"github.com/Southclaws/storyden/app/resources/authentication"
"github.com/Southclaws/storyden/app/resources/category"
"github.com/Southclaws/storyden/app/resources/notification"
Expand All @@ -22,6 +23,7 @@ func Build() fx.Option {
fx.Provide(
settings.New,
account.New,
asset.New,
authentication.New,
category.New,
post.New,
Expand Down
6 changes: 5 additions & 1 deletion app/resources/thread/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func (d *database) Update(ctx context.Context, id post.PostID, opts ...Option) (
WithAuthor().
WithCategory().
WithTags().
WithAssets().
Only(ctx)
if err != nil {
return nil, fault.Wrap(err, fctx.With(ctx), ftag.With(ftag.Internal))
Expand Down Expand Up @@ -186,7 +187,8 @@ func (d *database) List(
Where(filters...).
Limit(max).
WithCategory().
WithAuthor()
WithAuthor().
WithAssets()

for _, fn := range opts {
fn(query)
Expand Down Expand Up @@ -222,12 +224,14 @@ func (d *database) Get(ctx context.Context, threadID post.PostID) (*Thread, erro
}).
WithReacts().
WithAuthor().
WithAssets().
Order(ent.Asc(post_model.FieldCreatedAt))
}).
WithAuthor().
WithCategory().
WithTags().
WithReacts().
WithAssets().
Only(ctx)
if err != nil {
if ent.IsNotFound(err) {
Expand Down
3 changes: 3 additions & 0 deletions app/resources/thread/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/Southclaws/opt"

"github.com/Southclaws/storyden/app/resources/account"
"github.com/Southclaws/storyden/app/resources/asset"
"github.com/Southclaws/storyden/app/resources/category"
"github.com/Southclaws/storyden/app/resources/post"
"github.com/Southclaws/storyden/app/resources/react"
Expand All @@ -31,6 +32,7 @@ type Thread struct {
Posts []*post.Post
Reacts []*react.React
Meta map[string]any
Assets []*asset.Asset
}

func (*Thread) GetResourceName() string { return "thread" }
Expand Down Expand Up @@ -75,5 +77,6 @@ func FromModel(m *ent.Post) *Thread {
Posts: posts,
Reacts: dt.Map(m.Edges.Reacts, react.FromModel),
Meta: m.Metadata,
Assets: dt.Map(m.Edges.Assets, asset.FromModel),
}
}
Loading