handling nonexistent profiles

This commit is contained in:
2024-08-13 11:17:29 +03:00
parent 89a38be5d3
commit dc6c438c73
5 changed files with 39 additions and 15 deletions

View File

@@ -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
};

View File

@@ -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"]));