* Extend Plugin API

* Use Error, NoError in tests

* Address suggestions

* Simplify code

* Add new line

* Add featureflag and GetCollectionMetadataByIds hook

* Add enter at the end of file

* make build-templates

* Fix test

* Fix GetCollectionMetadataByIds ret type

* Add GetTopicMetadataByIds hook

* Add log

* Extract i18n

* Add experimental notice on hooks

* Update model/feature_flags.go

* Swap user to userId

* Change userId to userID

Co-authored-by: Jesse Hallam <jesse.hallam@gmail.com>
Этот коммит содержится в:
Shota Gvinepadze
2022-11-03 22:43:30 +04:00
коммит произвёл GitHub
родитель cc69c917f2
Коммит b8da473da7
16 изменённых файлов: 847 добавлений и 3 удалений

Просмотреть файл

@@ -114,6 +114,30 @@ type OnCloudLimitsUpdatedIFace interface {
OnCloudLimitsUpdated(limits *model.ProductLimits)
}
type UserHasPermissionToCollectionIFace interface {
UserHasPermissionToCollection(c *Context, userID string, collectionType, collectionId string, permission *model.Permission) (bool, error)
}
type GetAllCollectionIDsForUserIFace interface {
GetAllCollectionIDsForUser(c *Context, userID, collectionType string) ([]string, error)
}
type GetAllUserIdsForCollectionIFace interface {
GetAllUserIdsForCollection(c *Context, collectionType, collectionID string) ([]string, error)
}
type GetTopicRedirectIFace interface {
GetTopicRedirect(c *Context, topicType, topicID string) (string, error)
}
type GetCollectionMetadataByIdsIFace interface {
GetCollectionMetadataByIds(c *Context, collectionType string, collectionIds []string) (map[string]*model.CollectionMetadata, error)
}
type GetTopicMetadataByIdsIFace interface {
GetTopicMetadataByIds(c *Context, topicType string, topicIds []string) (map[string]*model.TopicMetadata, error)
}
type hooksAdapter struct {
implemented map[int]struct{}
productHooks any
@@ -352,6 +376,60 @@ func newAdapter(productHooks any) (*hooksAdapter, error) {
return nil, errors.New("hook has OnCloudLimitsUpdated method but does not implement plugin.OnCloudLimitsUpdated interface")
}
// Assessing the type of the productHooks if it individually implements UserHasPermissionToCollection interface.
tt = reflect.TypeOf((*UserHasPermissionToCollectionIFace)(nil)).Elem()
if ft.Implements(tt) {
a.implemented[UserHasPermissionToCollectionID] = struct{}{}
} else if _, ok := ft.MethodByName("UserHasPermissionToCollection"); ok {
return nil, errors.New("hook has UserHasPermissionToCollection method but does not implement plugin.UserHasPermissionToCollection interface")
}
// Assessing the type of the productHooks if it individually implements GetAllCollectionIDsForUser interface.
tt = reflect.TypeOf((*GetAllCollectionIDsForUserIFace)(nil)).Elem()
if ft.Implements(tt) {
a.implemented[GetAllCollectionIDsForUserID] = struct{}{}
} else if _, ok := ft.MethodByName("GetAllCollectionIDsForUser"); ok {
return nil, errors.New("hook has GetAllCollectionIDsForUser method but does not implement plugin.GetAllCollectionIDsForUser interface")
}
// Assessing the type of the productHooks if it individually implements GetAllUserIdsForCollection interface.
tt = reflect.TypeOf((*GetAllUserIdsForCollectionIFace)(nil)).Elem()
if ft.Implements(tt) {
a.implemented[GetAllUserIdsForCollectionID] = struct{}{}
} else if _, ok := ft.MethodByName("GetAllUserIdsForCollection"); ok {
return nil, errors.New("hook has GetAllUserIdsForCollection method but does not implement plugin.GetAllUserIdsForCollection interface")
}
// Assessing the type of the productHooks if it individually implements GetTopicRedirect interface.
tt = reflect.TypeOf((*GetTopicRedirectIFace)(nil)).Elem()
if ft.Implements(tt) {
a.implemented[GetTopicRedirectID] = struct{}{}
} else if _, ok := ft.MethodByName("GetTopicRedirect"); ok {
return nil, errors.New("hook has GetTopicRedirect method but does not implement plugin.GetTopicRedirect interface")
}
// Assessing the type of the productHooks if it individually implements GetCollectionMetadataByIds interface.
tt = reflect.TypeOf((*GetCollectionMetadataByIdsIFace)(nil)).Elem()
if ft.Implements(tt) {
a.implemented[GetCollectionMetadataByIdsID] = struct{}{}
} else if _, ok := ft.MethodByName("GetCollectionMetadataByIds"); ok {
return nil, errors.New("hook has GetCollectionMetadataByIds method but does not implement plugin.GetCollectionMetadataByIds interface")
}
// Assessing the type of the productHooks if it individually implements GetTopicMetadataByIds interface.
tt = reflect.TypeOf((*GetTopicMetadataByIdsIFace)(nil)).Elem()
if ft.Implements(tt) {
a.implemented[GetTopicMetadataByIdsID] = struct{}{}
} else if _, ok := ft.MethodByName("GetTopicMetadataByIds"); ok {
return nil, errors.New("hook has GetTopicMetadataByIds method but does not implement plugin.GetTopicMetadataByIds interface")
}
return a, nil
}
@@ -579,3 +657,57 @@ func (a *hooksAdapter) OnCloudLimitsUpdated(limits *model.ProductLimits) {
a.productHooks.(OnCloudLimitsUpdatedIFace).OnCloudLimitsUpdated(limits)
}
func (a *hooksAdapter) UserHasPermissionToCollection(c *Context, userID string, collectionType, collectionId string, permission *model.Permission) (bool, error) {
if _, ok := a.implemented[UserHasPermissionToCollectionID]; !ok {
panic("product hooks must implement UserHasPermissionToCollection")
}
return a.productHooks.(UserHasPermissionToCollectionIFace).UserHasPermissionToCollection(c, userID, collectionType, collectionId, permission)
}
func (a *hooksAdapter) GetAllCollectionIDsForUser(c *Context, userID, collectionType string) ([]string, error) {
if _, ok := a.implemented[GetAllCollectionIDsForUserID]; !ok {
panic("product hooks must implement GetAllCollectionIDsForUser")
}
return a.productHooks.(GetAllCollectionIDsForUserIFace).GetAllCollectionIDsForUser(c, userID, collectionType)
}
func (a *hooksAdapter) GetAllUserIdsForCollection(c *Context, collectionType, collectionID string) ([]string, error) {
if _, ok := a.implemented[GetAllUserIdsForCollectionID]; !ok {
panic("product hooks must implement GetAllUserIdsForCollection")
}
return a.productHooks.(GetAllUserIdsForCollectionIFace).GetAllUserIdsForCollection(c, collectionType, collectionID)
}
func (a *hooksAdapter) GetTopicRedirect(c *Context, topicType, topicID string) (string, error) {
if _, ok := a.implemented[GetTopicRedirectID]; !ok {
panic("product hooks must implement GetTopicRedirect")
}
return a.productHooks.(GetTopicRedirectIFace).GetTopicRedirect(c, topicType, topicID)
}
func (a *hooksAdapter) GetCollectionMetadataByIds(c *Context, collectionType string, collectionIds []string) (map[string]*model.CollectionMetadata, error) {
if _, ok := a.implemented[GetCollectionMetadataByIdsID]; !ok {
panic("product hooks must implement GetCollectionMetadataByIds")
}
return a.productHooks.(GetCollectionMetadataByIdsIFace).GetCollectionMetadataByIds(c, collectionType, collectionIds)
}
func (a *hooksAdapter) GetTopicMetadataByIds(c *Context, topicType string, topicIds []string) (map[string]*model.TopicMetadata, error) {
if _, ok := a.implemented[GetTopicMetadataByIdsID]; !ok {
panic("product hooks must implement GetTopicMetadataByIds")
}
return a.productHooks.(GetTopicMetadataByIdsIFace).GetTopicMetadataByIds(c, topicType, topicIds)
}