Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
312 lines
9.9 KiB
Go
312 lines
9.9 KiB
Go
package pluginapi
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/public/plugin"
|
|
)
|
|
|
|
// ChannelService exposes methods to manipulate channels.
|
|
type ChannelService struct {
|
|
api plugin.API
|
|
}
|
|
|
|
// Get gets a channel.
|
|
//
|
|
// Minimum server version: 5.2
|
|
func (c *ChannelService) Get(channelID string) (*model.Channel, error) {
|
|
channel, appErr := c.api.GetChannel(channelID)
|
|
|
|
return channel, normalizeAppErr(appErr)
|
|
}
|
|
|
|
// GetByName gets a channel by its name, given a team id.
|
|
//
|
|
// Minimum server version: 5.2
|
|
func (c *ChannelService) GetByName(teamID, channelName string, includeDeleted bool) (*model.Channel, error) {
|
|
channel, appErr := c.api.GetChannelByName(teamID, channelName, includeDeleted)
|
|
|
|
return channel, normalizeAppErr(appErr)
|
|
}
|
|
|
|
// GetDirect gets a direct message channel.
|
|
//
|
|
// Note that if the channel does not exist it will create it.
|
|
//
|
|
// Minimum server version: 5.2
|
|
func (c *ChannelService) GetDirect(userID1, userID2 string) (*model.Channel, error) {
|
|
channel, appErr := c.api.GetDirectChannel(userID1, userID2)
|
|
|
|
return channel, normalizeAppErr(appErr)
|
|
}
|
|
|
|
// GetGroup gets a group message channel.
|
|
//
|
|
// Note that if the channel does not exist it will create it.
|
|
//
|
|
// Minimum server version: 5.2
|
|
func (c *ChannelService) GetGroup(userIDs []string) (*model.Channel, error) {
|
|
channel, appErr := c.api.GetGroupChannel(userIDs)
|
|
|
|
return channel, normalizeAppErr(appErr)
|
|
}
|
|
|
|
// GetByNameForTeamName gets a channel by its name, given a team name.
|
|
//
|
|
// Minimum server version: 5.2
|
|
func (c *ChannelService) GetByNameForTeamName(teamName, channelName string, includeDeleted bool) (*model.Channel, error) {
|
|
channel, appErr := c.api.GetChannelByNameForTeamName(teamName, channelName, includeDeleted)
|
|
|
|
return channel, normalizeAppErr(appErr)
|
|
}
|
|
|
|
// ListForTeamForUser gets a list of channels for given user ID in given team ID.
|
|
//
|
|
// Minimum server version: 5.6
|
|
func (c *ChannelService) ListForTeamForUser(teamID, userID string, includeDeleted bool) ([]*model.Channel, error) {
|
|
channels, appErr := c.api.GetChannelsForTeamForUser(teamID, userID, includeDeleted)
|
|
|
|
return channels, normalizeAppErr(appErr)
|
|
}
|
|
|
|
// ListPublicChannelsForTeam gets a list of all channels.
|
|
//
|
|
// Minimum server version: 5.2
|
|
func (c *ChannelService) ListPublicChannelsForTeam(teamID string, page, perPage int) ([]*model.Channel, error) {
|
|
channels, appErr := c.api.GetPublicChannelsForTeam(teamID, page, perPage)
|
|
|
|
return channels, normalizeAppErr(appErr)
|
|
}
|
|
|
|
// Search returns the channels on a team matching the provided search term.
|
|
//
|
|
// Minimum server version: 5.6
|
|
func (c *ChannelService) Search(teamID, term string) ([]*model.Channel, error) {
|
|
channels, appErr := c.api.SearchChannels(teamID, term)
|
|
|
|
return channels, normalizeAppErr(appErr)
|
|
}
|
|
|
|
// Create creates a channel.
|
|
//
|
|
// Minimum server version: 5.2
|
|
func (c *ChannelService) Create(channel *model.Channel) error {
|
|
createdChannel, appErr := c.api.CreateChannel(channel)
|
|
if appErr != nil {
|
|
return normalizeAppErr(appErr)
|
|
}
|
|
|
|
*channel = *createdChannel
|
|
|
|
// The replica wait polls the generic GetChannel, which excludes opaque backing channel
|
|
// types, so it could never observe a space channel. Space channels are also resolved via
|
|
// GetChannelOfType, which reads from the master DB, so there is no replica lag to wait out.
|
|
if channel.IsSpace() {
|
|
return nil
|
|
}
|
|
|
|
return c.waitForChannelCreation(channel.Id)
|
|
}
|
|
|
|
// Update updates a channel.
|
|
//
|
|
// Minimum server version: 5.2
|
|
func (c *ChannelService) Update(channel *model.Channel) error {
|
|
updatedChannel, appErr := c.api.UpdateChannel(channel)
|
|
if appErr != nil {
|
|
return normalizeAppErr(appErr)
|
|
}
|
|
|
|
*channel = *updatedChannel
|
|
|
|
return nil
|
|
}
|
|
|
|
// Delete deletes a channel.
|
|
//
|
|
// Minimum server version: 5.2
|
|
func (c *ChannelService) Delete(channelID string) error {
|
|
return normalizeAppErr(c.api.DeleteChannel(channelID))
|
|
}
|
|
|
|
// Restore restores a previously deleted (archived) channel.
|
|
//
|
|
// Minimum server version: 11.10
|
|
func (c *ChannelService) Restore(channelID string) error {
|
|
return normalizeAppErr(c.api.RestoreChannel(channelID))
|
|
}
|
|
|
|
// GetChannelOfType resolves a channel by ID, requiring it to be of the given type. The generic
|
|
// Get excludes opaque backing channel types (e.g. space); a plugin that manages such a channel
|
|
// resolves it by its exact type here.
|
|
//
|
|
// Minimum server version: 11.10
|
|
func (c *ChannelService) GetChannelOfType(channelID string, channelType model.ChannelType) (*model.Channel, error) {
|
|
channel, appErr := c.api.GetChannelOfType(channelID, channelType)
|
|
|
|
return channel, normalizeAppErr(appErr)
|
|
}
|
|
|
|
// GetChannelStats gets statistics for a channel.
|
|
//
|
|
// Minimum server version: 5.6
|
|
func (c *ChannelService) GetChannelStats(channelID string) (*model.ChannelStats, error) {
|
|
channelStats, appErr := c.api.GetChannelStats(channelID)
|
|
|
|
return channelStats, normalizeAppErr(appErr)
|
|
}
|
|
|
|
// GetMember gets a channel membership for a user.
|
|
//
|
|
// Minimum server version: 5.2
|
|
func (c *ChannelService) GetMember(channelID, userID string) (*model.ChannelMember, error) {
|
|
channelMember, appErr := c.api.GetChannelMember(channelID, userID)
|
|
|
|
return channelMember, normalizeAppErr(appErr)
|
|
}
|
|
|
|
// ListMembers gets a channel membership for all users.
|
|
//
|
|
// Minimum server version: 5.6
|
|
func (c *ChannelService) ListMembers(channelID string, page, perPage int) ([]*model.ChannelMember, error) {
|
|
channelMembers, appErr := c.api.GetChannelMembers(channelID, page, perPage)
|
|
|
|
return channelMembersToChannelMemberSlice(channelMembers), normalizeAppErr(appErr)
|
|
}
|
|
|
|
// ListMembersByIDs gets a channel membership for a particular User
|
|
//
|
|
// Minimum server version: 5.6
|
|
func (c *ChannelService) ListMembersByIDs(channelID string, userIDs []string) ([]*model.ChannelMember, error) {
|
|
channelMembers, appErr := c.api.GetChannelMembersByIds(channelID, userIDs)
|
|
|
|
return channelMembersToChannelMemberSlice(channelMembers), normalizeAppErr(appErr)
|
|
}
|
|
|
|
// ListMembersForUser returns all channel memberships on a team for a user.
|
|
//
|
|
// Minimum server version: 5.10
|
|
func (c *ChannelService) ListMembersForUser(teamID, userID string, page, perPage int) ([]*model.ChannelMember, error) {
|
|
channelMembers, appErr := c.api.GetChannelMembersForUser(teamID, userID, page, perPage)
|
|
|
|
return channelMembers, normalizeAppErr(appErr)
|
|
}
|
|
|
|
// AddMember joins a user to a channel (as if they joined themselves).
|
|
// This means the user will not receive notifications for joining the channel.
|
|
//
|
|
// Minimum server version: 5.2
|
|
func (c *ChannelService) AddMember(channelID, userID string) (*model.ChannelMember, error) {
|
|
channelMember, appErr := c.api.AddChannelMember(channelID, userID)
|
|
|
|
return channelMember, normalizeAppErr(appErr)
|
|
}
|
|
|
|
// AddUser adds a user to a channel as if the specified user had invited them.
|
|
// This means the user will receive the regular notifications for being added to the channel.
|
|
//
|
|
// Minimum server version: 5.18
|
|
func (c *ChannelService) AddUser(channelID, userID, asUserID string) (*model.ChannelMember, error) {
|
|
channelMember, appErr := c.api.AddUserToChannel(channelID, userID, asUserID)
|
|
|
|
return channelMember, normalizeAppErr(appErr)
|
|
}
|
|
|
|
// DeleteMember deletes a channel membership for a user.
|
|
//
|
|
// Minimum server version: 5.2
|
|
func (c *ChannelService) DeleteMember(channelID, userID string) error {
|
|
appErr := c.api.DeleteChannelMember(channelID, userID)
|
|
|
|
return normalizeAppErr(appErr)
|
|
}
|
|
|
|
// UpdateChannelMemberRoles updates a user's roles for a channel.
|
|
//
|
|
// Minimum server version: 5.2
|
|
func (c *ChannelService) UpdateChannelMemberRoles(channelID, userID, newRoles string) (*model.ChannelMember, error) {
|
|
channelMember, appErr := c.api.UpdateChannelMemberRoles(channelID, userID, newRoles)
|
|
|
|
return channelMember, normalizeAppErr(appErr)
|
|
}
|
|
|
|
// UpdateChannelMemberNotifications updates a user's notification properties for a channel.
|
|
//
|
|
// Minimum server version: 5.2
|
|
func (c *ChannelService) UpdateChannelMemberNotifications(channelID, userID string, notifications map[string]string) (*model.ChannelMember, error) {
|
|
channelMember, appErr := c.api.UpdateChannelMemberNotifications(channelID, userID, notifications)
|
|
|
|
return channelMember, normalizeAppErr(appErr)
|
|
}
|
|
|
|
// CreateSidebarCategory creates a new sidebar category for a set of channels.
|
|
//
|
|
// Minimum server version: 5.38
|
|
func (c *ChannelService) CreateSidebarCategory(
|
|
userID, teamID string, newCategory *model.SidebarCategoryWithChannels) error {
|
|
category, appErr := c.api.CreateChannelSidebarCategory(userID, teamID, newCategory)
|
|
if appErr != nil {
|
|
return normalizeAppErr(appErr)
|
|
}
|
|
*newCategory = *category
|
|
|
|
return nil
|
|
}
|
|
|
|
// GetSidebarCategories returns sidebar categories.
|
|
//
|
|
// Minimum server version: 5.38
|
|
func (c *ChannelService) GetSidebarCategories(userID, teamID string) (*model.OrderedSidebarCategories, error) {
|
|
categories, appErr := c.api.GetChannelSidebarCategories(userID, teamID)
|
|
|
|
return categories, normalizeAppErr(appErr)
|
|
}
|
|
|
|
// UpdateSidebarCategories updates the channel sidebar categories.
|
|
//
|
|
// Minimum server version: 5.38
|
|
func (c *ChannelService) UpdateSidebarCategories(
|
|
userID, teamID string, categories []*model.SidebarCategoryWithChannels) error {
|
|
updatedCategories, appErr := c.api.UpdateChannelSidebarCategories(userID, teamID, categories)
|
|
if appErr != nil {
|
|
return normalizeAppErr(appErr)
|
|
}
|
|
copy(categories, updatedCategories)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *ChannelService) waitForChannelCreation(channelID string) error {
|
|
if len(c.api.GetConfig().SqlSettings.DataSourceReplicas) == 0 {
|
|
return nil
|
|
}
|
|
|
|
now := time.Now()
|
|
|
|
for time.Since(now) < 1500*time.Millisecond {
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
if _, err := c.api.GetChannel(channelID); err == nil {
|
|
// Channel found
|
|
return nil
|
|
} else if err.StatusCode != http.StatusNotFound {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return errors.Errorf("giving up waiting for channel creation, channelID=%s", channelID)
|
|
}
|
|
|
|
func channelMembersToChannelMemberSlice(cm model.ChannelMembers) []*model.ChannelMember {
|
|
cmp := make([]*model.ChannelMember, len(cm))
|
|
for i := range cm {
|
|
cmp[i] = &(cm)[i]
|
|
}
|
|
|
|
return cmp
|
|
}
|