Merge pull request #857 from gitroomhq/feat/videos

Generate video slides
This commit is contained in:
Nevo David 2025-07-06 19:18:21 +07:00 committed by GitHub
commit 29461017b8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
39 changed files with 6127 additions and 5560 deletions

View File

@ -12,14 +12,9 @@ import { GetOrgFromRequest } from '@gitroom/nestjs-libraries/user/org.from.reque
import { Organization } from '@prisma/client';
import { ApiTags } from '@nestjs/swagger';
import { CheckPolicies } from '@gitroom/backend/services/auth/permissions/permissions.ability';
import {
AuthorizationActions,
Sections,
} from '@gitroom/backend/services/auth/permissions/permissions.service';
import { AutopostService } from '@gitroom/nestjs-libraries/database/prisma/autopost/autopost.service';
import { AutopostDto } from '@gitroom/nestjs-libraries/dtos/autopost/autopost.dto';
import { XMLParser, XMLBuilder, XMLValidator } from 'fast-xml-parser';
import dayjs from 'dayjs';
import { AuthorizationActions, Sections } from '@gitroom/backend/services/auth/permissions/permission.exception.class';
@ApiTags('Autopost')
@Controller('/autopost')

View File

@ -1,4 +1,4 @@
import { Logger, Controller, Get, Post, Req, Res } from '@nestjs/common';
import { Logger, Controller, Get, Post, Req, Res, Query } from '@nestjs/common';
import {
CopilotRuntime,
OpenAIAdapter,
@ -39,7 +39,10 @@ export class CopilotController {
}
@Get('/credits')
calculateCredits(@GetOrgFromRequest() organization: Organization) {
return this._subscriptionService.checkCredits(organization);
calculateCredits(
@GetOrgFromRequest() organization: Organization,
@Query('type') type: 'ai_images' | 'ai_videos',
) {
return this._subscriptionService.checkCredits(organization, type || 'ai_images');
}
}

View File

@ -17,10 +17,6 @@ import { GetOrgFromRequest } from '@gitroom/nestjs-libraries/user/org.from.reque
import { Organization, User } from '@prisma/client';
import { ApiKeyDto } from '@gitroom/nestjs-libraries/dtos/integrations/api.key.dto';
import { IntegrationFunctionDto } from '@gitroom/nestjs-libraries/dtos/integrations/integration.function.dto';
import {
AuthorizationActions,
Sections,
} from '@gitroom/backend/services/auth/permissions/permissions.service';
import { CheckPolicies } from '@gitroom/backend/services/auth/permissions/permissions.ability';
import { pricing } from '@gitroom/nestjs-libraries/database/prisma/subscriptions/pricing';
import { ApiTags } from '@nestjs/swagger';
@ -37,6 +33,7 @@ import {
} from '@gitroom/nestjs-libraries/integrations/social.abstract';
import { timer } from '@gitroom/helpers/utils/timer';
import { TelegramProvider } from '@gitroom/nestjs-libraries/integrations/social/telegram.provider';
import { AuthorizationActions, Sections } from '@gitroom/backend/services/auth/permissions/permission.exception.class';
@ApiTags('Integrations')
@Controller('/integrations')

View File

@ -23,6 +23,7 @@ import { CustomFileValidationPipe } from '@gitroom/nestjs-libraries/upload/custo
import { SubscriptionService } from '@gitroom/nestjs-libraries/database/prisma/subscriptions/subscription.service';
import { UploadFactory } from '@gitroom/nestjs-libraries/upload/upload.factory';
import { SaveMediaInformationDto } from '@gitroom/nestjs-libraries/dtos/media/save.media.information.dto';
import { VideoDto } from '@gitroom/nestjs-libraries/dtos/videos/video.dto';
@ApiTags('Media')
@Controller('/media')
@ -108,10 +109,7 @@ export class MediaController {
@GetOrgFromRequest() org: Organization,
@Body() body: SaveMediaInformationDto
) {
return this._mediaService.saveMediaInformation(
org.id,
body
);
return this._mediaService.saveMediaInformation(org.id, body);
}
@Post('/upload-simple')
@ -167,4 +165,27 @@ export class MediaController {
) {
return this._mediaService.getMedia(org.id, page);
}
@Get('/video-options')
getVideos() {
return this._mediaService.getVideoOptions();
}
@Post('/video/:identifier/:function')
videoFunction(
@Param('identifier') identifier: string,
@Param('function') functionName: string,
@Body('params') body: any
) {
return this._mediaService.videoFunction(identifier, functionName, body);
}
@Post('/generate-video/:type')
generateVideo(
@GetOrgFromRequest() org: Organization,
@Body() body: VideoDto,
@Param('type') type: string
) {
return this._mediaService.generateVideo(org, body, type);
}
}

View File

@ -12,14 +12,9 @@ import {
import { PostsService } from '@gitroom/nestjs-libraries/database/prisma/posts/posts.service';
import { GetOrgFromRequest } from '@gitroom/nestjs-libraries/user/org.from.request';
import { Organization, User } from '@prisma/client';
import { CreatePostDto } from '@gitroom/nestjs-libraries/dtos/posts/create.post.dto';
import { GetPostsDto } from '@gitroom/nestjs-libraries/dtos/posts/get.posts.dto';
import { StarsService } from '@gitroom/nestjs-libraries/database/prisma/stars/stars.service';
import { CheckPolicies } from '@gitroom/backend/services/auth/permissions/permissions.ability';
import {
AuthorizationActions,
Sections,
} from '@gitroom/backend/services/auth/permissions/permissions.service';
import { ApiTags } from '@nestjs/swagger';
import { MessagesService } from '@gitroom/nestjs-libraries/database/prisma/marketplace/messages.service';
import { GeneratorDto } from '@gitroom/nestjs-libraries/dtos/generator/generator.dto';
@ -29,6 +24,7 @@ import { Response } from 'express';
import { GetUserFromRequest } from '@gitroom/nestjs-libraries/user/user.from.request';
import { ShortLinkService } from '@gitroom/nestjs-libraries/short-linking/short.link.service';
import { CreateTagDto } from '@gitroom/nestjs-libraries/dtos/posts/create.tag.dto';
import { AuthorizationActions, Sections } from '@gitroom/backend/services/auth/permissions/permission.exception.class';
@ApiTags('Posts')
@Controller('/posts')

View File

@ -11,11 +11,6 @@ import { GetOrgFromRequest } from '@gitroom/nestjs-libraries/user/org.from.reque
import { Organization } from '@prisma/client';
import { ApiTags } from '@nestjs/swagger';
import { SetsService } from '@gitroom/nestjs-libraries/database/prisma/sets/sets.service';
import { CheckPolicies } from '@gitroom/backend/services/auth/permissions/permissions.ability';
import {
AuthorizationActions,
Sections,
} from '@gitroom/backend/services/auth/permissions/permissions.service';
import {
UpdateSetsDto,
SetsDto,

View File

@ -3,13 +3,10 @@ import { GetOrgFromRequest } from '@gitroom/nestjs-libraries/user/org.from.reque
import { Organization } from '@prisma/client';
import { StarsService } from '@gitroom/nestjs-libraries/database/prisma/stars/stars.service';
import { CheckPolicies } from '@gitroom/backend/services/auth/permissions/permissions.ability';
import {
AuthorizationActions,
Sections,
} from '@gitroom/backend/services/auth/permissions/permissions.service';
import { OrganizationService } from '@gitroom/nestjs-libraries/database/prisma/organizations/organization.service';
import { AddTeamMemberDto } from '@gitroom/nestjs-libraries/dtos/settings/add.team.member.dto';
import { ApiTags } from '@nestjs/swagger';
import { AuthorizationActions, Sections } from '@gitroom/backend/services/auth/permissions/permission.exception.class';
@ApiTags('Settings')
@Controller('/settings')

View File

@ -17,10 +17,6 @@ import { Response, Request } from 'express';
import { AuthService } from '@gitroom/backend/services/auth/auth.service';
import { OrganizationService } from '@gitroom/nestjs-libraries/database/prisma/organizations/organization.service';
import { CheckPolicies } from '@gitroom/backend/services/auth/permissions/permissions.ability';
import {
AuthorizationActions,
Sections,
} from '@gitroom/backend/services/auth/permissions/permissions.service';
import { getCookieUrlFromDomain } from '@gitroom/helpers/subdomain/subdomain.management';
import { pricing } from '@gitroom/nestjs-libraries/database/prisma/subscriptions/pricing';
import { ApiTags } from '@nestjs/swagger';
@ -32,6 +28,7 @@ import { UserAgent } from '@gitroom/nestjs-libraries/user/user.agent';
import { TrackEnum } from '@gitroom/nestjs-libraries/user/track.enum';
import { TrackService } from '@gitroom/nestjs-libraries/track/track.service';
import { makeId } from '@gitroom/nestjs-libraries/services/make.is';
import { AuthorizationActions, Sections } from '@gitroom/backend/services/auth/permissions/permission.exception.class';
@ApiTags('User')
@Controller('/user')

View File

@ -13,14 +13,11 @@ import { Organization } from '@prisma/client';
import { ApiTags } from '@nestjs/swagger';
import { WebhooksService } from '@gitroom/nestjs-libraries/database/prisma/webhooks/webhooks.service';
import { CheckPolicies } from '@gitroom/backend/services/auth/permissions/permissions.ability';
import {
AuthorizationActions,
Sections,
} from '@gitroom/backend/services/auth/permissions/permissions.service';
import {
UpdateDto,
WebhooksDto,
} from '@gitroom/nestjs-libraries/dtos/webhooks/webhooks.dto';
import { AuthorizationActions, Sections } from '@gitroom/backend/services/auth/permissions/permission.exception.class';
@ApiTags('Webhooks')
@Controller('/webhooks')

View File

@ -12,6 +12,7 @@ import { ThrottlerModule } from '@nestjs/throttler';
import { AgentModule } from '@gitroom/nestjs-libraries/agent/agent.module';
import { McpModule } from '@gitroom/backend/mcp/mcp.module';
import { ThirdPartyModule } from '@gitroom/nestjs-libraries/3rdparties/thirdparty.module';
import { VideoModule } from '@gitroom/nestjs-libraries/videos/video.module';
@Global()
@Module({
@ -24,6 +25,7 @@ import { ThirdPartyModule } from '@gitroom/nestjs-libraries/3rdparties/thirdpart
AgentModule,
McpModule,
ThirdPartyModule,
VideoModule,
ThrottlerModule.forRoot([
{
ttl: 3600000,

View File

@ -15,16 +15,12 @@ import { GetOrgFromRequest } from '@gitroom/nestjs-libraries/user/org.from.reque
import { Organization } from '@prisma/client';
import { IntegrationService } from '@gitroom/nestjs-libraries/database/prisma/integrations/integration.service';
import { CheckPolicies } from '@gitroom/backend/services/auth/permissions/permissions.ability';
import {
AuthorizationActions,
Sections,
} from '@gitroom/backend/services/auth/permissions/permissions.service';
import { CreatePostDto } from '@gitroom/nestjs-libraries/dtos/posts/create.post.dto';
import { PostsService } from '@gitroom/nestjs-libraries/database/prisma/posts/posts.service';
import { FileInterceptor } from '@nestjs/platform-express';
import { UploadFactory } from '@gitroom/nestjs-libraries/upload/upload.factory';
import { MediaService } from '@gitroom/nestjs-libraries/database/prisma/media/media.service';
import { GetPostsDto } from '@gitroom/nestjs-libraries/dtos/posts/get.posts.dto';
import { AuthorizationActions, Sections } from '@gitroom/backend/services/auth/permissions/permission.exception.class';
@ApiTags('Public API')
@Controller('/public/v1')

View File

@ -0,0 +1,27 @@
import { HttpException, HttpStatus } from '@nestjs/common';
export enum Sections {
CHANNEL = 'channel',
POSTS_PER_MONTH = 'posts_per_month',
VIDEOS_PER_MONTH = 'videos_per_month',
TEAM_MEMBERS = 'team_members',
COMMUNITY_FEATURES = 'community_features',
FEATURED_BY_GITROOM = 'featured_by_gitroom',
AI = 'ai',
IMPORT_FROM_CHANNELS = 'import_from_channels',
ADMIN = 'admin',
WEBHOOKS = 'webhooks',
}
export enum AuthorizationActions {
Create = 'create',
Read = 'read',
Update = 'update',
Delete = 'delete',
}
export class SubscriptionException extends HttpException {
constructor(message: { section: Sections; action: AuthorizationActions }) {
super(message, HttpStatus.PAYMENT_REQUIRED);
}
}

View File

@ -1,8 +1,5 @@
import { SetMetadata } from '@nestjs/common';
import {
AuthorizationActions,
Sections,
} from '@gitroom/backend/services/auth/permissions/permissions.service';
import { AuthorizationActions, Sections } from './permission.exception.class';
export const CHECK_POLICIES_KEY = 'check_policy';
export type AbilityPolicy = [AuthorizationActions, Sections];

View File

@ -9,8 +9,8 @@ import {
CHECK_POLICIES_KEY,
} from '@gitroom/backend/services/auth/permissions/permissions.ability';
import { Organization } from '@prisma/client';
import { SubscriptionException } from '@gitroom/backend/services/auth/permissions/subscription.exception';
import { Request } from 'express';
import { SubscriptionException } from './permission.exception.class';
@Injectable()
export class PoliciesGuard implements CanActivate {

View File

@ -1,445 +0,0 @@
import { mock } from 'jest-mock-extended';
import { IntegrationService } from '@gitroom/nestjs-libraries/database/prisma/integrations/integration.service';
import { PostsService } from '@gitroom/nestjs-libraries/database/prisma/posts/posts.service';
import { SubscriptionService } from '@gitroom/nestjs-libraries/database/prisma/subscriptions/subscription.service';
import { WebhooksService } from '@gitroom/nestjs-libraries/database/prisma/webhooks/webhooks.service';
import { PermissionsService } from './permissions.service';
import { AuthorizationActions, Sections } from './permissions.service';
import { Period, SubscriptionTier } from '@prisma/client';
// Mock of dependent services
const mockSubscriptionService = mock<SubscriptionService>();
const mockPostsService = mock<PostsService>();
const mockIntegrationService = mock<IntegrationService>();
const mockWebHookService = mock<WebhooksService>();
describe('PermissionsService', () => {
let service: PermissionsService;
// Initial setup before each test
beforeEach(() => {
process.env.STRIPE_PUBLISHABLE_KEY = 'mock_stripe_key';
service = new PermissionsService(
mockSubscriptionService,
mockPostsService,
mockIntegrationService,
mockWebHookService
);
});
// Reusable mocks for `getPackageOptions`
const baseSubscription = {
id: 'mock-id',
organizationId: 'mock-org-id',
subscriptionTier: 'PRO' as SubscriptionTier,
identifier: 'mock-identifier',
cancelAt: new Date(),
period: {} as Period,
totalChannels: 5,
isLifetime: false,
createdAt: new Date(),
updatedAt: new Date(),
deletedAt: null,
disabled: false,
tokenExpiration: new Date(),
profile: 'mock-profile',
postingTimes: '[]',
lastPostedAt: new Date(),
};
const baseOptions = {
channel: 10,
current: 'mock-current',
month_price: 20,
year_price: 200,
posts_per_month: 100,
team_members: true,
community_features: true,
featured_by_gitroom: true,
ai: true,
import_from_channels: true,
image_generator: false,
image_generation_count: 50,
public_api: true,
webhooks: 10,
autoPost: true, // Added the missing property
};
const baseIntegration = {
id: 'mock-integration-id',
organizationId: 'mock-org-id',
createdAt: new Date(),
updatedAt: new Date(),
deletedAt: new Date(),
additionalSettings: '{}',
refreshNeeded: false,
refreshToken: 'mock-refresh-token',
name: 'Mock Integration',
internalId: 'mock-internal-id',
picture: 'mock-picture-url',
providerIdentifier: 'mock-provider',
token: 'mock-token',
type: 'social',
inBetweenSteps: false,
disabled: false,
tokenExpiration: new Date(),
profile: 'mock-profile',
postingTimes: '[]',
lastPostedAt: new Date(),
customInstanceDetails: 'mock-details',
customerId: 'mock-customer-id',
rootInternalId: 'mock-root-id',
customer: {
id: 'mock-customer-id',
createdAt: new Date(),
updatedAt: new Date(),
deletedAt: new Date(),
name: 'Mock Customer',
orgId: 'mock-org-id',
},
};
describe('check()', () => {
describe('Verification Bypass (64)', () => {
it('Bypass for Empty List', async () => {
// Setup: STRIPE_PUBLISHABLE_KEY exists and requestedPermission is empty
// Execution: call the check method with an empty list of permissions
const result = await service.check(
'mock-org-id',
new Date(),
'ADMIN',
[] // empty requestedPermission
);
// Verification: not requested, no authorization
expect(
result.cannot(AuthorizationActions.Create, Sections.CHANNEL)
).toBe(true);
});
it('Bypass for Missing Stripe', async () => {
// Setup: STRIPE_PUBLISHABLE_KEY does not exist
process.env.STRIPE_PUBLISHABLE_KEY = undefined;
// Necessary mock to avoid undefined filter error
jest
.spyOn(mockIntegrationService, 'getIntegrationsList')
.mockResolvedValue([{ ...baseIntegration, refreshNeeded: false }]);
// Mock of getPackageOptions (even if not used due to bypass)
jest.spyOn(service, 'getPackageOptions').mockResolvedValue({
subscription: baseSubscription,
options: baseOptions,
});
// List of requested permissions
const requestedPermissions: Array<[AuthorizationActions, Sections]> = [
[AuthorizationActions.Read, Sections.CHANNEL],
[AuthorizationActions.Create, Sections.AI],
];
// Execution: call the check method
const result = await service.check(
'mock-org-id',
new Date(),
'USER',
requestedPermissions
);
// Verification: should allow all requested actions due to the absence of the Stripe key
expect(result.can(AuthorizationActions.Read, Sections.CHANNEL)).toBe(
true
);
expect(result.can(AuthorizationActions.Create, Sections.AI)).toBe(true);
});
it('No Bypass', async () => {
// List of requested permissions
const requestedPermissions: Array<[AuthorizationActions, Sections]> = [
[AuthorizationActions.Read, Sections.CHANNEL],
[AuthorizationActions.Create, Sections.AI],
];
// Mock of getPackageOptions to force a scenario without permissions
jest.spyOn(service, 'getPackageOptions').mockResolvedValue({
subscription: { ...baseSubscription, totalChannels: 0 },
options: {
...baseOptions,
channel: 0,
ai: false,
},
});
// Mock of getIntegrationsList for the channel scenario
jest
.spyOn(mockIntegrationService, 'getIntegrationsList')
.mockResolvedValue([{ ...baseIntegration, refreshNeeded: false }]);
// Execution: call the check method
const result = await service.check(
'mock-org-id',
new Date(),
'USER',
requestedPermissions
);
// Verification: should not allow the requested actions as there is no bypass
expect(result.can(AuthorizationActions.Read, Sections.CHANNEL)).toBe(
false
);
expect(result.can(AuthorizationActions.Create, Sections.AI)).toBe(
false
);
});
});
describe('Channel Permission (82/87)', () => {
it('All Conditions True', async () => {
// Mock of getPackageOptions to set channel limits
jest.spyOn(service, 'getPackageOptions').mockResolvedValue({
subscription: { ...baseSubscription, totalChannels: 10 },
options: { ...baseOptions, channel: 10 },
});
// Mock of getIntegrationsList to set existing channels
jest
.spyOn(mockIntegrationService, 'getIntegrationsList')
.mockResolvedValue([
{ ...baseIntegration, refreshNeeded: false },
{ ...baseIntegration, refreshNeeded: false },
{ ...baseIntegration, refreshNeeded: false },
]);
// List of requested permissions
const requestedPermissions: Array<[AuthorizationActions, Sections]> = [
[AuthorizationActions.Create, Sections.CHANNEL],
];
// Execution: call the check method
const result = await service.check(
'mock-org-id',
new Date(),
'USER',
requestedPermissions
);
// Verification: should allow the requested action
expect(result.can(AuthorizationActions.Create, Sections.CHANNEL)).toBe(
true
);
});
it('Channel With Option Limit', async () => {
// Mock of getPackageOptions to set channel limits
jest.spyOn(service, 'getPackageOptions').mockResolvedValue({
subscription: { ...baseSubscription, totalChannels: 3 },
options: { ...baseOptions, channel: 10 },
});
// Mock of getIntegrationsList to set existing channels
jest
.spyOn(mockIntegrationService, 'getIntegrationsList')
.mockResolvedValue([
{ ...baseIntegration, refreshNeeded: false },
{ ...baseIntegration, refreshNeeded: false },
{ ...baseIntegration, refreshNeeded: false },
]);
// List of requested permissions
const requestedPermissions: Array<[AuthorizationActions, Sections]> = [
[AuthorizationActions.Create, Sections.CHANNEL],
];
// Execution: call the check method
const result = await service.check(
'mock-org-id',
new Date(),
'USER',
requestedPermissions
);
// Verification: should allow the requested action
expect(result.can(AuthorizationActions.Create, Sections.CHANNEL)).toBe(
true
);
});
it('Channel With Subscription Limit', async () => {
// Mock of getPackageOptions to set channel limits
jest.spyOn(service, 'getPackageOptions').mockResolvedValue({
subscription: { ...baseSubscription, totalChannels: 10 },
options: { ...baseOptions, channel: 3 },
});
// Mock of getIntegrationsList to set existing channels
jest
.spyOn(mockIntegrationService, 'getIntegrationsList')
.mockResolvedValue([
{ ...baseIntegration, refreshNeeded: false },
{ ...baseIntegration, refreshNeeded: false },
{ ...baseIntegration, refreshNeeded: false },
]);
// List of requested permissions
const requestedPermissions: Array<[AuthorizationActions, Sections]> = [
[AuthorizationActions.Create, Sections.CHANNEL],
];
// Execution: call the check method
const result = await service.check(
'mock-org-id',
new Date(),
'USER',
requestedPermissions
);
// Verification: should allow the requested action
expect(result.can(AuthorizationActions.Create, Sections.CHANNEL)).toBe(
true
);
});
it('Channel Without Available Limits', async () => {
// Mock of getPackageOptions to set channel limits
jest.spyOn(service, 'getPackageOptions').mockResolvedValue({
subscription: { ...baseSubscription, totalChannels: 3 },
options: { ...baseOptions, channel: 3 },
});
// Mock of getIntegrationsList to set existing channels
jest
.spyOn(mockIntegrationService, 'getIntegrationsList')
.mockResolvedValue([
{ ...baseIntegration, refreshNeeded: false },
{ ...baseIntegration, refreshNeeded: false },
{ ...baseIntegration, refreshNeeded: false },
]);
// List of requested permissions
const requestedPermissions: Array<[AuthorizationActions, Sections]> = [
[AuthorizationActions.Create, Sections.CHANNEL],
];
// Execution: call the check method
const result = await service.check(
'mock-org-id',
new Date(),
'USER',
requestedPermissions
);
// Verification: should not allow the requested action
expect(result.can(AuthorizationActions.Create, Sections.CHANNEL)).toBe(
false
);
});
it('Section Different from Channel', async () => {
// Mock of getPackageOptions to set channel limits
jest.spyOn(service, 'getPackageOptions').mockResolvedValue({
subscription: { ...baseSubscription, totalChannels: 10 },
options: { ...baseOptions, channel: 10 },
});
// Mock of getIntegrationsList to set existing channels
jest
.spyOn(mockIntegrationService, 'getIntegrationsList')
.mockResolvedValue([
{ ...baseIntegration, refreshNeeded: false },
{ ...baseIntegration, refreshNeeded: false },
{ ...baseIntegration, refreshNeeded: false },
]);
// List of requested permissions
const requestedPermissions: Array<[AuthorizationActions, Sections]> = [
[AuthorizationActions.Create, Sections.AI], // Requesting permission for AI instead of CHANNEL
];
// Execution: call the check method
const result = await service.check(
'mock-org-id',
new Date(),
'USER',
requestedPermissions
);
// Verification: should not allow the requested action in CHANNEL
expect(result.can(AuthorizationActions.Create, Sections.CHANNEL)).toBe(
false
);
});
});
describe('Monthly Posts Permission (97/110)', () => {
it('Posts Within Limit', async () => {
// Mock of getPackageOptions to set post limits
jest.spyOn(service, 'getPackageOptions').mockResolvedValue({
subscription: baseSubscription,
options: { ...baseOptions, posts_per_month: 100 },
});
// Mock of getSubscription
jest
.spyOn(mockSubscriptionService, 'getSubscription')
.mockResolvedValue({
...baseSubscription,
createdAt: new Date(),
});
// Mock of countPostsFromDay to return quantity within the limit
jest.spyOn(mockPostsService, 'countPostsFromDay').mockResolvedValue(50);
// List of requested permissions
const requestedPermissions: Array<[AuthorizationActions, Sections]> = [
[AuthorizationActions.Create, Sections.POSTS_PER_MONTH],
];
// Execution: call the check method
const result = await service.check(
'mock-org-id',
new Date(),
'USER',
requestedPermissions
);
// Verification: should allow the requested action
expect(
result.can(AuthorizationActions.Create, Sections.POSTS_PER_MONTH)
).toBe(true);
});
it('Posts Exceed Limit', async () => {
// Mock of getPackageOptions to set post limits
jest.spyOn(service, 'getPackageOptions').mockResolvedValue({
subscription: baseSubscription,
options: { ...baseOptions, posts_per_month: 100 },
});
// Mock of getSubscription
jest
.spyOn(mockSubscriptionService, 'getSubscription')
.mockResolvedValue({
...baseSubscription,
createdAt: new Date(),
});
// Mock of countPostsFromDay to return quantity above the limit
jest
.spyOn(mockPostsService, 'countPostsFromDay')
.mockResolvedValue(150);
// List of requested permissions
const requestedPermissions: Array<[AuthorizationActions, Sections]> = [
[AuthorizationActions.Create, Sections.POSTS_PER_MONTH],
];
// Execution: call the check method
const result = await service.check(
'mock-org-id',
new Date(),
'USER',
requestedPermissions
);
// Verification: should not allow the requested action
expect(
result.can(AuthorizationActions.Create, Sections.POSTS_PER_MONTH)
).toBe(false);
});
it('Section Different with Posts Within Limit', async () => {
// Mock of getPackageOptions to set post limits
jest.spyOn(service, 'getPackageOptions').mockResolvedValue({
subscription: baseSubscription,
options: { ...baseOptions, posts_per_month: 100 },
});
// Mock of getSubscription
jest
.spyOn(mockSubscriptionService, 'getSubscription')
.mockResolvedValue({
...baseSubscription,
createdAt: new Date(),
});
// Mock of countPostsFromDay to return quantity within the limit
jest.spyOn(mockPostsService, 'countPostsFromDay').mockResolvedValue(50);
// List of requested permissions
const requestedPermissions: Array<[AuthorizationActions, Sections]> = [
[AuthorizationActions.Create, Sections.AI], // Requesting permission for AI instead of POSTS_PER_MONTH
];
// Execution: call the check method
const result = await service.check(
'mock-org-id',
new Date(),
'USER',
requestedPermissions
);
// Verification: should not allow the requested action in POSTS_PER_MONTH
expect(
result.can(AuthorizationActions.Create, Sections.POSTS_PER_MONTH)
).toBe(false);
});
});
});
});

View File

@ -6,25 +6,7 @@ import { PostsService } from '@gitroom/nestjs-libraries/database/prisma/posts/po
import { IntegrationService } from '@gitroom/nestjs-libraries/database/prisma/integrations/integration.service';
import dayjs from 'dayjs';
import { WebhooksService } from '@gitroom/nestjs-libraries/database/prisma/webhooks/webhooks.service';
export enum Sections {
CHANNEL = 'channel',
POSTS_PER_MONTH = 'posts_per_month',
TEAM_MEMBERS = 'team_members',
COMMUNITY_FEATURES = 'community_features',
FEATURED_BY_GITROOM = 'featured_by_gitroom',
AI = 'ai',
IMPORT_FROM_CHANNELS = 'import_from_channels',
ADMIN = 'admin',
WEBHOOKS = 'webhooks',
}
export enum AuthorizationActions {
Create = 'create',
Read = 'read',
Update = 'update',
Delete = 'delete',
}
import { AuthorizationActions, Sections } from './permission.exception.class';
export type AppAbility = Ability<[AuthorizationActions, Sections]>;

View File

@ -3,18 +3,8 @@ import {
Catch,
ExceptionFilter,
HttpException,
HttpStatus,
} from '@nestjs/common';
import {
AuthorizationActions,
Sections,
} from '@gitroom/backend/services/auth/permissions/permissions.service';
export class SubscriptionException extends HttpException {
constructor(message: { section: Sections; action: AuthorizationActions }) {
super(message, HttpStatus.PAYMENT_REQUIRED);
}
}
import { AuthorizationActions, Sections, SubscriptionException } from '@gitroom/backend/services/auth/permissions/permission.exception.class';
@Catch(SubscriptionException)
export class SubscriptionExceptionFilter implements ExceptionFilter {
@ -55,5 +45,10 @@ const getErrorMessage = (error: {
default:
return 'You have reached the maximum number of webhooks for your subscription. Please upgrade your subscription to add more webhooks.';
}
case Sections.VIDEOS_PER_MONTH:
switch (error.action) {
default:
return 'You have reached the maximum number of generated videos for your subscription. Please upgrade your subscription to generate more videos.';
}
}
};

View File

@ -98,7 +98,7 @@ ${type}
</svg>
</div>
<div className="text-[12px] font-[500] !text-current">
{t('ai', 'AI')}
{t('ai', 'AI')} Image
</div>
</div>
</Button>

View File

@ -0,0 +1,257 @@
import { Button } from '@gitroom/react/form/button';
import React, { FC, useCallback, useState } from 'react';
import clsx from 'clsx';
import Loading from 'react-loading';
import { useFetch } from '@gitroom/helpers/utils/custom.fetch';
import { useT } from '@gitroom/react/translation/get.transation.service.client';
import { useLaunchStore } from '@gitroom/frontend/components/new-launch/store';
import useSWR from 'swr';
import { TopTitle } from '@gitroom/frontend/components/launches/helpers/top.title.component';
import { Input } from '@gitroom/react/form/input';
import { timer } from '@gitroom/helpers/utils/timer';
import { VideoWrapper } from '@gitroom/frontend/components/videos/video.render.component';
import { FormProvider, useForm } from 'react-hook-form';
export const Modal: FC<{
close: () => void;
value: string;
type: any;
setLoading: (loading: boolean) => void;
onChange: (params: { id: string; path: string }) => void;
}> = (props) => {
const { type, value, onChange, close, setLoading } = props;
const fetch = useFetch();
const setLocked = useLaunchStore((state) => state.setLocked);
const form = useForm();
const [position, setPosition] = useState('vertical');
const loadCredits = useCallback(async () => {
return (
await fetch(`/copilot/credits?type=ai_videos`, {
method: 'GET',
})
).json();
}, []);
const { data, mutate } = useSWR('copilot-credits', loadCredits);
const generate = useCallback(async () => {
setLoading(true);
close();
setLocked(true);
console.log('lock');
const customParams = form.getValues();
try {
const image = await fetch(`/media/generate-video/${type.identifier}`, {
method: 'POST',
body: JSON.stringify({
prompt: [{ type: 'prompt', value }],
output: position,
customParams,
}),
});
console.log(image);
if (image.status == 200 || image.status == 201) {
onChange(await image.json());
}
} catch (e) {}
console.log('remove lock');
setLocked(false);
setLoading(false);
}, [type, value, position]);
return (
<form
onSubmit={form.handleSubmit(generate)}
className="flex flex-col gap-[10px]"
>
<FormProvider {...form}>
<div className="text-textColor fixed start-0 top-0 bg-primary/80 z-[300] w-full h-full p-[60px] animate-fade justify-center flex bg-black/50">
<div>
<div className="flex gap-[10px] flex-col w-[500px] h-auto bg-sixth border-tableBorder border-2 rounded-xl pb-[20px] px-[20px] relative">
<div className="flex">
<div className="flex-1">
<TopTitle title={'Video Type'}>
<div className="mr-[25px]">
{data?.credits || 0} credits left
</div>
</TopTitle>
</div>
<button
onClick={props.close}
className="outline-none absolute end-[10px] top-[10px] mantine-UnstyledButton-root mantine-ActionIcon-root bg-primary 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>
</div>
<div className="relative h-[400px]">
<div className="absolute left-0 top-0 w-full h-full overflow-hidden overflow-y-auto">
<div className="mt-[10px] flex w-full justify-center items-center gap-[10px]">
<div className="flex-1 flex">
<Button
className="!flex-1"
onClick={() => setPosition('vertical')}
secondary={position === 'horizontal'}
>
Vertical (Stories, Reels)
</Button>
</div>
<div className="flex-1 flex mt-[10px]">
<Button
className="!flex-1"
onClick={() => setPosition('horizontal')}
secondary={position === 'vertical'}
>
Horizontal (Normal Post)
</Button>
</div>
</div>
<VideoWrapper identifier={type.identifier} />
</div>
</div>
<div className="flex">
<Button type="submit" className="flex-1">
Generate
</Button>
</div>
</div>
</div>
</div>
</FormProvider>
</form>
);
};
export const AiVideo: FC<{
value: string;
onChange: (params: { id: string; path: string }) => void;
}> = (props) => {
const t = useT();
const { value, onChange } = props;
const [loading, setLoading] = useState(false);
const [type, setType] = useState<any | null>(null);
const [modal, setModal] = useState(false);
const fetch = useFetch();
const loadVideoList = useCallback(async () => {
return (await (await fetch('/media/video-options')).json()).filter(
(f: any) => f.placement === 'text-to-image'
);
}, []);
const { isLoading, data } = useSWR('load-videos-ai', loadVideoList, {
revalidateOnFocus: false,
revalidateOnReconnect: false,
refreshWhenHidden: false,
revalidateIfStale: false,
refreshWhenOffline: false,
keepPreviousData: true,
});
const generateVideo = useCallback(
(type: { identifier: string }) => async () => {
setType(type);
setModal(true);
},
[value, onChange]
);
if (isLoading || data?.length === 0) {
return null;
}
return (
<>
{modal && (
<Modal
onChange={onChange}
setLoading={setLoading}
close={() => {
setModal(false);
setType(null);
}}
type={type}
value={props.value}
/>
)}
<div className="relative group">
<Button
{...(value.length < 30
? {
'data-tooltip-id': 'tooltip',
'data-tooltip-content':
'Please add at least 30 characters to generate AI video',
}
: {})}
className={clsx(
'relative ms-[10px] rounded-[4px] gap-[8px] !text-primary justify-center items-center flex border border-dashed border-customColor21 bg-input',
value.length < 30 && 'opacity-25'
)}
>
{loading && (
<div className="absolute start-[50%] -translate-x-[50%]">
<Loading height={30} width={30} type="spin" color="#fff" />
</div>
)}
<div
className={clsx(
'flex gap-[5px] items-center',
loading && 'invisible'
)}
>
<div>
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
>
<path
d="M19.5 3H7.5C7.10218 3 6.72064 3.15804 6.43934 3.43934C6.15804 3.72064 6 4.10218 6 4.5V6H4.5C4.10218 6 3.72064 6.15804 3.43934 6.43934C3.15804 6.72064 3 7.10218 3 7.5V19.5C3 19.8978 3.15804 20.2794 3.43934 20.5607C3.72064 20.842 4.10218 21 4.5 21H16.5C16.8978 21 17.2794 20.842 17.5607 20.5607C17.842 20.2794 18 19.8978 18 19.5V18H19.5C19.8978 18 20.2794 17.842 20.5607 17.5607C20.842 17.2794 21 16.8978 21 16.5V4.5C21 4.10218 20.842 3.72064 20.5607 3.43934C20.2794 3.15804 19.8978 3 19.5 3ZM7.5 4.5H19.5V11.0044L17.9344 9.43875C17.6531 9.15766 17.2717 8.99976 16.8741 8.99976C16.4764 8.99976 16.095 9.15766 15.8137 9.43875L8.75344 16.5H7.5V4.5ZM16.5 19.5H4.5V7.5H6V16.5C6 16.8978 6.15804 17.2794 6.43934 17.5607C6.72064 17.842 7.10218 18 7.5 18H16.5V19.5ZM19.5 16.5H10.875L16.875 10.5L19.5 13.125V16.5ZM11.25 10.5C11.695 10.5 12.13 10.368 12.5 10.1208C12.87 9.87357 13.1584 9.52217 13.3287 9.11104C13.499 8.6999 13.5436 8.2475 13.4568 7.81105C13.37 7.37459 13.1557 6.97368 12.841 6.65901C12.5263 6.34434 12.1254 6.13005 11.689 6.04323C11.2525 5.95642 10.8001 6.00097 10.389 6.17127C9.97783 6.34157 9.62643 6.62996 9.37919 6.99997C9.13196 7.36998 9 7.80499 9 8.25C9 8.84674 9.23705 9.41903 9.65901 9.84099C10.081 10.2629 10.6533 10.5 11.25 10.5ZM11.25 7.5C11.3983 7.5 11.5433 7.54399 11.6667 7.6264C11.79 7.70881 11.8861 7.82594 11.9429 7.96299C11.9997 8.10003 12.0145 8.25083 11.9856 8.39632C11.9566 8.5418 11.8852 8.67544 11.7803 8.78033C11.6754 8.88522 11.5418 8.95665 11.3963 8.98559C11.2508 9.01453 11.1 8.99968 10.963 8.94291C10.8259 8.88614 10.7088 8.79001 10.6264 8.66668C10.544 8.54334 10.5 8.39834 10.5 8.25C10.5 8.05109 10.579 7.86032 10.7197 7.71967C10.8603 7.57902 11.0511 7.5 11.25 7.5Z"
fill="currentColor"
/>
</svg>
</div>
<div className="text-[12px] font-[500] !text-current">
{t('ai', 'AI')} Video
</div>
</div>
</Button>
{value.length >= 30 && !loading && (
<div className="text-[12px] ms-[10px] -mt-[10px] w-[200px] absolute top-[100%] z-[500] start-0 hidden group-hover:block">
<ul className="cursor-pointer rounded-[4px] border border-dashed border-customColor21 mt-[3px] p-[5px] bg-customColor2">
{data.map((p: any) => (
<li
onClick={generateVideo(p)}
key={p}
className="hover:bg-sixth"
>
{p.title}
</li>
))}
</ul>
</div>
)}
</div>
</>
);
};

View File

@ -96,8 +96,9 @@ function LayoutContextInner(params: { children: ReactNode }) {
)
) {
window.open('/billing', '_blank');
return false;
}
return false;
return true;
}
return true;
},

View File

@ -32,6 +32,7 @@ import { ThirdPartyMedia } from '@gitroom/frontend/components/third-parties/thir
import { ReactSortable } from 'react-sortablejs';
import { useMediaSettings } from '@gitroom/frontend/components/launches/helpers/media.settings.component';
import { useLaunchStore } from '@gitroom/frontend/components/new-launch/store';
import { AiVideo } from '@gitroom/frontend/components/launches/ai.video';
const Polonto = dynamic(
() => import('@gitroom/frontend/components/launches/polonto')
);
@ -710,7 +711,10 @@ export const MultiMediaComponent: FC<{
<ThirdPartyMedia allData={allData} onChange={changeMedia} />
{!!user?.tier?.ai && (
<AiImage value={text} onChange={changeMedia} />
<>
<AiImage value={text} onChange={changeMedia} />
<AiVideo value={text} onChange={changeMedia} />
</>
)}
</div>
</div>

View File

@ -0,0 +1,166 @@
import { videoWrapper } from '@gitroom/frontend/components/videos/video.wrapper';
import { FC, useCallback, useRef, useState, useEffect } from 'react';
import { useVideoFunction } from '@gitroom/frontend/components/videos/video.render.component';
import useSWR from 'swr';
import { useFormContext } from 'react-hook-form';
import { Button } from '@gitroom/react/form/button';
import clsx from 'clsx';
export interface Voices {
voices: Voice[];
}
export interface Voice {
id: string;
name: string;
preview_url: string;
}
const VoiceSelector: FC = () => {
const { register, watch, setValue } = useFormContext();
const videoFunction = useVideoFunction();
const [currentlyPlaying, setCurrentlyPlaying] = useState<string | null>(null);
const [loadingVoice, setLoadingVoice] = useState<string | null>(null);
const audioRef = useRef<HTMLAudioElement | null>(null);
const loadVideos = useCallback(() => {
return videoFunction('loadVoices', {});
}, []);
const selectedVoice = watch('voice');
const { isLoading, data } = useSWR<Voices>('load-voices', loadVideos);
// Auto-select first voice when data loads
useEffect(() => {
if (data?.voices?.length && !selectedVoice) {
setValue('voice', data.voices[0].id);
}
}, [data, selectedVoice, setValue]);
const playVoice = useCallback(
async (voiceId: string, previewUrl: string) => {
try {
setLoadingVoice(voiceId);
// Stop current audio if playing
if (audioRef.current) {
audioRef.current.pause();
audioRef.current = null;
}
// If clicking the same voice that's playing, stop it
if (currentlyPlaying === voiceId) {
setCurrentlyPlaying(null);
setLoadingVoice(null);
return;
}
// Create new audio element
const audio = new Audio(previewUrl);
audioRef.current = audio;
audio.addEventListener('loadeddata', () => {
setLoadingVoice(null);
setCurrentlyPlaying(voiceId);
});
audio.addEventListener('ended', () => {
setCurrentlyPlaying(null);
audioRef.current = null;
});
audio.addEventListener('error', () => {
setLoadingVoice(null);
setCurrentlyPlaying(null);
audioRef.current = null;
});
await audio.play();
} catch (error) {
console.error('Error playing voice:', error);
setLoadingVoice(null);
setCurrentlyPlaying(null);
}
},
[currentlyPlaying]
);
const selectVoice = useCallback(
(voiceId: string) => {
setValue('voice', voiceId);
},
[setValue]
);
if (isLoading || !data?.voices?.length) {
return (
<div className="flex items-center justify-center py-4">
<div className="text-sm text-gray-500">Loading voices...</div>
</div>
);
}
return (
<div className="space-y-3">
<div className="text-sm font-medium text-textColor mb-4">
Select a Voice
</div>
<div className="space-y-2">
{data.voices.map((voice) => (
<div
key={voice.id}
className={clsx(
'flex items-center justify-between p-3 rounded-lg border transition-colors cursor-pointer',
selectedVoice === voice.id
? 'border-primary bg-primary/10'
: 'border-tableBorder bg-sixth hover:bg-seventh'
)}
onClick={() => selectVoice(voice.id)}
>
<div className="flex items-center space-x-3">
<input
{...register('voice')}
type="radio"
value={voice.id}
className="w-4 h-4 text-primary border-gray-300 focus:ring-primary"
checked={selectedVoice === voice.id}
onChange={() => selectVoice(voice.id)}
/>
<div>
<div className="text-sm font-medium text-textColor">
{voice.name}
</div>
</div>
</div>
<Button
type="button"
className={clsx(
'px-3 py-1 text-xs',
loadingVoice === voice.id && 'opacity-50 cursor-not-allowed',
currentlyPlaying === voice.id && 'bg-red-500 hover:bg-red-600'
)}
onClick={(e) => {
e.stopPropagation();
playVoice(voice.id, voice.preview_url);
}}
disabled={loadingVoice === voice.id}
>
{loadingVoice === voice.id
? '...'
: currentlyPlaying === voice.id
? '⏹ Stop'
: '▶ Play'}
</Button>
</div>
))}
</div>
</div>
);
};
const ImageSlidesComponent = () => {
return <VoiceSelector />;
};
videoWrapper('image-text-slides', ImageSlidesComponent);

View File

@ -0,0 +1,43 @@
import { createContext, FC, useCallback, useContext } from 'react';
import './providers/image-text-slides.provider';
import { videosList } from '@gitroom/frontend/components/videos/video.wrapper';
import { useFetch } from '@gitroom/helpers/utils/custom.fetch';
const VideoFunctionWrapper = createContext({
identifier: '',
});
export const useVideoFunction = () => {
const { identifier } = useContext(VideoFunctionWrapper);
const fetch = useFetch();
return useCallback(
async (funcName: string, params: any) => {
return (
await fetch(`/media/video/${identifier}/${funcName}`, {
method: 'POST',
body: JSON.stringify({ params }),
headers: {
'Content-Type': 'application/json',
},
})
).json();
},
[identifier]
);
};
export const VideoWrapper: FC<{ identifier: string }> = (props) => {
const { identifier } = props;
const Component = videosList.find(
(v) => v.identifier === identifier
)?.Component;
if (!Component) {
return null;
}
return (
<VideoFunctionWrapper.Provider value={{ identifier }}>
<Component />
</VideoFunctionWrapper.Provider>
);
};

View File

@ -0,0 +1,16 @@
import { FC } from 'react';
export const videosList: {identifier: string, Component: FC}[] = [];
export const videoWrapper = (identifier: string, Component: any): null => {
if (videosList.map(p => p.identifier).includes(identifier)) {
return null;
}
videosList.push({
identifier,
Component
});
return null;
}

View File

@ -39,6 +39,8 @@ import { SetsService } from '@gitroom/nestjs-libraries/database/prisma/sets/sets
import { SetsRepository } from '@gitroom/nestjs-libraries/database/prisma/sets/sets.repository';
import { ThirdPartyRepository } from '@gitroom/nestjs-libraries/database/prisma/third-party/third-party.repository';
import { ThirdPartyService } from '@gitroom/nestjs-libraries/database/prisma/third-party/third-party.service';
import { VideoManager } from '@gitroom/nestjs-libraries/videos/video.manager';
import { FalService } from '@gitroom/nestjs-libraries/openai/fal.service';
@Global()
@Module({
@ -79,6 +81,7 @@ import { ThirdPartyService } from '@gitroom/nestjs-libraries/database/prisma/thi
IntegrationManager,
ExtractContentService,
OpenaiService,
FalService,
EmailService,
TrackService,
ShortLinkService,
@ -86,6 +89,7 @@ import { ThirdPartyService } from '@gitroom/nestjs-libraries/database/prisma/thi
SetsRepository,
ThirdPartyRepository,
ThirdPartyService,
VideoManager,
],
get exports() {
return this.providers;

View File

@ -4,13 +4,20 @@ import { OpenaiService } from '@gitroom/nestjs-libraries/openai/openai.service';
import { SubscriptionService } from '@gitroom/nestjs-libraries/database/prisma/subscriptions/subscription.service';
import { Organization } from '@prisma/client';
import { SaveMediaInformationDto } from '@gitroom/nestjs-libraries/dtos/media/save.media.information.dto';
import { VideoManager } from '@gitroom/nestjs-libraries/videos/video.manager';
import { VideoDto } from '@gitroom/nestjs-libraries/dtos/videos/video.dto';
import { UploadFactory } from '@gitroom/nestjs-libraries/upload/upload.factory';
import { AuthorizationActions, Sections, SubscriptionException } from '@gitroom/backend/services/auth/permissions/permission.exception.class';
@Injectable()
export class MediaService {
private storage = UploadFactory.createStorage();
constructor(
private _mediaRepository: MediaRepository,
private _openAi: OpenaiService,
private _subscriptionService: SubscriptionService
private _subscriptionService: SubscriptionService,
private _videoManager: VideoManager
) {}
async deleteMedia(org: string, id: string) {
@ -49,4 +56,54 @@ export class MediaService {
saveMediaInformation(org: string, data: SaveMediaInformationDto) {
return this._mediaRepository.saveMediaInformation(org, data);
}
getVideoOptions() {
return this._videoManager.getAllVideos();
}
async generateVideo(org: Organization, body: VideoDto, type: string) {
const totalCredits = await this._subscriptionService.checkCredits(
org,
'ai_videos'
);
if (totalCredits.credits <= 0) {
throw new SubscriptionException({
action: AuthorizationActions.Create,
section: Sections.VIDEOS_PER_MONTH,
});
}
const video = this._videoManager.getVideoByName(type);
if (!video) {
throw new Error(`Video type ${type} not found`);
}
const loadedData = await video.instance.process(
body.prompt,
body.output,
body.customParams
);
const file = await this.storage.uploadSimple(loadedData);
const save = await this.saveFile(org.id, file.split('/').pop(), file);
await this._subscriptionService.useCredit(org, 'ai_videos');
return save;
}
async videoFunction(identifier: string, functionName: string, body: any) {
const video = this._videoManager.getVideoByName(identifier);
if (!video) {
throw new Error(`Video with identifier ${identifier} not found`);
}
// @ts-ignore
const functionToCall = video.instance[functionName];
if (typeof functionToCall !== 'function') {
throw new Error(`Function ${functionName} not found on video instance`);
}
return functionToCall(body);
}
}

View File

@ -177,9 +177,9 @@ model ItemUser {
userId String
key String
@@unique([userId, key])
@@index([userId])
@@index([key])
@@unique([userId, key])
}
model Star {
@ -263,6 +263,7 @@ model Credits {
organization Organization @relation(fields: [organizationId], references: [id])
organizationId String
credits Int
type String @default("ai_images")
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@ -332,11 +333,11 @@ model Integration {
additionalSettings String? @default("[]")
webhooks IntegrationsWebhooks[]
@@unique([organizationId, internalId])
@@index([rootInternalId])
@@index([updatedAt])
@@index([deletedAt])
@@index([customerId])
@@unique([organizationId, internalId])
}
model Signatures {
@ -566,8 +567,8 @@ model IntegrationsWebhooks {
webhookId String
webhook Webhooks @relation(fields: [webhookId], references: [id])
@@unique([integrationId, webhookId])
@@id([integrationId, webhookId])
@@unique([integrationId, webhookId])
@@index([integrationId])
@@index([webhookId])
}
@ -632,9 +633,9 @@ model ThirdParty {
updatedAt DateTime @updatedAt
deletedAt DateTime?
@@unique([organizationId, internalId])
@@index([organizationId])
@@index([deletedAt])
@@unique([organizationId, internalId])
}
enum OrderStatus {

View File

@ -11,6 +11,7 @@ export interface PricingInnerInterface {
import_from_channels: boolean;
image_generator?: boolean;
image_generation_count: number;
generate_videos: number;
public_api: boolean;
webhooks: number;
autoPost: boolean;
@ -35,6 +36,7 @@ export const pricing: PricingInterface = {
public_api: false,
webhooks: 0,
autoPost: false,
generate_videos: 0,
},
STANDARD: {
current: 'STANDARD',
@ -52,6 +54,7 @@ export const pricing: PricingInterface = {
public_api: true,
webhooks: 2,
autoPost: false,
generate_videos: 3,
},
TEAM: {
current: 'TEAM',
@ -69,6 +72,7 @@ export const pricing: PricingInterface = {
public_api: true,
webhooks: 10,
autoPost: true,
generate_videos: 10,
},
PRO: {
current: 'PRO',
@ -86,6 +90,7 @@ export const pricing: PricingInterface = {
public_api: true,
webhooks: 30,
autoPost: true,
generate_videos: 50,
},
ULTIMATE: {
current: 'ULTIMATE',
@ -103,5 +108,6 @@ export const pricing: PricingInterface = {
public_api: true,
webhooks: 10000,
autoPost: true,
generate_videos: 100,
},
};

View File

@ -201,11 +201,12 @@ export class SubscriptionRepository {
});
}
async getCreditsFrom(organizationId: string, from: dayjs.Dayjs) {
async getCreditsFrom(organizationId: string, from: dayjs.Dayjs, type = 'ai_images') {
const load = await this._credits.model.credits.groupBy({
by: ['organizationId'],
where: {
organizationId,
type,
createdAt: {
gte: from.toDate(),
},
@ -218,11 +219,12 @@ export class SubscriptionRepository {
return load?.[0]?._sum?.credits || 0;
}
useCredit(org: Organization) {
useCredit(org: Organization, type = 'ai_images') {
return this._credits.model.credits.create({
data: {
organizationId: org.id,
credits: 1,
type,
},
});
}

View File

@ -21,8 +21,8 @@ export class SubscriptionService {
);
}
useCredit(organization: Organization) {
return this._subscriptionRepository.useCredit(organization);
useCredit(organization: Organization, type = 'ai_images') {
return this._subscriptionRepository.useCredit(organization, type);
}
getCode(code: string) {
@ -189,7 +189,7 @@ export class SubscriptionService {
return this._subscriptionRepository.getSubscription(organizationId);
}
async checkCredits(organization: Organization) {
async checkCredits(organization: Organization, checkType = 'ai_images') {
// @ts-ignore
const type = organization?.subscription?.subscriptionTier || 'FREE';
@ -204,11 +204,12 @@ export class SubscriptionService {
}
const checkFromMonth = date.subtract(1, 'month');
const imageGenerationCount = pricing[type].image_generation_count;
const imageGenerationCount = checkType === 'ai_images' ? pricing[type].image_generation_count : pricing[type].generate_videos
const totalUse = await this._subscriptionRepository.getCreditsFrom(
organization.id,
checkFromMonth
checkFromMonth,
checkType
);
return {

View File

@ -0,0 +1,22 @@
import { Type } from 'class-transformer';
import { IsArray, IsIn, IsString, ValidateNested } from 'class-validator';
export class Prompt {
@IsIn(['prompt', 'image'])
type: 'prompt' | 'image';
@IsString()
value: string;
}
export class VideoDto {
@ValidateNested({ each: true })
@IsArray()
@Type(() => Prompt)
prompt: Prompt[];
@IsIn(['vertical', 'horizontal'])
output: 'vertical' | 'horizontal';
customParams: any;
}

View File

@ -0,0 +1,41 @@
import { Injectable } from '@nestjs/common';
import pLimit from 'p-limit';
const limit = pLimit(10);
@Injectable()
export class FalService {
async generateImageFromText(
model: string,
text: string,
isVertical: boolean = false
): Promise<string> {
const { images, video, ...all } = await (
await limit(() =>
fetch(`https://fal.run/fal-ai/${model}`, {
method: 'POST',
headers: {
Authorization: `Key ${process.env.FAL_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt: text,
aspect_ratio: isVertical ? '9:16' : '16:9',
resolution: '720p',
num_images: 1,
output_format: 'jpeg',
expand_prompt: true,
}),
})
)
).json();
console.log(all, video, images);
if (video) {
return video.url;
}
return images[0].url as string;
}
}

View File

@ -18,12 +18,13 @@ const VoicePrompt = z.object({
@Injectable()
export class OpenaiService {
async generateImage(prompt: string, isUrl: boolean) {
async generateImage(prompt: string, isUrl: boolean, isVertical = false) {
const generate = (
await openai.images.generate({
prompt,
response_format: isUrl ? 'url' : 'b64_json',
model: 'dall-e-3',
...(isVertical ? { size: '1024x1792' } : {}),
})
).data[0];
@ -224,4 +225,36 @@ export class OpenaiService {
),
};
}
async generateSlidesFromText(text: string) {
const message = `You are an assistant that takes a text and break it into slides, each slide should have an image prompt and voice text to be later used to generate a video and voice, image prompt should capture the essence of the slide and also have a back dark gradient on top, image prompt should not contain text in the picture, generate between 3-5 slides maximum`;
return (
(
await openai.beta.chat.completions.parse({
model: 'gpt-4.1',
messages: [
{
role: 'system',
content: message,
},
{
role: 'user',
content: text,
},
],
response_format: zodResponseFormat(
z.object({
slides: z.array(
z.object({
imagePrompt: z.string(),
voiceText: z.string(),
})
),
}),
'slides'
),
})
).choices[0].message.parsed?.slides || []
);
}
}

View File

@ -0,0 +1,244 @@
import { OpenaiService } from '@gitroom/nestjs-libraries/openai/openai.service';
import {
Prompt,
URL,
Video,
VideoAbstract,
} from '@gitroom/nestjs-libraries/videos/video.interface';
import { chunk } from 'lodash';
import Transloadit from 'transloadit';
import { UploadFactory } from '@gitroom/nestjs-libraries/upload/upload.factory';
import { Readable } from 'stream';
import { parseBuffer } from 'music-metadata';
import { stringifySync } from 'subtitle';
import pLimit from 'p-limit';
import { FalService } from '@gitroom/nestjs-libraries/openai/fal.service';
const limit = pLimit(2);
const transloadit = new Transloadit({
authKey: process.env.TRANSLOADIT_AUTH,
authSecret: process.env.TRANSLOADIT_SECRET,
});
async function getAudioDuration(buffer: Buffer): Promise<number> {
const metadata = await parseBuffer(buffer, 'audio/mpeg');
return metadata.format.duration || 0;
}
@Video({
identifier: 'image-text-slides',
title: 'Image Text Slides',
description: 'Generate videos slides from images and text',
placement: 'text-to-image',
available:
!!process.env.ELEVENSLABS_API_KEY &&
!!process.env.TRANSLOADIT_AUTH &&
!!process.env.TRANSLOADIT_SECRET &&
!!process.env.OPENAI_API_KEY &&
!!process.env.FAL_KEY,
})
export class ImagesSlides extends VideoAbstract {
private storage = UploadFactory.createStorage();
constructor(
private _openaiService: OpenaiService,
private _falService: FalService
) {
super();
}
async process(
prompt: Prompt[],
output: 'vertical' | 'horizontal',
customParams: { voice: string }
): Promise<URL> {
const list = await this._openaiService.generateSlidesFromText(
prompt[0].value
);
const generated = await Promise.all(
list.reduce((all, current) => {
all.push(
new Promise(async (res) => {
res({
len: 0,
url: await this._falService.generateImageFromText(
'ideogram/v2',
current.imagePrompt,
output === 'vertical'
),
});
})
);
all.push(
new Promise(async (res) => {
const buffer = Buffer.from(
await (
await limit(() =>
fetch(
`https://api.elevenlabs.io/v1/text-to-speech/${customParams.voice}?output_format=mp3_44100_128`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'xi-api-key': process.env.ELEVENSLABS_API_KEY || '',
},
body: JSON.stringify({
text: current.voiceText,
model_id: 'eleven_multilingual_v2'
}),
}
)
)
).arrayBuffer()
);
const { path } = await this.storage.uploadFile({
buffer,
mimetype: 'audio/mp3',
size: buffer.length,
path: '',
fieldname: '',
destination: '',
stream: new Readable(),
filename: '',
originalname: '',
encoding: '',
});
res({
len: await getAudioDuration(buffer),
url:
path.indexOf('http') === -1
? process.env.FRONTEND_URL +
'/' +
process.env.NEXT_PUBLIC_UPLOAD_STATIC_DIRECTORY +
path
: path,
});
})
);
return all;
}, [] as Promise<any>[])
);
const split = chunk(generated, 2);
const srt = stringifySync(
list
.reduce((all, current, index) => {
const start = all.length ? all[all.length - 1].end : 0;
const end = start + split[index][1].len * 1000 + 1000;
all.push({
start: start,
end: end,
text: current.voiceText,
});
return all;
}, [] as { start: number; end: number; text: string }[])
.map((item) => ({
type: 'cue',
data: item,
})),
{ format: 'SRT' }
);
const { results } = await transloadit.createAssembly({
uploads: {
'subtitles.srt': srt,
},
waitForCompletion: true,
params: {
steps: {
...split.reduce((all, current, index) => {
all[`image${index}`] = {
robot: '/http/import',
url: current[0].url,
};
all[`audio${index}`] = {
robot: '/http/import',
url: current[1].url,
};
all[`merge${index}`] = {
use: [
{
name: `image${index}`,
as: 'image',
},
{
name: `audio${index}`,
as: 'audio',
},
],
robot: '/video/merge',
duration: current[1].len + 1,
audio_delay: 0.5,
preset: 'hls-1080p',
resize_strategy: 'min_fit',
loop: true,
};
return all;
}, {} as any),
concatenated: {
robot: '/video/concat',
result: false,
video_fade_seconds: 0.5,
use: split.map((p, index) => ({
name: `merge${index}`,
as: `video_${index + 1}`,
})),
},
subtitled: {
robot: '/video/subtitle',
result: true,
preset: 'hls-1080p',
use: {
bundle_steps: true,
steps: [
{
name: 'concatenated',
as: 'video',
},
{
name: ':original',
as: 'subtitles',
},
],
},
position: 'top',
font_size: 10,
subtitles_type: 'burned',
},
},
},
});
return results.subtitled[0].url;
}
async loadVoices(data: any) {
const { voices } = await (
await fetch(
'https://api.elevenlabs.io/v2/voices?page_size=40&category=premade',
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
'xi-api-key': process.env.ELEVENSLABS_API_KEY || '',
},
}
)
).json();
return {
voices: voices.map((voice: any) => ({
id: voice.voice_id,
name: voice.name,
preview_url: voice.preview_url,
})),
};
}
}

View File

@ -0,0 +1,40 @@
import { Injectable } from '@nestjs/common';
export interface Prompt {
type: 'prompt' | 'image';
value: string;
}
export type URL = string;
export abstract class VideoAbstract {
abstract process(
prompt: Prompt[],
output: 'vertical' | 'horizontal',
customParams?: any
): Promise<URL>;
}
export interface VideoParams {
identifier: string;
title: string;
description: string;
placement: 'text-to-image' | 'image-to-video' | 'video-to-video';
available: boolean;
}
export function Video(params: VideoParams) {
return function (target: any) {
// Apply @Injectable decorator to the target class
Injectable()(target);
// Retrieve existing metadata or initialize an empty array
const existingMetadata = Reflect.getMetadata('video', VideoAbstract) || [];
// Add the metadata information for this method
existingMetadata.push({ target, ...params });
// Define metadata on the class prototype (so it can be retrieved from the class)
Reflect.defineMetadata('video', existingMetadata, VideoAbstract);
};
}

View File

@ -0,0 +1,38 @@
import { Injectable } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import {
VideoAbstract,
VideoParams,
} from '@gitroom/nestjs-libraries/videos/video.interface';
@Injectable()
export class VideoManager {
constructor(private _moduleRef: ModuleRef) {}
getAllVideos(): any[] {
return (Reflect.getMetadata('video', VideoAbstract) || []).filter((f: any) => f.available).map(
(p: any) => ({
identifier: p.identifier,
title: p.title,
description: p.description,
placement: p.placement,
})
);
}
getVideoByName(
identifier: string
): (VideoParams & { instance: VideoAbstract }) | undefined {
const video = (Reflect.getMetadata('video', VideoAbstract) || []).find(
(p: any) => p.identifier === identifier
);
return {
...video,
instance: this._moduleRef.get(video.target, {
strict: false,
}),
};
}
}

View File

@ -0,0 +1,12 @@
import { Global, Module } from '@nestjs/common';
import { ImagesSlides } from '@gitroom/nestjs-libraries/videos/images-slides/images.slides';
import { VideoManager } from '@gitroom/nestjs-libraries/videos/video.manager';
@Global()
@Module({
providers: [ImagesSlides, VideoManager],
get exports() {
return this.providers;
},
})
export class VideoModule {}

View File

@ -159,6 +159,7 @@
"mime": "^3.0.0",
"mime-types": "^2.1.35",
"multer": "^1.4.5-lts.1",
"music-metadata": "^11.6.0",
"nestjs-command": "^3.1.4",
"nestjs-real-ip": "^3.0.1",
"next": "^14.2.14",
@ -169,6 +170,7 @@
"nostr-tools": "^2.10.4",
"nx": "19.7.2",
"openai": "^4.47.1",
"p-limit": "^6.2.0",
"polotno": "^2.10.5",
"posthog-js": "^1.178.0",
"react": "18.3.1",
@ -197,12 +199,14 @@
"simple-statistics": "^7.8.3",
"stripe": "^15.5.0",
"striptags": "^3.2.0",
"subtitle": "4.2.2-alpha.0",
"sweetalert2": "11.4.8",
"swr": "^2.2.5",
"tailwind-scrollbar": "^3.1.0",
"tailwindcss": "3.4.17",
"tailwindcss-rtl": "^0.9.0",
"tldts": "^6.1.47",
"transloadit": "^3.0.2",
"tslib": "^2.3.0",
"tweetnacl": "^1.0.3",
"twitter-api-v2": "^1.23.2",

File diff suppressed because it is too large Load Diff