-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/check contact request allowed (#267)
* feat: check for contact request allowed * fix: status code * fix: mail template * feat: reply to
- Loading branch information
Showing
4 changed files
with
212 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { SupabaseClient } from "npm:@supabase/supabase-js"; | ||
import { sub } from "npm:date-fns"; | ||
|
||
export interface CheckResult { | ||
isAllowed: boolean; | ||
reason: string | undefined; | ||
lookupData: ContactRequestLookupData | undefined; | ||
} | ||
|
||
export interface ContactRequestLookupData { | ||
senderUsername: string; | ||
senderEmail: string; | ||
senderUserId: string; | ||
recipientUserId: string; | ||
} | ||
|
||
export async function checkIfContactRequestIsAllowed( | ||
recipientContactName: string, | ||
token: string, | ||
supabaseClient: SupabaseClient, | ||
supabaseServiceRoleClient: SupabaseClient | ||
): Promise<CheckResult> { | ||
// Get the user (= sender) data from the token | ||
const { data: senderData, error: senderDataError } = | ||
await supabaseClient.auth.getUser(token); | ||
|
||
console.log(senderData); | ||
|
||
if (senderDataError) { | ||
console.log(senderDataError); | ||
return { isAllowed: false, reason: "unauthorized", lookupData: undefined }; | ||
} | ||
|
||
// Lookup the sender username | ||
const { data: senderLookupData, error: senderLookupDataError } = | ||
await supabaseServiceRoleClient | ||
.from("profiles") | ||
.select("*") | ||
.eq("id", senderData.user.id) | ||
.single(); | ||
|
||
console.log(senderLookupData); | ||
|
||
if (senderLookupDataError) { | ||
console.log(senderLookupDataError); | ||
return { isAllowed: false, reason: "not_found", lookupData: undefined }; | ||
} | ||
|
||
// Lookup the recipient user id | ||
const { data: recipientData, error: recipientDataError } = | ||
await supabaseServiceRoleClient | ||
.from("profiles") | ||
.select("*") | ||
.eq("username", recipientContactName) | ||
.single(); | ||
|
||
if (recipientDataError) { | ||
console.log(recipientDataError); | ||
return { isAllowed: false, reason: "not_found", lookupData: undefined }; | ||
} | ||
|
||
// Check if the user has already tried to contact the recipient | ||
const { data: requestsToRecipient, error: requestsToRecipientError } = | ||
await supabaseClient | ||
.from("contact_requests") | ||
.select("*") | ||
.eq("user_id", senderData.user.id) | ||
.eq("contact_id", recipientData.id) | ||
.not("contact_mail_id", "is", null); // only count sent emails | ||
|
||
if (requestsToRecipientError) { | ||
console.log(requestsToRecipientError); | ||
return { | ||
isAllowed: false, | ||
reason: "internal_server_error", | ||
lookupData: undefined, | ||
}; | ||
} | ||
|
||
if (requestsToRecipient.length > 0) { | ||
return { | ||
isAllowed: false, | ||
reason: "already_contacted_the_recipient_before", | ||
lookupData: undefined, | ||
}; | ||
} | ||
|
||
// Check if the user has sent 3 contact requests in the last 24 hours | ||
const { data: requestsOfLast24h, error: requestsOfLast24hError } = | ||
await supabaseClient | ||
.from("contact_requests") | ||
.select("*") | ||
.eq("user_id", senderData.user.id) | ||
.not("contact_mail_id", "is", null) // only count sent emails | ||
.gt("created_at", sub(new Date(), { days: 1 }).toISOString()); | ||
|
||
if (requestsOfLast24hError) { | ||
console.log(requestsOfLast24hError); | ||
return { | ||
isAllowed: false, | ||
reason: "internal_server_error", | ||
lookupData: undefined, | ||
}; | ||
} | ||
|
||
if (requestsOfLast24h.length >= 3) { | ||
return { | ||
isAllowed: false, | ||
reason: "already_sent_more_than_3_contact_requests", | ||
lookupData: undefined, | ||
}; | ||
} | ||
|
||
return { | ||
isAllowed: true, | ||
reason: undefined, | ||
lookupData: { | ||
senderUsername: senderLookupData.username, | ||
senderEmail: senderData.user.email, | ||
senderUserId: senderData.user.id, | ||
recipientUserId: recipientData.id, | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { createClient } from "https://esm.sh/@supabase/supabase-js@2"; | ||
import { checkIfContactRequestIsAllowed } from "../_shared/checks.ts"; | ||
import { corsHeaders } from "../_shared/cors.ts"; | ||
|
||
const SUPABASE_URL = Deno.env.get("SUPABASE_URL"); | ||
const SUPABASE_ANON_KEY = Deno.env.get("SUPABASE_ANON_KEY"); | ||
const SUPABASE_SERVICE_ROLE_KEY = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY"); | ||
|
||
const handler = async (_request: Request): Promise<Response> => { | ||
if (_request.method === "OPTIONS") { | ||
return new Response(null, { headers: corsHeaders, status: 204 }); | ||
} | ||
|
||
const { recipientContactName } = await _request.json(); | ||
|
||
const authHeader = _request.headers.get("Authorization")!; | ||
|
||
const supabaseClient = createClient(SUPABASE_URL, SUPABASE_ANON_KEY, { | ||
global: { headers: { Authorization: authHeader } }, | ||
}); | ||
|
||
const supabaseServiceRoleClient = createClient( | ||
SUPABASE_URL, | ||
SUPABASE_SERVICE_ROLE_KEY | ||
); | ||
|
||
const token = authHeader.replace("Bearer ", ""); | ||
|
||
const { isAllowed, reason } = await checkIfContactRequestIsAllowed( | ||
recipientContactName, | ||
token, | ||
supabaseClient, | ||
supabaseServiceRoleClient | ||
); | ||
|
||
if (!isAllowed) { | ||
return new Response( | ||
JSON.stringify({ | ||
isContactRequestAllowed: false, | ||
reason, | ||
}), | ||
{ | ||
status: 200, // We have to use 200 here to allow the client to read the response body | ||
headers: { | ||
...corsHeaders, | ||
"Content-Type": "application/json", | ||
}, | ||
} | ||
); | ||
} | ||
|
||
return new Response( | ||
JSON.stringify({ | ||
isContactRequestAllowed: true, | ||
reason: undefined, | ||
}), | ||
{ | ||
status: 200, | ||
headers: { | ||
...corsHeaders, | ||
"Content-Type": "application/json", | ||
}, | ||
} | ||
); | ||
}; | ||
|
||
Deno.serve(handler); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters