Skip to content

Commit 0b69f65

Browse files
committed
feat: refresh channels on repost
1 parent b06ccaf commit 0b69f65

File tree

2 files changed

+99
-39
lines changed

2 files changed

+99
-39
lines changed

libraries/nestjs-libraries/src/database/prisma/integrations/integration.service.ts

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class IntegrationService {
3333
private _autopostsRepository: AutopostRepository,
3434
private _integrationManager: IntegrationManager,
3535
private _notificationService: NotificationService,
36-
private _workerServiceProducer: BullMqClient
36+
private _workerServiceProducer: BullMqClient,
3737
) {}
3838

3939
async changeActiveCron(orgId: string) {
@@ -460,15 +460,18 @@ export class IntegrationService {
460460
);
461461
}
462462

463-
async processInternalPlug(data: {
464-
post: string;
465-
originalIntegration: string;
466-
integration: string;
467-
plugName: string;
468-
orgId: string;
469-
delay: number;
470-
information: any;
471-
}) {
463+
async processInternalPlug(
464+
data: {
465+
post: string;
466+
originalIntegration: string;
467+
integration: string;
468+
plugName: string;
469+
orgId: string;
470+
delay: number;
471+
information: any;
472+
},
473+
forceRefresh = false
474+
): Promise<any> {
472475
const originalIntegration =
473476
await this._integrationRepository.getIntegrationById(
474477
data.orgId,
@@ -496,6 +499,63 @@ export class IntegrationService {
496499
getIntegration.providerIdentifier
497500
);
498501

502+
if (
503+
dayjs(getIntegration?.tokenExpiration).isBefore(dayjs()) ||
504+
forceRefresh
505+
) {
506+
const { accessToken, expiresIn, refreshToken, additionalSettings } =
507+
await new Promise<AuthTokenDetails>((res) => {
508+
getSocialIntegration
509+
.refreshToken(getIntegration.refreshToken!)
510+
.then((r) => res(r))
511+
.catch(() =>
512+
res({
513+
accessToken: '',
514+
expiresIn: 0,
515+
refreshToken: '',
516+
id: '',
517+
name: '',
518+
username: '',
519+
picture: '',
520+
additionalSettings: undefined,
521+
})
522+
);
523+
});
524+
525+
if (!accessToken) {
526+
await this.refreshNeeded(
527+
getIntegration.organizationId,
528+
getIntegration.id
529+
);
530+
531+
await this.informAboutRefreshError(
532+
getIntegration.organizationId,
533+
getIntegration
534+
);
535+
return {};
536+
}
537+
538+
await this.createOrUpdateIntegration(
539+
additionalSettings,
540+
!!getSocialIntegration.oneTimeToken,
541+
getIntegration.organizationId,
542+
getIntegration.name,
543+
getIntegration.picture!,
544+
'social',
545+
getIntegration.internalId,
546+
getIntegration.providerIdentifier,
547+
accessToken,
548+
refreshToken,
549+
expiresIn
550+
);
551+
552+
getIntegration.token = accessToken;
553+
554+
if (getSocialIntegration.refreshWait) {
555+
await timer(10000);
556+
}
557+
}
558+
499559
try {
500560
// @ts-ignore
501561
await getSocialIntegration?.[getAllInternalPlugs.methodName]?.(
@@ -505,6 +565,10 @@ export class IntegrationService {
505565
data.information
506566
);
507567
} catch (err) {
568+
if (err instanceof RefreshToken) {
569+
return this.processInternalPlug(data, true);
570+
}
571+
508572
return;
509573
}
510574
}

libraries/nestjs-libraries/src/integrations/social/linkedin.provider.ts

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -687,35 +687,31 @@ export class LinkedinProvider extends SocialAbstract implements SocialProvider {
687687
information: any,
688688
isPersonal = true
689689
) {
690-
try {
691-
await this.fetch(`https://api.linkedin.com/rest/posts`, {
692-
body: JSON.stringify({
693-
author:
694-
(isPersonal ? 'urn:li:person:' : `urn:li:organization:`) +
695-
`${integration.internalId}`,
696-
commentary: '',
697-
visibility: 'PUBLIC',
698-
distribution: {
699-
feedDistribution: 'MAIN_FEED',
700-
targetEntities: [],
701-
thirdPartyDistributionChannels: [],
702-
},
703-
lifecycleState: 'PUBLISHED',
704-
isReshareDisabledByAuthor: false,
705-
reshareContext: {
706-
parent: postId,
707-
},
708-
}),
709-
method: 'POST',
710-
headers: {
711-
'X-Restli-Protocol-Version': '2.0.0',
712-
'Content-Type': 'application/json',
713-
'LinkedIn-Version': '202504',
714-
Authorization: `Bearer ${integration.token}`,
690+
await this.fetch(`https://api.linkedin.com/rest/posts`, {
691+
body: JSON.stringify({
692+
author:
693+
(isPersonal ? 'urn:li:person:' : `urn:li:organization:`) +
694+
`${integration.internalId}`,
695+
commentary: '',
696+
visibility: 'PUBLIC',
697+
distribution: {
698+
feedDistribution: 'MAIN_FEED',
699+
targetEntities: [],
700+
thirdPartyDistributionChannels: [],
715701
},
716-
});
717-
} catch (err) {
718-
return;
719-
}
702+
lifecycleState: 'PUBLISHED',
703+
isReshareDisabledByAuthor: false,
704+
reshareContext: {
705+
parent: postId,
706+
},
707+
}),
708+
method: 'POST',
709+
headers: {
710+
'X-Restli-Protocol-Version': '2.0.0',
711+
'Content-Type': 'application/json',
712+
'LinkedIn-Version': '202504',
713+
Authorization: `Bearer ${integration.token}`,
714+
},
715+
});
720716
}
721717
}

0 commit comments

Comments
 (0)