Skip to content

Commit

Permalink
- Activities and follow system work
Browse files Browse the repository at this point in the history
  • Loading branch information
Lamarcke committed Apr 19, 2024
1 parent 51559e3 commit 6b61ed8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion server_swagger.json

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions src/follow/follow.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Get,
HttpCode,
HttpStatus,
Param,
Post,
Query,
UseGuards,
Expand All @@ -22,6 +23,7 @@ import { Public } from "../auth/public.decorator";
import { FollowInfoRequestDto } from "./dto/follow-info-request.dto";
import { PaginationInterceptor } from "../interceptor/pagination.interceptor";
import { FollowInfoResponseDto } from "./dto/follow-info-response.dto";
import { UserFollow } from "./entity/user-follow.entity";

@Controller("follow")
@ApiTags("follow")
Expand All @@ -42,6 +44,15 @@ export class FollowController {
return this.followService.getStatus(followerUserId, followedUserId);
}

@Get(":id")
@ApiOkResponse({
status: 200,
type: UserFollow,
})
async getUserFollowById(@Param("id") userFollowId: number) {
return this.followService.findOneByIdOrFail(userFollowId);
}

@Post("info")
@UseInterceptors(PaginationInterceptor)
@ApiOkResponse({
Expand Down
7 changes: 6 additions & 1 deletion src/follow/follow.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import { TypeOrmModule } from "@nestjs/typeorm";
import { UserFollow } from "./entity/user-follow.entity";
import { FollowController } from "./follow.controller";
import { NotificationsModule } from "../notifications/notifications.module";
import { ActivitiesQueueModule } from "../activities/activities-queue/activities-queue.module";

@Module({
imports: [TypeOrmModule.forFeature([UserFollow]), NotificationsModule],
imports: [
TypeOrmModule.forFeature([UserFollow]),
NotificationsModule,
ActivitiesQueueModule,
],
providers: [FollowService],
controllers: [FollowController],
exports: [FollowService],
Expand Down
31 changes: 30 additions & 1 deletion src/follow/follow.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
import { FollowInfoRequestDto } from "./dto/follow-info-request.dto";
import { buildBaseFindOptions } from "../utils/buildBaseFindOptions";
import { TPaginationData } from "../utils/pagination/pagination-response.dto";
import { ActivitiesQueueService } from "../activities/activities-queue/activities-queue.service";
import { ActivityType } from "../activities/activities-queue/activities-queue.constants";

@Injectable()
export class FollowService {
Expand All @@ -20,8 +22,28 @@ export class FollowService {
@InjectRepository(UserFollow)
private userFollowRepository: Repository<UserFollow>,
private readonly notificationsQueue: NotificationsQueueService,
private readonly activitiesQueueService: ActivitiesQueueService,
) {}

public async findOneById(id: number) {
return this.userFollowRepository.findOne({
where: {
id,
},
});
}

public async findOneByIdOrFail(id: number) {
const entity = await this.findOneById(id);
if (!entity) {
throw new HttpException(
"No user follow matches specified id",
HttpStatus.NOT_FOUND,
);
}
return entity;
}

public async registerFollow(
followerUserId: string,
followedUserId: string,
Expand All @@ -33,21 +55,28 @@ export class FollowService {
);
}
try {
await this.userFollowRepository.save({
const persistedEntry = await this.userFollowRepository.save({
follower: {
userId: followerUserId,
},
followed: {
userId: followedUserId,
},
});

this.notificationsQueue.registerNotification({
userId: followerUserId,
targetUserId: followedUserId,
category: ENotificationCategory.FOLLOW,
sourceType: ENotificationSourceType.PROFILE,
sourceId: followerUserId,
});

this.activitiesQueueService.register({
type: ActivityType.FOLLOW,
sourceId: persistedEntry.id,
profileUserId: followerUserId,
});
} catch (e) {
this.logger.error(e);
throw new HttpException(
Expand Down

0 comments on commit 6b61ed8

Please sign in to comment.