Skip to content

Commit

Permalink
Refactor code to add lazy loading for images in TalkListContent
Browse files Browse the repository at this point in the history
  • Loading branch information
tako0614 committed Jul 18, 2024
1 parent afd8c3d commit 201ee77
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 57 deletions.
5 changes: 4 additions & 1 deletion islands/Register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,11 @@ function SendEmailRegisterRequest({ state, sitekeyv2, sitekeyv3 }: { state: any;
case "Already Registered":
alert("既に登録されています");
break;
case "rechapchav3":
alert("reCAPTCHAが無効です");
break;
default:
alert("エラーが発生しました");
alert("エラーが発生しました: "+ data.message);
break;
}
}
Expand Down
14 changes: 3 additions & 11 deletions models/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,10 @@ export const roomsSchema = new mongoose.Schema({
required: true,
default: [],
},
messageCount: {
type: Number,
default: 0,
},
latestmessage: {
type: String,
default: "",
},
latestMessageTime: {
type: Date,
readInfo: {
type: Map,
required: true,
default: Date.now,
default: {},
},
timestamp: { type: Date, default: Date.now },
});
Expand Down
25 changes: 24 additions & 1 deletion routes/api/v2/client/csrftoken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// -> { status: boolean, message: string, csrftoken: string }
import csrfToken from "../../../../models/csrftoken.ts";
import { getCookies } from "$std/http/cookie.ts";
import { load } from "$std/dotenv/mod.ts";
const env = await load();
export const handler = {
async GET(req: Request, ctx: any) {
if (!ctx.state.data.loggedIn && ctx.state.data.isSetUp === false) {
Expand All @@ -20,6 +22,24 @@ export const handler = {
const cookies = getCookies(req.headers);
const sessionid = cookies.sessionid;
const userid = ctx.state.data.userid;
const origin = req.headers.get('origin')
const origins = env["serverOrigin"].split(",")
//localhostだとoriginがnullになるので
/*
if(origin === null){
console.log(origins)
console.log(origin)
return new Response(
JSON.stringify({ status: false, message: "Invalid origin" }),
{ status: 400, headers: { "Content-Type": "application/json" } },
);
}
if (origins.includes(origin) === false) {
return new Response(
JSON.stringify({ status: false, message: "Invalid origin" }),
{ status: 400, headers: { "Content-Type": "application/json" } },
);
}*/
await csrfToken.create({
token: csrftoken,
sessionID: sessionid,
Expand All @@ -30,7 +50,10 @@ export const handler = {
status: true,
csrftoken: csrftoken,
}),
{ status: 200, headers: { "Content-Type": "application/json" } },
{ status: 200, headers: { "Content-Type": "application/json",
//cors
"Access-Control-Allow-Origin": origin,
} },
);
},
};
11 changes: 5 additions & 6 deletions routes/api/v2/client/sessions/registers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,34 @@ export const handler = {
status: 400,
});
}
const RECAPTCHA_SECRET_KEY = recaptchakind === "v3" ? env["rechapcha_seecret_key_v3"] : env["rechapcha_seecret_key_v2"];
if (recaptchakind === "v3") {
//
const RECAPTCHA_SECRET_KEY = env["rechapcha_seecret_key_v3"]
const isSecsusRechapcha = await fetch(
`https://www.google.com/recaptcha/api/siteverify?secret=${RECAPTCHA_SECRET_KEY}&response=${recaptcha}`,
);
const score = await isSecsusRechapcha.json();
if (score.score < 0.5 || score.success == false) {
console.log(score);
return new Response(
JSON.stringify({ "status": false, error: "rechapchav3" }),
JSON.stringify({ "status": false, message: "rechapchav3" }),
{
headers: { "Content-Type": "application/json" },
status: 403,
},
);
}
} else if (recaptchakind === "v2") {
const RECAPTCHA_SECRET_KEY = env["rechapcha_seecret_key_v2"]
const response = await fetch(`https://www.google.com/recaptcha/api/siteverify`, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: `secret=YOUR_SECRET_KEY&response=${recaptcha}`,
body: `secret=${RECAPTCHA_SECRET_KEY}&response=${recaptcha}`,
});
const data = await response.json();
if (!data.success) {
return new Response(
JSON.stringify({ "status": false, error: "rechapchav2" }),
JSON.stringify({ "status": false, message: "rechapchav2" }),
{
headers: { "Content-Type": "application/json" },
status: 403,
Expand Down
11 changes: 5 additions & 6 deletions routes/api/v2/client/sessions/registers/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,34 @@ export const handler = {
status: 400,
});
}
const RECAPTCHA_SECRET_KEY = recaptchakind === "v3" ? env["rechapcha_seecret_key_v3"] : env["rechapcha_seecret_key_v2"];
if (recaptchakind === "v3") {
//
const RECAPTCHA_SECRET_KEY = env["rechapcha_seecret_key_v3"]
const isSecsusRechapcha = await fetch(
`https://www.google.com/recaptcha/api/siteverify?secret=${RECAPTCHA_SECRET_KEY}&response=${recaptcha}`,
);
const score = await isSecsusRechapcha.json();
if (score.score < 0.5 || score.success == false) {
console.log(score);
return new Response(
JSON.stringify({ "status": false, error: "rechapchav3" }),
JSON.stringify({ "status": false, message: "rechapchav3" }),
{
headers: { "Content-Type": "application/json" },
status: 403,
},
);
}
} else if (recaptchakind === "v2") {
const RECAPTCHA_SECRET_KEY = env["rechapcha_seecret_key_v2"]
const response = await fetch(`https://www.google.com/recaptcha/api/siteverify`, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: `secret=YOUR_SECRET_KEY&response=${recaptcha}`,
body: `secret=${RECAPTCHA_SECRET_KEY}&response=${recaptcha}`,
});
const data = await response.json();
if (!data.success) {
return new Response(
JSON.stringify({ "status": false, error: "rechapchav2" }),
JSON.stringify({ "status": false, message: "rechapchav2" }),
{
headers: { "Content-Type": "application/json" },
status: 403,
Expand Down
11 changes: 5 additions & 6 deletions routes/api/v2/client/sessions/registers/temp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,34 @@ export const handler = {
status: 400,
});
}
const RECAPTCHA_SECRET_KEY = recaptchakind === "v3" ? env["rechapcha_seecret_key_v3"] : env["rechapcha_seecret_key_v2"];
if (recaptchakind === "v3") {
//
const RECAPTCHA_SECRET_KEY = env["rechapcha_seecret_key_v3"]
const isSecsusRechapcha = await fetch(
`https://www.google.com/recaptcha/api/siteverify?secret=${RECAPTCHA_SECRET_KEY}&response=${recaptcha}`,
);
const score = await isSecsusRechapcha.json();
if (score.score < 0.5 || score.success == false) {
console.log(score);
return new Response(
JSON.stringify({ "status": false, error: "rechapchav3" }),
JSON.stringify({ "status": false, message: "rechapchav3" }),
{
headers: { "Content-Type": "application/json" },
status: 403,
},
);
}
} else if (recaptchakind === "v2") {
const RECAPTCHA_SECRET_KEY = env["rechapcha_seecret_key_v2"]
const response = await fetch(`https://www.google.com/recaptcha/api/siteverify`, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: `secret=YOUR_SECRET_KEY&response=${recaptcha}`,
body: `secret=${RECAPTCHA_SECRET_KEY}&response=${recaptcha}`,
});
const data = await response.json();
if (!data.success) {
return new Response(
JSON.stringify({ "status": false, error: "rechapchav2" }),
JSON.stringify({ "status": false, message: "rechapchav2" }),
{
headers: { "Content-Type": "application/json" },
status: 403,
Expand Down
34 changes: 8 additions & 26 deletions routes/api/v2/client/talks/friend/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import users from "../../../../../../models/users.ts";
import takos from "../../../../../../util/takos.ts";
import remoteFriends from "../../../../../../models/remoteFriends.ts";
import { load } from "$std/dotenv/mod.ts";
import rooms from "../../../../../../models/rooms.ts";
const env = await load();
const usersCache = new Map();
export const handler = {
Expand Down Expand Up @@ -78,33 +79,15 @@ export const handler = {
//そのメッセージの後のメッセージを取得
messagesData = await messages.find({ roomid: roomid, messageid: { $gt: after } }).sort({ _id: -1 }).limit(parseInt(limit));
}
let lastMessageId = "";
let lastMessageTimestamp: Date;
const result = await Promise.all(messagesData.map(async (message) => {
const CacheUser = usersCache.get(message.userid);
const read = await Promise.all(message.read.map(async (read: { userid: string; read: any }) => {
const user = usersCache.get(read.userid);
if (!user) {
let userInfo;
//= await users.findOne({ uuid: message.userid });
if (takos.splitUserName(message.userid).domain !== env["DOMAIN"]) {
const remoteFriend = await remoteFriends.findOne({ uuid: message.userid });
userInfo = remoteFriend;
} else {
userInfo = await users.findOne({ uuid: message.userid });
}
usersCache.set(message.userid, userInfo);
if (!userInfo) {
return {
userid: read.userid,
userName: "Unknown",
read: read.read,
};
}
return userInfo.userName + "@" + takos.splitUserName(read.userid).domain;
}
return user.userName + "@" + takos.splitUserName(read.userid).domain;
}));
console.log(read);
if (CacheUser) {
if(lastMessageTimestamp === undefined || new Date(lastMessageTimestamp) < new Date(message.timestamp)){
lastMessageTimestamp = message.timestamp;
lastMessageId = message.messageid;
}
if (message.messageType === "text") {
return {
messageid: message.messageid,
Expand All @@ -113,7 +96,6 @@ export const handler = {
message: message.message,
timestamp: message.timestamp,
type: message.messageType,
read: read,
};
}
}
Expand Down Expand Up @@ -147,10 +129,10 @@ export const handler = {
message: message.message,
timestamp: message.timestamp,
type: message.messageType,
read: read,
};
}
}));
await rooms.updateOne({ uuid: roomid }, { $set: { "readInfo.latestMessageId": lastMessageId} })
return new Response(JSON.stringify({ status: true, message: "Success", data: result }), { status: 200, headers: { "Content-Type": "application/json" } });
},
};
3 changes: 3 additions & 0 deletions routes/api/v2/client/talks/roomsInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const handler = {

}

0 comments on commit 201ee77

Please sign in to comment.