translations and remade location
This commit is contained in:
		
							
								
								
									
										109
									
								
								src/db.js
									
									
									
									
									
								
							
							
						
						
									
										109
									
								
								src/db.js
									
									
									
									
									
								
							@@ -48,6 +48,13 @@ const appendUserPictures = async (roomId, mxc, type) => {
 | 
			
		||||
    await db.query("INSERT INTO media (owner, type, purpose, url) VALUES ($1, $2, 'p', $3)", [roomId, type, mxc]);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const fullEraseUser = async (roomId) => {
 | 
			
		||||
    await eraseUser(roomId)
 | 
			
		||||
    await eraseUserLikes(roomId);
 | 
			
		||||
    await eraseUserMedia(roomId);
 | 
			
		||||
    await eraseUserMessages(roomId);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const eraseUser = async (roomId) => {
 | 
			
		||||
    await db.query("DELETE FROM users WHERE room_id = $1", [roomId]);
 | 
			
		||||
};
 | 
			
		||||
@@ -60,16 +67,19 @@ const eraseUserMedia = async (roomId) => {
 | 
			
		||||
    await db.query("DELETE FROM media WHERE owner = $1", [roomId]);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const eraseUserMessages = async (roomid) => {
 | 
			
		||||
    await db.query("DELETE FROM messages WHERE sender = $1 OR recipient = $1", [roomId]);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const selectProfilesForUser = async (roomId) => {
 | 
			
		||||
    const { myrange, myinterest, mysex, myage, mycity, mycountry } = (await db.query(`SELECT range AS myrange, 
 | 
			
		||||
    const { myrange, myinterest, mysex, myage, mycity } = (await db.query(`SELECT range AS myrange, 
 | 
			
		||||
                                                                                             interest AS myinterest, 
 | 
			
		||||
                                                                                             sex AS mysex, 
 | 
			
		||||
                                                                                             age AS myage, 
 | 
			
		||||
                                                                                             city AS mycity, 
 | 
			
		||||
                                                                                             country AS mycountry 
 | 
			
		||||
                                                                                             location AS mycity 
 | 
			
		||||
                                                                                      FROM users WHERE room_id = $1`, [roomId])
 | 
			
		||||
    ).rows[0];
 | 
			
		||||
    const { lat, lng } = (await db.query("SELECT lat, lng FROM cities WHERE name = $1 AND country = $2", [mycity, mycountry])).rows[0];
 | 
			
		||||
    const { lat, lng } = (await db.query("SELECT lat, lng FROM cities WHERE ID = $1", [mycity])).rows[0];
 | 
			
		||||
    //Selecting profiles other than user's and fitting their needs
 | 
			
		||||
    /*
 | 
			
		||||
    2 * ASIN(SQRT(
 | 
			
		||||
@@ -80,27 +90,24 @@ const selectProfilesForUser = async (roomId) => {
 | 
			
		||||
                                                                             )) 
 | 
			
		||||
                                                                             * 6371 <= $5::double precision
 | 
			
		||||
    */
 | 
			
		||||
   //  AND (range >= $5::double precision AND range != 0)
 | 
			
		||||
    //  
 | 
			
		||||
    let user = (await db.query(`SELECT 
 | 
			
		||||
                                room_id, name, age, sex, description, country, city FROM users
 | 
			
		||||
                                room_id, name, age, sex, description, location, range FROM users
 | 
			
		||||
                            WHERE
 | 
			
		||||
                                age::numeric <@ ANY(ARRAY[numrange($1 - 2, $1 + 2)])
 | 
			
		||||
                            AND room_id != $2
 | 
			
		||||
                            AND ${myinterest !== 'b' ? "sex = $3" : "$3 = $3 AND $4 = $4 AND $5 = $5 AND $6 = $6 AND $7 = $7"}
 | 
			
		||||
                            AND ${myrange !== 0 ?
 | 
			
		||||
                                `city = ANY(ARRAY(SELECT name 
 | 
			
		||||
                                                    FROM cities 
 | 
			
		||||
                                                    WHERE name = ANY(ARRAY(SELECT name 
 | 
			
		||||
                                                                            FROM cities 
 | 
			
		||||
                                                                            WHERE 
 | 
			
		||||
                                                                            check_distance($6::double precision, $7::double precision, lat, lng, $5::double precision)
 | 
			
		||||
                                                                    
 | 
			
		||||
                                                    ))
 | 
			
		||||
                                ))`
 | 
			
		||||
                                :
 | 
			
		||||
                                `check_distance($6::double precision, $7::double precision, (SELECT lat FROM cities WHERE name = city AND cities.country = country), (SELECT lng FROM cities WHERE name = city AND cities.country = country), range) 
 | 
			
		||||
                                 OR range = 0`
 | 
			
		||||
                            }
 | 
			
		||||
            `location = ANY(ARRAY(SELECT ID 
 | 
			
		||||
                FROM cities 
 | 
			
		||||
                WHERE 
 | 
			
		||||
                check_distance($6::double precision, $7::double precision, lat, lng, $5::double precision)
 | 
			
		||||
                AND (check_distance($6::double precision, $7::double precision, lat, lng, range) OR range = 0)
 | 
			
		||||
            ))`
 | 
			
		||||
            :
 | 
			
		||||
            `range = 0 OR
 | 
			
		||||
            check_distance($6::double precision, $7::double precision, (SELECT lat FROM cities WHERE ID = location), (SELECT lng FROM cities WHERE ID = location), range)`
 | 
			
		||||
        }
 | 
			
		||||
                            AND (interest = $4 OR interest = 'b')
 | 
			
		||||
                            ORDER BY RANDOM()
 | 
			
		||||
                            LIMIT 1`, [myage, roomId, myinterest, mysex, myrange, lat, lng])
 | 
			
		||||
@@ -148,7 +155,7 @@ const checkForMutualLike = async (roomId1, roomId2) => {
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const getProfileInfo = async (roomId) => {
 | 
			
		||||
    let user = (await db.query("SELECT mx_id, room_id, name, age, sex, description, country, city FROM users WHERE room_id = $1", [roomId])).rows[0];
 | 
			
		||||
    let user = (await db.query("SELECT mx_id, room_id, name, age, sex, description, location FROM users WHERE room_id = $1", [roomId])).rows[0];
 | 
			
		||||
    if (!user) return null;
 | 
			
		||||
    let media = await getUserProfilePictures(user.room_id);
 | 
			
		||||
    user.media = media;
 | 
			
		||||
@@ -192,45 +199,42 @@ const getUserLanguage = async (roomId) => {
 | 
			
		||||
    return (await db.query("SELECT language FROM users WHERE room_id = $1", [roomId])).rows[0].language;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const checkCountry = async (name) => {
 | 
			
		||||
    let res = (await db.query(`SELECT country AS name, levenshtein(country, $1) AS similarity
 | 
			
		||||
                               FROM cities
 | 
			
		||||
                               ORDER BY similarity ASC
 | 
			
		||||
                               LIMIT 3`, [name])
 | 
			
		||||
    ).rows;
 | 
			
		||||
// const checkCountry = async (name) => {
 | 
			
		||||
//     let res = (await db.query(`SELECT country AS name, levenshtein(country, $1) AS similarity
 | 
			
		||||
//                                FROM cities
 | 
			
		||||
//                                ORDER BY similarity ASC
 | 
			
		||||
//                                LIMIT 3`, [name])
 | 
			
		||||
//     ).rows;
 | 
			
		||||
 | 
			
		||||
    if (res[0].similarity == 0) { // 'similarity' is actually inversed. The less 'similarity', the more it similar. 0 means the same
 | 
			
		||||
        return {
 | 
			
		||||
            exists: true
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
    return {
 | 
			
		||||
        exists: false,
 | 
			
		||||
        variants: res
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
//     if (res[0].similarity == 0) { // 'similarity' is actually inversed. The less 'similarity', the more it similar. 0 means the same
 | 
			
		||||
//         return {
 | 
			
		||||
//             exists: true
 | 
			
		||||
//         };
 | 
			
		||||
//     }
 | 
			
		||||
//     return {
 | 
			
		||||
//         exists: false,
 | 
			
		||||
//         variants: res
 | 
			
		||||
//     };
 | 
			
		||||
// }
 | 
			
		||||
 | 
			
		||||
const checkCity = async (name) => {
 | 
			
		||||
    let res = (await db.query(`SELECT name, country, levenshtein(name, $1) AS similarity
 | 
			
		||||
const findCity = async (name) => {
 | 
			
		||||
    return (await db.query(`SELECT ID, name, lat, lng, country, levenshtein(name, $1) AS similarity
 | 
			
		||||
                               FROM cities
 | 
			
		||||
                               ORDER BY similarity ASC
 | 
			
		||||
                               LIMIT 5`, [name])
 | 
			
		||||
    ).rows;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
    if (res[0].similarity == 0) {
 | 
			
		||||
        return {
 | 
			
		||||
            exists: true,
 | 
			
		||||
            country: res[0].country
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return {
 | 
			
		||||
        exists: false,
 | 
			
		||||
        variants: res
 | 
			
		||||
    }
 | 
			
		||||
const getCityNameByID = async (id) => {
 | 
			
		||||
    return (await db.query(`SELECT name FROM cities WHERE ID = $1`, [id])).rows[0].name;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const getCountryNameByID = async (id) => {
 | 
			
		||||
    return (await db.query(`SELECT country FROM cities WHERE ID = $1`, [id])).rows[0].country;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export {
 | 
			
		||||
    eraseUser,
 | 
			
		||||
    fullEraseUser,
 | 
			
		||||
    appendUserPictures,
 | 
			
		||||
    setUserState,
 | 
			
		||||
    getCurrentUserAction,
 | 
			
		||||
@@ -244,14 +248,13 @@ export {
 | 
			
		||||
    getProfileInfo,
 | 
			
		||||
    getAllLikesForUser,
 | 
			
		||||
    markLikeAsRead,
 | 
			
		||||
    eraseUserLikes,
 | 
			
		||||
    eraseUserMedia,
 | 
			
		||||
    uploadMediaAsMessage,
 | 
			
		||||
    insertMessageIntoDB,
 | 
			
		||||
    getUnreadMessages,
 | 
			
		||||
    markMessageAsRead,
 | 
			
		||||
    setUserLanguage,
 | 
			
		||||
    getUserLanguage,
 | 
			
		||||
    checkCountry,
 | 
			
		||||
    checkCity
 | 
			
		||||
    findCity,
 | 
			
		||||
    getCityNameByID,
 | 
			
		||||
    getCountryNameByID
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										118
									
								
								src/index.js
									
									
									
									
									
								
							
							
						
						
									
										118
									
								
								src/index.js
									
									
									
									
									
								
							@@ -20,9 +20,7 @@ import {
 | 
			
		||||
    appendUserLikes,
 | 
			
		||||
    appendUserPictures,
 | 
			
		||||
    checkForMutualLike,
 | 
			
		||||
    eraseUser,
 | 
			
		||||
    eraseUserLikes,
 | 
			
		||||
    eraseUserMedia,
 | 
			
		||||
    fullEraseUser,
 | 
			
		||||
    getAmountOfUserPictures,
 | 
			
		||||
    getCurrentUserAction,
 | 
			
		||||
    getUnreadMessages,
 | 
			
		||||
@@ -33,8 +31,9 @@ import {
 | 
			
		||||
    uploadMediaAsMessage,
 | 
			
		||||
    setUserLanguage,
 | 
			
		||||
    getUserLanguage,
 | 
			
		||||
    checkCountry,
 | 
			
		||||
    checkCity
 | 
			
		||||
    findCity,
 | 
			
		||||
    getCityNameByID,
 | 
			
		||||
    getCountryNameByID
 | 
			
		||||
} from './db.js';
 | 
			
		||||
 | 
			
		||||
import { processRequest, showRandomProfileToUser, showNewLikes } from "./interactions.js";
 | 
			
		||||
@@ -81,42 +80,66 @@ client.on("room.message", async (roomId, event) => {
 | 
			
		||||
                if (a[1] !== "ru" && a[1] !== "en") return;
 | 
			
		||||
                await setUserLanguage(roomId, a[1]);
 | 
			
		||||
                i18n.locale = a[1]
 | 
			
		||||
                await setUserState(roomId, "country");
 | 
			
		||||
                await client.sendText(roomId, i18n.t(["setup", "country"]));
 | 
			
		||||
                await setUserState(roomId, "location");
 | 
			
		||||
                await client.sendText(roomId, i18n.t(["setup", "location"]));
 | 
			
		||||
 | 
			
		||||
                break;
 | 
			
		||||
            case "country":
 | 
			
		||||
                let checkResultCountry = await checkCountry(answer);
 | 
			
		||||
                if (!checkResultCountry.exists) {
 | 
			
		||||
                    console.log(checkResultCountry.variants)
 | 
			
		||||
                    await client.sendText(roomId, i18n.t(
 | 
			
		||||
                        ["errors", "wrongcountry"],
 | 
			
		||||
                        {
 | 
			
		||||
                            first: checkResultCountry.variants[0].name,
 | 
			
		||||
                            second: checkResultCountry.variants[1].name,
 | 
			
		||||
                            third: checkResultCountry.variants[2].name
 | 
			
		||||
                        }
 | 
			
		||||
                    ));
 | 
			
		||||
                    return;
 | 
			
		||||
            case "location":
 | 
			
		||||
                let number = parseInt(answer)
 | 
			
		||||
                if (!number) {
 | 
			
		||||
                    let cities = await findCity(answer);
 | 
			
		||||
                    await client.sendText(roomId, i18n.t(["setup", "choosecity"], {
 | 
			
		||||
                        number1: cities[0].id,
 | 
			
		||||
                        name1: cities[0].name,
 | 
			
		||||
                        country1: cities[0].country,
 | 
			
		||||
                        lat1: cities[0].lat,
 | 
			
		||||
                        lng1: cities[0].lng,
 | 
			
		||||
 | 
			
		||||
                        number2: cities[1].id,
 | 
			
		||||
                        name2: cities[1].name,
 | 
			
		||||
                        country2: cities[1].country,
 | 
			
		||||
                        lat2: cities[1].lat,
 | 
			
		||||
                        lng2: cities[1].lng,
 | 
			
		||||
 | 
			
		||||
                        number3: cities[2].id,
 | 
			
		||||
                        name3: cities[2].name,
 | 
			
		||||
                        country3: cities[2].country,
 | 
			
		||||
                        lat3: cities[2].lat,
 | 
			
		||||
                        lng3: cities[2].lng,
 | 
			
		||||
 | 
			
		||||
                        number4: cities[3].id,
 | 
			
		||||
                        name4: cities[3].name,
 | 
			
		||||
                        country4: cities[3].country,
 | 
			
		||||
                        lat4: cities[3].lat,
 | 
			
		||||
                        lng4: cities[3].lng,
 | 
			
		||||
 | 
			
		||||
                        number5: cities[4].id,
 | 
			
		||||
                        name5: cities[4].name,
 | 
			
		||||
                        country5: cities[4].country,
 | 
			
		||||
                        lat5: cities[4].lat,
 | 
			
		||||
                        lng5: cities[4].lng,
 | 
			
		||||
                    }))
 | 
			
		||||
                } else {
 | 
			
		||||
                    // let cityName = await getCityNameByID(number);
 | 
			
		||||
                    // let countryName = await getCountryNameByID(number);
 | 
			
		||||
 | 
			
		||||
                    await processRequest(client, roomId, current_action, number, 'range');
 | 
			
		||||
                }
 | 
			
		||||
                await processRequest(client, roomId, current_action, answer, 'city');
 | 
			
		||||
                break;
 | 
			
		||||
            case "city":
 | 
			
		||||
                let checkResultCity = await checkCity(answer); 
 | 
			
		||||
                if (!checkResultCity.exists) {
 | 
			
		||||
                    await client.sendText(roomId, i18n.t(
 | 
			
		||||
                        ["errors", "wrongcity"],
 | 
			
		||||
                        {
 | 
			
		||||
                            first: checkResultCity.variants[0].name,
 | 
			
		||||
                            second: checkResultCity.variants[1].name,
 | 
			
		||||
                            third: checkResultCity.variants[2].name,
 | 
			
		||||
                            fourth: checkResultCity.variants[3].name,
 | 
			
		||||
                            fifth: checkResultCity.variants[4].name,
 | 
			
		||||
                        }
 | 
			
		||||
                    ));
 | 
			
		||||
                    return;
 | 
			
		||||
                }
 | 
			
		||||
                await processRequest(client, roomId, current_action, answer, 'range');
 | 
			
		||||
                // let checkResultCity = await checkCity(answer); 
 | 
			
		||||
                // if (!checkResultCity.exists) {
 | 
			
		||||
                //     await client.sendText(roomId, i18n.t(
 | 
			
		||||
                //         ["errors", "wrongcity"],
 | 
			
		||||
                //         {
 | 
			
		||||
                //             first: checkResultCity.variants[0].name,
 | 
			
		||||
                //             second: checkResultCity.variants[1].name,
 | 
			
		||||
                //             third: checkResultCity.variants[2].name,
 | 
			
		||||
                //             fourth: checkResultCity.variants[3].name,
 | 
			
		||||
                //             fifth: checkResultCity.variants[4].name,
 | 
			
		||||
                //         }
 | 
			
		||||
                //     ));
 | 
			
		||||
                //     return;
 | 
			
		||||
                // }
 | 
			
		||||
                // await processRequest(client, roomId, current_action, answer, 'range');
 | 
			
		||||
                break;
 | 
			
		||||
            case "range":
 | 
			
		||||
                answer = parseInt(answer.split(" ")[0]);
 | 
			
		||||
@@ -140,7 +163,7 @@ client.on("room.message", async (roomId, event) => {
 | 
			
		||||
                    await client.sendText(roomId, i18n.t(["errors", "tooyoung"]));
 | 
			
		||||
 | 
			
		||||
                    await client.leaveRoom(roomId);
 | 
			
		||||
                    await eraseUser(roomId);
 | 
			
		||||
                    await fullEraseUser(roomId);
 | 
			
		||||
                    return;
 | 
			
		||||
                }
 | 
			
		||||
                await processRequest(client, roomId, current_action, answer, 'sex');
 | 
			
		||||
@@ -204,7 +227,7 @@ client.on("room.message", async (roomId, event) => {
 | 
			
		||||
                    await appendUserLikes(roomId, currently_viewing);
 | 
			
		||||
                    let value = await checkForMutualLike(roomId, currently_viewing);
 | 
			
		||||
                    if (value) {
 | 
			
		||||
                        await client.sendText(roomId, i18n.t(["general", "newlikes"]));
 | 
			
		||||
                        await client.sendText(currently_viewing, i18n.t(["general", "newlikes"]));
 | 
			
		||||
 | 
			
		||||
                        await client.sendText(roomId, i18n.t(["general", "newlikes"]));
 | 
			
		||||
 | 
			
		||||
@@ -238,7 +261,6 @@ client.on("room.message", async (roomId, event) => {
 | 
			
		||||
 | 
			
		||||
                } else {
 | 
			
		||||
                    await client.sendText(roomId, i18n.t(["errors", "alreadymessaged"]));
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
                await setUserState(roomId, 'view_profiles');
 | 
			
		||||
                await showRandomProfileToUser(client, roomId);
 | 
			
		||||
@@ -306,12 +328,14 @@ client.on("room.message", async (roomId, event) => {
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
client.on("room.event", async (roomId, event) => {
 | 
			
		||||
    if (event.type === "m.room.member" && event.content?.membership === "leave") {
 | 
			
		||||
        await eraseUser(roomId);
 | 
			
		||||
        await eraseUserLikes(roomId);
 | 
			
		||||
        await eraseUserMedia(roomId);
 | 
			
		||||
        logInfo(`Bot has left a room with ID  ${roomId}`)
 | 
			
		||||
        client.leaveRoom(roomId);
 | 
			
		||||
    try {
 | 
			
		||||
        if (event.type === "m.room.member" && event.content?.membership === "leave") {
 | 
			
		||||
            await fullEraseUser(roomId);
 | 
			
		||||
            logInfo(`Bot has left a room with ID  ${roomId}`)
 | 
			
		||||
            client.leaveRoom(roomId);
 | 
			
		||||
        }
 | 
			
		||||
    } catch (e) {
 | 
			
		||||
        logError(e);
 | 
			
		||||
    }
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -13,7 +13,9 @@ import {
 | 
			
		||||
    getAllLikesForUser,
 | 
			
		||||
    checkForMutualLike,
 | 
			
		||||
    markLikeAsRead,
 | 
			
		||||
    getUserLanguage
 | 
			
		||||
    getUserLanguage,
 | 
			
		||||
    getCountryNameByID,
 | 
			
		||||
    getCityNameByID
 | 
			
		||||
} from "./db.js";
 | 
			
		||||
 | 
			
		||||
import fs from 'fs';
 | 
			
		||||
@@ -58,11 +60,17 @@ const showRandomProfileToUser = async (client, roomId) => {
 | 
			
		||||
    i18n.locale = preferredLanguage;
 | 
			
		||||
 | 
			
		||||
    let chosenProfile = await selectProfilesForUser(roomId);
 | 
			
		||||
    console.log(chosenProfile);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    if (!chosenProfile) {
 | 
			
		||||
        await client.sendText(roomId, i18n.t(["errors", "noprofiles"]));
 | 
			
		||||
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    chosenProfile.country = await getCountryNameByID(chosenProfile.location);
 | 
			
		||||
    chosenProfile.city = await getCityNameByID(chosenProfile.location);
 | 
			
		||||
 | 
			
		||||
    let message =
 | 
			
		||||
        `${chosenProfile.country}, ${chosenProfile.city}.
 | 
			
		||||
${chosenProfile.name}, ${chosenProfile.sex == 'm' ? 'male' : 'female'}, ${chosenProfile.age}.
 | 
			
		||||
@@ -96,6 +104,10 @@ const showProfileToUser = async (client, roomId, profileId) => {
 | 
			
		||||
    i18n.locale = preferredLanguage;
 | 
			
		||||
 | 
			
		||||
    let profileInfo = await getProfileInfo(profileId);
 | 
			
		||||
 | 
			
		||||
    profileInfo.country = await getCountryNameByID(profileInfo.location);
 | 
			
		||||
    profileInfo.city = await getCityNameByID(profileInfo.location);
 | 
			
		||||
 | 
			
		||||
    let message =
 | 
			
		||||
        `${profileInfo.country}, ${profileInfo.city}.
 | 
			
		||||
${profileInfo.name}, ${profileInfo.sex == 'm' ? 'male' : 'female'}, ${profileInfo.age}.
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user