Hi,
I am currently attempting to create a custom chat system using velo, and would like to implement the following feature: like messenger for example, I would like the user to be able to get in contact with the users they previously got in contact with previously by displaying these users next to the chat.
It means the user would need to search for the users they have NOT yet got in contact with via a search bar.
(in the photo: on the left should be a repeater of users who have already messaged)
My question is that I do not know where to start. What would be the best way to organise the backend for that? I currently have 2 separate datasets with respectively users and messages (including the ID of the sender and the ID of the receiver)
I have explored the possibility of adding a new column to the users database with the ID of every user they have interacted but I have so far not found a way to add multiple values to the same record in a dataset? I also thought about searching through the message dataset for messages matching the connected user’s ID and capture the ID of the other user, but that does not seem efficient as the message database grows.
You can create an array fields for interaction user IDs,
On every new interaction, remove the current interacting userId from the array and unshift it to the beginning of the array and update the collection.
For the search input query the users collection and on the results use JS .filter() method on the results to remove users who are already in the previously-interacted array.
That makes sense, thank you. Since I would like to save all this information for everytime the user logs back in, I would need to store that in a dataset. I don’t know if we can save arrays in a dataset? I’ve heard JSON would be an alternative to saving an array to a dataset?
function setSidePanel(dataset, userId, j) {
let container
let img
let name
if (j == 0) {
container = "#repeaterInUser";
img = "#imageIn";
name = "#textNameIn";
} else if (j==1) {
container = "#repeaterSciUser";
img = "#imageSci";
name = "#textNameSci";
}
wixData.query(dataset)
.eq("_id", userId)
.find()
.then(async (results) => {
let currentUser = [];
for (let i = 0; i < results.items[0].interactions.length; i++) {
let x = results.items[0].interactions[i];
let y = [{ '_id': x, 'value': x }];
currentUser = currentUser.concat(y);
}
$w(container).onItemReady(async ($w, itemData, index) => {
$w(img).src = (await getProfile(itemData._id, 1, 1, dataset));
$w(name).text = (await getProfile(itemData._id, 0, 1, dataset));
});
$w("#repeaterInUser").data = currentUser;
});
function getProfile(usrId, i, j, dataset) { //i is a flag, 0 to find the name, 1 to find the profile picture
let plan = dataset; //session.getItem("datasetName");
if (j == 1) {
if (plan == "Members") {
plan = "InfluencerMembers"
} else {
plan = "Members";
}
}
if (i == 0) {
return wixData.query(plan)
.eq("_id", usrId)
.find()
.then((results) => {
if (results.items.length > 0) {
let memberItem = results.items;
let name = memberItem[0].fullName;
return name;
} else {
// handle case where no matching items found
}
});
} else if (i == 1) {
return wixData.query(plan)
.eq("_id", usrId)
.find()
.then((results) => {
if (results.items.length > 0) {
let memberItem = results.items;
let profilePic = memberItem[0].profilePic;
return profilePic;
} else {
// handle case where no matching items found
}
})
}
}
function updateInteractions(userId, user2Id, dataset) {
let plan = dataset;
if (plan == "Members") {
plan = "InfluencerMembers"
} else {
plan = "Members";
}
wixData.get(dataset, userId)
.then((item) => {
if (typeof item.interactions == 'undefined') {
item.interactions = [user2Id];
wixData.update(dataset, item);
} else if (item.interactions.length > 0 && item.interactions.includes(user2Id) == false) {
item.interactions.push(user2Id);
wixData.update(dataset, item);
}
})
.catch((err) => {
let errorMsg = err;
console.log(errorMsg);
});
wixData.get(plan, user2Id)
.then((item) => {
if (typeof item.interactions == 'undefined') {
item.interactions = [userId];
wixData.update(plan, item);
} else if (item.interactions.length > 0 && item.interactions.includes(userId) == false) {
item.interactions.push(userId);
wixData.update(plan, item);
}
})
.catch((err) => {
let errorMsg = err;
console.log(errorMsg);
});
}
updateInteractions is triggered when a user sends a message
setSidePanel is triggered on the initialisation of the page