mirror of
https://github.com/4ev-link/4ev.link.git
synced 2026-01-13 16:18:05 +00:00
Fix: Add ban check and update ntfy actions
This commit is contained in:
@@ -1,16 +1,18 @@
|
|||||||
const ntfy = (env,title,msg,act,p=2) =>
|
const ntfy = (env,title,msg,slug,user,p=2) => {
|
||||||
env.NTFY_TOPIC
|
if(!env.NTFY_TOPIC) return Promise.resolve();
|
||||||
? fetch(`https://ntfy.sh/${env.NTFY_TOPIC}`,{
|
const origin = "https://4ev.link";
|
||||||
|
const actions = `view, Seize, ${origin}/admin?slug=${slug}; view, Ban User, ${origin}/admin?user=${user}`;
|
||||||
|
return fetch(`https://ntfy.sh/${env.NTFY_TOPIC}`,{
|
||||||
method:"POST",
|
method:"POST",
|
||||||
headers:{
|
headers:{
|
||||||
"Title":`✏️ ${title}`,
|
"Title":`✏️ ${title}`,
|
||||||
"Priority":String(p),
|
"Priority":String(p),
|
||||||
"Content-Type":"text/plain",
|
"Content-Type":"text/plain",
|
||||||
...(act?{"Actions":`view, Seize, ${act}`}:{})
|
"Actions": actions
|
||||||
},
|
},
|
||||||
body:msg
|
body:msg
|
||||||
}).catch(()=>{})
|
}).catch(()=>{});
|
||||||
: Promise.resolve();
|
};
|
||||||
|
|
||||||
export async function onRequestPost({ request, env }) {
|
export async function onRequestPost({ request, env }) {
|
||||||
try {
|
try {
|
||||||
@@ -37,12 +39,17 @@ export async function onRequestPost({ request, env }) {
|
|||||||
return new Response("Missing fields",{ status:400 });
|
return new Response("Missing fields",{ status:400 });
|
||||||
|
|
||||||
const user = await env.D1_EV
|
const user = await env.D1_EV
|
||||||
.prepare("SELECT pass_hash, custom_slugs FROM users WHERE username = ?")
|
.prepare("SELECT pass_hash, custom_slugs, banned_until FROM users WHERE username = ?")
|
||||||
.bind(username)
|
.bind(username)
|
||||||
.first();
|
.first();
|
||||||
if (user?.pass_hash !== pass_hash)
|
if (user?.pass_hash !== pass_hash)
|
||||||
return new Response("Invalid credentials",{ status:401 });
|
return new Response("Invalid credentials",{ status:401 });
|
||||||
|
|
||||||
|
if (user.banned_until && user.banned_until > Date.now()) {
|
||||||
|
const days = Math.ceil((user.banned_until - Date.now()) / 86400000);
|
||||||
|
return new Response(`Account banned for ${days} more days.`, { status: 403 });
|
||||||
|
}
|
||||||
|
|
||||||
let slugs = [];
|
let slugs = [];
|
||||||
try { slugs = JSON.parse(user.custom_slugs) } catch {}
|
try { slugs = JSON.parse(user.custom_slugs) } catch {}
|
||||||
if (!Array.isArray(slugs) || !slugs.includes(slug))
|
if (!Array.isArray(slugs) || !slugs.includes(slug))
|
||||||
@@ -69,7 +76,8 @@ export async function onRequestPost({ request, env }) {
|
|||||||
env,
|
env,
|
||||||
`link-${evt}`,
|
`link-${evt}`,
|
||||||
`event=${evt}\nuser=${username}\nslug=${slug}\ndestination=${dest_no_proto}\nanalytics_enabled=${!!analytics_enabled}`,
|
`event=${evt}\nuser=${username}\nslug=${slug}\ndestination=${dest_no_proto}\nanalytics_enabled=${!!analytics_enabled}`,
|
||||||
`${new URL(request.url).origin}/admin?slug=${slug}`,
|
slug,
|
||||||
|
username,
|
||||||
2
|
2
|
||||||
)
|
)
|
||||||
]);
|
]);
|
||||||
|
|||||||
Reference in New Issue
Block a user