Done basic profile setting

This commit is contained in:
leca 2024-08-03 21:10:36 +03:00
parent d8d50d1525
commit 01454892f8
3 changed files with 27 additions and 41 deletions

View File

@ -10,7 +10,7 @@
"interest": "Write who you are interested to talk to? Write 'both', 'male', or 'female'.", "interest": "Write who you are interested to talk to? Write 'both', 'male', or 'female'.",
"pictures": "Send up to five pictures that will be shown in your profile. Write any text message when you are done.", "pictures": "Send up to five pictures that will be shown in your profile. Write any text message when you are done.",
"done": "Phew! You are done filling your profile! Now I'll start showing you others profiles!", "done": "Phew! You are done filling your profile! Now I'll start showing you others profiles!",
"more": "Got that cool picture! You still can upload up to this amount of pictures:", "more": "Got that cool picture! You still can upload up to this amount of pictures: ",
"enough": "That's enough of pictures! Let's go further!" "enough": "That's enough of pictures! Let's go further!"
}, },
"errors": { "errors": {

View File

@ -34,27 +34,14 @@ const storage = new SimpleFsStorageProvider("./bot_data/bot.json");
const client = new MatrixClient(homeserverUrl, accessToken, storage, crypto); const client = new MatrixClient(homeserverUrl, accessToken, storage, crypto);
// client.on("room.message", async (roomId, event) => {
// let current_action = (await db.query("SELECT current_action FROM users WHERE room_id = $1", [roomId])).rows[0];
// console.log(state)
// if (body?.startsWith("!hello")) {
// await client.sendText(roomId, messages.welcome);
// await client.sendText(roomId, messages.setup.country);
// }
// });
client.on("room.message", async (roomId, event) => { client.on("room.message", async (roomId, event) => {
try { try {
if (event.content?.msgtype !== 'm.text' || event.content?.msgtype !== 'm.image') return;
if (event.content?.msgtype !== 'm.text' && event.content?.msgtype !== 'm.image') return;
if (event.sender === await client.getUserId()) return; if (event.sender === await client.getUserId()) return;
let current_action = (await db.query('SELECT current_action FROM users WHERE room_id = $1', [roomId])).rows[0]; let current_action = (await db.query('SELECT current_action FROM users WHERE room_id = $1', [roomId])).rows[0].current_action;
let answer = event.content.body; let answer = event.content.body;
console.log(answer)
switch (current_action) { switch (current_action) {
case "country": case "country":
if (answer.length > 64) { if (answer.length > 64) {
@ -94,21 +81,21 @@ client.on("room.message", async (roomId, event) => {
} }
await db.query("UPDATE users SET age = $1 WHERE room_id = $2", [answer, roomId]); await db.query("UPDATE users SET age = $1 WHERE room_id = $2", [answer, roomId]);
await client.sendText(roomId, `Set your age setting to "${answer}".`); await client.sendText(roomId, `Set your age setting to "${answer}".`);
await client.sendText(roomId, messages.setup.gender); //next question await client.sendText(roomId, messages.setup.sex); //next question
await db.query("UPDATE users SET current_action = 'gender' WHERE room_id = $1", [roomId]); await db.query("UPDATE users SET current_action = 'sex' WHERE room_id = $1", [roomId]);
break; break;
case "gender": case "sex":
if (answer.toLowerCase() != "male" || answer.toLowerCase() != "female") { if (answer.toLowerCase() != "male" && answer.toLowerCase() != "female") {
await client.sendText(roomId, messages.errors.twogenders); await client.sendText(roomId, messages.errors.twosexes);
return; return;
} }
await db.query("UPDATE users SET gender = $1 WHERE room_id = $2", [answer.toLowerCase() == "male" ? true : false, roomId]); await db.query("UPDATE users SET sex = $1 WHERE room_id = $2", [answer.toLowerCase() == "male" ? true : false, roomId]);
await client.sendText(roomId, `Set your gender setting to "${answer}".`); await client.sendText(roomId, `Set your sex setting to "${answer}".`);
await client.sendText(roomId, messages.setup.interest); //next question await client.sendText(roomId, messages.setup.interest); //next question
await db.query("UPDATE users SET current_action = 'interest' WHERE room_id = $1", [roomId]); await db.query("UPDATE users SET current_action = 'interest' WHERE room_id = $1", [roomId]);
break; break;
case "interest": case "interest":
if (answer.toLowerCase() != "male" || answer.toLowerCase() != "female" || answer.toLowerCase() != "both") { if (answer.toLowerCase() != "male" && answer.toLowerCase() != "female" && answer.toLowerCase() != "both") {
await client.sendText(roomId, messages.errors.didntunderstand); await client.sendText(roomId, messages.errors.didntunderstand);
return; return;
} }
@ -132,30 +119,27 @@ client.on("room.message", async (roomId, event) => {
await client.sendText(roomId, messages.setup.done); await client.sendText(roomId, messages.setup.done);
await db.query("UPDATE users SET current_action = 'view_profiles' WHERE room_id = $1", [roomId]); await db.query("UPDATE users SET current_action = 'view_profiles' WHERE room_id = $1", [roomId]);
} else { } else {
let pictures_count = (await db.query("SELECT array_length(pictures_urls) FROM users WHERE room_id = $1"), [roomId]).rows[0]; let pictures_count = (await db.query("SELECT cardinality(pictures_urls) AS length FROM users WHERE room_id = $1", [roomId])).rows[0].length;
if (pictures_count == 5) { if (pictures_count == 5) {
await client.sendText(roomId, messages.error.toomuch); await client.sendText(roomId, messages.errors.toomuch);
await db.query("UPDATE users SET current_action = 'view_profiles' WHERE room_id = $1", [roomId]); await db.query("UPDATE users SET current_action = 'view_profiles' WHERE room_id = $1", [roomId]);
} else { } else {
const message = new MessageEvent(event); const message = new MessageEvent(event);
const fileEvent = new MessageEvent(message.raw); const fileEvent = new MessageEvent(message.raw);
const decrypted = await client.crypto.decryptMedia(fileEvent.content.file); const decrypted = await client.crypto.decryptMedia(fileEvent.content.file);
const encrypted = await client.crypto.encryptMedia(Buffer.from(decrypted)); // const encrypted = await client.crypto.encryptMedia(Buffer.from(decrypted));
const mxc = await client.uploadContent(encrypted.buffer) const mxc = await client.uploadContent(decrypted)
logInfo(`New media: ${mxc}`) let pictures_count = (await db.query("UPDATE users SET pictures_urls = array_append(pictures_urls, $1) WHERE room_id = $2 RETURNING cardinality(pictures_urls) AS amount", [mxc, roomId])).rows[0].amount;
let pictures_count = (await db.query("UPDATE users SET pictures_urls = array_append(pictures_urls, $1) WHERE room_id = $2 RETURNING array_length(pictures_urls)", [mxc, roomId])).rows[0];
if (pictures_count < 5) { if (pictures_count < 5) {
await client.sendText(roomId, messages.setup.more); await client.sendText(roomId, messages.setup.more + String(5 - pictures_count));
} else { } else {
await client.sendText(roomId, messages.setup.enough); await client.sendText(roomId, messages.setup.enough);
await db.query("UPDATE users SET current_action = 'view_profiles' WHERE room_id = $1", [roomId]); await db.query("UPDATE users SET current_action = 'view_profiles' WHERE room_id = $1", [roomId]);
} }
} }
} }
break;
} }
} catch (e) { } catch (e) {
logError(e) logError(e)
} }
@ -175,13 +159,17 @@ client.on("room.invite", async (roomId) => {
logInfo(`Bot has joined a room with ID ${roomId}`) logInfo(`Bot has joined a room with ID ${roomId}`)
let mx_id = members[0].event.sender let mx_id = members[0].event.sender
console.log(`Sender is ${mx_id}`)
let is_profile_exists = (await db.query("SELECT * FROM users WHERE room_id = $1", [roomId])).rowCount > 0;
if (!is_profile_exists){
await db.query("INSERT INTO users(mx_id, room_id, current_action) VALUES ($1, $2, $3)", [mx_id, roomId, "country"]) await db.query("INSERT INTO users(mx_id, room_id, current_action) VALUES ($1, $2, $3)", [mx_id, roomId, "country"])
await client.sendText(roomId, messages.welcome); await client.sendText(roomId, messages.welcome);
await client.sendText(roomId, messages.setup.country); await client.sendText(roomId, messages.setup.country);
} else {
await db.query("UPDATE users SET current_action = 'view_profiles' WHERE room_id = $1", [roomId]);
}
} catch (e) { } catch (e) {
logError(e) logError(e)

View File

@ -13,8 +13,6 @@ export const getClient = async () => {
}); });
await client.connect() await client.connect()
console.log(fs.readFileSync('./scheme.psql').toString())
await client.query(fs.readFileSync('./scheme.psql').toString()) await client.query(fs.readFileSync('./scheme.psql').toString())
return client return client