From 01454892f8a57a52814ae664a95fa48a6c27924f Mon Sep 17 00:00:00 2001 From: leca Date: Sat, 3 Aug 2024 21:10:36 +0300 Subject: [PATCH] Done basic profile setting --- messages.json | 2 +- src/index.js | 64 +++++++++++++++++++++------------------------------ src/initDb.js | 2 -- 3 files changed, 27 insertions(+), 41 deletions(-) diff --git a/messages.json b/messages.json index ed16d06..337e13b 100644 --- a/messages.json +++ b/messages.json @@ -10,7 +10,7 @@ "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.", "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!" }, "errors": { diff --git a/src/index.js b/src/index.js index 00d71af..be6dadd 100644 --- a/src/index.js +++ b/src/index.js @@ -34,27 +34,14 @@ const storage = new SimpleFsStorageProvider("./bot_data/bot.json"); 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) => { 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; - 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; - console.log(answer) switch (current_action) { case "country": 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 client.sendText(roomId, `Set your age setting to "${answer}".`); - await client.sendText(roomId, messages.setup.gender); //next question - await db.query("UPDATE users SET current_action = 'gender' WHERE room_id = $1", [roomId]); + await client.sendText(roomId, messages.setup.sex); //next question + await db.query("UPDATE users SET current_action = 'sex' WHERE room_id = $1", [roomId]); break; - case "gender": - if (answer.toLowerCase() != "male" || answer.toLowerCase() != "female") { - await client.sendText(roomId, messages.errors.twogenders); + case "sex": + if (answer.toLowerCase() != "male" && answer.toLowerCase() != "female") { + await client.sendText(roomId, messages.errors.twosexes); return; } - await db.query("UPDATE users SET gender = $1 WHERE room_id = $2", [answer.toLowerCase() == "male" ? true : false, roomId]); - await client.sendText(roomId, `Set your gender setting to "${answer}".`); + await db.query("UPDATE users SET sex = $1 WHERE room_id = $2", [answer.toLowerCase() == "male" ? true : false, roomId]); + await client.sendText(roomId, `Set your sex setting to "${answer}".`); await client.sendText(roomId, messages.setup.interest); //next question await db.query("UPDATE users SET current_action = 'interest' WHERE room_id = $1", [roomId]); break; 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); return; } @@ -132,30 +119,27 @@ client.on("room.message", async (roomId, event) => { await client.sendText(roomId, messages.setup.done); await db.query("UPDATE users SET current_action = 'view_profiles' WHERE room_id = $1", [roomId]); } 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) { - 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]); } else { const message = new MessageEvent(event); const fileEvent = new MessageEvent(message.raw); const decrypted = await client.crypto.decryptMedia(fileEvent.content.file); - const encrypted = await client.crypto.encryptMedia(Buffer.from(decrypted)); - const mxc = await client.uploadContent(encrypted.buffer) - 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 array_length(pictures_urls)", [mxc, roomId])).rows[0]; + // const encrypted = await client.crypto.encryptMedia(Buffer.from(decrypted)); + const mxc = await client.uploadContent(decrypted) + 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; if (pictures_count < 5) { - await client.sendText(roomId, messages.setup.more); + await client.sendText(roomId, messages.setup.more + String(5 - pictures_count)); } else { await client.sendText(roomId, messages.setup.enough); await db.query("UPDATE users SET current_action = 'view_profiles' WHERE room_id = $1", [roomId]); } } } + break; } - - - } catch (e) { logError(e) } @@ -175,13 +159,17 @@ client.on("room.invite", async (roomId) => { logInfo(`Bot has joined a room with ID ${roomId}`) let mx_id = members[0].event.sender - console.log(`Sender is ${mx_id}`) - await db.query("INSERT INTO users(mx_id, room_id, current_action) VALUES ($1, $2, $3)", [mx_id, roomId, "country"]) + let is_profile_exists = (await db.query("SELECT * FROM users WHERE room_id = $1", [roomId])).rowCount > 0; - await client.sendText(roomId, messages.welcome); - await client.sendText(roomId, messages.setup.country); + 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 client.sendText(roomId, messages.welcome); + 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) { logError(e) diff --git a/src/initDb.js b/src/initDb.js index efcbe4e..3a5f8e6 100644 --- a/src/initDb.js +++ b/src/initDb.js @@ -13,8 +13,6 @@ export const getClient = async () => { }); await client.connect() - console.log(fs.readFileSync('./scheme.psql').toString()) - await client.query(fs.readFileSync('./scheme.psql').toString()) return client