app: add channels wrapper (#19861)

This commit is contained in:
Ibrahim Serdar Acikgoz
2022-04-08 08:59:37 +03:00
committed by GitHub
parent 6da1d90c4a
commit 54cb6d889f
2 changed files with 39 additions and 3 deletions

View File

@@ -21,6 +21,24 @@ import (
"github.com/mattermost/mattermost-server/v6/utils"
)
type channelsWrapper struct {
srv *Server
}
func (s *channelsWrapper) GetDirectChannel(userID1, userID2 string) (*model.Channel, error) {
return s.srv.getDirectChannel(userID1, userID2)
}
// GetChannelByID gets a Channel by its ID.
func (s *channelsWrapper) GetChannelByID(channelID string) (*model.Channel, error) {
return s.srv.getChannel(channelID)
}
// GetChannelMember gets a channel member by userID.
func (s *channelsWrapper) GetChannelMember(channelID string, userID string) (*model.ChannelMember, error) {
return s.srv.getChannelMember(context.Background(), channelID, userID)
}
// DefaultChannelNames returns the list of system-wide default channel names.
//
// By default the list will be (not necessarily in this order):
@@ -1701,7 +1719,11 @@ func (a *App) PostUpdateChannelDisplayNameMessage(c *request.Context, userID str
}
func (a *App) GetChannel(channelID string) (*model.Channel, *model.AppError) {
channel, err := a.Srv().Store.Channel().Get(channelID, true)
return a.Srv().getChannel(channelID)
}
func (s *Server) getChannel(channelID string) (*model.Channel, *model.AppError) {
channel, err := s.Store.Channel().Get(channelID, true)
if err != nil {
var nfErr *store.ErrNotFound
switch {
@@ -1919,7 +1941,11 @@ func (a *App) GetPrivateChannelsForTeam(teamID string, offset int, limit int) (m
}
func (a *App) GetChannelMember(ctx context.Context, channelID string, userID string) (*model.ChannelMember, *model.AppError) {
channelMember, err := a.Srv().Store.Channel().GetMember(ctx, channelID, userID)
return a.Srv().getChannelMember(ctx, channelID, userID)
}
func (s *Server) getChannelMember(ctx context.Context, channelID string, userID string) (*model.ChannelMember, *model.AppError) {
channelMember, err := s.Store.Channel().GetMember(ctx, channelID, userID)
if err != nil {
var nfErr *store.ErrNotFound
switch {
@@ -3383,7 +3409,11 @@ func (a *App) GetMemberCountsByGroup(ctx context.Context, channelID string, incl
}
func (a *App) getDirectChannel(userID, otherUserID string) (*model.Channel, *model.AppError) {
channel, nErr := a.Srv().Store.Channel().GetByName("", model.GetDMNameFromIds(userID, otherUserID), true)
return a.Srv().getDirectChannel(userID, otherUserID)
}
func (s *Server) getDirectChannel(userID, otherUserID string) (*model.Channel, *model.AppError) {
channel, nErr := s.Store.Channel().GetByName("", model.GetDMNameFromIds(userID, otherUserID), true)
if nErr != nil {
var nfErr *store.ErrNotFound
if errors.As(nErr, &nfErr) {

View File

@@ -90,6 +90,7 @@ var SentryDSN = "placeholder_sentry_dsn"
type ServiceKey string
const (
ChannelKey ServiceKey = "channel"
ConfigKey ServiceKey = "config"
LicenseKey ServiceKey = "license"
FilestoreKey ServiceKey = "filestore"
@@ -363,6 +364,10 @@ func NewServer(options ...Option) (*Server, error) {
}
s.filestore = backend
channelWrapper := &channelsWrapper{
srv: s,
}
s.licenseWrapper = &licenseWrapper{
srv: s,
}
@@ -385,6 +390,7 @@ func NewServer(options ...Option) (*Server, error) {
}
serviceMap := map[ServiceKey]interface{}{
ChannelKey: channelWrapper,
ConfigKey: s.configStore,
LicenseKey: s.licenseWrapper,
FilestoreKey: s.filestore,