Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EN-7454] Messaging - Report user #205

Merged
merged 13 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ USE_SMS=
BITLY_TOKEN=

# Admins
[email protected]
[email protected]
[email protected]
[email protected]
Expand Down
2 changes: 1 addition & 1 deletion src/db/migrations/20240807082823-create-messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module.exports = {
},
content: {
allowNull: false,
type: Sequelize.STRING,
type: Sequelize.TEXT,
},
authorId: {
type: Sequelize.UUID,
Expand Down
2 changes: 2 additions & 0 deletions src/external-services/mailjet/mailjet.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ export const MailjetTemplates = {
INTERNAL_MESSAGE: 5625323,
INTERNAL_MESSAGE_CONFIRMATION: 5625495,
USER_EMAIL_VERIFICATION: 5899611,
USER_REPORTED_ADMIN: 6223181,
CONVERSATION_REPORTED_ADMIN: 6276909,
ONBOARDING_J1_BAO: 6129684,
ONBOARDING_J3_PROFILE_COMPLETION: 6129711,
} as const;
Expand Down
60 changes: 60 additions & 0 deletions src/external-services/slack/slack.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Injectable } from '@nestjs/common';
import { App, Block, KnownBlock } from '@slack/bolt';
import { User } from 'src/users/models';
import {
SlackBlockConfig,
slackChannels,
SlackMsgAction,
SlackMsgContext,
SlackMsgContextImage,
Expand Down Expand Up @@ -43,6 +45,24 @@ export class SlackService {
}
};

sendMessageUserReported = async (
userReporter: User,
userReported: User,
reason: string,
comment: string
): Promise<void> => {
return this.sendMessage(
slackChannels.ENTOURAGE_PRO_MODERATION,
this.generateProfileReportedBlocks(
userReporter,
userReported,
reason,
comment
),
`Le profil de ${userReported.firstName} ${userReported.lastName} a été signalé`
);
};

/**
* Generate a the slack blocks for a message
* @param param0 message configuration
Expand Down Expand Up @@ -82,6 +102,46 @@ export class SlackService {
return blocksBuffer;
};

/**
* Generate a slack message for a profile reported
* @param userReporter - The user who reported
* @param userReported - The user who was reported
* @param reason - The reason of the report
* @param comment - The comment of the report
* @returns blocks for the message
*/
generateProfileReportedBlocks = (
userReporter: User,
userReported: User,
reason: string,
comment: string
) => {
return this.generateSlackBlockMsg({
title: '🚨 Un profil a été signalé',
context: [
{
title: 'Signalé par',
content: `\n${userReporter.firstName} ${userReporter.lastName} <${userReporter.email}>`,
},
],
msgParts: [
{
content: `Profil signalé : ${userReported.firstName} ${userReported.lastName} <${userReported.email}>`,
},
{
content: `Raison du signalement : ${reason}`,
},
{
content: `Commentaire : ${comment}`,
},
],
});
};

/***************** */
/* Private methods */
/***************** */

/**
* Generate title block
* @param title - The title
Expand Down
44 changes: 44 additions & 0 deletions src/mails/mails.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
} from 'src/messages/messages.types';
import { InternalMessage } from 'src/messages/models';
import { ExternalMessage } from 'src/messages/models/external-message.model';
import { ReportConversationDto } from 'src/messaging/dto/report-conversation.dto';
import { Conversation } from 'src/messaging/models';
import { Opportunity, OpportunityUser } from 'src/opportunities/models';
import {
ContactEmployerType,
Expand All @@ -25,6 +27,7 @@ import {
import { getMailjetVariablesForPrivateOrPublicOffer } from 'src/opportunities/opportunities.utils';
import { QueuesService } from 'src/queues/producers/queues.service';
import { Jobs } from 'src/queues/queues.types';
import { ReportAbuseUserProfileDto } from 'src/user-profiles/dto/report-abuse-user-profile.dto';
import { User } from 'src/users/models';
import {
CandidateUserRoles,
Expand Down Expand Up @@ -742,6 +745,47 @@ export class MailsService {
},
});
}

async sendUserReportedMail(
reportAbuseUserProfileDto: ReportAbuseUserProfileDto,
reportedUser: User,
reporterUser: User
) {
const { candidatesAdminMail } = getAdminMailsFromZone(reportedUser.zone);

await this.queuesService.addToWorkQueue(Jobs.SEND_MAIL, {
toEmail: candidatesAdminMail,
templateId: MailjetTemplates.USER_REPORTED_ADMIN,
replyTo: candidatesAdminMail,
variables: {
reportedFirstName: reportedUser.firstName,
reportedLastName: reportedUser.lastName,
reportedEmail: reportedUser.email,
reporterFirstName: reporterUser.firstName,
reporterLastName: reporterUser.lastName,
reporterEmail: reporterUser.email,
...reportAbuseUserProfileDto,
},
});
}

async sendConversationReportedMail(
reportConversationDto: ReportConversationDto,
reportedConversation: Conversation,
reporterUser: User
) {
await this.queuesService.addToWorkQueue(Jobs.SEND_MAIL, {
toEmail: process.env.ADMIN_NATIONAL || '[email protected]',
templateId: MailjetTemplates.CONVERSATION_REPORTED_ADMIN,
variables: {
reporterFirstName: reporterUser.firstName,
reporterLastName: reporterUser.lastName,
reporterEmail: reporterUser.email,
reportedConversationId: reportedConversation.id,
...reportConversationDto,
},
});
}
}

const getRoleString = (user: User): string => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';

export class ReportAbuseDto {
export class ReportConversationDto {
@ApiProperty()
@IsString()
reason: string;

@ApiProperty()
@IsString()
comment: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ import {
} from '@nestjs/common';
import { plainToInstance } from 'class-transformer';
import { validate } from 'class-validator';
import { ReportAbuseDto } from './report-abuse.dto';
import { ReportConversationDto } from './report-conversation.dto';

export class ReportAbusePipe
implements PipeTransform<ReportAbuseDto, Promise<ReportAbuseDto>>
implements
PipeTransform<ReportConversationDto, Promise<ReportConversationDto>>
{
private static toValidate(metatype: Function): boolean {
const types: Function[] = [String, Boolean, Number, Array, Object];
return !types.includes(metatype);
}

async transform(
value: ReportAbuseDto,
value: ReportConversationDto,
{ metatype }: ArgumentMetadata
): Promise<ReportAbuseDto> {
): Promise<ReportConversationDto> {
if (!metatype || !ReportAbusePipe.toValidate(metatype)) {
return value;
}
Expand Down
24 changes: 17 additions & 7 deletions src/messaging/messaging.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
import { ApiBearerAuth } from '@nestjs/swagger';
import { UserPayload } from 'src/auth/guards';
import { CreateMessagePipe, CreateMessageDto } from './dto';
import { ReportAbuseDto } from './dto/report-abuse.dto';
import { ReportAbusePipe } from './dto/report-abuse.pipe';
import { ReportConversationDto } from './dto/report-conversation.dto';
import { ReportAbusePipe } from './dto/report-conversation.pipe';
import { MessagingService } from './messaging.service';

@ApiBearerAuth()
Expand All @@ -33,11 +33,12 @@ export class MessagingController {
@UserPayload('id', new ParseUUIDPipe()) userId: string,
@Param('conversationId', new ParseUUIDPipe()) conversationId: string
) {
await this.messagingService.setConversationHasSeen(conversationId, userId);
return this.messagingService.getConversationForUser(conversationId, userId);
}

@Post('messages')
async createInternalMessage(
async postMessage(
@UserPayload('id', new ParseUUIDPipe()) userId: string,
@Body(new CreateMessagePipe())
createMessageDto: CreateMessageDto
Expand All @@ -61,22 +62,31 @@ export class MessagingController {
// Add createMessageDto properties without participantIds
...createMessageDto,
});
return createdMessage;
// Set conversation as seen because the user has sent a message
await this.messagingService.setConversationHasSeen(
createMessageDto.conversationId,
userId
);
// Fetch the message to return it
const message = await this.messagingService.findOneMessage(
createdMessage.id
);
return message;
} catch (error) {
console.error(error);
}
}

@Post('conversation/:conversationId/report')
@Post('conversations/:conversationId/report')
async reportMessageAbuse(
@UserPayload('id', new ParseUUIDPipe()) userId: string,
@Param('conversationId', new ParseUUIDPipe()) conversationId: string,
@Body(new ReportAbusePipe())
reportAbuseDto: ReportAbuseDto
reportConversationDto: ReportConversationDto
) {
return this.messagingService.reportConversation(
conversationId,
reportAbuseDto.reason,
reportConversationDto,
userId
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/messaging/messaging.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Module } from '@nestjs/common';
import { SequelizeModule } from '@nestjs/sequelize';
import { SlackModule } from 'src/external-services/slack/slack.module';
import { MailsModule } from 'src/mails/mails.module';
import { UsersModule } from 'src/users/users.module';
import { MessagingController } from './messaging.controller';
import { MessagingService } from './messaging.service';
Expand All @@ -14,6 +15,7 @@ import { Message, Conversation, ConversationParticipant } from './models';
ConversationParticipant,
]),
SlackModule,
MailsModule,
UsersModule,
],
controllers: [MessagingController],
Expand Down
Loading