Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: lazy load global profile from db #1798

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

nico-famedly
Copy link
Member

When using the getProfileFromUserId with getFromRooms == true, it would always fail to load any profile from the database. This is because by default rooms are partial/not postLoaded, which means no state is in memory. However we usually also don't want to load all members in all rooms just to have a usable offline behaviour.

So this changes the function to first check the non-partial rooms. If none of them have the profile, it then starts looking for the specific user profile in the partial rooms. This should only load one member event, which hopefully is faster than loading all members in all rooms.

When using the getProfileFromUserId with getFromRooms == true, it would
always fail to load any profile from the database. This is because by
default rooms are partial/not postLoaded, which means no state is in
memory. However we usually also don't want to load all members in all
rooms just to have a usable offline behaviour.

So this changes the function to first check the non-partial rooms. If
none of them have the profile, it then starts looking for the specific
user profile in the partial rooms. This should only load one member
event, which hopefully is faster than loading all members in all rooms.
User? foundUser;
// check in memory first
for (final room in rooms) {
if (!room.partial) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!room.partial) {
if (room.partial) continue;

// check in memory first
for (final room in rooms) {
if (!room.partial) {
foundUser = await room.requestUser(userId,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does request the user from the database and even from the server but we only want to check the cache, right? So we can do this synchroniously by:

foundUser = room.getState(EventTypes.RoomMember, userId)?.asUser;

if (foundUser == null) {
for (final room in rooms) {
if (room.partial) {
foundUser = await room.requestUser(userId,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would try to request the user from server for each room which would result into 1000 failed requests if we have 1000 rooms where this user is not participating

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it would not, because both requestProfile and requestState are set to false. None of these requests should be doing any server requests.

Future<User?> requestUser(
String mxID, {
bool ignoreErrors = false,
bool requestProfile = true,
bool requestState = true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick but requestState for requesting a profile from server sounds like a really confusing name for that variable. Can you make this a bit more verbose?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants