Skip to content

Commit 9826f98

Browse files
authored
Merge pull request #369 from Quickchive/develop
Develop to master
2 parents 4a22792 + 991792b commit 9826f98

File tree

7 files changed

+56
-56
lines changed

7 files changed

+56
-56
lines changed

src/categories/category.controller.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
} from './dtos/load-personal-categories.dto';
4242
import { User } from '../users/entities/user.entity';
4343
import { CategoryService } from './category.service';
44+
import { AutoCategorizeRequest } from './dtos/auto-categorize.dto';
4445

4546
@Controller('categories')
4647
@ApiTags('Category')
@@ -167,7 +168,7 @@ export class CategoryController {
167168
@Get('auto-categorize')
168169
async autoCategorize(
169170
@AuthUser() user: User,
170-
@Query('link') link: string,
171+
@Query() { link }: AutoCategorizeRequest,
171172
): Promise<AutoCategorizeOutput> {
172173
return this.categoryService.autoCategorize(user, link);
173174
}

src/categories/category.service.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -477,11 +477,24 @@ export class CategoryService {
477477

478478
async autoCategorizeWithId(user: User, link: string) {
479479
try {
480-
const categories = await this.categoryRepository.findByUserId(user.id);
481-
if (categories.length === 0) {
480+
const _categories = await this.categoryRepository.findByUserId(user.id);
481+
if (_categories.length === 0) {
482482
throw new NotFoundException('Categories not found');
483483
}
484484

485+
const categories = _categories.map((category) => ({
486+
...category,
487+
depth: 0,
488+
}));
489+
490+
categories.map((category, index) => {
491+
categories.slice(index + 1).map((subCategory) => {
492+
if (subCategory.parentId && subCategory.parentId === category.id) {
493+
subCategory.depth = category.depth + 1;
494+
}
495+
});
496+
});
497+
485498
const { title, siteName, description } = await getLinkInfo(link);
486499

487500
const content = await getLinkContent(link);
@@ -497,18 +510,22 @@ You can only answer a single category name. Here is the article's information:
497510
description && `description: "${description.trim()}"`
498511
}</description>
499512
<siteName>${siteName && `site name: "${siteName.trim()}"`}</siteName>
500-
Please provide the most suitable category among the following. Here is Category options: [${[
501-
...categories,
502-
'None',
503-
].join(', ')}]
504513
505514
Given the following categories, please provide the most suitable category for the article.
515+
- The deeper the category depth, the more specific the category is.
516+
- If the 1, 2, and 3 depth categories are equally worthy of saving links, then the deeper categories should be recommended more.
506517
<categories>${categories
507518
.map((category) =>
508-
JSON.stringify({ id: category.id, name: category.name }),
519+
JSON.stringify({
520+
id: category.id,
521+
name: category.name,
522+
depth: category.depth,
523+
}),
509524
)
510525
.join('\n')}</categories>
511526
527+
If there's no suitable category, must provide reply with "None".
528+
512529
Present your reply options in JSON format below.
513530
\`\`\`json
514531
{
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import { IsString, IsUrl } from 'class-validator';
3+
4+
export class AutoCategorizeRequest {
5+
@ApiProperty({
6+
description: '링크',
7+
type: String,
8+
})
9+
@IsString()
10+
@IsUrl({}, { message: '링크가 올바르지 않습니다.' })
11+
link: string;
12+
}

src/categories/v2/category.v2.controller.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { CategoryService } from '../category.service';
1010
import { AuthUser } from '../../auth/auth-user.decorator';
1111
import { User } from '../../users/entities/user.entity';
1212
import { RecommendedCategoryResponseDto } from './dto/recommended-category-response.dto';
13+
import { AutoCategorizeRequest } from '../dtos/auto-categorize.dto';
1314

1415
@Controller('v2/categories')
1516
@ApiTags('Category v2')
@@ -29,7 +30,10 @@ export class CategoryV2Controller {
2930
type: RecommendedCategoryResponseDto,
3031
})
3132
@Get('auto-categorize')
32-
async autoCategorize(@AuthUser() user: User, @Query('link') link: string) {
33+
async autoCategorize(
34+
@AuthUser() user: User,
35+
@Query() { link }: AutoCategorizeRequest,
36+
) {
3337
const { category } = await this.categoryService.autoCategorizeWithId(
3438
user,
3539
link,

src/contents/dtos/content.dto.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
IsOptional,
1515
IsPositive,
1616
IsString,
17+
IsUrl,
1718
} from 'class-validator';
1819
import { CoreOutput } from '../../common/dtos/output.dto';
1920
import { Content } from '../entities/content.entity';
@@ -22,6 +23,7 @@ import { Type } from 'class-transformer';
2223
export class AddContentBodyDto {
2324
@ApiProperty({ example: 'ex.com', description: '아티클 주소' })
2425
@IsString()
26+
@IsUrl({}, { message: '아티클 주소가 올바르지 않습니다.' })
2527
link: string;
2628

2729
@ApiPropertyOptional({

src/contents/util/content.util.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import {
22
BadRequestException,
3+
ForbiddenException,
34
InternalServerErrorException,
45
} from '@nestjs/common';
56
import * as cheerio from 'cheerio';
6-
import axios, { AxiosResponse } from 'axios';
7+
import axios, { AxiosError, AxiosResponse } from 'axios';
78

89
interface OGCrawlerOptions {
910
timeout?: number;
@@ -32,13 +33,13 @@ class OGCrawler {
3233
}
3334

3435
public async fetch(url: string): Promise<any> {
35-
try {
36-
// YouTube 비디오 ID 추출
37-
const videoId = this.extractVideoId(url);
38-
if (videoId) {
39-
return await this.fetchYouTubeData(videoId);
40-
}
36+
// YouTube 비디오 ID 추출
37+
const videoId = this.extractVideoId(url);
38+
if (videoId) {
39+
return await this.fetchYouTubeData(videoId);
40+
}
4141

42+
try {
4243
const response: AxiosResponse = await axios({
4344
method: 'get',
4445
url,
@@ -71,11 +72,10 @@ class OGCrawler {
7172

7273
return this.parse(response.data);
7374
} catch (error) {
74-
if (error instanceof Error) {
75-
throw new InternalServerErrorException(
76-
`Failed to fetch URL: ${error.message}`,
77-
);
75+
if (error instanceof AxiosError && error.response?.status === 403) {
76+
throw new ForbiddenException('og 데이터를 가져올 수 없는 링크입니다.');
7877
}
78+
7979
throw new InternalServerErrorException('An unknown error occurred');
8080
}
8181
}

src/database/ormconfig.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)