forked from xiangwang25/whale-town-end-v2
74 lines
2.3 KiB
TypeScript
74 lines
2.3 KiB
TypeScript
import { Controller, Get, Header, Param, Query, Res } from '@nestjs/common';
|
||
import { ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger';
|
||
import { Response } from 'express';
|
||
import { RankingsService } from './rankings.service';
|
||
import { RankingCategoryId } from './rankings.types';
|
||
|
||
@ApiTags('rankings')
|
||
@Controller('rankings')
|
||
export class RankingsController {
|
||
constructor(private readonly rankingsService: RankingsService) {}
|
||
|
||
@Get('datawhale-honor/avatar/:memberId')
|
||
@Header('Cache-Control', 'public, max-age=86400, stale-while-revalidate=604800')
|
||
@ApiOperation({ summary: '代理 Datawhale 荣誉榜成员头像' })
|
||
async getDatawhaleMemberAvatar(
|
||
@Param('memberId') memberId: string,
|
||
@Res() res: Response,
|
||
): Promise<void> {
|
||
const avatar = await this.rankingsService.getMemberAvatar(memberId);
|
||
res.type(avatar.contentType).send(avatar.body);
|
||
}
|
||
|
||
@Get('datawhale-honor')
|
||
@ApiOperation({
|
||
summary: '获取Datawhale荣誉榜',
|
||
description: '返回后端同步并计算后的Datawhale贡献者排行榜数据,供游戏荣誉榜UI使用。',
|
||
})
|
||
@ApiQuery({
|
||
name: 'category',
|
||
required: false,
|
||
description: '榜单分类:weekly_commits/night_owl/popularity/productive/social/rising/comprehensive',
|
||
})
|
||
@ApiQuery({
|
||
name: 'limit',
|
||
required: false,
|
||
description: '返回数量,范围 3-10,默认 10',
|
||
})
|
||
@ApiQuery({
|
||
name: 'refresh',
|
||
required: false,
|
||
description: '为 true 时先实时同步 Datawhale 公开数据,再返回当前榜单',
|
||
})
|
||
async getDatawhaleHonorRanking(
|
||
@Query('category') category?: RankingCategoryId,
|
||
@Query('limit') limit?: string,
|
||
@Query('refresh') refresh?: string,
|
||
) {
|
||
const data = await this.rankingsService.getDatawhaleHonorRanking(
|
||
category,
|
||
Number(limit || 10),
|
||
refresh === 'true',
|
||
);
|
||
return {
|
||
success: true,
|
||
data,
|
||
message: '荣誉榜获取成功',
|
||
};
|
||
}
|
||
|
||
@Get('datawhale-honor/sync')
|
||
@ApiOperation({
|
||
summary: '手动同步Datawhale荣誉榜',
|
||
description: '开发调试用:立即从Datawhale公开数据源同步并返回默认榜单。',
|
||
})
|
||
async syncDatawhaleHonorRanking() {
|
||
const data = await this.rankingsService.syncNow();
|
||
return {
|
||
success: true,
|
||
data,
|
||
message: '荣誉榜同步成功',
|
||
};
|
||
}
|
||
}
|