Skip to content

Commit

Permalink
Merge branch 'main' into feat/preview-post
Browse files Browse the repository at this point in the history
  • Loading branch information
yurimutti authored Nov 16, 2024
2 parents 9a6ac0c + cdc3841 commit e9cfd02
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 73 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<a href="https://postiz.com" target="_blank">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/user-attachments/assets/765e9d72-3ee7-4a56-9d59-a2c9befe2311">
<img alt="Novu Logo" src="https://github.com/user-attachments/assets/f0d30d70-dddb-4142-8876-e9aa6ed1cb99" width="280"/>
<img alt="Postiz Logo" src="https://github.com/user-attachments/assets/f0d30d70-dddb-4142-8876-e9aa6ed1cb99" width="280"/>
</picture>
</a>
</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class IntegrationRepository {
createOrUpdateIntegration(
org: string,
name: string,
picture: string,
picture: string | undefined,
type: 'article' | 'social',
internalId: string,
provider: string,
Expand Down Expand Up @@ -98,7 +98,7 @@ export class IntegrationRepository {
providerIdentifier: provider,
token,
profile: username,
picture,
...(picture ? { picture } : {}),
inBetweenSteps: isBetweenSteps,
refreshToken,
...(expiresIn
Expand All @@ -117,7 +117,7 @@ export class IntegrationRepository {
inBetweenSteps: isBetweenSteps,
}
: {}),
picture,
...(picture ? { picture } : {}),
profile: username,
providerIdentifier: provider,
token,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {
HttpException,
HttpStatus,
Injectable,
Param,
Query,
} from '@nestjs/common';
import { IntegrationRepository } from '@gitroom/nestjs-libraries/database/prisma/integrations/integration.repository';
import { IntegrationManager } from '@gitroom/nestjs-libraries/integrations/integration.manager';
Expand All @@ -13,9 +11,6 @@ import { AnalyticsData, SocialProvider } from '@gitroom/nestjs-libraries/integra
import { Integration, Organization } from '@prisma/client';
import { NotificationService } from '@gitroom/nestjs-libraries/database/prisma/notifications/notification.service';
import { LinkedinPageProvider } from '@gitroom/nestjs-libraries/integrations/social/linkedin.page.provider';
import { simpleUpload } from '@gitroom/nestjs-libraries/upload/r2.uploader';
import axios from 'axios';
import { makeId } from '@gitroom/nestjs-libraries/services/make.is';
import dayjs from 'dayjs';
import { timer } from '@gitroom/helpers/utils/timer';
import { ioRedis } from '@gitroom/nestjs-libraries/redis/redis.service';
Expand All @@ -39,7 +34,7 @@ export class IntegrationService {
async createOrUpdateIntegration(
org: string,
name: string,
picture: string,
picture: string | undefined,
type: 'article' | 'social',
internalId: string,
provider: string,
Expand All @@ -52,7 +47,7 @@ export class IntegrationService {
timezone?: number,
customInstanceDetails?: string
) {
const uploadedPicture = await this.storage.uploadSimple(picture);
const uploadedPicture = picture ? await this.storage.uploadSimple(picture) : undefined;
return this._integrationRepository.createOrUpdateIntegration(
org,
name,
Expand Down Expand Up @@ -151,7 +146,7 @@ export class IntegrationService {
await this.createOrUpdateIntegration(
integration.organizationId,
integration.name,
integration.picture!,
undefined,
'social',
integration.internalId,
integration.providerIdentifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { makeId } from '@gitroom/nestjs-libraries/services/make.is';
import { timer } from '@gitroom/helpers/utils/timer';
import dayjs from 'dayjs';
import { SocialAbstract } from '@gitroom/nestjs-libraries/integrations/social.abstract';
import { string } from 'yup';

export class InstagramProvider
extends SocialAbstract
Expand Down
103 changes: 49 additions & 54 deletions libraries/nestjs-libraries/src/integrations/social/tiktok.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class TiktokProvider extends SocialAbstract implements SocialProvider {
},
} = await (
await fetch(
'https://open.tiktokapis.com/v2/user/info/?fields=open_id,avatar_url,display_name,username',
'https://open.tiktokapis.com/v2/user/info/?fields=open_id,avatar_url,display_name,union_id,username',
{
method: 'GET',
headers: {
Expand Down Expand Up @@ -102,10 +102,11 @@ export class TiktokProvider extends SocialAbstract implements SocialProvider {
code: params.code,
grant_type: 'authorization_code',
code_verifier: params.codeVerifier,
redirect_uri:
process.env.NODE_ENV === 'development' || !process.env.NODE_ENV
? `https://integration.git.sn/integrations/social/tiktok`
: `${process.env.FRONTEND_URL}/integrations/social/tiktok`,
redirect_uri: `${
process?.env?.FRONTEND_URL?.indexOf('https') === -1
? 'https://redirectmeto.com/'
: ''
}${process?.env?.FRONTEND_URL}/integrations/social/tiktok`
};

const { access_token, refresh_token, scope } = await (
Expand All @@ -118,6 +119,7 @@ export class TiktokProvider extends SocialAbstract implements SocialProvider {
})
).json();

console.log(this.scopes, scope);
this.checkScopes(this.scopes, scope);

const {
Expand Down Expand Up @@ -222,58 +224,51 @@ export class TiktokProvider extends SocialAbstract implements SocialProvider {
postDetails: PostDetails<TikTokDto>[],
integration: Integration
): Promise<PostResponse[]> {
try {
const [firstPost, ...comments] = postDetails;
const [firstPost, ...comments] = postDetails;

const {
data: { publish_id },
} = await (
await this.fetch(
'https://open.tiktokapis.com/v2/post/publish/video/init/',
{
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=UTF-8',
Authorization: `Bearer ${accessToken}`,
const {
data: { publish_id },
} = await (
await this.fetch(
'https://open.tiktokapis.com/v2/post/publish/video/init/',
{
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=UTF-8',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({
post_info: {
title: firstPost.message,
privacy_level: firstPost.settings.privacy_level,
disable_duet: !firstPost.settings.duet,
disable_comment: !firstPost.settings.comment,
disable_stitch: !firstPost.settings.stitch,
brand_content_toggle: firstPost.settings.brand_content_toggle,
brand_organic_toggle: firstPost.settings.brand_organic_toggle,
},
body: JSON.stringify({
post_info: {
title: firstPost.message,
privacy_level: firstPost.settings.privacy_level,
disable_duet: !firstPost.settings.duet,
disable_comment: !firstPost.settings.comment,
disable_stitch: !firstPost.settings.stitch,
brand_content_toggle: firstPost.settings.brand_content_toggle,
brand_organic_toggle: firstPost.settings.brand_organic_toggle,
},
source_info: {
source: 'PULL_FROM_URL',
video_url: firstPost?.media?.[0]?.url!,
},
}),
}
)
).json();
source_info: {
source: 'PULL_FROM_URL',
video_url: firstPost?.media?.[0]?.url!,
},
}),
}
)
).json();

const { url, id: videoId } = await this.uploadedVideoSuccess(
integration.profile!,
publish_id,
accessToken
);
const { url, id: videoId } = await this.uploadedVideoSuccess(
integration.profile!,
publish_id,
accessToken
);

return [
{
id: firstPost.id,
releaseURL: url,
postId: String(videoId),
status: 'success',
},
];
} catch (err) {
throw new BadBody('titok-error', JSON.stringify(err), {
// @ts-ignore
postDetails,
});
}
return [
{
id: firstPost.id,
releaseURL: url,
postId: String(videoId),
status: 'success',
},
];
}
}
24 changes: 18 additions & 6 deletions libraries/nestjs-libraries/src/upload/r2.uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import {
} from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { Request, Response } from 'express';
import crypto from 'crypto';
import path from 'path';
import { makeId } from '@gitroom/nestjs-libraries/services/make.is';

const { CLOUDFLARE_ACCOUNT_ID, CLOUDFLARE_ACCESS_KEY, CLOUDFLARE_SECRET_ACCESS_KEY, CLOUDFLARE_BUCKETNAME, CLOUDFLARE_BUCKET_URL } =
process.env;
Expand All @@ -16,12 +19,16 @@ const R2 = new S3Client({
},
});

// Function to generate a random string
function generateRandomString() {
return makeId(20);
}

export default async function handleR2Upload(
endpoint: string,
req: Request,
res: Response
) {

switch (endpoint) {
case 'create-multipart-upload':
return createMultipartUpload(req, res);
Expand All @@ -39,30 +46,35 @@ export default async function handleR2Upload(
return res.status(404).end();
}

export async function simpleUpload(data: Buffer, key: string, contentType: string) {
export async function simpleUpload(data: Buffer, originalFilename: string, contentType: string) {
const fileExtension = path.extname(originalFilename); // Extract extension
const randomFilename = generateRandomString() + fileExtension; // Append extension

const params = {
Bucket: CLOUDFLARE_BUCKETNAME,
Key: key,
Key: randomFilename,
Body: data,
ContentType: contentType,
};

const command = new PutObjectCommand({ ...params });
await R2.send(command);

return CLOUDFLARE_BUCKET_URL + '/' + key;
return CLOUDFLARE_BUCKET_URL + '/' + randomFilename;
}

export async function createMultipartUpload(
req: Request,
res: Response
) {
const { file, fileHash, contentType } = req.body;
const filename = file.name;
const fileExtension = path.extname(file.name); // Extract extension
const randomFilename = generateRandomString() + fileExtension; // Append extension

try {
const params = {
Bucket: CLOUDFLARE_BUCKETNAME,
Key: `resources/${fileHash}/${filename}`,
Key: `${randomFilename}`,
ContentType: contentType,
Metadata: {
'x-amz-meta-file-hash': fileHash,
Expand Down

0 comments on commit e9cfd02

Please sign in to comment.