refactor: rename EventData to Source and expand event data model

This commit is contained in:
divyam234 2025-04-09 05:46:23 +02:00 committed by divyam234 (aider)
parent d66dc78e35
commit 4ef27d5ab8
No known key found for this signature in database
5 changed files with 46 additions and 30 deletions

View file

@ -4,7 +4,7 @@ CREATE TABLE IF NOT EXISTS teldrive.events (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
type text NOT NULL,
user_id bigint NOT NULL,
data jsonb,
source jsonb,
created_at timestamp DEFAULT timezone('utc'::text, now()) NOT NULL
);
-- +goose StatementEnd

View file

@ -38,12 +38,12 @@ func NewRecorder(ctx context.Context, db *gorm.DB, logger *zap.Logger) *Recorder
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{
Type: string(eventType),
UserID: userID,
Data: datatypes.NewJSONType(details),
Source: datatypes.NewJSONType(source),
}
select {

View file

@ -7,15 +7,17 @@ import (
)
type Event struct {
ID string `gorm:"type:uuid;primaryKey;default:gen_random_uuid()()"`
Type string `gorm:"type:text;not null"`
UserID int64 `gorm:"type:bigint"`
Data datatypes.JSONType[*EventData] `gorm:"type:jsonb"`
CreatedAt time.Time `gorm:"default:timezone('utc'::text, now())"`
ID string `gorm:"type:uuid;primaryKey;default:gen_random_uuid()()"`
Type string `gorm:"type:text;not null"`
UserID int64 `gorm:"type:bigint"`
Source datatypes.JSONType[*Source] `gorm:"type:jsonb"`
CreatedAt time.Time `gorm:"default:timezone('utc'::text, now())"`
}
type EventData struct {
FileID string `json:"id,omitempty"`
FolderID string `json:"folderId,omitempty"`
OldFolderID string `json:"oldFolderId,omitempty"`
type Source struct {
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
ParentID string `json:"parentId,omitempty"`
DestParentID string `json:"destParentId,omitempty"`
}

View file

@ -47,10 +47,12 @@ func (a *apiService) EventsGetEvents(ctx context.Context) ([]api.Event, error) {
ID: item.ID,
Type: item.Type,
CreatedAt: item.CreatedAt,
Data: api.EventData{
FileId: api.NewOptString(item.Data.Data().FileID),
FolderId: item.Data.Data().FolderID,
OldFolderId: api.NewOptString(item.Data.Data().OldFolderID),
Source: api.EventSource{
ID: item.Source.Data().ID,
Type: api.EventSourceType(item.Source.Data().Type),
Name: item.Source.Data().Name,
ParentId: item.Source.Data().ParentID,
DestParentId: api.NewOptString(item.Source.Data().DestParentID),
},
}
}), nil

View file

@ -231,9 +231,11 @@ func (a *apiService) FilesCopy(ctx context.Context, req *api.FileCopy, params ap
return nil, &apiError{err: err}
}
a.events.Record(events.OpCopy, userId, &models.EventData{
FileID: dbFile.ID,
FolderID: parentId,
a.events.Record(events.OpCopy, userId, &models.Source{
ID: dbFile.ID,
Type: dbFile.Type,
Name: dbFile.Name,
ParentID: parentId,
})
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 {
return nil, &apiError{err: err}
}
a.events.Record(events.OpCreate, userId, &models.EventData{
FileID: fileDB.ID,
FolderID: *fileDB.ParentId,
a.events.Record(events.OpCreate, userId, &models.Source{
ID: fileDB.ID,
Type: fileDB.Type,
Name: fileDB.Name,
ParentID: *fileDB.ParentId,
})
return mapper.ToFileOut(fileDB), nil
}
@ -381,8 +385,11 @@ func (a *apiService) FilesDelete(ctx context.Context, req *api.FileDelete) error
return &apiError{err: err}
}
a.events.Record(events.OpDelete, userId, &models.EventData{
FolderID: *fileDB.ParentId,
a.events.Record(events.OpDelete, userId, &models.Source{
ID: fileDB.ID,
Type: fileDB.Type,
Name: fileDB.Name,
ParentID: *fileDB.ParentId,
})
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 {
return err
}
a.events.Record(events.OpMove, userId, &models.EventData{
FolderID: req.DestinationParent,
OldFolderID: *srcFile.ParentId,
a.events.Record(events.OpMove, userId, &models.Source{
ID: req.DestinationParent,
Type: srcFile.Type,
Name: srcFile.Name,
ParentID: *srcFile.ParentId,
DestParentID: req.DestinationParent,
})
return nil
@ -594,9 +604,11 @@ func (a *apiService) FilesUpdate(ctx context.Context, req *api.FileUpdate, param
return nil, &apiError{err: err}
}
a.events.Record(events.OpUpdate, userId, &models.EventData{
FileID: file.ID,
FolderID: *file.ParentId,
a.events.Record(events.OpUpdate, userId, &models.Source{
ID: file.ID,
Type: file.Type,
Name: file.Name,
ParentID: *file.ParentId,
})
return mapper.ToFileOut(file), nil
}