diff --git a/db.drawio b/db.drawio index 87f9a3b..4d52056 100644 --- a/db.drawio +++ b/db.drawio @@ -1,6 +1,6 @@ - + @@ -50,7 +50,7 @@ - + @@ -62,7 +62,7 @@ - + @@ -95,7 +95,7 @@ - + @@ -125,7 +125,7 @@ - + diff --git a/src/db.js b/src/db.js index a442cf1..58db7e0 100644 --- a/src/db.js +++ b/src/db.js @@ -205,6 +205,14 @@ const getCountryNameByID = async (id) => { return (await db.query(`SELECT country FROM cities WHERE ID = $1`, [id])).rows[0].country; } +const doesUserExist = async (roomId) => { + return (await db.query("SELECT EXISTS (SELECT * FROM users WHERE room_id = $1)", [roomId])).rows[0].exists; +} + +const createUser = async (mx_id, roomId) => { + await db.query("INSERT INTO users(mx_id, room_id, current_action) VALUES ($1, $2, $3)", [mx_id, roomId, "wait_start"]); +} + export { fullEraseUser, appendUserPictures, @@ -228,5 +236,7 @@ export { getUserLanguage, findCity, getCityNameByID, - getCountryNameByID + getCountryNameByID, + doesUserExist, + createUser }; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 592bd9c..82d1a8c 100644 --- a/src/index.js +++ b/src/index.js @@ -31,7 +31,9 @@ import { uploadMediaAsMessage, setUserLanguage, getUserLanguage, - findCity + findCity, + doesUserExist, + createUser } from './db.js'; import { processRequest, showRandomProfileToUser, showNewLikes } from "./interactions.js"; @@ -60,9 +62,21 @@ const client = new MatrixClient(homeserverUrl, accessToken, storage, crypto); client.on("room.message", async (roomId, event) => { try { - if (event.content?.msgtype !== 'm.text' && event.content?.msgtype !== 'm.image' && event.content?.msgtype !== 'm.video') return; if (event.sender === await client.getUserId()) return; + if (!await doesUserExist(roomId)) { + i18n.locale = 'en' + await client.sendText(roomId, i18n.t(["errors", "usernotexists"])); + i18n.locale = 'ru' + await client.sendText(roomId, i18n.t(["errors", "usernotexists"])); + let mx_id = event.sender; + await createUser(mx_id, roomId); + + return; + } + + if (event.content?.msgtype !== 'm.text' && event.content?.msgtype !== 'm.image' && event.content?.msgtype !== 'm.video') return; + let current_action = await getCurrentUserAction(roomId); let answer = event.content.body; let msgtype = event.content.msgtype @@ -299,7 +313,7 @@ client.on("room.event", async (roomId, event) => { } catch (e) { logError(e); } -}) +}); client.on("room.invite", async (roomId, event) => { try { @@ -317,10 +331,8 @@ client.on("room.invite", async (roomId, event) => { let mx_id = event.sender; - 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, "wait_start"]); + if (!await doesUserExist(roomId)) { + await createUser(mx_id, roomId); i18n.locale = "en"; await client.sendText(roomId, i18n.t(["general", "welcome"])); diff --git a/translations/en.json b/translations/en.json index e6367d6..73f60ef 100644 --- a/translations/en.json +++ b/translations/en.json @@ -38,6 +38,7 @@ "notimplemented": "This feature is not yet implemented! Please, keep track of my updates at https://git.foxarmy.org/leca/heart2heart. Bother your admins to update, if released! ;)", "noprofiles": "There are no profiles left for you! I'm sorry. Please, come back later!", "alreadymessaged": "You have already messaged that person. Your message was not sent!", - "wrongcity": "There is no such city. Perhaps, you meant %{first}, %{second}, %{third}, %{fourth} or %{fifth}?" + "wrongcity": "There is no such city. Perhaps, you meant %{first}, %{second}, %{third}, %{fourth} or %{fifth}?", + "usernotexists": "I cannot find your profile in my database. Perhaps, your profile was erased for some reason: ban, database reset or some other similar reason. Please, write !start , where language is either en or ru." } } \ No newline at end of file diff --git a/translations/ru.json b/translations/ru.json index 7237166..219c6c8 100644 --- a/translations/ru.json +++ b/translations/ru.json @@ -38,6 +38,7 @@ "notimplemented": "Эта возможность ещё не добавлена! Пожалуйста, следите за обновлениями на моём git'е: https://git.foxarmy.org/leca/heart2heart. Доставайте своих админов, чтобы обновлялись, когда новая версия выходит ;)", "noprofiles": "Не могу найти профили, которые подходят вашим критериям! Извините. Попробуйте позже!", "alreadymessaged": "Вы уже отправляли сообщение этому человеку. Ваше сообщение не было отправлено!", - "wrongcity": "Не знаю такого города. Возможно, вы имели в виду %{first}, %{second}, %{third}, %{fourth} или %{fifth}?" + "wrongcity": "Не знаю такого города. Возможно, вы имели в виду %{first}, %{second}, %{third}, %{fourth} или %{fifth}?", + "usernotexists": "Не могу найти ваш профиль в своей базе данных. Возможно, ваш профиль был удалён по какой-то причине: блокировка, сброс базы данных, или другая похожая причина. Пожалуйста, напишите !start <язык>, где язык или \"en\", или \"ru\"." } } \ No newline at end of file