import { logError, logInfo, readConfig, readMessages } from './utils.js'; import { db, setUserState, selectProfilesForUser, setUserCurrentlyViewingProfile, getProfileInfo, getAllLikesForUser, checkForMutualLike, markLikeAsRead, } from "./db.js" const messages = readMessages() const requiresLengths = { "country": 64, "city": 64, "description": 512, "name": 32, } const processRequest = async (client, roomId, question, answer, nextQuestion) => { if (answer.length > requiresLengths[question]) { await client.sendText(roomId, messages.errors.toobig); return; } await db.query(`UPDATE users SET ${question} = $1 WHERE room_id = $2`, [answer, roomId]); await client.sendText(roomId, `Set your ${question} setting to "${answer}".`); await client.sendText(roomId, messages.setup[nextQuestion]); //next question setUserState(roomId, nextQuestion); } const showRandomProfileToUser = async (client, roomId) => { let chosenProfile = await selectProfilesForUser(roomId); if (!chosenProfile) { await client.sendText(roomId, messages.errors.noprofiles); return; } let message = `${chosenProfile.country}, ${chosenProfile.city}. ${chosenProfile.name}, ${chosenProfile.sex == 'm' ? 'male' : 'female'}, ${chosenProfile.age}. ${chosenProfile.description}` await setUserCurrentlyViewingProfile(roomId, chosenProfile.room_id) await client.sendText(roomId, message); if (chosenProfile.media) { for (let media of chosenProfile.media) { let msgtype if (media.type == 'p') { msgtype = "m.image" } else if (media.type == 'v') { msgtype = "m.video" } await client.sendMessage(roomId, { msgtype: msgtype, body: "Profile media", url: media.url }); } } await client.sendText(roomId, messages.general.rate) } const showProfileToUser = async (client, roomId, profileId) => { let profileInfo = await getProfileInfo(profileId); let message = `${profileInfo.country}, ${profileInfo.city}. ${profileInfo.name}, ${profileInfo.sex == 'm' ? 'male' : 'female'}, ${profileInfo.age}. ${profileInfo.description}` await client.sendText(roomId, messages.general.showalike); await client.sendText(roomId, message); if (profileInfo.media) { for (let media of profileInfo.media) { let msgtype if (media.type == 'p') { msgtype = "m.image" } else if (media.type == 'v') { msgtype = "m.video" } await client.sendMessage(roomId, { msgtype: msgtype, body: "Profile media", url: media.url }); } } await client.sendText(roomId, messages.general.mxid + profileInfo.mx_id) } const showNewLikes = async (client, roomId) => { let likes = (await getAllLikesForUser(roomId)); for (let liked of likes) { if (await checkForMutualLike(liked.sender, roomId)) { await showProfileToUser(client, roomId, liked.sender); await markLikeAsRead(liked.sender, roomId); } } } export { processRequest, showRandomProfileToUser, showNewLikes, showProfileToUser };