Merge branch 'feat/external-social-media'

This commit is contained in:
Nevo David 2024-10-05 12:11:15 +07:00
commit 5206ce16b6
13 changed files with 480 additions and 50 deletions

View File

@ -27,6 +27,7 @@ import { GetUserFromRequest } from '@gitroom/nestjs-libraries/user/user.from.req
import { NotEnoughScopesFilter } from '@gitroom/nestjs-libraries/integrations/integration.missing.scopes'; import { NotEnoughScopesFilter } from '@gitroom/nestjs-libraries/integrations/integration.missing.scopes';
import { PostsService } from '@gitroom/nestjs-libraries/database/prisma/posts/posts.service'; import { PostsService } from '@gitroom/nestjs-libraries/database/prisma/posts/posts.service';
import { IntegrationTimeDto } from '@gitroom/nestjs-libraries/dtos/integrations/integration.time.dto'; import { IntegrationTimeDto } from '@gitroom/nestjs-libraries/dtos/integrations/integration.time.dto';
import { AuthService } from '@gitroom/helpers/auth/auth.service';
@ApiTags('Integrations') @ApiTags('Integrations')
@Controller('/integrations') @Controller('/integrations')
@ -127,7 +128,8 @@ export class IntegrationsController {
@CheckPolicies([AuthorizationActions.Create, Sections.CHANNEL]) @CheckPolicies([AuthorizationActions.Create, Sections.CHANNEL])
async getIntegrationUrl( async getIntegrationUrl(
@Param('integration') integration: string, @Param('integration') integration: string,
@Query('refresh') refresh: string @Query('refresh') refresh: string,
@Query('externalUrl') externalUrl: string
) { ) {
if ( if (
!this._integrationManager !this._integrationManager
@ -139,11 +141,33 @@ export class IntegrationsController {
const integrationProvider = const integrationProvider =
this._integrationManager.getSocialIntegration(integration); this._integrationManager.getSocialIntegration(integration);
const { codeVerifier, state, url } =
await integrationProvider.generateAuthUrl(refresh);
await ioRedis.set(`login:${state}`, codeVerifier, 'EX', 300);
return { url }; if (integrationProvider.externalUrl && !externalUrl) {
throw new Error('Missing external url');
}
try {
const getExternalUrl = integrationProvider.externalUrl
? {
...(await integrationProvider.externalUrl(externalUrl)),
instanceUrl: externalUrl,
}
: undefined;
const { codeVerifier, state, url } =
await integrationProvider.generateAuthUrl(refresh, getExternalUrl);
await ioRedis.set(`login:${state}`, codeVerifier, 'EX', 300);
await ioRedis.set(
`external:${state}`,
JSON.stringify(getExternalUrl),
'EX',
300
);
return { url };
} catch (err) {
return { err: true };
}
} }
@Post('/:id/time') @Post('/:id/time')
@ -273,6 +297,15 @@ export class IntegrationsController {
const integrationProvider = const integrationProvider =
this._integrationManager.getSocialIntegration(integration); this._integrationManager.getSocialIntegration(integration);
const details = integrationProvider.externalUrl
? await ioRedis.get(`external:${body.state}`)
: undefined;
if (details) {
await ioRedis.del(`external:${body.state}`);
}
const { const {
accessToken, accessToken,
expiresIn, expiresIn,
@ -281,11 +314,14 @@ export class IntegrationsController {
name, name,
picture, picture,
username, username,
} = await integrationProvider.authenticate({ } = await integrationProvider.authenticate(
code: body.code, {
codeVerifier: getCodeVerifier, code: body.code,
refresh: body.refresh, codeVerifier: getCodeVerifier,
}); refresh: body.refresh,
},
details ? JSON.parse(details) : undefined
);
if (!id) { if (!id) {
throw new Error('Invalid api key'); throw new Error('Invalid api key');
@ -304,7 +340,8 @@ export class IntegrationsController {
username, username,
integrationProvider.isBetweenSteps, integrationProvider.isBetweenSteps,
body.refresh, body.refresh,
+body.timezone +body.timezone,
AuthService.fixedEncryption(details)
); );
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

@ -11,6 +11,7 @@ import { ApiKeyDto } from '@gitroom/nestjs-libraries/dtos/integrations/api.key.d
import { useRouter } from 'next/navigation'; import { useRouter } from 'next/navigation';
import { TopTitle } from '@gitroom/frontend/components/launches/helpers/top.title.component'; import { TopTitle } from '@gitroom/frontend/components/launches/helpers/top.title.component';
import { useVariables } from '@gitroom/react/helpers/variable.context'; import { useVariables } from '@gitroom/react/helpers/variable.context';
import { useToaster } from '@gitroom/react/toaster/toaster';
const resolver = classValidatorResolver(ApiKeyDto); const resolver = classValidatorResolver(ApiKeyDto);
@ -127,23 +128,107 @@ export const ApiModal: FC<{
</div> </div>
); );
}; };
export const UrlModal: FC<{
gotoUrl(url: string): void;
}> = (props) => {
const { gotoUrl } = props;
const methods = useForm({
mode: 'onChange',
});
const submit = useCallback(async (data: FieldValues) => {
gotoUrl(data.url);
}, []);
return (
<div className="rounded-[4px] border border-customColor6 bg-sixth px-[16px] pb-[16px] relative">
<TopTitle title={`Instance URL`} />
<button
onClick={close}
className="outline-none absolute right-[20px] top-[20px] mantine-UnstyledButton-root mantine-ActionIcon-root hover:bg-tableBorder cursor-pointer mantine-Modal-close mantine-1dcetaa"
type="button"
>
<svg
viewBox="0 0 15 15"
fill="none"
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
>
<path
d="M11.7816 4.03157C12.0062 3.80702 12.0062 3.44295 11.7816 3.2184C11.5571 2.99385 11.193 2.99385 10.9685 3.2184L7.50005 6.68682L4.03164 3.2184C3.80708 2.99385 3.44301 2.99385 3.21846 3.2184C2.99391 3.44295 2.99391 3.80702 3.21846 4.03157L6.68688 7.49999L3.21846 10.9684C2.99391 11.193 2.99391 11.557 3.21846 11.7816C3.44301 12.0061 3.80708 12.0061 4.03164 11.7816L7.50005 8.31316L10.9685 11.7816C11.193 12.0061 11.5571 12.0061 11.7816 11.7816C12.0062 11.557 12.0062 11.193 11.7816 10.9684L8.31322 7.49999L11.7816 4.03157Z"
fill="currentColor"
fillRule="evenodd"
clipRule="evenodd"
></path>
</svg>
</button>
<FormProvider {...methods}>
<form
className="gap-[8px] flex flex-col"
onSubmit={methods.handleSubmit(submit)}
>
<div className="pt-[10px]">
<Input label="URL" name="url" />
</div>
<div>
<Button type="submit">Connect</Button>
</div>
</form>
</FormProvider>
</div>
);
};
export const AddProviderComponent: FC<{ export const AddProviderComponent: FC<{
social: Array<{ identifier: string; name: string }>; social: Array<{ identifier: string; name: string; isExternal: boolean }>;
article: Array<{ identifier: string; name: string }>; article: Array<{ identifier: string; name: string }>;
update?: () => void; update?: () => void;
}> = (props) => { }> = (props) => {
const { update } = props; const { update } = props;
const {isGeneral} = useVariables(); const { isGeneral } = useVariables();
const toaster = useToaster();
const fetch = useFetch(); const fetch = useFetch();
const modal = useModals(); const modal = useModals();
const { social, article } = props; const { social, article } = props;
const getSocialLink = useCallback( const getSocialLink = useCallback(
(identifier: string) => async () => { (identifier: string, isExternal: boolean) => async () => {
const { url } = await ( const gotoIntegration = async (externalUrl?: string) => {
await fetch('/integrations/social/' + identifier) const { url, err } = await (
).json(); await fetch(
window.location.href = url; `/integrations/social/${identifier}${
externalUrl ? `?externalUrl=${externalUrl}` : ``
}`
)
).json();
if (err) {
toaster.show('Could not connect to the platform', 'warning');
return ;
}
window.location.href = url;
};
if (isExternal) {
modal.closeAll();
modal.openModal({
title: '',
withCloseButton: false,
classNames: {
modal: 'bg-transparent text-textColor',
},
children: (
<UrlModal gotoUrl={gotoIntegration} />
),
});
return;
}
await gotoIntegration();
}, },
[] []
); );
@ -196,7 +281,7 @@ export const AddProviderComponent: FC<{
{social.map((item) => ( {social.map((item) => (
<div <div
key={item.identifier} key={item.identifier}
onClick={getSocialLink(item.identifier)} onClick={getSocialLink(item.identifier, item.isExternal)}
className={ className={
'w-[120px] h-[100px] bg-input text-textColor justify-center items-center flex flex-col gap-[10px] cursor-pointer' 'w-[120px] h-[100px] bg-input text-textColor justify-center items-center flex flex-col gap-[10px] cursor-pointer'
} }

View File

@ -0,0 +1,8 @@
import { FC } from 'react';
import { withProvider } from '@gitroom/frontend/components/launches/providers/high.order.provider';
const Empty: FC = (props) => {
return null;
};
export default withProvider(null, Empty, undefined, undefined);

View File

@ -15,6 +15,7 @@ import DribbbleProvider from '@gitroom/frontend/components/launches/providers/dr
import ThreadsProvider from '@gitroom/frontend/components/launches/providers/threads/threads.provider'; import ThreadsProvider from '@gitroom/frontend/components/launches/providers/threads/threads.provider';
import DiscordProvider from '@gitroom/frontend/components/launches/providers/discord/discord.provider'; import DiscordProvider from '@gitroom/frontend/components/launches/providers/discord/discord.provider';
import SlackProvider from '@gitroom/frontend/components/launches/providers/slack/slack.provider'; import SlackProvider from '@gitroom/frontend/components/launches/providers/slack/slack.provider';
import MastodonProvider from '@gitroom/frontend/components/launches/providers/mastodon/mastodon.provider';
export const Providers = [ export const Providers = [
{identifier: 'devto', component: DevtoProvider}, {identifier: 'devto', component: DevtoProvider},
@ -33,6 +34,7 @@ export const Providers = [
{identifier: 'threads', component: ThreadsProvider}, {identifier: 'threads', component: ThreadsProvider},
{identifier: 'discord', component: DiscordProvider}, {identifier: 'discord', component: DiscordProvider},
{identifier: 'slack', component: SlackProvider}, {identifier: 'slack', component: SlackProvider},
{identifier: 'mastodon', component: MastodonProvider},
]; ];

View File

@ -79,7 +79,8 @@ export class IntegrationRepository {
username?: string, username?: string,
isBetweenSteps = false, isBetweenSteps = false,
refresh?: string, refresh?: string,
timezone?: number timezone?: number,
customInstanceDetails?: string
) { ) {
const postTimes = timezone const postTimes = timezone
? { ? {
@ -113,6 +114,7 @@ export class IntegrationRepository {
...postTimes, ...postTimes,
organizationId: org, organizationId: org,
refreshNeeded: false, refreshNeeded: false,
...(customInstanceDetails ? { customInstanceDetails } : {}),
}, },
update: { update: {
type: type as any, type: type as any,

View File

@ -47,7 +47,8 @@ export class IntegrationService {
username?: string, username?: string,
isBetweenSteps = false, isBetweenSteps = false,
refresh?: string, refresh?: string,
timezone?: number timezone?: number,
customInstanceDetails?: string
) { ) {
const loadImage = await axios.get(picture, { responseType: 'arraybuffer' }); const loadImage = await axios.get(picture, { responseType: 'arraybuffer' });
const uploadedPicture = await simpleUpload( const uploadedPicture = await simpleUpload(
@ -69,7 +70,8 @@ export class IntegrationService {
username, username,
isBetweenSteps, isBetweenSteps,
refresh, refresh,
timezone timezone,
customInstanceDetails
); );
} }

View File

@ -242,27 +242,28 @@ model Subscription {
} }
model Integration { model Integration {
id String @id @default(cuid()) id String @id @default(cuid())
internalId String internalId String
organizationId String organizationId String
name String name String
organization Organization @relation(fields: [organizationId], references: [id]) organization Organization @relation(fields: [organizationId], references: [id])
picture String? picture String?
providerIdentifier String providerIdentifier String
type String type String
token String token String
disabled Boolean @default(false) disabled Boolean @default(false)
tokenExpiration DateTime? tokenExpiration DateTime?
refreshToken String? refreshToken String?
posts Post[] posts Post[]
profile String? profile String?
deletedAt DateTime? deletedAt DateTime?
createdAt DateTime @default(now()) createdAt DateTime @default(now())
updatedAt DateTime? @updatedAt updatedAt DateTime? @updatedAt
orderItems OrderItems[] orderItems OrderItems[]
inBetweenSteps Boolean @default(false) inBetweenSteps Boolean @default(false)
refreshNeeded Boolean @default(false) refreshNeeded Boolean @default(false)
postingTimes String @default("[{\"time\":120}, {\"time\":400}, {\"time\":700}]") postingTimes String @default("[{\"time\":120}, {\"time\":400}, {\"time\":700}]")
customInstanceDetails String?
@@index([updatedAt]) @@index([updatedAt])
@@index([deletedAt]) @@index([deletedAt])

View File

@ -17,8 +17,10 @@ import { LinkedinPageProvider } from '@gitroom/nestjs-libraries/integrations/soc
import { ThreadsProvider } from '@gitroom/nestjs-libraries/integrations/social/threads.provider'; import { ThreadsProvider } from '@gitroom/nestjs-libraries/integrations/social/threads.provider';
import { DiscordProvider } from '@gitroom/nestjs-libraries/integrations/social/discord.provider'; import { DiscordProvider } from '@gitroom/nestjs-libraries/integrations/social/discord.provider';
import { SlackProvider } from '@gitroom/nestjs-libraries/integrations/social/slack.provider'; import { SlackProvider } from '@gitroom/nestjs-libraries/integrations/social/slack.provider';
import { MastodonProvider } from '@gitroom/nestjs-libraries/integrations/social/mastodon.provider';
// import { MastodonCustomProvider } from '@gitroom/nestjs-libraries/integrations/social/mastodon.custom.provider';
const socialIntegrationList = [ const socialIntegrationList: SocialProvider[] = [
new XProvider(), new XProvider(),
new LinkedinProvider(), new LinkedinProvider(),
new LinkedinPageProvider(), new LinkedinPageProvider(),
@ -32,6 +34,8 @@ const socialIntegrationList = [
new DribbbleProvider(), new DribbbleProvider(),
new DiscordProvider(), new DiscordProvider(),
new SlackProvider(), new SlackProvider(),
new MastodonProvider(),
// new MastodonCustomProvider(),
]; ];
const articleIntegrationList = [ const articleIntegrationList = [
@ -47,6 +51,7 @@ export class IntegrationManager {
social: socialIntegrationList.map((p) => ({ social: socialIntegrationList.map((p) => ({
name: p.name, name: p.name,
identifier: p.identifier, identifier: p.identifier,
isExternal: !!p.externalUrl
})), })),
article: articleIntegrationList.map((p) => ({ article: articleIntegrationList.map((p) => ({
name: p.name, name: p.name,

View File

@ -0,0 +1,81 @@
import {
ClientInformation,
PostDetails,
PostResponse,
} from '@gitroom/nestjs-libraries/integrations/social/social.integrations.interface';
import { MastodonProvider } from '@gitroom/nestjs-libraries/integrations/social/mastodon.provider';
import { makeId } from '@gitroom/nestjs-libraries/services/make.is';
export class MastodonCustomProvider extends MastodonProvider {
override identifier = 'mastodon-custom';
override name = 'M. Instance';
async externalUrl(url: string) {
const form = new FormData();
form.append('client_name', 'Postiz');
form.append(
'redirect_uris',
`${process.env.FRONTEND_URL}/integrations/social/mastodon`
);
form.append('scopes', this.scopes.join(' '));
form.append('website', process.env.FRONTEND_URL!);
const { client_id, client_secret, ...all } = await (
await fetch(url + '/api/v1/apps', {
method: 'POST',
body: form,
})
).json();
return {
client_id,
client_secret,
};
}
override async generateAuthUrl(
refresh?: string,
external?: ClientInformation
) {
const state = makeId(6);
const url = this.generateUrlDynamic(
external?.instanceUrl!,
state,
external?.client_id!,
process.env.FRONTEND_URL!,
refresh
);
return {
url,
codeVerifier: makeId(10),
state,
};
}
override async authenticate(
params: {
code: string;
codeVerifier: string;
refresh?: string;
},
clientInformation?: ClientInformation
) {
return this.dynamicAuthenticate(
clientInformation?.client_id!,
clientInformation?.client_secret!,
clientInformation?.instanceUrl!,
params.code
);
}
override async post(
id: string,
accessToken: string,
postDetails: PostDetails[]
): Promise<PostResponse[]> {
return this.dynamicPost(
id,
accessToken,
'https://mastodon.social',
postDetails
);
}
}

View File

@ -0,0 +1,193 @@
import {
AuthTokenDetails,
ClientInformation,
PostDetails,
PostResponse,
SocialProvider,
} from '@gitroom/nestjs-libraries/integrations/social/social.integrations.interface';
import { makeId } from '@gitroom/nestjs-libraries/services/make.is';
import { SocialAbstract } from '@gitroom/nestjs-libraries/integrations/social.abstract';
import dayjs from 'dayjs';
export class MastodonProvider extends SocialAbstract implements SocialProvider {
identifier = 'mastodon';
name = 'Mastodon';
isBetweenSteps = false;
scopes = ['write:statuses', 'profile', 'write:media'];
async refreshToken(refreshToken: string): Promise<AuthTokenDetails> {
return {
refreshToken: '',
expiresIn: 0,
accessToken: '',
id: '',
name: '',
picture: '',
username: '',
};
}
protected generateUrlDynamic(
customUrl: string,
state: string,
clientId: string,
url: string,
refresh?: string
) {
return `${customUrl}/oauth/authorize?client_id=${clientId}&response_type=code&redirect_uri=${encodeURIComponent(
`${url}/integrations/social/mastodon${
refresh ? `?refresh=${refresh}` : ''
}`
)}&scope=${this.scopes.join('+')}&state=${state}`;
}
async generateAuthUrl(refresh?: string) {
const state = makeId(6);
const url = this.generateUrlDynamic(
'https://mastodon.social',
state,
process.env.MASTODON_CLIENT_ID!,
process.env.FRONTEND_URL!,
refresh
);
return {
url,
codeVerifier: makeId(10),
state,
};
}
protected async dynamicAuthenticate(
clientId: string,
clientSecret: string,
url: string,
code: string
) {
const form = new FormData();
form.append('client_id', clientId);
form.append('client_secret', clientSecret);
form.append('code', code);
form.append('grant_type', 'authorization_code');
form.append(
'redirect_uri',
`${process.env.FRONTEND_URL}/integrations/social/mastodon`
);
form.append('scope', this.scopes.join(' '));
const tokenInformation = await (
await this.fetch(`${url}/oauth/token`, {
method: 'POST',
body: form,
})
).json();
const personalInformation = await (
await this.fetch(`${url}/api/v1/accounts/verify_credentials`, {
headers: {
Authorization: `Bearer ${tokenInformation.access_token}`,
},
})
).json();
return {
id: personalInformation.id,
name: personalInformation.display_name || personalInformation.acct,
accessToken: tokenInformation.access_token,
refreshToken: 'null',
expiresIn: dayjs().add(100, 'years').unix() - dayjs().unix(),
picture: personalInformation.avatar,
username: personalInformation.username,
};
}
async authenticate(
params: {
code: string;
codeVerifier: string;
refresh?: string;
}
) {
return this.dynamicAuthenticate(
process.env.MASTODON_CLIENT_ID!,
process.env.MASTODON_CLIENT_SECRET!,
'https://mastodon.social',
params.code
);
}
async uploadFile(instanceUrl: string, fileUrl: string, accessToken: string) {
const form = new FormData();
form.append('file', await fetch(fileUrl).then((r) => r.blob()));
const media = await (
await this.fetch(`${instanceUrl}/api/v1/media`, {
method: 'POST',
headers: {
Authorization: `Bearer ${accessToken}`,
},
body: form,
})
).json();
return media.id;
}
async dynamicPost(
id: string,
accessToken: string,
url: string,
postDetails: PostDetails[]
): Promise<PostResponse[]> {
let loadId = '';
const ids = [] as string[];
for (const getPost of postDetails) {
const uploadFiles = await Promise.all(
getPost?.media?.map((media) =>
this.uploadFile(url, media.url, accessToken)
) || []
);
const form = new FormData();
form.append('status', getPost.message);
form.append('visibility', 'public');
if (loadId) {
form.append('in_reply_to_id', loadId);
}
if (uploadFiles.length) {
for (const file of uploadFiles) {
form.append('media_ids[]', file);
}
}
const post = await (
await this.fetch(`${url}/api/v1/statuses`, {
method: 'POST',
headers: {
Authorization: `Bearer ${accessToken}`,
},
body: form,
})
).json();
ids.push(post.id);
loadId = loadId || post.id;
}
return postDetails.map((p, i) => ({
id: p.id,
postId: ids[i],
releaseURL: `${url}/statuses/${ids[i]}`,
status: 'completed',
}));
}
async post(
id: string,
accessToken: string,
postDetails: PostDetails[]
): Promise<PostResponse[]> {
return this.dynamicPost(
id,
accessToken,
'https://mastodon.social',
postDetails
);
}
}

View File

@ -1,13 +1,24 @@
import { Integration } from '@prisma/client'; import { Integration } from '@prisma/client';
export interface ClientInformation {
client_id: string;
client_secret: string;
instanceUrl: string;
}
export interface IAuthenticator { export interface IAuthenticator {
authenticate(params: { authenticate(
code: string; params: {
codeVerifier: string; code: string;
refresh?: string; codeVerifier: string;
}): Promise<AuthTokenDetails>; refresh?: string;
},
clientInformation?: ClientInformation
): Promise<AuthTokenDetails>;
refreshToken(refreshToken: string): Promise<AuthTokenDetails>; refreshToken(refreshToken: string): Promise<AuthTokenDetails>;
generateAuthUrl(refresh?: string): Promise<GenerateAuthUrlResponse>; generateAuthUrl(
refresh?: string,
clientInformation?: ClientInformation
): Promise<GenerateAuthUrlResponse>;
analytics?( analytics?(
id: string, id: string,
accessToken: string, accessToken: string,
@ -90,4 +101,7 @@ export interface SocialProvider
name: string; name: string;
isBetweenSteps: boolean; isBetweenSteps: boolean;
scopes: string[]; scopes: string[];
externalUrl?: (
url: string
) => Promise<{ client_id: string; client_secret: string }>;
} }