chore: revert resource visibility changes (#1444)

This commit is contained in:
boojack 2023-04-02 14:09:25 +08:00 committed by GitHub
parent d71bfce1a0
commit 41c50e758a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 76 deletions

View file

@ -9,13 +9,12 @@ type Resource struct {
UpdatedTs int64 `json:"updatedTs"`
// Domain specific fields
Filename string `json:"filename"`
Blob []byte `json:"-"`
InternalPath string `json:"internalPath"`
ExternalLink string `json:"externalLink"`
Type string `json:"type"`
Size int64 `json:"size"`
Visibility Visibility `json:"visibility"`
Filename string `json:"filename"`
Blob []byte `json:"-"`
InternalPath string `json:"internalPath"`
ExternalLink string `json:"externalLink"`
Type string `json:"type"`
Size int64 `json:"size"`
// Related fields
LinkedMemoAmount int `json:"linkedMemoAmount"`
@ -26,13 +25,12 @@ type ResourceCreate struct {
CreatorID int `json:"-"`
// Domain specific fields
Filename string `json:"filename"`
Blob []byte `json:"-"`
InternalPath string `json:"internalPath"`
ExternalLink string `json:"externalLink"`
Type string `json:"type"`
Size int64 `json:"-"`
Visibility Visibility `json:"visibility"`
Filename string `json:"filename"`
Blob []byte `json:"-"`
InternalPath string `json:"internalPath"`
ExternalLink string `json:"externalLink"`
Type string `json:"type"`
Size int64 `json:"-"`
}
type ResourceFind struct {
@ -58,8 +56,7 @@ type ResourcePatch struct {
UpdatedTs *int64
// Domain specific fields
Filename *string `json:"filename"`
Visibility *Visibility `json:"visibility"`
Filename *string `json:"filename"`
}
type ResourceDelete struct {

View file

@ -47,27 +47,6 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
if resourceCreate.ExternalLink != "" && !strings.HasPrefix(resourceCreate.ExternalLink, "http") {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid external link")
}
if resourceCreate.Visibility == "" {
userResourceVisibilitySetting, err := s.Store.FindUserSetting(ctx, &api.UserSettingFind{
UserID: userID,
Key: api.UserSettingResourceVisibilityKey,
})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user setting").SetInternal(err)
}
if userResourceVisibilitySetting != nil {
resourceVisibility := api.Private
err := json.Unmarshal([]byte(userResourceVisibilitySetting.Value), &resourceVisibility)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal user setting value").SetInternal(err)
}
resourceCreate.Visibility = resourceVisibility
} else {
// Private is the default resource visibility.
resourceCreate.Visibility = api.Private
}
}
resource, err := s.Store.CreateResource(ctx, resourceCreate)
if err != nil {
@ -215,28 +194,6 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
}
}
if resourceCreate.Visibility == "" {
userResourceVisibilitySetting, err := s.Store.FindUserSetting(ctx, &api.UserSettingFind{
UserID: userID,
Key: api.UserSettingResourceVisibilityKey,
})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user setting").SetInternal(err)
}
if userResourceVisibilitySetting != nil {
resourceVisibility := api.Private
err := json.Unmarshal([]byte(userResourceVisibilitySetting.Value), &resourceVisibility)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal user setting value").SetInternal(err)
}
resourceCreate.Visibility = resourceVisibility
} else {
// Private is the default resource visibility.
resourceCreate.Visibility = api.Private
}
}
resource, err := s.Store.CreateResource(ctx, resourceCreate)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create resource").SetInternal(err)

View file

@ -77,8 +77,7 @@ CREATE TABLE resource (
internal_path TEXT NOT NULL DEFAULT '',
external_link TEXT NOT NULL DEFAULT '',
type TEXT NOT NULL DEFAULT '',
size INTEGER NOT NULL DEFAULT 0,
visibility TEXT NOT NULL CHECK (visibility IN ('PUBLIC', 'PROTECTED', 'PRIVATE')) DEFAULT 'PRIVATE'
size INTEGER NOT NULL DEFAULT 0
);
-- memo_resource

View file

@ -28,7 +28,6 @@ type resourceRaw struct {
ExternalLink string
Type string
Size int64
Visibility api.Visibility
LinkedMemoAmount int
}
@ -48,7 +47,6 @@ func (raw *resourceRaw) toResource() *api.Resource {
ExternalLink: raw.ExternalLink,
Type: raw.Type,
Size: raw.Size,
Visibility: raw.Visibility,
LinkedMemoAmount: raw.LinkedMemoAmount,
}
}
@ -197,9 +195,9 @@ func (s *Store) createResourceImpl(ctx context.Context, tx *sql.Tx, create *api.
values := []any{create.Filename, create.Blob, create.ExternalLink, create.Type, create.Size, create.CreatorID}
placeholders := []string{"?", "?", "?", "?", "?", "?"}
if s.profile.IsDev() {
fields = append(fields, "visibility", "internal_path")
values = append(values, create.Visibility, create.InternalPath)
placeholders = append(placeholders, "?", "?")
fields = append(fields, "internal_path")
values = append(values, create.InternalPath)
placeholders = append(placeholders, "?")
}
query := `
@ -220,7 +218,7 @@ func (s *Store) createResourceImpl(ctx context.Context, tx *sql.Tx, create *api.
&resourceRaw.CreatorID,
}
if s.profile.IsDev() {
dests = append(dests, &resourceRaw.Visibility, &resourceRaw.InternalPath)
dests = append(dests, &resourceRaw.InternalPath)
}
dests = append(dests, []any{&resourceRaw.CreatedTs, &resourceRaw.UpdatedTs}...)
if err := tx.QueryRowContext(ctx, query, values...).Scan(dests...); err != nil {
@ -239,17 +237,12 @@ func (s *Store) patchResourceImpl(ctx context.Context, tx *sql.Tx, patch *api.Re
if v := patch.Filename; v != nil {
set, args = append(set, "filename = ?"), append(args, *v)
}
if s.profile.IsDev() {
if v := patch.Visibility; v != nil {
set, args = append(set, "visibility = ?"), append(args, *v)
}
}
args = append(args, patch.ID)
fields := []string{"id", "filename", "external_link", "type", "size", "creator_id", "created_ts", "updated_ts"}
if s.profile.IsDev() {
fields = append(fields, "visibility", "internal_path")
fields = append(fields, "internal_path")
}
query := `
@ -269,7 +262,7 @@ func (s *Store) patchResourceImpl(ctx context.Context, tx *sql.Tx, patch *api.Re
&resourceRaw.UpdatedTs,
}
if s.profile.IsDev() {
dests = append(dests, &resourceRaw.Visibility, &resourceRaw.InternalPath)
dests = append(dests, &resourceRaw.InternalPath)
}
if err := tx.QueryRowContext(ctx, query, args...).Scan(dests...); err != nil {
return nil, FormatError(err)
@ -299,7 +292,7 @@ func (s *Store) findResourceListImpl(ctx context.Context, tx *sql.Tx, find *api.
fields = append(fields, "resource.blob")
}
if s.profile.IsDev() {
fields = append(fields, "visibility", "internal_path")
fields = append(fields, "internal_path")
}
query := fmt.Sprintf(`
@ -343,7 +336,7 @@ func (s *Store) findResourceListImpl(ctx context.Context, tx *sql.Tx, find *api.
dests = append(dests, &resourceRaw.Blob)
}
if s.profile.IsDev() {
dests = append(dests, &resourceRaw.Visibility, &resourceRaw.InternalPath)
dests = append(dests, &resourceRaw.InternalPath)
}
if err := rows.Scan(dests...); err != nil {
return nil, FormatError(err)