From cf6aa85d28377160b109f6fb8ccd0d256efe6751 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Mon, 7 Mar 2022 20:14:53 +0530 Subject: [PATCH] MM-42268: We add more fields to Channel and Team (#19675) Also added sessions as part of the user object https://mattermost.atlassian.net/browse/MM-42268 ```release-note NONE ``` Co-authored-by: Mattermod --- api4/resolver_user.go | 24 ++++++++++++++++ api4/resolver_user_test.go | 57 ++++++++++++++++++++++++-------------- api4/schema.graphqls | 16 +++++++++++ model/channel.go | 8 ++++++ model/session.go | 8 ++++++ model/team.go | 13 +++++++++ 6 files changed, 105 insertions(+), 21 deletions(-) diff --git a/api4/resolver_user.go b/api4/resolver_user.go index cfe469116c..4572dc0074 100644 --- a/api4/resolver_user.go +++ b/api4/resolver_user.go @@ -116,3 +116,27 @@ func (u *user) Status(ctx context.Context) (*model.Status, error) { return statuses[0], nil } + +// match with api4.getSessions +func (u *user) Sessions(ctx context.Context) ([]*model.Session, error) { + c, err := getCtx(ctx) + if err != nil { + return nil, err + } + + if !c.App.SessionHasPermissionToUser(*c.AppContext.Session(), u.Id) { + c.SetPermissionError(model.PermissionEditOtherUsers) + return nil, c.Err + } + + sessions, appErr := c.App.GetSessions(u.Id) + if appErr != nil { + return nil, appErr + } + + for _, session := range sessions { + session.Sanitize() + } + + return sessions, nil +} diff --git a/api4/resolver_user_test.go b/api4/resolver_user_test.go index 43d52ccbda..1788da4152 100644 --- a/api4/resolver_user_test.go +++ b/api4/resolver_user_test.go @@ -56,6 +56,10 @@ func TestGraphQLUser(t *testing.T) { Name string `json:"name"` Value string `json:"value"` } `json:"preferences"` + Sessions []struct { + ID string `json:"id"` + CreateAt float64 `json:"createAt"` + } `json:"sessions"` } `json:"user"` } @@ -64,27 +68,31 @@ func TestGraphQLUser(t *testing.T) { OperationName: "user", Query: ` query user($id: String = "me") { - user(id: $id) { - id - username - email - firstName - lastName - isBot - isGuest - isSystemAdmin - timezone - props - notifyProps - roles { - id - name - } - preferences { - name - value - } - } + user(id: $id) { + id + username + email + firstName + lastName + isBot + isGuest + isSystemAdmin + timezone + props + notifyProps + roles { + id + name + } + preferences { + name + value + } + sessions { + id + createAt + } + } } `, } @@ -126,6 +134,13 @@ func TestGraphQLUser(t *testing.T) { assert.Equal(t, q.User.Preferences[i].Name, prefs[i].Name) assert.Equal(t, q.User.Preferences[i].Value, prefs[i].Value) } + + assert.Len(t, q.User.Sessions, 2) + now := float64(model.GetMillis()) + for _, session := range q.User.Sessions { + assert.NotEmpty(t, session.ID) + assert.Less(t, session.CreateAt, now) + } }) t.Run("Update", func(t *testing.T) { diff --git a/api4/schema.graphqls b/api4/schema.graphqls index c01f6cdd7b..e4f2c0be0b 100644 --- a/api4/schema.graphqls +++ b/api4/schema.graphqls @@ -48,6 +48,10 @@ type Channel { creatorId: String! schemeId: String team: Team + groupConstrained: Boolean + shared: Boolean + lastPostAt: Float! + totalMsgCount: Float! cursor: String } @@ -91,6 +95,7 @@ type User { position: String! roles: [Role]! preferences: [Preference!]! + sessions: [Session]! } type CustomStatus { @@ -127,12 +132,16 @@ type Team { id: String! displayName : String! name : String! + updateAt : Float! description : String! email : String! type : String! companyName : String! allowedDomains : String! inviteId : String! + lastTeamIconUpdate: Float! + groupConstrained: Boolean + allowOpenInvite: Boolean! } type TeamMember { @@ -155,3 +164,10 @@ type SidebarCategory { collapsed: Boolean! channelIds: [String!]! } + +type Session { + id: String! + token: String! + createAt: Float! + expiresAt: Float! +} \ No newline at end of file diff --git a/model/channel.go b/model/channel.go index 2f353c7cc7..350a923017 100644 --- a/model/channel.go +++ b/model/channel.go @@ -178,6 +178,14 @@ func (o *Channel) DeleteAt_() float64 { return float64(o.DeleteAt) } +func (o *Channel) LastPostAt_() float64 { + return float64(o.LastPostAt) +} + +func (o *Channel) TotalMsgCount_() float64 { + return float64(o.TotalMsgCount) +} + func (o *Channel) DeepCopy() *Channel { copy := *o if copy.SchemeId != nil { diff --git a/model/session.go b/model/session.go index 36955583ef..c2194aefa6 100644 --- a/model/session.go +++ b/model/session.go @@ -218,3 +218,11 @@ func (s *Session) GetCSRF() string { return s.Props["csrf"] } + +func (s *Session) CreateAt_() float64 { + return float64(s.CreateAt) +} + +func (s *Session) ExpiresAt_() float64 { + return float64(s.ExpiresAt) +} diff --git a/model/team.go b/model/team.go index a5aa7f94be..7a21ffc790 100644 --- a/model/team.go +++ b/model/team.go @@ -251,3 +251,16 @@ func (o *Team) Patch(patch *TeamPatch) { func (o *Team) IsGroupConstrained() bool { return o.GroupConstrained != nil && *o.GroupConstrained } + +// The following are some GraphQL methods necessary to return the +// data in float64 type. The spec doesn't support 64 bit integers, +// so we have to pass the data in float64. The _ at the end is +// a hack to keep the attribute name same in GraphQL schema. + +func (o *Team) UpdateAt_() float64 { + return float64(o.UpdateAt) +} + +func (o *Team) LastTeamIconUpdate_() float64 { + return float64(o.LastTeamIconUpdate) +}