MM-63285: Add property field methods to plugin API (#31035)

Co-authored-by: Claude <noreply@anthropic.com>
Этот коммит содержится в:
Julien Tant
2025-06-10 16:10:28 -07:00
коммит произвёл GitHub
родитель 81856f9e7d
Коммит 731bd7c414
13 изменённых файлов: 2675 добавлений и 0 удалений

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

@@ -25,6 +25,7 @@ type Client struct {
Mail MailService
Plugin PluginService
Post PostService
Property PropertyService
Session SessionService
Store *StoreService
System SystemService
@@ -55,6 +56,7 @@ func NewClient(api plugin.API, driver plugin.Driver) *Client {
Mail: MailService{api: api},
Plugin: PluginService{api: api},
Post: PostService{api: api},
Property: PropertyService{api: api},
Session: SessionService{api: api},
Store: &StoreService{
api: api,

158
server/public/pluginapi/property.go Обычный файл
Просмотреть файл

@@ -0,0 +1,158 @@
package pluginapi
import (
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/plugin"
)
// PropertyService exposes methods to manipulate property fields and values.
type PropertyService struct {
api plugin.API
}
// CreatePropertyField creates a new property field.
//
// Minimum server version: 10.10
func (p *PropertyService) CreatePropertyField(field *model.PropertyField) (*model.PropertyField, error) {
return p.api.CreatePropertyField(field)
}
// GetPropertyField gets a property field by groupID and fieldID.
//
// Minimum server version: 10.10
func (p *PropertyService) GetPropertyField(groupID, fieldID string) (*model.PropertyField, error) {
return p.api.GetPropertyField(groupID, fieldID)
}
// GetPropertyFields gets multiple property fields by groupID and a list of IDs.
//
// Minimum server version: 10.10
func (p *PropertyService) GetPropertyFields(groupID string, ids []string) ([]*model.PropertyField, error) {
return p.api.GetPropertyFields(groupID, ids)
}
// UpdatePropertyField updates an existing property field.
//
// Minimum server version: 10.10
func (p *PropertyService) UpdatePropertyField(groupID string, field *model.PropertyField) (*model.PropertyField, error) {
return p.api.UpdatePropertyField(groupID, field)
}
// DeletePropertyField deletes a property field (soft delete).
//
// Minimum server version: 10.10
func (p *PropertyService) DeletePropertyField(groupID, fieldID string) error {
return p.api.DeletePropertyField(groupID, fieldID)
}
// SearchPropertyFields searches for property fields with filtering options.
//
// Minimum server version: 10.10
func (p *PropertyService) SearchPropertyFields(groupID, targetID string, opts model.PropertyFieldSearchOpts) ([]*model.PropertyField, error) {
return p.api.SearchPropertyFields(groupID, targetID, opts)
}
// CreatePropertyValue creates a new property value.
//
// Minimum server version: 10.10
func (p *PropertyService) CreatePropertyValue(value *model.PropertyValue) (*model.PropertyValue, error) {
return p.api.CreatePropertyValue(value)
}
// GetPropertyValue gets a property value by groupID and valueID.
//
// Minimum server version: 10.10
func (p *PropertyService) GetPropertyValue(groupID, valueID string) (*model.PropertyValue, error) {
return p.api.GetPropertyValue(groupID, valueID)
}
// GetPropertyValues gets multiple property values by groupID and a list of IDs.
//
// Minimum server version: 10.10
func (p *PropertyService) GetPropertyValues(groupID string, ids []string) ([]*model.PropertyValue, error) {
return p.api.GetPropertyValues(groupID, ids)
}
// UpdatePropertyValue updates an existing property value.
//
// Minimum server version: 10.10
func (p *PropertyService) UpdatePropertyValue(groupID string, value *model.PropertyValue) (*model.PropertyValue, error) {
return p.api.UpdatePropertyValue(groupID, value)
}
// UpsertPropertyValue creates a new property value or updates it if it already exists.
//
// Minimum server version: 10.10
func (p *PropertyService) UpsertPropertyValue(value *model.PropertyValue) (*model.PropertyValue, error) {
return p.api.UpsertPropertyValue(value)
}
// DeletePropertyValue deletes a property value (soft delete).
//
// Minimum server version: 10.10
func (p *PropertyService) DeletePropertyValue(groupID, valueID string) error {
return p.api.DeletePropertyValue(groupID, valueID)
}
// SearchPropertyValues searches for property values with filtering options.
//
// Minimum server version: 10.10
func (p *PropertyService) SearchPropertyValues(groupID, targetID string, opts model.PropertyValueSearchOpts) ([]*model.PropertyValue, error) {
return p.api.SearchPropertyValues(groupID, targetID, opts)
}
// RegisterPropertyGroup registers a new property group.
//
// Minimum server version: 10.10
func (p *PropertyService) RegisterPropertyGroup(name string) (*model.PropertyGroup, error) {
return p.api.RegisterPropertyGroup(name)
}
// GetPropertyGroup gets a property group by name.
//
// Minimum server version: 10.10
func (p *PropertyService) GetPropertyGroup(name string) (*model.PropertyGroup, error) {
return p.api.GetPropertyGroup(name)
}
// GetPropertyFieldByName gets a property field by groupID, targetID and name.
//
// Minimum server version: 10.10
func (p *PropertyService) GetPropertyFieldByName(groupID, targetID, name string) (*model.PropertyField, error) {
return p.api.GetPropertyFieldByName(groupID, targetID, name)
}
// UpdatePropertyFields updates multiple property fields in a single operation.
//
// Minimum server version: 10.10
func (p *PropertyService) UpdatePropertyFields(groupID string, fields []*model.PropertyField) ([]*model.PropertyField, error) {
return p.api.UpdatePropertyFields(groupID, fields)
}
// UpdatePropertyValues updates multiple property values in a single operation.
//
// Minimum server version: 10.10
func (p *PropertyService) UpdatePropertyValues(groupID string, values []*model.PropertyValue) ([]*model.PropertyValue, error) {
return p.api.UpdatePropertyValues(groupID, values)
}
// UpsertPropertyValues creates or updates multiple property values in a single operation.
//
// Minimum server version: 10.10
func (p *PropertyService) UpsertPropertyValues(values []*model.PropertyValue) ([]*model.PropertyValue, error) {
return p.api.UpsertPropertyValues(values)
}
// DeletePropertyValuesForTarget deletes all property values for a specific target.
//
// Minimum server version: 10.10
func (p *PropertyService) DeletePropertyValuesForTarget(groupID, targetType, targetID string) error {
return p.api.DeletePropertyValuesForTarget(groupID, targetType, targetID)
}
// DeletePropertyValuesForField deletes all property values for a specific field.
//
// Minimum server version: 10.10
func (p *PropertyService) DeletePropertyValuesForField(groupID, fieldID string) error {
return p.api.DeletePropertyValuesForField(groupID, fieldID)
}

428
server/public/pluginapi/property_test.go Обычный файл
Просмотреть файл

@@ -0,0 +1,428 @@
package pluginapi
import (
"encoding/json"
"testing"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/plugin/plugintest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPropertyFieldAPI(t *testing.T) {
t.Run("CreatePropertyField", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
field := &model.PropertyField{
ID: "field1",
GroupID: "group1",
Name: "Test Field",
Type: model.PropertyFieldTypeText,
}
api.On("CreatePropertyField", field).Return(field, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.CreatePropertyField(field)
// Verify the results
assert.NoError(t, err)
assert.Equal(t, field, result)
api.AssertExpectations(t)
})
t.Run("GetPropertyField", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
field := &model.PropertyField{
ID: "field1",
GroupID: "group1",
Name: "Test Field",
Type: model.PropertyFieldTypeText,
}
api.On("GetPropertyField", "group1", "field1").Return(field, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.GetPropertyField("group1", "field1")
// Verify the results
assert.NoError(t, err)
assert.Equal(t, field, result)
api.AssertExpectations(t)
})
t.Run("GetPropertyFields", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
fields := []*model.PropertyField{
{
ID: "field1",
GroupID: "group1",
Name: "Test Field 1",
Type: model.PropertyFieldTypeText,
},
{
ID: "field2",
GroupID: "group1",
Name: "Test Field 2",
Type: model.PropertyFieldTypeSelect,
},
}
api.On("GetPropertyFields", "group1", []string{"field1", "field2"}).Return(fields, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.GetPropertyFields("group1", []string{"field1", "field2"})
// Verify the results
assert.NoError(t, err)
assert.Equal(t, fields, result)
api.AssertExpectations(t)
})
t.Run("UpdatePropertyField", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
field := &model.PropertyField{
ID: "field1",
GroupID: "group1",
Name: "Updated Field",
Type: model.PropertyFieldTypeText,
}
api.On("UpdatePropertyField", "group1", field).Return(field, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.UpdatePropertyField("group1", field)
// Verify the results
assert.NoError(t, err)
assert.Equal(t, field, result)
api.AssertExpectations(t)
})
t.Run("DeletePropertyField", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
api.On("DeletePropertyField", "group1", "field1").Return(nil)
// Create the client
client := NewClient(api, nil)
// Call the method
err := client.Property.DeletePropertyField("group1", "field1")
// Verify the results
assert.NoError(t, err)
api.AssertExpectations(t)
})
t.Run("SearchPropertyFields", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
opts := model.PropertyFieldSearchOpts{
PerPage: 10,
}
fields := []*model.PropertyField{
{
ID: "field1",
GroupID: "group1",
Name: "Test Field 1",
Type: model.PropertyFieldTypeText,
},
{
ID: "field2",
GroupID: "group1",
Name: "Test Field 2",
Type: model.PropertyFieldTypeSelect,
},
}
api.On("SearchPropertyFields", "group1", "target1", opts).Return(fields, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.SearchPropertyFields("group1", "target1", opts)
// Verify the results
require.NoError(t, err)
assert.Equal(t, fields, result)
api.AssertExpectations(t)
})
}
func TestPropertyValueAPI(t *testing.T) {
t.Run("CreatePropertyValue", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
value := &model.PropertyValue{
ID: "value1",
GroupID: "group1",
FieldID: "field1",
TargetID: "target1",
TargetType: "post",
Value: json.RawMessage(`"Test Value"`),
}
api.On("CreatePropertyValue", value).Return(value, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.CreatePropertyValue(value)
// Verify the results
assert.NoError(t, err)
assert.Equal(t, value, result)
api.AssertExpectations(t)
})
t.Run("GetPropertyValue", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
value := &model.PropertyValue{
ID: "value1",
GroupID: "group1",
FieldID: "field1",
TargetID: "target1",
TargetType: "post",
Value: json.RawMessage(`"Test Value"`),
}
api.On("GetPropertyValue", "group1", "value1").Return(value, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.GetPropertyValue("group1", "value1")
// Verify the results
assert.NoError(t, err)
assert.Equal(t, value, result)
api.AssertExpectations(t)
})
t.Run("GetPropertyValues", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
values := []*model.PropertyValue{
{
ID: "value1",
GroupID: "group1",
FieldID: "field1",
TargetID: "target1",
TargetType: "post",
Value: json.RawMessage(`"Test Value 1"`),
},
{
ID: "value2",
GroupID: "group1",
FieldID: "field2",
TargetID: "target1",
TargetType: "post",
Value: json.RawMessage(`"Test Value 2"`),
},
}
api.On("GetPropertyValues", "group1", []string{"value1", "value2"}).Return(values, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.GetPropertyValues("group1", []string{"value1", "value2"})
// Verify the results
assert.NoError(t, err)
assert.Equal(t, values, result)
api.AssertExpectations(t)
})
t.Run("UpdatePropertyValue", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
value := &model.PropertyValue{
ID: "value1",
GroupID: "group1",
FieldID: "field1",
TargetID: "target1",
TargetType: "post",
Value: json.RawMessage(`"Updated Value"`),
}
api.On("UpdatePropertyValue", "group1", value).Return(value, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.UpdatePropertyValue("group1", value)
// Verify the results
assert.NoError(t, err)
assert.Equal(t, value, result)
api.AssertExpectations(t)
})
t.Run("UpsertPropertyValue", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
value := &model.PropertyValue{
ID: "value1",
GroupID: "group1",
FieldID: "field1",
TargetID: "target1",
TargetType: "post",
Value: json.RawMessage(`"Upsert Value"`),
}
api.On("UpsertPropertyValue", value).Return(value, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.UpsertPropertyValue(value)
// Verify the results
assert.NoError(t, err)
assert.Equal(t, value, result)
api.AssertExpectations(t)
})
t.Run("DeletePropertyValue", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
api.On("DeletePropertyValue", "group1", "value1").Return(nil)
// Create the client
client := NewClient(api, nil)
// Call the method
err := client.Property.DeletePropertyValue("group1", "value1")
// Verify the results
assert.NoError(t, err)
api.AssertExpectations(t)
})
t.Run("SearchPropertyValues", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
opts := model.PropertyValueSearchOpts{
PerPage: 10,
}
values := []*model.PropertyValue{
{
ID: "value1",
GroupID: "group1",
FieldID: "field1",
TargetID: "target1",
TargetType: "post",
Value: json.RawMessage(`"Test Value 1"`),
},
{
ID: "value2",
GroupID: "group1",
FieldID: "field2",
TargetID: "target1",
TargetType: "post",
Value: json.RawMessage(`"Test Value 2"`),
},
}
api.On("SearchPropertyValues", "group1", "target1", opts).Return(values, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.SearchPropertyValues("group1", "target1", opts)
// Verify the results
require.NoError(t, err)
assert.Equal(t, values, result)
api.AssertExpectations(t)
})
}
func TestPropertyGroupAPI(t *testing.T) {
t.Run("RegisterPropertyGroup", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
group := &model.PropertyGroup{
ID: "group1",
Name: "Test Group",
}
api.On("RegisterPropertyGroup", "Test Group").Return(group, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.RegisterPropertyGroup("Test Group")
// Verify the results
assert.NoError(t, err)
assert.Equal(t, group, result)
api.AssertExpectations(t)
})
t.Run("GetPropertyGroup", func(t *testing.T) {
// Setup
api := &plugintest.API{}
// Mock the API call
group := &model.PropertyGroup{
ID: "group1",
Name: "Test Group",
}
api.On("GetPropertyGroup", "Test Group").Return(group, nil)
// Create the client
client := NewClient(api, nil)
// Call the method
result, err := client.Property.GetPropertyGroup("Test Group")
// Verify the results
assert.NoError(t, err)
assert.Equal(t, group, result)
api.AssertExpectations(t)
})
}