MM-40817: Use license interface in Channels (#19678)

* MM-40817: Use license interface in Channels

https://mattermost.atlassian.net/browse/MM-40817

```release-note
NONE
```

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Agniva De Sarker
2022-03-10 09:00:29 +05:30
коммит произвёл GitHub
родитель 06c2aa70b0
Коммит 856f9b11af
19 изменённых файлов: 100 добавлений и 151 удалений

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

@@ -256,22 +256,7 @@ func (a *App) RequestLicenseAndAckWarnMetric(c *request.Context, warnMetricId st
return model.NewAppError("RequestLicenseAndAckWarnMetric", "api.license.request_trial_license.fail_get_user_count.app_error", nil, err.Error(), http.StatusBadRequest)
}
trialLicenseRequest := &model.TrialLicenseRequest{
ServerID: a.TelemetryId(),
Name: currentUser.GetDisplayName(model.ShowFullName),
Email: currentUser.Email,
SiteName: *a.Config().TeamSettings.SiteName,
SiteURL: *a.Config().ServiceSettings.SiteURL,
Users: int(registeredUsersCount),
TermsAccepted: true,
ReceiveEmailsAccepted: true,
}
if trialLicenseRequest.SiteURL == "" {
return model.NewAppError("RequestLicenseAndAckWarnMetric", "api.license.request_trial_license.no-site-url.app_error", nil, "", http.StatusBadRequest)
}
if err := a.Srv().RequestTrialLicense(trialLicenseRequest); err != nil {
if err := a.Channels().RequestTrialLicense(c.Session().UserId, int(registeredUsersCount), true, true); err != nil {
// turn off warn metric warning even in case of StartTrial failure
if nerr := a.setWarnMetricsStatusAndNotify(warnMetricId); nerr != nil {
return nerr

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

@@ -33,7 +33,7 @@ type configSvc interface {
// licenseSvc is added to act as a starting point for future integrated products.
// It has the same signature and functionality with the license related APIs of the plugin-api.
type licenseSvc interface { // nolint: unused,deadcode
type licenseSvc interface {
GetLicense() *model.License
RequestTrialLicense(requesterID string, users int, termsAccepted bool, receiveEmailsAccepted bool) *model.AppError
}
@@ -46,8 +46,9 @@ type namer interface {
// Channels contains all channels related state.
type Channels struct {
srv *Server
cfgSvc configSvc
srv *Server
cfgSvc configSvc
licenseSvc licenseSvc
postActionCookieSecret []byte
@@ -111,7 +112,10 @@ func NewChannels(s *Server, services map[ServiceKey]interface{}) (*Channels, err
// 2. Add the field to *Channels
// 3. Add the service key to the slice.
// 4. Add a new case in the switch statement.
requiredServices := []ServiceKey{ConfigKey}
requiredServices := []ServiceKey{
ConfigKey,
LicenseKey,
}
for _, svcKey := range requiredServices {
svc, ok := services[svcKey]
if !ok {
@@ -129,8 +133,17 @@ func NewChannels(s *Server, services map[ServiceKey]interface{}) (*Channels, err
return nil, errors.New("Config service does not contain Name method")
}
ch.cfgSvc = cfgSvc
case LicenseKey:
svc, ok := svc.(licenseSvc)
if !ok {
return nil, errors.New("License service did not satisfy licenseSvc interface")
}
_, ok = svc.(namer)
if !ok {
return nil, errors.New("License service does not contain Name method")
}
ch.licenseSvc = svc
}
}
// We are passing a partially filled Channels struct so that the enterprise
// methods can have access to app methods.
@@ -253,3 +266,12 @@ func (ch *Channels) AddConfigListener(listener func(*model.Config, *model.Config
func (ch *Channels) RemoveConfigListener(id string) {
ch.cfgSvc.RemoveConfigListener(id)
}
func (ch *Channels) License() *model.License {
return ch.licenseSvc.GetLicense()
}
func (ch *Channels) RequestTrialLicense(requesterID string, users int, termsAccepted bool, receiveEmailsAccepted bool) *model.AppError {
return ch.licenseSvc.RequestTrialLicense(requesterID, users, termsAccepted,
receiveEmailsAccepted)
}

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

@@ -1168,33 +1168,5 @@ func (api *PluginAPI) RequestTrialLicense(requesterID string, users int, termsAc
return model.NewAppError("RequestTrialLicense", "api.restricted_system_admin", nil, "", http.StatusForbidden)
}
if !termsAccepted {
return model.NewAppError("RequestTrialLicense", "api.license.request-trial.bad-request.terms-not-accepted", nil, "", http.StatusBadRequest)
}
if users == 0 {
return model.NewAppError("RequestTrialLicense", "api.license.request-trial.bad-request", nil, "", http.StatusBadRequest)
}
requester, err := api.app.GetUser(requesterID)
if err != nil {
return err
}
trialLicenseRequest := &model.TrialLicenseRequest{
ServerID: api.app.TelemetryId(),
Name: requester.GetDisplayName(model.ShowFullName),
Email: requester.Email,
SiteName: *api.app.Config().TeamSettings.SiteName,
SiteURL: *api.app.Config().ServiceSettings.SiteURL,
Users: users,
TermsAccepted: termsAccepted,
ReceiveEmailsAccepted: receiveEmailsAccepted,
}
if trialLicenseRequest.SiteURL == "" {
return model.NewAppError("RequestTrialLicense", "api.license.request_trial_license.no-site-url.app_error", nil, "", http.StatusBadRequest)
}
return api.app.Srv().RequestTrialLicense(trialLicenseRequest)
return api.app.Channels().RequestTrialLicense(requesterID, users, termsAccepted, receiveEmailsAccepted)
}

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

@@ -676,7 +676,7 @@ func (wc *WebConn) createHelloMessage() *model.WebSocketEvent {
msg.Add("server_version", fmt.Sprintf("%v.%v.%v.%v", model.CurrentVersion,
model.BuildNumber,
wc.App.ClientConfigHash(),
wc.App.Srv().License() != nil))
wc.App.Channels().License() != nil))
msg.Add("connection_id", wc.connectionID.Load())
return msg
}