mirror of
https://github.com/tgdrive/teldrive.git
synced 2025-09-06 06:25:47 +08:00
refactor: rename EventData to Source and expand event data model
This commit is contained in:
parent
d66dc78e35
commit
4ef27d5ab8
5 changed files with 46 additions and 30 deletions
|
@ -4,7 +4,7 @@ CREATE TABLE IF NOT EXISTS teldrive.events (
|
||||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
type text NOT NULL,
|
type text NOT NULL,
|
||||||
user_id bigint NOT NULL,
|
user_id bigint NOT NULL,
|
||||||
data jsonb,
|
source jsonb,
|
||||||
created_at timestamp DEFAULT timezone('utc'::text, now()) NOT NULL
|
created_at timestamp DEFAULT timezone('utc'::text, now()) NOT NULL
|
||||||
);
|
);
|
||||||
-- +goose StatementEnd
|
-- +goose StatementEnd
|
|
@ -38,12 +38,12 @@ func NewRecorder(ctx context.Context, db *gorm.DB, logger *zap.Logger) *Recorder
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Recorder) Record(eventType EventType, userID int64, details *models.EventData) {
|
func (r *Recorder) Record(eventType EventType, userID int64, source *models.Source) {
|
||||||
|
|
||||||
evt := models.Event{
|
evt := models.Event{
|
||||||
Type: string(eventType),
|
Type: string(eventType),
|
||||||
UserID: userID,
|
UserID: userID,
|
||||||
Data: datatypes.NewJSONType(details),
|
Source: datatypes.NewJSONType(source),
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
|
|
|
@ -7,15 +7,17 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Event struct {
|
type Event struct {
|
||||||
ID string `gorm:"type:uuid;primaryKey;default:gen_random_uuid()()"`
|
ID string `gorm:"type:uuid;primaryKey;default:gen_random_uuid()()"`
|
||||||
Type string `gorm:"type:text;not null"`
|
Type string `gorm:"type:text;not null"`
|
||||||
UserID int64 `gorm:"type:bigint"`
|
UserID int64 `gorm:"type:bigint"`
|
||||||
Data datatypes.JSONType[*EventData] `gorm:"type:jsonb"`
|
Source datatypes.JSONType[*Source] `gorm:"type:jsonb"`
|
||||||
CreatedAt time.Time `gorm:"default:timezone('utc'::text, now())"`
|
CreatedAt time.Time `gorm:"default:timezone('utc'::text, now())"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventData struct {
|
type Source struct {
|
||||||
FileID string `json:"id,omitempty"`
|
ID string `json:"id,omitempty"`
|
||||||
FolderID string `json:"folderId,omitempty"`
|
Type string `json:"type,omitempty"`
|
||||||
OldFolderID string `json:"oldFolderId,omitempty"`
|
Name string `json:"name,omitempty"`
|
||||||
|
ParentID string `json:"parentId,omitempty"`
|
||||||
|
DestParentID string `json:"destParentId,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,10 +47,12 @@ func (a *apiService) EventsGetEvents(ctx context.Context) ([]api.Event, error) {
|
||||||
ID: item.ID,
|
ID: item.ID,
|
||||||
Type: item.Type,
|
Type: item.Type,
|
||||||
CreatedAt: item.CreatedAt,
|
CreatedAt: item.CreatedAt,
|
||||||
Data: api.EventData{
|
Source: api.EventSource{
|
||||||
FileId: api.NewOptString(item.Data.Data().FileID),
|
ID: item.Source.Data().ID,
|
||||||
FolderId: item.Data.Data().FolderID,
|
Type: api.EventSourceType(item.Source.Data().Type),
|
||||||
OldFolderId: api.NewOptString(item.Data.Data().OldFolderID),
|
Name: item.Source.Data().Name,
|
||||||
|
ParentId: item.Source.Data().ParentID,
|
||||||
|
DestParentId: api.NewOptString(item.Source.Data().DestParentID),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}), nil
|
}), nil
|
||||||
|
|
|
@ -231,9 +231,11 @@ func (a *apiService) FilesCopy(ctx context.Context, req *api.FileCopy, params ap
|
||||||
return nil, &apiError{err: err}
|
return nil, &apiError{err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
a.events.Record(events.OpCopy, userId, &models.EventData{
|
a.events.Record(events.OpCopy, userId, &models.Source{
|
||||||
FileID: dbFile.ID,
|
ID: dbFile.ID,
|
||||||
FolderID: parentId,
|
Type: dbFile.Type,
|
||||||
|
Name: dbFile.Name,
|
||||||
|
ParentID: parentId,
|
||||||
})
|
})
|
||||||
return mapper.ToFileOut(dbFile), nil
|
return mapper.ToFileOut(dbFile), nil
|
||||||
}
|
}
|
||||||
|
@ -330,9 +332,11 @@ func (a *apiService) FilesCreate(ctx context.Context, fileIn *api.File) (*api.Fi
|
||||||
).Scan(&fileDB).Error; err != nil {
|
).Scan(&fileDB).Error; err != nil {
|
||||||
return nil, &apiError{err: err}
|
return nil, &apiError{err: err}
|
||||||
}
|
}
|
||||||
a.events.Record(events.OpCreate, userId, &models.EventData{
|
a.events.Record(events.OpCreate, userId, &models.Source{
|
||||||
FileID: fileDB.ID,
|
ID: fileDB.ID,
|
||||||
FolderID: *fileDB.ParentId,
|
Type: fileDB.Type,
|
||||||
|
Name: fileDB.Name,
|
||||||
|
ParentID: *fileDB.ParentId,
|
||||||
})
|
})
|
||||||
return mapper.ToFileOut(fileDB), nil
|
return mapper.ToFileOut(fileDB), nil
|
||||||
}
|
}
|
||||||
|
@ -381,8 +385,11 @@ func (a *apiService) FilesDelete(ctx context.Context, req *api.FileDelete) error
|
||||||
return &apiError{err: err}
|
return &apiError{err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
a.events.Record(events.OpDelete, userId, &models.EventData{
|
a.events.Record(events.OpDelete, userId, &models.Source{
|
||||||
FolderID: *fileDB.ParentId,
|
ID: fileDB.ID,
|
||||||
|
Type: fileDB.Type,
|
||||||
|
Name: fileDB.Name,
|
||||||
|
ParentID: *fileDB.ParentId,
|
||||||
})
|
})
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -516,9 +523,12 @@ func (a *apiService) FilesMove(ctx context.Context, req *api.FileMove) error {
|
||||||
Update("parent_id", req.DestinationParent).Error; err != nil {
|
Update("parent_id", req.DestinationParent).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
a.events.Record(events.OpMove, userId, &models.EventData{
|
a.events.Record(events.OpMove, userId, &models.Source{
|
||||||
FolderID: req.DestinationParent,
|
ID: req.DestinationParent,
|
||||||
OldFolderID: *srcFile.ParentId,
|
Type: srcFile.Type,
|
||||||
|
Name: srcFile.Name,
|
||||||
|
ParentID: *srcFile.ParentId,
|
||||||
|
DestParentID: req.DestinationParent,
|
||||||
})
|
})
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
|
@ -594,9 +604,11 @@ func (a *apiService) FilesUpdate(ctx context.Context, req *api.FileUpdate, param
|
||||||
return nil, &apiError{err: err}
|
return nil, &apiError{err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
a.events.Record(events.OpUpdate, userId, &models.EventData{
|
a.events.Record(events.OpUpdate, userId, &models.Source{
|
||||||
FileID: file.ID,
|
ID: file.ID,
|
||||||
FolderID: *file.ParentId,
|
Type: file.Type,
|
||||||
|
Name: file.Name,
|
||||||
|
ParentID: *file.ParentId,
|
||||||
})
|
})
|
||||||
return mapper.ToFileOut(file), nil
|
return mapper.ToFileOut(file), nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue