Skip to content

Commit 54f86e7

Browse files
authoredSep 3, 2020
Migration of TeamStore Part 1 (mattermost#15246)
Automatic Merge
1 parent 953899a commit 54f86e7

29 files changed

+1131
-616
lines changed
 

‎api4/team_local.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99

1010
"github.com/mattermost/mattermost-server/v5/audit"
1111
"github.com/mattermost/mattermost-server/v5/model"
12+
"github.com/mattermost/mattermost-server/v5/store"
13+
14+
"github.com/pkg/errors"
1215
)
1316

1417
func (api *API) InitTeamLocal() {
@@ -89,9 +92,15 @@ func localInviteUsersToTeam(c *Context, w http.ResponseWriter, r *http.Request)
8992
auditRec.AddMeta("count", len(emailList))
9093
auditRec.AddMeta("emails", emailList)
9194

92-
team, err := c.App.Srv().Store.Team().Get(c.Params.TeamId)
93-
if err != nil {
94-
c.Err = err
95+
team, nErr := c.App.Srv().Store.Team().Get(c.Params.TeamId)
96+
if nErr != nil {
97+
var nfErr *store.ErrNotFound
98+
switch {
99+
case errors.As(nErr, &nfErr):
100+
c.Err = model.NewAppError("localInviteUsersToTeam", "app.team.get.find.app_error", nil, nfErr.Error(), http.StatusNotFound)
101+
default:
102+
c.Err = model.NewAppError("localInviteUsersToTeam", "app.team.get.finding.app_error", nil, nErr.Error(), http.StatusInternalServerError)
103+
}
95104
return
96105
}
97106

@@ -115,7 +124,7 @@ func localInviteUsersToTeam(c *Context, w http.ResponseWriter, r *http.Request)
115124
}
116125
auditRec.AddMeta("errors", errList)
117126
if len(goodEmails) > 0 {
118-
err = c.App.Srv().EmailService.SendInviteEmails(team, "Administrator", "mmctl "+model.NewId(), goodEmails, *c.App.Config().ServiceSettings.SiteURL)
127+
err := c.App.Srv().EmailService.SendInviteEmails(team, "Administrator", "mmctl "+model.NewId(), goodEmails, *c.App.Config().ServiceSettings.SiteURL)
119128
if err != nil {
120129
c.Err = err
121130
return
@@ -136,7 +145,7 @@ func localInviteUsersToTeam(c *Context, w http.ResponseWriter, r *http.Request)
136145
c.Err = model.NewAppError("localInviteUsersToTeam", "api.team.invite_members.invalid_email.app_error", map[string]interface{}{"Addresses": s}, "", http.StatusBadRequest)
137146
return
138147
}
139-
err = c.App.Srv().EmailService.SendInviteEmails(team, "Administrator", "mmctl "+model.NewId(), emailList, *c.App.Config().ServiceSettings.SiteURL)
148+
err := c.App.Srv().EmailService.SendInviteEmails(team, "Administrator", "mmctl "+model.NewId(), emailList, *c.App.Config().ServiceSettings.SiteURL)
140149
if err != nil {
141150
c.Err = err
142151
return

‎api4/team_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestCreateTeam(t *testing.T) {
4242

4343
rteam.Id = ""
4444
_, resp = client.CreateTeam(rteam)
45-
CheckErrorMessage(t, resp, "store.sql_team.save.domain_exists.app_error")
45+
CheckErrorMessage(t, resp, "app.team.save.existing.app_error")
4646
CheckBadRequestStatus(t, resp)
4747

4848
rteam.Name = ""

‎api4/user_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ func TestCreateUserWithInviteId(t *testing.T) {
495495

496496
_, resp := th.Client.CreateUserWithInviteId(&user, inviteId)
497497
CheckNotFoundStatus(t, resp)
498-
CheckErrorMessage(t, resp, "store.sql_team.get_by_invite_id.finding.app_error")
498+
CheckErrorMessage(t, resp, "app.team.get_by_invite_id.finding.app_error")
499499
})
500500

501501
t.Run("NoInviteId", func(t *testing.T) {
@@ -516,7 +516,7 @@ func TestCreateUserWithInviteId(t *testing.T) {
516516

517517
_, resp = th.Client.CreateUserWithInviteId(&user, inviteId)
518518
CheckNotFoundStatus(t, resp)
519-
CheckErrorMessage(t, resp, "store.sql_team.get_by_invite_id.finding.app_error")
519+
CheckErrorMessage(t, resp, "app.team.get_by_invite_id.finding.app_error")
520520
})
521521

522522
t.Run("EnableUserCreationDisable", func(t *testing.T) {

‎app/analytics.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (a *App) GetAnalytics(name string, teamId string) (model.AnalyticsRows, *mo
8787
teamCountChan := make(chan store.StoreResult, 1)
8888
go func() {
8989
teamCount, err2 := a.Srv().Store.Team().AnalyticsTeamCount(false)
90-
teamCountChan <- store.StoreResult{Data: teamCount, Err: err2}
90+
teamCountChan <- store.StoreResult{Data: teamCount, NErr: err2}
9191
close(teamCountChan)
9292
}()
9393

@@ -148,8 +148,8 @@ func (a *App) GetAnalytics(name string, teamId string) (model.AnalyticsRows, *mo
148148
}
149149

150150
r = <-teamCountChan
151-
if r.Err != nil {
152-
return nil, r.Err
151+
if r.NErr != nil {
152+
return nil, model.NewAppError("GetAnalytics", "app.team.analytics_team_count.app_error", nil, r.NErr.Error(), http.StatusInternalServerError)
153153
}
154154
rows[4].Value = float64(r.Data.(int64))
155155

‎app/channel.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -1528,8 +1528,13 @@ func (a *App) GetChannelByNameForTeamName(channelName, teamName string, includeD
15281528

15291529
team, err := a.Srv().Store.Team().GetByName(teamName)
15301530
if err != nil {
1531-
err.StatusCode = http.StatusNotFound
1532-
return nil, err
1531+
var nfErr *store.ErrNotFound
1532+
switch {
1533+
case errors.As(err, &nfErr):
1534+
return nil, model.NewAppError("GetChannelByNameForTeamName", "app.team.get_by_name.missing.app_error", nil, nfErr.Error(), http.StatusNotFound)
1535+
default:
1536+
return nil, model.NewAppError("GetChannelByNameForTeamName", "app.team.get_by_name.app_error", nil, err.Error(), http.StatusNotFound)
1537+
}
15331538
}
15341539

15351540
var result *model.Channel
@@ -2399,9 +2404,15 @@ func (a *App) MoveChannel(team *model.Team, channel *model.Channel, user *model.
23992404
}
24002405

24012406
// keep instance of the previous team
2402-
previousTeam, err := a.Srv().Store.Team().Get(channel.TeamId)
2403-
if err != nil {
2404-
return err
2407+
previousTeam, nErr := a.Srv().Store.Team().Get(channel.TeamId)
2408+
if nErr != nil {
2409+
var nfErr *store.ErrNotFound
2410+
switch {
2411+
case errors.As(nErr, &nfErr):
2412+
return model.NewAppError("MoveChannel", "app.team.get.find.app_error", nil, nfErr.Error(), http.StatusNotFound)
2413+
default:
2414+
return model.NewAppError("MoveChannel", "app.team.get.finding.app_error", nil, nErr.Error(), http.StatusInternalServerError)
2415+
}
24052416
}
24062417

24072418
if appErr := a.Srv().Store.Channel().UpdateSidebarChannelCategoryOnMove(channel, team.Id); appErr != nil {

‎app/command.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ func (a *App) tryExecuteCustomCommand(args *model.CommandArgs, trigger string, m
360360
teamChan := make(chan store.StoreResult, 1)
361361
go func() {
362362
team, err := a.Srv().Store.Team().Get(args.TeamId)
363-
teamChan <- store.StoreResult{Data: team, Err: err}
363+
teamChan <- store.StoreResult{Data: team, NErr: err}
364364
close(teamChan)
365365
}()
366366

@@ -377,8 +377,14 @@ func (a *App) tryExecuteCustomCommand(args *model.CommandArgs, trigger string, m
377377
}
378378

379379
tr := <-teamChan
380-
if tr.Err != nil {
381-
return nil, nil, tr.Err
380+
if tr.NErr != nil {
381+
var nfErr *store.ErrNotFound
382+
switch {
383+
case errors.As(tr.NErr, &nfErr):
384+
return nil, nil, model.NewAppError("tryExecuteCustomCommand", "app.team.get.find.app_error", nil, nfErr.Error(), http.StatusNotFound)
385+
default:
386+
return nil, nil, model.NewAppError("tryExecuteCustomCommand", "app.team.get.finding.app_error", nil, tr.NErr.Error(), http.StatusInternalServerError)
387+
}
382388
}
383389
team := tr.Data.(*model.Team)
384390

‎app/email_batching.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ func (job *EmailBatchingJob) checkPendingNotifications(now time.Time, handler fu
140140
continue
141141
}
142142

143-
team, err := job.server.Store.Team().GetByName(notifications[0].teamName)
144-
if err != nil {
145-
mlog.Error("Unable to find Team id for notification", mlog.Err(err))
143+
team, nErr := job.server.Store.Team().GetByName(notifications[0].teamName)
144+
if nErr != nil {
145+
mlog.Error("Unable to find Team id for notification", mlog.Err(nErr))
146146
continue
147147
}
148148

‎app/group.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,15 @@ func (a *App) UpsertGroupSyncable(groupSyncable *model.GroupSyncable) (*model.Gr
108108
}
109109

110110
var team *model.Team
111-
team, err = a.Srv().Store.Team().Get(channel.TeamId)
112-
if err != nil {
113-
return nil, err
111+
team, nErr = a.Srv().Store.Team().Get(channel.TeamId)
112+
if nErr != nil {
113+
var nfErr *store.ErrNotFound
114+
switch {
115+
case errors.As(nErr, &nfErr):
116+
return nil, model.NewAppError("UpsertGroupSyncable", "app.team.get.find.app_error", nil, nfErr.Error(), http.StatusNotFound)
117+
default:
118+
return nil, model.NewAppError("UpsertGroupSyncable", "app.team.get.finding.app_error", nil, nErr.Error(), http.StatusInternalServerError)
119+
}
114120
}
115121
if team.IsGroupConstrained() {
116122
var teamGroups []*model.GroupWithSchemeAdmin

‎app/integration_action.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (a *App) DoPostActionWithCookie(postId, actionId, userId, selectedOption st
140140

141141
action := post.GetAction(actionId)
142142
if action == nil || action.Integration == nil {
143-
return "", model.NewAppError("DoPostAction", "api.post.do_action.action_id.app_error", nil, fmt.Sprintf("action=%v", action), http.StatusNotFound)
143+
return "", model.NewAppError("DoPostActionWithCookie", "api.post.do_action.action_id.app_error", nil, fmt.Sprintf("action=%v", action), http.StatusNotFound)
144144
}
145145

146146
upstreamRequest.ChannelId = post.ChannelId
@@ -184,7 +184,7 @@ func (a *App) DoPostActionWithCookie(postId, actionId, userId, selectedOption st
184184
}
185185

186186
team, err := a.Srv().Store.Team().Get(upstreamRequest.TeamId)
187-
teamChan <- store.StoreResult{Data: team, Err: err}
187+
teamChan <- store.StoreResult{Data: team, NErr: err}
188188
}()
189189

190190
ur := <-userChan
@@ -196,8 +196,14 @@ func (a *App) DoPostActionWithCookie(postId, actionId, userId, selectedOption st
196196

197197
tr, ok := <-teamChan
198198
if ok {
199-
if tr.Err != nil {
200-
return "", tr.Err
199+
if tr.NErr != nil {
200+
var nfErr *store.ErrNotFound
201+
switch {
202+
case errors.As(tr.NErr, &nfErr):
203+
return "", model.NewAppError("DoPostActionWithCookie", "app.team.get.find.app_error", nil, nfErr.Error(), http.StatusNotFound)
204+
default:
205+
return "", model.NewAppError("DoPostActionWithCookie", "app.team.get.finding.app_error", nil, tr.NErr.Error(), http.StatusInternalServerError)
206+
}
201207
}
202208

203209
team := tr.Data.(*model.Team)
@@ -236,7 +242,7 @@ func (a *App) DoPostActionWithCookie(postId, actionId, userId, selectedOption st
236242

237243
var response model.PostActionIntegrationResponse
238244
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
239-
return "", model.NewAppError("DoPostAction", "api.post.do_action.action_integration.app_error", nil, "err="+err.Error(), http.StatusBadRequest)
245+
return "", model.NewAppError("DoPostActionWithCookie", "api.post.do_action.action_integration.app_error", nil, "err="+err.Error(), http.StatusBadRequest)
240246
}
241247

242248
if response.Update != nil {

‎app/notification_email.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"html"
99
"html/template"
10+
"net/http"
1011
"net/url"
1112
"path/filepath"
1213
"strings"
@@ -25,7 +26,7 @@ func (a *App) sendNotificationEmail(notification *PostNotification, user *model.
2526
if channel.IsGroupOrDirect() {
2627
teams, err := a.Srv().Store.Team().GetTeamsByUserId(user.Id)
2728
if err != nil {
28-
return err
29+
return model.NewAppError("sendNotificationEmail", "app.team.get_all.app_error", nil, err.Error(), http.StatusInternalServerError)
2930
}
3031

3132
// if the recipient isn't in the current user's team, just pick one

0 commit comments

Comments
 (0)
Please sign in to comment.