Skip to content

Commit

Permalink
- Activities system work
Browse files Browse the repository at this point in the history
  • Loading branch information
Lamarcke committed Apr 23, 2024
1 parent 6b61ed8 commit 3516639
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 8 deletions.
2 changes: 1 addition & 1 deletion server_swagger.json

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions src/activities/activities-feed/activities-feed.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { HttpException, HttpStatus, Inject, Injectable } from "@nestjs/common";
import { HttpException, HttpStatus, Injectable } from "@nestjs/common";
import { Activity } from "../activities-repository/entities/activity.entity";
import {
ActivitiesFeedRequestDto,
ActivityFeedCriteria,
} from "./dto/activities-feed-request.dto";
import { ActivitiesRepositoryService } from "../activities-repository/activities-repository.service";
import { CACHE_MANAGER } from "@nestjs/cache-manager";
import { Cache } from "cache-manager";
import { TPaginationData } from "../../utils/pagination/pagination-response.dto";
import { ActivityType } from "../activities-queue/activities-queue.constants";
import { FollowService } from "../../follow/follow.service";
Expand Down Expand Up @@ -84,7 +82,7 @@ export class ActivitiesFeedService {
userId: string,
dto: ActivitiesFeedRequestDto,
): Promise<TPaginationData<Activity>> {
const [followedUsersIds] = await this.followService.getFollowerData({
const [followedUsersIds] = await this.followService.getFollowerInfo({
targetUserId: userId,
criteria: "followers",
offset: 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
Controller,
Get,
Query,
UseGuards,
UseInterceptors,
} from "@nestjs/common";
import { ApiOkResponse, ApiTags } from "@nestjs/swagger";
import { FindLatestActivitiesDto } from "./dto/find-latest-activities.dto";
import { ActivitiesRepositoryService } from "./activities-repository.service";
import { ActivitiesPaginatedResponseDto } from "./dto/activities-paginated-response.dto";
import { PaginationInterceptor } from "../../interceptor/pagination.interceptor";
import { AuthGuard } from "../../auth/auth.guard";
import { Public } from "../../auth/public.decorator";

@Controller("activities")
@ApiTags("activities")
@UseGuards(AuthGuard)
export class ActivitiesRepositoryController {
constructor(
private readonly activitiesRepositoryService: ActivitiesRepositoryService,
) {}

@Get()
@ApiOkResponse({
type: ActivitiesPaginatedResponseDto,
})
@UseInterceptors(PaginationInterceptor)
@Public()
async findLatest(@Query() dto: FindLatestActivitiesDto) {
return this.activitiesRepositoryService.findLatest(dto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { ActivitiesRepositoryService } from "./activities-repository.service";
import { TypeOrmModule } from "@nestjs/typeorm";
import { Activity } from "./entities/activity.entity";
import { StatisticsQueueModule } from "../../statistics/statistics-queue/statistics-queue.module";
import { ActivitiesRepositoryController } from './activities-repository.controller';

@Module({
imports: [TypeOrmModule.forFeature([Activity]), StatisticsQueueModule],
providers: [ActivitiesRepositoryService],
exports: [ActivitiesRepositoryService],
controllers: [ActivitiesRepositoryController],
})
export class ActivitiesRepositoryModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
} from "../activities-queue/activities-queue.constants";
import { StatisticsQueueService } from "../../statistics/statistics-queue/statistics-queue.service";
import { StatisticsSourceType } from "../../statistics/statistics.constants";
import { FindLatestActivitiesDto } from "./dto/find-latest-activities.dto";
import { buildBaseFindOptions } from "../../utils/buildBaseFindOptions";

@Injectable()
export class ActivitiesRepositoryService {
Expand Down Expand Up @@ -107,4 +109,14 @@ export class ActivitiesRepositoryService {
async findOneBy(by: FindOneOptions<Activity>) {
return await this.activitiesRepository.findOne(by);
}

async findLatest(dto: FindLatestActivitiesDto) {
const baseFindOptions = buildBaseFindOptions(dto);
return await this.findLatestBy({
...baseFindOptions,
where: {
profileUserId: dto.userId,
},
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Activity } from "../entities/activity.entity";
import { PaginationInfo } from "../../../utils/pagination/pagination-response.dto";

export class ActivitiesPaginatedResponseDto {
data: Activity[];
pagination: PaginationInfo;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { IsOptional, IsString, Length } from "class-validator";
import { OmitType } from "@nestjs/swagger";
import { BaseFindDto } from "../../../utils/base-find.dto";
import { Activity } from "../entities/activity.entity";

export class FindLatestActivitiesDto extends OmitType(BaseFindDto<Activity>, [
"orderBy",
"search",
]) {
@IsOptional()
@IsString()
@Length(36)
userId?: string;
}
2 changes: 1 addition & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function getRedisConfig() {
autoLoadEntities: true,
// Never turn this on. Use migrations instead.
synchronize: false,
logging: false,

/**
* Allows us to cache select queries using ioredis. Default duration of 1000ms.
* https://orkhan.gitbook.io/typeorm/docs/caching
Expand Down
2 changes: 1 addition & 1 deletion src/follow/follow.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class FollowController {
@Public()
@HttpCode(HttpStatus.OK)
async getFollowInfo(@Body() dto: FollowInfoRequestDto) {
return await this.followService.getFollowerData(dto);
return await this.followService.getFollowerInfo(dto);
}

@Post()
Expand Down
2 changes: 1 addition & 1 deletion src/follow/follow.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class FollowService {
* Gets followers or followed user ids with pagination data.
* @param dto
*/
public async getFollowerData(
public async getFollowerInfo(
dto: FollowInfoRequestDto,
): Promise<TPaginationData<string>> {
const baseFindOptions = buildBaseFindOptions(dto);
Expand Down
1 change: 1 addition & 0 deletions src/statistics/game-statistics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export class GameStatisticsService implements StatisticsService {
.addOrderBy(`s.viewsCount`, `DESC`)
.skip(0)
.take(fixedStatisticsLimit)
.printSql()
.cache(`trending-games-statistics`, hours(6))
.getMany();

Expand Down

0 comments on commit 3516639

Please sign in to comment.