-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Mattermost Plugin): Configure notifications
- Loading branch information
1 parent
baf76ce
commit 88d5e62
Showing
8 changed files
with
156 additions
and
11 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
packages/server/graphql/private/mutations/loginMattermost.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import AuthToken from '../../../database/types/AuthToken' | ||
import getKysely from '../../../postgres/getKysely' | ||
import encodeAuthToken from '../../../utils/encodeAuthToken' | ||
import {MutationResolvers} from '../resolverTypes' | ||
|
||
const loginMattermost: MutationResolvers['loginMattermost'] = async ( | ||
_source, | ||
{email} | ||
) => { | ||
const pg = getKysely() | ||
const user = await pg | ||
.selectFrom('User') | ||
.selectAll() | ||
.where('email', '=', email) | ||
.executeTakeFirst() | ||
if (!user) { | ||
return {error: {message: 'Unknown user'}} | ||
} | ||
const {id: userId, tms} = user | ||
const authToken = new AuthToken({sub: userId, tms}) | ||
|
||
return { | ||
userId, | ||
authToken: encodeAuthToken(authToken), | ||
isNewUser: false | ||
} | ||
} | ||
|
||
export default loginMattermost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/server/graphql/public/mutations/linkNotificationChannel.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import {sql} from 'kysely' | ||
import IntegrationProviderId from '~/shared/gqlIds/IntegrationProviderId' | ||
import {OAuth2AuthorizeResponse} from '../../../integrations/OAuth2Manager' | ||
import GcalOAuth2Manager from '../../../integrations/gcal/GcalOAuth2Manager' | ||
import GitLabOAuth2Manager from '../../../integrations/gitlab/GitLabOAuth2Manager' | ||
import JiraServerOAuth1Manager, { | ||
OAuth1Auth | ||
} from '../../../integrations/jiraServer/JiraServerOAuth1Manager' | ||
import getKysely from '../../../postgres/getKysely' | ||
import {IntegrationProviderAzureDevOps} from '../../../postgres/queries/getIntegrationProvidersByIds' | ||
import AzureDevOpsServerManager from '../../../utils/AzureDevOpsServerManager' | ||
import {analytics} from '../../../utils/analytics/analytics' | ||
import {getUserId, isTeamMember} from '../../../utils/authorization' | ||
import standardError from '../../../utils/standardError' | ||
import updateRepoIntegrationsCacheByPerms from '../../queries/helpers/updateRepoIntegrationsCacheByPerms' | ||
import {MutationResolvers} from '../resolverTypes' | ||
import {link} from 'fs' | ||
|
||
interface OAuth2Auth { | ||
accessToken: string | ||
refreshToken: string | ||
scopes: string | ||
expiresAt?: Date | null | ||
} | ||
|
||
const linkNotificationChannel: MutationResolvers['linkNotificationChannel'] = async ( | ||
_source, | ||
{providerId, teamId, channelId}, | ||
context | ||
) => { | ||
const {authToken, dataLoader} = context | ||
const viewerId = getUserId(authToken) | ||
const pg = getKysely() | ||
|
||
//AUTH | ||
if (!isTeamMember(authToken, teamId)) { | ||
return standardError(new Error('Attempted teamId spoof'), {userId: viewerId}) | ||
} | ||
|
||
// VALIDATION | ||
const providerDbId = IntegrationProviderId.split(providerId) | ||
const integrationProvider = await dataLoader.get('integrationProviders').load(providerDbId) | ||
|
||
if (!integrationProvider) { | ||
return standardError( | ||
new Error(`Unable to find appropriate integration provider for providerId ${providerId}`), | ||
{ | ||
userId: viewerId | ||
} | ||
) | ||
} | ||
|
||
const {service} = integrationProvider | ||
if (service !== 'msTeams' && service !== 'mattermost') { | ||
return standardError( | ||
new Error(`Unsupported notification service`), {userId: viewerId, tags: {service}} | ||
) | ||
} | ||
|
||
// RESOLUTION | ||
await pg | ||
.insertInto('TeamNotificationSettings') | ||
.columns(['providerId', 'teamId', 'events', 'channelId']) | ||
.values(() => ({ | ||
providerId: providerDbId, | ||
teamId, | ||
events: sql`enum_range(NULL::"SlackNotificationEventEnum")`, | ||
channelId | ||
})) | ||
.onConflict((oc) => oc.doNothing()) | ||
.execute() | ||
|
||
const data = {userId: viewerId, teamId, service} | ||
return data | ||
} | ||
|
||
export default linkNotificationChannel |
2 changes: 1 addition & 1 deletion
2
packages/server/graphql/public/typeDefs/AddTeamMemberIntegrationAuthPayload.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
""" | ||
Return object for AddTeamMemberIntegrationAuthPayload | ||
Return object for addTeamMemberIntegrationAuth | ||
""" | ||
union AddTeamMemberIntegrationAuthPayload = ErrorPayload | AddTeamMemberIntegrationAuthSuccess |
4 changes: 4 additions & 0 deletions
4
packages/server/graphql/public/typeDefs/LinkNotificationChannelPayload.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
""" | ||
Return object for linkNotificationChannel | ||
""" | ||
union LinkNotificationChannelPayload = ErrorPayload | LinkNotificationChannelSuccess |
2 changes: 2 additions & 0 deletions
2
packages/server/graphql/public/typeDefs/LinkNotificationChannelSuccess.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
type LinkNotificationChannelSuccess { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters