Skip to content

Commit f536326

Browse files
authored
Added a route to send Profile Blocked Messages (#198)
* Added a route to sent Profile Blocked Messages * Modification to the message bot is sending * Authentication Added * Fixed Lint issues * Code Cleaning * Update variables.ts * Code Cleaning
1 parent 7f07d77 commit f536326

File tree

7 files changed

+196
-1
lines changed

7 files changed

+196
-1
lines changed

config/config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,32 @@ import {
1010
DEVELOPMENT_RDS_TRACKING_CHANNEL_URL,
1111
STAGING_RDS_TRACKING_CHANNEL_URL,
1212
} from "../src/constants/urls";
13+
import {
14+
DISCORD_PROFILE_SERVICE_HELP_GROUP,
15+
DISCORD_PROFILE_SERVICE_STAGING_HELP_GROUP,
16+
DISCORD_PROFILE_SERVICE_DEVELOPMENT_HELP_GROUP,
17+
} from "../src/constants/variables";
1318

1419
const config = (env: env) => {
1520
const environment: environment = {
1621
production: {
1722
RDS_BASE_API_URL: RDS_BASE_API_URL,
1823
VERIFICATION_SITE_URL: VERIFICATION_SITE_URL,
1924
TRACKING_CHANNEL_URL: RDS_TRACKING_CHANNEL_URL,
25+
PROFILE_SERVICE_HELP_GROUP_ID: DISCORD_PROFILE_SERVICE_HELP_GROUP,
2026
},
2127
staging: {
2228
RDS_BASE_API_URL: RDS_BASE_STAGING_API_URL,
2329
VERIFICATION_SITE_URL: STAGING_VERIFICATION_SITE_URL,
2430
TRACKING_CHANNEL_URL: STAGING_RDS_TRACKING_CHANNEL_URL,
31+
PROFILE_SERVICE_HELP_GROUP_ID: DISCORD_PROFILE_SERVICE_STAGING_HELP_GROUP,
2532
},
2633
default: {
2734
RDS_BASE_API_URL: RDS_BASE_DEVELOPMENT_API_URL,
2835
VERIFICATION_SITE_URL: DEVELOPMENT_VERIFICATION_SITE_URL,
2936
TRACKING_CHANNEL_URL: DEVELOPMENT_RDS_TRACKING_CHANNEL_URL,
37+
PROFILE_SERVICE_HELP_GROUP_ID:
38+
DISCORD_PROFILE_SERVICE_DEVELOPMENT_HELP_GROUP,
3039
},
3140
};
3241

src/constants/variables.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
export const SUPER_USER_ONE = "154585730465660929";
22
export const SUPER_USER_TWO = "1040700289348542566";
3+
4+
export const DISCORD_PROFILE_SERVICE_HELP_GROUP = "1209244798557360138";
5+
export const DISCORD_PROFILE_SERVICE_STAGING_HELP_GROUP = "1209248671170953236";
6+
export const DISCORD_PROFILE_SERVICE_DEVELOPMENT_HELP_GROUP =
7+
"1209237447083303014"; //Change this for your local environment

src/controllers/profileHandler.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { env } from "../typeDefinitions/default.types";
2+
import { sendProfileServiceBlockedMessage } from "../utils/sendProfileServiceBlockedMessage";
3+
import JSONResponse from "../utils/JsonResponse";
4+
import * as response from "../constants/responses";
5+
import { verifyAuthToken } from "../utils/verifyAuthToken";
6+
7+
export const sendProfileBlockedMessage = async (request: any, env: env) => {
8+
const authHeader = request.headers.get("Authorization");
9+
if (!authHeader) {
10+
return new JSONResponse(response.BAD_SIGNATURE);
11+
}
12+
try {
13+
await verifyAuthToken(authHeader, env);
14+
const messageRequest: { userId: string; reason: string } =
15+
await request.json();
16+
const { userId, reason } = messageRequest;
17+
await sendProfileServiceBlockedMessage(userId, reason, env);
18+
return new JSONResponse("Message sent in tracking channel on discord");
19+
} catch (e) {
20+
return new JSONResponse(response.BAD_SIGNATURE);
21+
}
22+
};

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { changeNickname } from "./controllers/changeNickname";
1919
import { getGuildMemberDetailsHandler } from "./controllers/getGuildMemberDetailsHandler";
2020
import { send } from "./handlers/scheduledEventHandler";
2121
import { generateInviteLink } from "./controllers/generateDiscordInvite";
22+
import { sendProfileBlockedMessage } from "./controllers/profileHandler";
2223

2324
const router = Router();
2425

@@ -54,6 +55,8 @@ router.put("/roles/add", addGroupRoleHandler);
5455

5556
router.delete("/roles", removeGuildRoleHandler);
5657

58+
router.post("/profile/blocked", sendProfileBlockedMessage);
59+
5760
router.post("/", async (request, env) => {
5861
const message: discordMessageRequest = await request.json();
5962

@@ -76,7 +79,7 @@ router.all("*", async () => {
7679

7780
export default {
7881
async fetch(request: Request, env: env): Promise<Response> {
79-
const apiUrls = ["/invite", "/roles"];
82+
const apiUrls = ["/invite", "/roles", "/profile/blocked"];
8083
const url = new URL(request.url);
8184
if (request.method === "POST" && !apiUrls.includes(url.pathname)) {
8285
const isVerifiedRequest = await verifyBot(request, env);

src/typeDefinitions/default.types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface variables {
1010
RDS_BASE_API_URL: string;
1111
VERIFICATION_SITE_URL: string;
1212
TRACKING_CHANNEL_URL: string;
13+
PROFILE_SERVICE_HELP_GROUP_ID: string;
1314
}
1415

1516
export interface discordCommand {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { env } from "../typeDefinitions/default.types";
2+
import config from "../../config/config";
3+
4+
export const generateStringToBeSent = (
5+
userId: string,
6+
reason: string,
7+
env: env
8+
) => {
9+
const helpGroupRoleId = config(env).PROFILE_SERVICE_HELP_GROUP_ID;
10+
return `Hello${userId ? ` <@${userId}>` : ""},\n${
11+
userId ? "Your" : "Someone's"
12+
} Profile Service is **BLOCKED** because of the below-mentioned reason.${
13+
userId
14+
? ` Please visit the [MY SITE](https://my.realdevsquad.com/identity) to fix this.\nIf you have any issue related to profile service, you can tag <@&${helpGroupRoleId}> and ask for help.`
15+
: ""
16+
}\n\n**Reason:** \`${reason ? reason : "No reason provided"}\``;
17+
};
18+
19+
export async function sendProfileServiceBlockedMessage(
20+
userId: string,
21+
reason: string,
22+
env: env
23+
): Promise<void> {
24+
const stringToBeSent = generateStringToBeSent(userId, reason, env);
25+
26+
const bodyObj = {
27+
content: stringToBeSent,
28+
};
29+
30+
const url = config(env).TRACKING_CHANNEL_URL;
31+
32+
await fetch(url, {
33+
method: "POST",
34+
body: JSON.stringify(bodyObj),
35+
headers: {
36+
"Content-Type": "application/json",
37+
Authorization: `Bot ${env.DISCORD_TOKEN}`,
38+
},
39+
});
40+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import config from "../../../config/config";
2+
import JSONResponse from "../../../src/utils/JsonResponse";
3+
import {
4+
generateStringToBeSent,
5+
sendProfileServiceBlockedMessage,
6+
} from "../../../src/utils/sendProfileServiceBlockedMessage";
7+
8+
describe("Send Profile Service Blocked Message", () => {
9+
const mockEnv = {
10+
BOT_PUBLIC_KEY: "xyz",
11+
DISCORD_GUILD_ID: "123",
12+
DISCORD_TOKEN: "abc",
13+
};
14+
15+
const mockData = {
16+
discordId: "12345678910111213",
17+
reason: "Unauthenticated access to profile service",
18+
};
19+
20+
test("should send the message if both userId and reason provided", async () => {
21+
const url = config(mockEnv).TRACKING_CHANNEL_URL;
22+
const data = {
23+
content: generateStringToBeSent(
24+
mockData.discordId,
25+
mockData.reason,
26+
mockEnv
27+
),
28+
};
29+
30+
jest
31+
.spyOn(global, "fetch")
32+
.mockImplementation(() => Promise.resolve(new JSONResponse("")));
33+
34+
await sendProfileServiceBlockedMessage(
35+
mockData.discordId,
36+
mockData.reason,
37+
mockEnv
38+
);
39+
40+
expect(global.fetch).toHaveBeenCalledWith(url, {
41+
method: "POST",
42+
headers: {
43+
"Content-Type": "application/json",
44+
Authorization: `Bot ${mockEnv.DISCORD_TOKEN}`,
45+
},
46+
body: JSON.stringify(data),
47+
});
48+
});
49+
50+
test("should send the message if userId not present", async () => {
51+
const data = {
52+
content: generateStringToBeSent("", mockData.reason, mockEnv),
53+
};
54+
const url = config(mockEnv).TRACKING_CHANNEL_URL;
55+
56+
jest
57+
.spyOn(global, "fetch")
58+
.mockImplementation(() => Promise.resolve(new JSONResponse("")));
59+
60+
await sendProfileServiceBlockedMessage("", mockData.reason, mockEnv);
61+
62+
expect(global.fetch).toHaveBeenCalledWith(url, {
63+
method: "POST",
64+
headers: {
65+
"Content-Type": "application/json",
66+
Authorization: `Bot ${mockEnv.DISCORD_TOKEN}`,
67+
},
68+
body: JSON.stringify(data),
69+
});
70+
});
71+
72+
test("should send the message if both are not present", async () => {
73+
const data = {
74+
content: generateStringToBeSent("", "", mockEnv),
75+
};
76+
const url = config(mockEnv).TRACKING_CHANNEL_URL;
77+
78+
jest
79+
.spyOn(global, "fetch")
80+
.mockImplementation(() => Promise.resolve(new JSONResponse("")));
81+
82+
await sendProfileServiceBlockedMessage("", "", mockEnv);
83+
84+
expect(global.fetch).toHaveBeenCalledWith(url, {
85+
method: "POST",
86+
headers: {
87+
"Content-Type": "application/json",
88+
Authorization: `Bot ${mockEnv.DISCORD_TOKEN}`,
89+
},
90+
body: JSON.stringify(data),
91+
});
92+
});
93+
94+
test("should send the message if reason is not present", async () => {
95+
const data = {
96+
content: generateStringToBeSent(mockData.discordId, "", mockEnv),
97+
};
98+
const url = config(mockEnv).TRACKING_CHANNEL_URL;
99+
100+
jest
101+
.spyOn(global, "fetch")
102+
.mockImplementation(() => Promise.resolve(new JSONResponse("")));
103+
104+
await sendProfileServiceBlockedMessage(mockData.discordId, "", mockEnv);
105+
106+
expect(global.fetch).toHaveBeenCalledWith(url, {
107+
method: "POST",
108+
headers: {
109+
"Content-Type": "application/json",
110+
Authorization: `Bot ${mockEnv.DISCORD_TOKEN}`,
111+
},
112+
body: JSON.stringify(data),
113+
});
114+
});
115+
});

0 commit comments

Comments
 (0)