C
C#15mo ago
ItaM

❔ SOLVER Ordering messages based on the receiver or sender last message

I have this piece of code
public async Task<List<UserLimitedDto>> GetUsersThatHaveMessagedMe()
{
var (_, _, username) = await _tokenService.DecodeHS512Token();

var users = MessageJoinQuery()
.Where(u => u.Receiver_Username == username)
.GroupBy(u => u.Sender)
.Select(group => group.OrderByDescending(m => m.Timestamp).FirstOrDefault())
.ToList();

return users
.OrderByDescending(m => m.Timestamp)
.Select(u => new UserLimitedDto
{
User_Id = u.Sender_User_Id,
Username = u.Sender_Username,
Profile_Picture = u.Sender_Profile_Picture
})
.ToList();
}
public async Task<List<UserLimitedDto>> GetUsersThatHaveMessagedMe()
{
var (_, _, username) = await _tokenService.DecodeHS512Token();

var users = MessageJoinQuery()
.Where(u => u.Receiver_Username == username)
.GroupBy(u => u.Sender)
.Select(group => group.OrderByDescending(m => m.Timestamp).FirstOrDefault())
.ToList();

return users
.OrderByDescending(m => m.Timestamp)
.Select(u => new UserLimitedDto
{
User_Id = u.Sender_User_Id,
Username = u.Sender_Username,
Profile_Picture = u.Sender_Profile_Picture
})
.ToList();
}
What this does its that it gets a list of Users which meet a certain criteria. I have managed to order them based on the last message that they sent me, and it works pretty well. However, I am unable to order them based on the last message that I (receiver) sent them. It wont get updated
16 Replies
ItaM
ItaMOP15mo ago
This is an example of the functionality so far, as you can see. When another user (sender) sends a message, their icon goes up. As you can see, at the beggining it all works fine, new messages from sender, their profiles go up. But when the receiver sends a message, it does not make the icon go up I need to order the users based on the last message of either the sender or receiver, and currently its only getting ordered based on the senders last message
ItaM
ItaMOP15mo ago
I suspect the issue might be inside this select
ItaM
ItaMOP15mo ago
For reference, this is the MessageJoinQuery
Omnissiah
Omnissiah15mo ago
is this a practice/test application? because to me looks like this queries when you have upwards of thousands messages would probably slow down a bit also it's a bit unclear to me what's happening in the video why does the left window have the icons and the right one doesn't? is it a room?
ItaM
ItaMOP15mo ago
kind of a practice application im creating a table called messages where i pretty much store all the messages, which you are right, might store thousands and thousands of messages which might be slow
ItaM
ItaMOP15mo ago
This is pretty much it
ItaM
ItaMOP15mo ago
the left window has icons because its the actual users chat, in their profile. similar to facebook messenger chat, the second one does not have icons because its just another user visiting someone elses profile and opening a chat and sending them a message
Omnissiah
Omnissiah15mo ago
ok, so next the .Where(u => u.Receiver_Username == username) kinda seems limiting your query based on the logic you described if that's the method that does all the work and there aren't other queries
ItaM
ItaMOP15mo ago
yep, something i tried was doing the .Where like this .Where(m => m.Receiver_Username == username || m.Sender_Username == username) This selected all the messages of a certain conversation, pretty much. Then grouping them like this .GroupBy(m => new { m.Sender_Username, m.Receiver_Username }) And this ordered them almost the way I wanted to, however, the result was having the sender icons in the final result
ItaM
ItaMOP15mo ago
So I kinda wanted to do this, get sender and receiver messages, group them together, order them based on the timestamp of the message, and then remove the sender messages. But this code didnt return anything
ItaM
ItaMOP15mo ago
Do you know if there is a way to add a breakpoint on every like line of code on that link query without having to materialize the query/ save it into memory?
Omnissiah
Omnissiah15mo ago
put breakpoints and debug like this would take a lot of time because it would make you step through every item what i would do is separate the query sort of for every line like
var users = MessageJoinQuery();
var users_where = users.Where(_ => _.Receiver_Username == ...);
var users_select = users_where.Select(group => ...);
...
var users = MessageJoinQuery();
var users_where = users.Where(_ => _.Receiver_Username == ...);
var users_select = users_where.Select(group => ...);
...
and then look at the result for every operation if it corresponds to what i think is happening using the table view it shouldn't be a long process
ItaM
ItaMOP15mo ago
that pretty much saves it into memory, its a good way. Ill do it like that Btw, I fixed it
ItaM
ItaMOP15mo ago
ItaM
ItaMOP15mo ago
This was the final result
Accord
Accord15mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server