idi527
2
You can probably use a lateral join to preload the newest message for each chat.
As for your code snippet, it might be simplified to
%{chat_users: chat_users} = result
Enum.map(chat_users, fn
%{chat_messages: [first_message | _rest]} = chat_user -> {chat_user, first_message}
chat_user -> {chat_user, nil}
end)
It would return a list of user/message tuples with the latter possibly being nil
.
1 Like