activity

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2024 License: MIT Imports: 20 Imported by: 2

README

Activity

Usage

  • Firstly, you should create an activity instance in your project.

    activity := activity.New(presetsBuilder, db, logModel)
    
    • presetsBuilder (Required), it is a instance of presets.Builder to register activity into presets.
    • db (Required), it is a global database instance. we will use it if we don't find the specific database instance in a operation.
    • logModel (Optional), you can use your own model table to record the activity log as long as the model implements the ActivityLogModel interface.
  • Register normal model or a presets.ModelBuilder into activity

    activity.RegisterModel(normalModel) // It need you to record the activity log manually
    activity.RegisterModel(presetModel) // It will record the activity log automatically when you create, update or delete the model data via preset admin
    
  • Skip recording activity log for preset model if you don't want to record the activity log automatically

    activity.RegisterModel(presetModel).SkipCreate().SkipUpdate().SkipDelete()
    
  • Configure more options for the presets.ModelBuilder to record more custom information

    activity.RegisterModel(presetModel).UseDefaultTab() //use activity tab on the admin model edit page
    activity.RegisterModel(presetModel).AddKeys("ID", "Version") // will record value of the ID and Version field as the keyword of a model table
    activity.RegisterModel(presetModel).AddIgnoredFields("UpdateAt") // will ignore the UpdateAt field when recording activity log for update operation
    activity.RegisterModel(presetModel).AddTypeHanders(
      time.Time{},
      func(old, now interface{}, prefixField string) []Diff {
    		oldString := old.(time.Time).Format(time.RFC3339)
    		nowString := now.(time.Time).Format(time.RFC3339)
    		if oldString != nowString {
    			return []Diff{
    				{Field: prefixField, Old: oldString, Now: nowString},
    			}
    		}
    		return []Diff{}
      }
      ) // you define your own type handler to record some custom type for update operation
    
  • Record log manually when you use a normal model or save the model data via db directly

    • When a struct type only have one activity.ModelBuilder, you can use activity to record the log directly.

        activity.AddRecords(ActivityEdit, ctx, record)
        activity.AddRecords(ActivityCreate, ctx, record)
      
    • When a struct type have multiple activity.ModelBuilder, you need to get the corresponding activity.ModelBuilder and then use it to record the log.

        activity.MustGetModelBuilder(presetModel1).AddRecords(ActivityEdit, ctx, record)
        activity.MustGetModelBuilder(presetModel2).AddRecords(ActivityEdit, ctx, record)
      

Documentation

Index

Constants

View Source
const (
	Create = 1 << iota
	Delete
	Update
)
View Source
const (
	CreatorContextKey contextKey = iota
	DBContextKey
)
View Source
const (
	ActivityView   = "View"
	ActivityEdit   = "Edit"
	ActivityCreate = "Create"
	ActivityDelete = "Delete"
)
View Source
const (
	I18nActivityKey i18n.ModuleKey = "I18nActivityKey"
)

Variables

View Source
var (
	// @snippet_begin(ActivityDefaultIgnoredFields)
	DefaultIgnoredFields = []string{"ID", "UpdatedAt", "DeletedAt", "CreatedAt"}

	// @snippet_begin(ActivityDefaultTypeHandles)
	DefaultTypeHandles = map[reflect.Type]TypeHandler{
		reflect.TypeOf(time.Time{}): func(old, now interface{}, prefixField string) []Diff {
			oldString := old.(time.Time).Format(time.RFC3339)
			nowString := now.(time.Time).Format(time.RFC3339)
			if oldString != nowString {
				return []Diff{
					{Field: prefixField, Old: oldString, Now: nowString},
				}
			}
			return []Diff{}
		},
		reflect.TypeOf(media_library.MediaBox{}): func(old, now interface{}, prefixField string) (diffs []Diff) {
			oldMediaBox := old.(media_library.MediaBox)
			nowMediaBox := now.(media_library.MediaBox)

			if oldMediaBox.Url != nowMediaBox.Url {
				diffs = append(diffs, Diff{Field: formatFieldByDot(prefixField, "Url"), Old: oldMediaBox.Url, Now: nowMediaBox.Url})
			}

			if oldMediaBox.Description != nowMediaBox.Description {
				diffs = append(diffs, Diff{Field: formatFieldByDot(prefixField, "Description"), Old: oldMediaBox.Description, Now: nowMediaBox.Description})
			}

			if oldMediaBox.VideoLink != nowMediaBox.VideoLink {
				diffs = append(diffs, Diff{Field: formatFieldByDot(prefixField, "VideoLink"), Old: oldMediaBox.VideoLink, Now: nowMediaBox.VideoLink})
			}

			return diffs
		},
	}
)
View Source
var Messages_en_US = &Messages{
	Activities:   "Activities",
	ActionAll:    "All",
	ActionView:   "View",
	ActionEdit:   "Edit",
	ActionCreate: "Create",
	ActionDelete: "Delete",

	ModelUserID:    "Creator ID",
	ModelCreatedAt: "Date Time",
	ModelAction:    "Action",
	ModelCreator:   "Creator",
	ModelKeys:      "Keys",
	ModelName:      "Table Name",
	ModelLabel:     "Menu Name",
	ModelLink:      "Link",
	ModelDiffs:     "Diffs",

	FilterAction:    "Action",
	FilterCreatedAt: "Create Time",
	FilterCreator:   "Creator",
	FilterModel:     "Model Name",

	DiffDetail:  "Detail",
	DiffNew:     "New",
	DiffDelete:  "Delete",
	DiffChanges: "Changes",
	DiffField:   "Filed",
	DiffOld:     "Old",
	DiffNow:     "Now",
	DiffValue:   "Value",
}
View Source
var Messages_zh_CN = &Messages{
	Activities:   "活动",
	ActionAll:    "全部",
	ActionView:   "查看",
	ActionEdit:   "编辑",
	ActionCreate: "创建",
	ActionDelete: "删除",

	ModelUserID:    "操作者ID",
	ModelCreatedAt: "日期时间",
	ModelAction:    "操作",
	ModelCreator:   "操作者",
	ModelKeys:      "表的主键值",
	ModelName:      "表名",
	ModelLabel:     "菜单名",
	ModelLink:      "链接",
	ModelDiffs:     "差异",

	FilterAction:    "操作类型",
	FilterCreatedAt: "操作时间",
	FilterCreator:   "操作人",
	FilterModel:     "操作对象",
	DiffDetail:      "详情",
	DiffNew:         "新加",
	DiffDelete:      "删除",
	DiffChanges:     "修改",
	DiffField:       "字段",
	DiffOld:         "之前的值",
	DiffNow:         "当前的值",
	DiffValue:       "值",
}

Functions

func ContextWithCreator

func ContextWithCreator(ctx context.Context, name string) context.Context

func ContextWithDB

func ContextWithDB(ctx context.Context, db *gorm.DB) context.Context

func DiffComponent

func DiffComponent(diffstr string, req *http.Request) h.HTMLComponent

Types

type ActivityBuilder

type ActivityBuilder struct {
	// contains filtered or unexported fields
}

@snippet_begin(ActivityBuilder)

func New

func New(b *presets.Builder, db *gorm.DB, logModel ...ActivityLogInterface) *ActivityBuilder

func (*ActivityBuilder) AddCreateRecord

func (ab *ActivityBuilder) AddCreateRecord(creator interface{}, v interface{}, db *gorm.DB) error

AddCreateRecord add create record

func (*ActivityBuilder) AddCustomizedRecord

func (ab *ActivityBuilder) AddCustomizedRecord(action string, diff bool, ctx context.Context, obj interface{}) error

AddCustomizedRecord add customized record

func (*ActivityBuilder) AddDeleteRecord

func (ab *ActivityBuilder) AddDeleteRecord(creator interface{}, v interface{}, db *gorm.DB) error

AddDeleteRecord add delete record

func (*ActivityBuilder) AddEditRecord

func (ab *ActivityBuilder) AddEditRecord(creator interface{}, now interface{}, db *gorm.DB) error

AddEditRecord add edit record

func (*ActivityBuilder) AddEditRecordWithOld

func (ab *ActivityBuilder) AddEditRecordWithOld(creator interface{}, old, now interface{}, db *gorm.DB) error

AddEditRecord add edit record

func (*ActivityBuilder) AddEditRecordWithOldAndContext

func (ab *ActivityBuilder) AddEditRecordWithOldAndContext(ctx context.Context, old, now interface{}) error

AddEditRecordWithOldAndContext add edit record

func (*ActivityBuilder) AddRecords

func (ab *ActivityBuilder) AddRecords(action string, ctx context.Context, vs ...interface{}) error

AddRecords add records log

func (*ActivityBuilder) AddSaveRecord

func (ab *ActivityBuilder) AddSaveRecord(creator interface{}, now interface{}, db *gorm.DB) error

AddSaverRecord will save a create log or a edit log

func (*ActivityBuilder) AddViewRecord

func (ab *ActivityBuilder) AddViewRecord(creator interface{}, v interface{}, db *gorm.DB) error

AddViewRecord add view record

func (ActivityBuilder) GetActivityLogs

func (ab ActivityBuilder) GetActivityLogs(m interface{}, db *gorm.DB) []*ActivityLog

GetActivityLogs get activity logs

func (ActivityBuilder) GetCustomizeActivityLogs

func (ab ActivityBuilder) GetCustomizeActivityLogs(m interface{}, db *gorm.DB) interface{}

GetCustomizeActivityLogs get customize activity logs

func (ActivityBuilder) GetModelBuilder

func (ab ActivityBuilder) GetModelBuilder(v interface{}) (*ModelBuilder, bool)

GetModelBuilder get model builder

func (ActivityBuilder) GetModelBuilders

func (ab ActivityBuilder) GetModelBuilders() []*ModelBuilder

GetModelBuilders get all model builders

func (ActivityBuilder) GetPresetModelBuilder

func (ab ActivityBuilder) GetPresetModelBuilder() *presets.ModelBuilder

GetPresetModelBuilder return the preset model builder

func (ActivityBuilder) MustGetModelBuilder

func (ab ActivityBuilder) MustGetModelBuilder(v interface{}) *ModelBuilder

GetModelBuilder get model builder

func (ActivityBuilder) NewLogModelData

func (ab ActivityBuilder) NewLogModelData() interface{}

NewLogModelData new a log model data

func (ActivityBuilder) NewLogModelSlice

func (ab ActivityBuilder) NewLogModelSlice() interface{}

NewLogModelSlice new a log model slice

func (*ActivityBuilder) RegisterModel

func (ab *ActivityBuilder) RegisterModel(m interface{}) (mb *ModelBuilder)

Model register a model and return model builder

func (*ActivityBuilder) RegisterModels

func (ab *ActivityBuilder) RegisterModels(models ...interface{}) *ActivityBuilder

RegisterModels register mutiple models

func (*ActivityBuilder) SetCreatorContextKey

func (ab *ActivityBuilder) SetCreatorContextKey(key interface{}) *ActivityBuilder

SetCreatorContextKey change the default creator context key

func (*ActivityBuilder) SetDBContextKey

func (ab *ActivityBuilder) SetDBContextKey(key interface{}) *ActivityBuilder

SetDBContextKey change the default db context key

func (*ActivityBuilder) SetTabHeading

func (ab *ActivityBuilder) SetTabHeading(f func(log ActivityLogInterface) string) *ActivityBuilder

type ActivityLog

type ActivityLog struct {
	ID         uint `gorm:"primary_key"`
	UserID     uint
	CreatedAt  time.Time
	Creator    string
	Action     string
	ModelKeys  string `gorm:"index"`
	ModelName  string `gorm:"index"`
	ModelLabel string

	ModelLink  string
	ModelDiffs string `sql:"type:text;"`
}

func (*ActivityLog) GetAction

func (al *ActivityLog) GetAction() string

func (ActivityLog) GetCreatedAt

func (al ActivityLog) GetCreatedAt() time.Time

func (*ActivityLog) GetCreator

func (al *ActivityLog) GetCreator() string

func (*ActivityLog) GetModelDiffs

func (al *ActivityLog) GetModelDiffs() string

func (*ActivityLog) GetModelKeys

func (al *ActivityLog) GetModelKeys() string

func (*ActivityLog) GetModelLabel

func (al *ActivityLog) GetModelLabel() string
func (al *ActivityLog) GetModelLink() string

func (*ActivityLog) GetModelName

func (al *ActivityLog) GetModelName() string

func (ActivityLog) GetUserID

func (al ActivityLog) GetUserID() uint

func (*ActivityLog) SetAction

func (al *ActivityLog) SetAction(s string)

func (*ActivityLog) SetCreatedAt

func (al *ActivityLog) SetCreatedAt(t time.Time)

func (*ActivityLog) SetCreator

func (al *ActivityLog) SetCreator(s string)

func (*ActivityLog) SetModelDiffs

func (al *ActivityLog) SetModelDiffs(s string)

func (*ActivityLog) SetModelKeys

func (al *ActivityLog) SetModelKeys(s string)

func (*ActivityLog) SetModelLabel

func (al *ActivityLog) SetModelLabel(s string)
func (al *ActivityLog) SetModelLink(s string)

func (*ActivityLog) SetModelName

func (al *ActivityLog) SetModelName(s string)

func (*ActivityLog) SetUserID

func (al *ActivityLog) SetUserID(id uint)

type ActivityLogInterface

type ActivityLogInterface interface {
	SetCreatedAt(time.Time)
	GetCreatedAt() time.Time
	SetUserID(uint)
	GetUserID() uint
	SetCreator(string)
	GetCreator() string
	SetAction(string)
	GetAction() string
	SetModelKeys(string)
	GetModelKeys() string
	SetModelName(string)
	GetModelName() string
	SetModelLabel(string)
	GetModelLabel() string
	SetModelLink(string)
	GetModelLink() string
	SetModelDiffs(string)
	GetModelDiffs() string
}

type CreatorInterface

type CreatorInterface interface {
	GetID() uint
	GetName() string
}

type Diff

type Diff struct {
	Field string
	Old   string
	Now   string
}

type DiffBuilder

type DiffBuilder struct {
	// contains filtered or unexported fields
}

func NewDiffBuilder

func NewDiffBuilder(mb *ModelBuilder) *DiffBuilder

func (*DiffBuilder) Diff

func (db *DiffBuilder) Diff(old, now interface{}) ([]Diff, error)

type Messages

type Messages struct {
	Activities   string
	ActionAll    string
	ActionView   string
	ActionEdit   string
	ActionCreate string
	ActionDelete string

	ModelUserID    string
	ModelCreatedAt string
	ModelAction    string
	ModelCreator   string
	ModelKeys      string
	ModelName      string
	ModelLabel     string
	ModelLink      string
	ModelDiffs     string

	FilterAction    string
	FilterCreatedAt string
	FilterCreator   string
	FilterModel     string

	DiffDetail  string
	DiffNew     string
	DiffDelete  string
	DiffChanges string
	DiffField   string
	DiffOld     string
	DiffNow     string
	DiffValue   string
}

type ModelBuilder

type ModelBuilder struct {
	// contains filtered or unexported fields
}

@snippet_begin(ActivityModelBuilder) a unique model builder is consist of typ and presetModel

func (*ModelBuilder) AddCreateRecord

func (mb *ModelBuilder) AddCreateRecord(creator interface{}, v interface{}, db *gorm.DB) error

AddCreateRecord add create record

func (*ModelBuilder) AddCustomizedRecord

func (mb *ModelBuilder) AddCustomizedRecord(action string, diff bool, ctx context.Context, obj interface{}) error

AddCustomizedRecord add customized record

func (*ModelBuilder) AddDeleteRecord

func (mb *ModelBuilder) AddDeleteRecord(creator interface{}, v interface{}, db *gorm.DB) error

AddDeleteRecord add delete record

func (*ModelBuilder) AddEditRecord

func (mb *ModelBuilder) AddEditRecord(creator interface{}, now interface{}, db *gorm.DB) error

AddEditRecord add edit record

func (*ModelBuilder) AddEditRecordWithOld

func (mb *ModelBuilder) AddEditRecordWithOld(creator interface{}, old, now interface{}, db *gorm.DB) error

AddEditRecord add edit record

func (*ModelBuilder) AddIgnoredFields

func (mb *ModelBuilder) AddIgnoredFields(fields ...string) *ModelBuilder

AddIgnoredFields append ignored fields to the default ignored fields, this would not overwrite the default ignored fields

func (*ModelBuilder) AddKeys

func (mb *ModelBuilder) AddKeys(keys ...string) *ModelBuilder

AddKeys add keys to the model builder

func (*ModelBuilder) AddRecords

func (mb *ModelBuilder) AddRecords(action string, ctx context.Context, vs ...interface{}) error

AddRecords add records log

func (*ModelBuilder) AddSaveRecord

func (mb *ModelBuilder) AddSaveRecord(creator interface{}, now interface{}, db *gorm.DB) error

AddSaverRecord will save a create log or a edit log

func (*ModelBuilder) AddTypeHanders

func (mb *ModelBuilder) AddTypeHanders(v interface{}, f TypeHandler) *ModelBuilder

AddTypeHanders add type handers for the model builder

func (*ModelBuilder) AddViewRecord

func (mb *ModelBuilder) AddViewRecord(creator interface{}, v interface{}, db *gorm.DB) error

AddViewRecord add view record

func (*ModelBuilder) Diff

func (mb *ModelBuilder) Diff(old, now interface{}) ([]Diff, error)

Diff get diffs between old and now value

func (*ModelBuilder) EnableActivityInfoTab

func (mb *ModelBuilder) EnableActivityInfoTab() *ModelBuilder

EnableActivityInfoTab enable activity info tab on the given model's editing page

func (*ModelBuilder) GetType

func (mb *ModelBuilder) GetType() reflect.Type

GetType get ModelBuilder type

func (*ModelBuilder) KeysValue

func (mb *ModelBuilder) KeysValue(v interface{}) string

KeysValue get model keys value

func (*ModelBuilder) SetIgnoredFields

func (mb *ModelBuilder) SetIgnoredFields(fields ...string) *ModelBuilder

SetIgnoredFields set ignored fields to replace the default ignored fields with the new set.

func (*ModelBuilder) SetKeys

func (mb *ModelBuilder) SetKeys(keys ...string) *ModelBuilder

SetKeys set keys for the model builder

func (mb *ModelBuilder) SetLink(f func(interface{}) string) *ModelBuilder

SetLink set the link that linked to the modified record

func (*ModelBuilder) SkipCreate

func (mb *ModelBuilder) SkipCreate() *ModelBuilder

SkipCreate skip the create action for preset.ModelBuilder

func (*ModelBuilder) SkipDelete

func (mb *ModelBuilder) SkipDelete() *ModelBuilder

SkipDelete skip the delete action for preset.ModelBuilder

func (*ModelBuilder) SkipUpdate

func (mb *ModelBuilder) SkipUpdate() *ModelBuilder

SkipUpdate skip the update action for preset.ModelBuilder

type TypeHandler

type TypeHandler func(old, now interface{}, prefixField string) []Diff

@snippet_begin(ActivityTypeHandle)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL