mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
feat: add visibility
field to resource (#743)
This commit is contained in:
@ -9,10 +9,11 @@ type Resource struct {
|
|||||||
UpdatedTs int64 `json:"updatedTs"`
|
UpdatedTs int64 `json:"updatedTs"`
|
||||||
|
|
||||||
// Domain specific fields
|
// Domain specific fields
|
||||||
Filename string `json:"filename"`
|
Filename string `json:"filename"`
|
||||||
Blob []byte `json:"-"`
|
Blob []byte `json:"-"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Size int64 `json:"size"`
|
Size int64 `json:"size"`
|
||||||
|
Visibility Visibility `json:"visibility"`
|
||||||
|
|
||||||
// Related fields
|
// Related fields
|
||||||
LinkedMemoAmount int `json:"linkedMemoAmount"`
|
LinkedMemoAmount int `json:"linkedMemoAmount"`
|
||||||
@ -23,10 +24,11 @@ type ResourceCreate struct {
|
|||||||
CreatorID int
|
CreatorID int
|
||||||
|
|
||||||
// Domain specific fields
|
// Domain specific fields
|
||||||
Filename string `json:"filename"`
|
Filename string `json:"filename"`
|
||||||
Blob []byte `json:"blob"`
|
Blob []byte `json:"blob"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Size int64 `json:"size"`
|
Size int64 `json:"size"`
|
||||||
|
Visibility Visibility `json:"visibility"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResourceFind struct {
|
type ResourceFind struct {
|
||||||
@ -36,8 +38,9 @@ type ResourceFind struct {
|
|||||||
CreatorID *int `json:"creatorId"`
|
CreatorID *int `json:"creatorId"`
|
||||||
|
|
||||||
// Domain specific fields
|
// Domain specific fields
|
||||||
Filename *string `json:"filename"`
|
Filename *string `json:"filename"`
|
||||||
MemoID *int
|
MemoID *int
|
||||||
|
Visibility *Visibility `json:"visibility"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResourcePatch struct {
|
type ResourcePatch struct {
|
||||||
@ -47,7 +50,8 @@ type ResourcePatch struct {
|
|||||||
UpdatedTs *int64
|
UpdatedTs *int64
|
||||||
|
|
||||||
// Domain specific fields
|
// Domain specific fields
|
||||||
Filename *string `json:"filename"`
|
Filename *string `json:"filename"`
|
||||||
|
Visibility *Visibility `json:"visibility"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResourceDelete struct {
|
type ResourceDelete struct {
|
||||||
|
@ -75,7 +75,8 @@ CREATE TABLE resource (
|
|||||||
blob BLOB DEFAULT NULL,
|
blob BLOB DEFAULT NULL,
|
||||||
external_link TEXT NOT NULL DEFAULT '',
|
external_link TEXT NOT NULL DEFAULT '',
|
||||||
type TEXT NOT NULL DEFAULT '',
|
type TEXT NOT NULL DEFAULT '',
|
||||||
size INTEGER NOT NULL DEFAULT 0
|
size INTEGER NOT NULL DEFAULT 0,
|
||||||
|
visibility TEXT NOT NULL CHECK (visibility IN ('PUBLIC', 'PROTECTED', 'PRIVATE')) DEFAULT 'PRIVATE'
|
||||||
);
|
);
|
||||||
|
|
||||||
-- memo_resource
|
-- memo_resource
|
||||||
|
2
store/db/migration/prod/0.9/00__resource_visibility.sql
Normal file
2
store/db/migration/prod/0.9/00__resource_visibility.sql
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
-- Add visibility field to resource
|
||||||
|
ALTER TABLE resource ADD COLUMN visibility TEXT NOT NULL CHECK (visibility IN ('PUBLIC', 'PROTECTED' 'PRIVATE')) DEFAULT 'PRIVATE';
|
@ -22,10 +22,11 @@ type resourceRaw struct {
|
|||||||
UpdatedTs int64
|
UpdatedTs int64
|
||||||
|
|
||||||
// Domain specific fields
|
// Domain specific fields
|
||||||
Filename string
|
Filename string
|
||||||
Blob []byte
|
Blob []byte
|
||||||
Type string
|
Type string
|
||||||
Size int64
|
Size int64
|
||||||
|
Visibility api.Visibility
|
||||||
}
|
}
|
||||||
|
|
||||||
func (raw *resourceRaw) toResource() *api.Resource {
|
func (raw *resourceRaw) toResource() *api.Resource {
|
||||||
@ -38,10 +39,11 @@ func (raw *resourceRaw) toResource() *api.Resource {
|
|||||||
UpdatedTs: raw.UpdatedTs,
|
UpdatedTs: raw.UpdatedTs,
|
||||||
|
|
||||||
// Domain specific fields
|
// Domain specific fields
|
||||||
Filename: raw.Filename,
|
Filename: raw.Filename,
|
||||||
Blob: raw.Blob,
|
Blob: raw.Blob,
|
||||||
Type: raw.Type,
|
Type: raw.Type,
|
||||||
Size: raw.Size,
|
Size: raw.Size,
|
||||||
|
Visibility: raw.Visibility,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -217,18 +219,20 @@ func createResource(ctx context.Context, tx *sql.Tx, create *api.ResourceCreate)
|
|||||||
blob,
|
blob,
|
||||||
type,
|
type,
|
||||||
size,
|
size,
|
||||||
|
visibility,
|
||||||
creator_id
|
creator_id
|
||||||
)
|
)
|
||||||
VALUES (?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?)
|
||||||
RETURNING id, filename, blob, type, size, creator_id, created_ts, updated_ts
|
RETURNING id, filename, blob, type, size, visibility, creator_id, created_ts, updated_ts
|
||||||
`
|
`
|
||||||
var resourceRaw resourceRaw
|
var resourceRaw resourceRaw
|
||||||
if err := tx.QueryRowContext(ctx, query, create.Filename, create.Blob, create.Type, create.Size, create.CreatorID).Scan(
|
if err := tx.QueryRowContext(ctx, query, create.Filename, create.Blob, create.Type, create.Size, create.Visibility, create.CreatorID).Scan(
|
||||||
&resourceRaw.ID,
|
&resourceRaw.ID,
|
||||||
&resourceRaw.Filename,
|
&resourceRaw.Filename,
|
||||||
&resourceRaw.Blob,
|
&resourceRaw.Blob,
|
||||||
&resourceRaw.Type,
|
&resourceRaw.Type,
|
||||||
&resourceRaw.Size,
|
&resourceRaw.Size,
|
||||||
|
&resourceRaw.Visibility,
|
||||||
&resourceRaw.CreatorID,
|
&resourceRaw.CreatorID,
|
||||||
&resourceRaw.CreatedTs,
|
&resourceRaw.CreatedTs,
|
||||||
&resourceRaw.UpdatedTs,
|
&resourceRaw.UpdatedTs,
|
||||||
@ -248,6 +252,9 @@ func patchResource(ctx context.Context, tx *sql.Tx, patch *api.ResourcePatch) (*
|
|||||||
if v := patch.Filename; v != nil {
|
if v := patch.Filename; v != nil {
|
||||||
set, args = append(set, "filename = ?"), append(args, *v)
|
set, args = append(set, "filename = ?"), append(args, *v)
|
||||||
}
|
}
|
||||||
|
if v := patch.Visibility; v != nil {
|
||||||
|
set, args = append(set, "visibility = ?"), append(args, *v)
|
||||||
|
}
|
||||||
|
|
||||||
args = append(args, patch.ID)
|
args = append(args, patch.ID)
|
||||||
|
|
||||||
@ -255,7 +262,7 @@ func patchResource(ctx context.Context, tx *sql.Tx, patch *api.ResourcePatch) (*
|
|||||||
UPDATE resource
|
UPDATE resource
|
||||||
SET ` + strings.Join(set, ", ") + `
|
SET ` + strings.Join(set, ", ") + `
|
||||||
WHERE id = ?
|
WHERE id = ?
|
||||||
RETURNING id, filename, blob, type, size, creator_id, created_ts, updated_ts
|
RETURNING id, filename, blob, type, size, visibility, creator_id, created_ts, updated_ts
|
||||||
`
|
`
|
||||||
var resourceRaw resourceRaw
|
var resourceRaw resourceRaw
|
||||||
if err := tx.QueryRowContext(ctx, query, args...).Scan(
|
if err := tx.QueryRowContext(ctx, query, args...).Scan(
|
||||||
@ -264,6 +271,7 @@ func patchResource(ctx context.Context, tx *sql.Tx, patch *api.ResourcePatch) (*
|
|||||||
&resourceRaw.Blob,
|
&resourceRaw.Blob,
|
||||||
&resourceRaw.Type,
|
&resourceRaw.Type,
|
||||||
&resourceRaw.Size,
|
&resourceRaw.Size,
|
||||||
|
&resourceRaw.Visibility,
|
||||||
&resourceRaw.CreatorID,
|
&resourceRaw.CreatorID,
|
||||||
&resourceRaw.CreatedTs,
|
&resourceRaw.CreatedTs,
|
||||||
&resourceRaw.UpdatedTs,
|
&resourceRaw.UpdatedTs,
|
||||||
@ -286,6 +294,9 @@ func findResourceList(ctx context.Context, tx *sql.Tx, find *api.ResourceFind) (
|
|||||||
if v := find.Filename; v != nil {
|
if v := find.Filename; v != nil {
|
||||||
where, args = append(where, "filename = ?"), append(args, *v)
|
where, args = append(where, "filename = ?"), append(args, *v)
|
||||||
}
|
}
|
||||||
|
if v := find.Visibility; v != nil {
|
||||||
|
where, args = append(where, "visibility = ?"), append(args, *v)
|
||||||
|
}
|
||||||
if v := find.MemoID; v != nil {
|
if v := find.MemoID; v != nil {
|
||||||
where, args = append(where, "id in (SELECT resource_id FROM memo_resource WHERE memo_id = ?)"), append(args, *v)
|
where, args = append(where, "id in (SELECT resource_id FROM memo_resource WHERE memo_id = ?)"), append(args, *v)
|
||||||
}
|
}
|
||||||
@ -297,6 +308,7 @@ func findResourceList(ctx context.Context, tx *sql.Tx, find *api.ResourceFind) (
|
|||||||
blob,
|
blob,
|
||||||
type,
|
type,
|
||||||
size,
|
size,
|
||||||
|
visibility,
|
||||||
creator_id,
|
creator_id,
|
||||||
created_ts,
|
created_ts,
|
||||||
updated_ts
|
updated_ts
|
||||||
@ -319,6 +331,7 @@ func findResourceList(ctx context.Context, tx *sql.Tx, find *api.ResourceFind) (
|
|||||||
&resourceRaw.Blob,
|
&resourceRaw.Blob,
|
||||||
&resourceRaw.Type,
|
&resourceRaw.Type,
|
||||||
&resourceRaw.Size,
|
&resourceRaw.Size,
|
||||||
|
&resourceRaw.Visibility,
|
||||||
&resourceRaw.CreatorID,
|
&resourceRaw.CreatorID,
|
||||||
&resourceRaw.CreatedTs,
|
&resourceRaw.CreatedTs,
|
||||||
&resourceRaw.UpdatedTs,
|
&resourceRaw.UpdatedTs,
|
||||||
|
Reference in New Issue
Block a user