Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Allow Matrix SDK client to configure the filter used for pagination #483

Merged
merged 1 commit into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Changes to Matrix Android SDK in 0.9.X (2018-XX-XX)
=======================================================

Features:
-
- Allow Matrix SDK client to configure the filter used for pagination (vector-im/riot-android#3237)

Improvements:
-
Expand Down
24 changes: 24 additions & 0 deletions matrix-sdk/src/main/java/org/matrix/androidsdk/MXDataHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ public interface RequestNetworkErrorListener {
// tell if the lazy loading is enabled
private boolean mIsLazyLoadingEnabled;

// Filter for pagination
@Nullable
private RoomEventFilter mCustomPaginationFilter;

/**
* Default constructor.
*
Expand Down Expand Up @@ -1755,6 +1759,26 @@ private String getLeftRoomsFilter() {
return filterBody.toJSONString();
}

/* package */
void setPaginationFilter(@Nullable RoomEventFilter paginationFilter) {
mCustomPaginationFilter = paginationFilter;
}

/**
* Get the pagination filter, which can be customized by the client. Lazy loading param is managed here.
*
* @return the RoomEventFilter to use for pagination
*/
public RoomEventFilter getPaginationFilter() {
if (mCustomPaginationFilter != null) {
// Ensure lazy loading param is correct
FilterUtil.enableLazyLoading(mCustomPaginationFilter, isLazyLoadingEnabled());
return mCustomPaginationFilter;
} else {
return FilterUtil.createRoomEventFilter(isLazyLoadingEnabled());
}
}

/*
* Handle a 'toDevice' event
* @param event the event
Expand Down
12 changes: 12 additions & 0 deletions matrix-sdk/src/main/java/org/matrix/androidsdk/MXSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
import org.matrix.androidsdk.rest.model.bingrules.BingRule;
import org.matrix.androidsdk.rest.model.filter.FilterBody;
import org.matrix.androidsdk.rest.model.filter.FilterResponse;
import org.matrix.androidsdk.rest.model.filter.RoomEventFilter;
import org.matrix.androidsdk.rest.model.login.Credentials;
import org.matrix.androidsdk.rest.model.login.LoginFlow;
import org.matrix.androidsdk.rest.model.login.RegistrationFlowResponse;
Expand Down Expand Up @@ -1068,6 +1069,17 @@ public synchronized void setSyncFilter(FilterBody filter) {
convertFilterToFilterId();
}

/**
* Allows setting the filter used for the pagination
* The lazyLoading attribute will be overidden by the Matrix SDK, you do not have to take care of it
*
* @param filter the content of the filter param on pagination requests. Null to reset the filter.
*/
public void setPaginationFilter(@Nullable RoomEventFilter filter) {
Log.d(LOG_TAG, "setPaginationFilter ## " + filter);
mDataHandler.setPaginationFilter(filter);
}

/**
* Convert a filter to a filterId
* Either it is already known to the server, or send the filter to the server to get a filterId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ public static void enableLazyLoading(FilterBody filterBody, boolean useLazyLoadi
}
}

/**
* Patch the roomEventFilter to enable or disable the lazy loading
*
* @param roomEventFilter the roomEventFilter to patch
* @param useLazyLoading true to enable lazy loading
*/
public static void enableLazyLoading(@NonNull RoomEventFilter roomEventFilter, boolean useLazyLoading) {
roomEventFilter.lazyLoadMembers = useLazyLoading;
}

/**
* Create a RoomEventFilter
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import android.os.Looper;
import android.text.TextUtils;

import org.matrix.androidsdk.core.FilterUtil;
import org.matrix.androidsdk.core.Log;
import org.matrix.androidsdk.core.callback.ApiCallback;
import org.matrix.androidsdk.core.callback.SimpleApiCallback;
Expand All @@ -29,6 +28,7 @@
import org.matrix.androidsdk.rest.client.RoomsRestClient;
import org.matrix.androidsdk.rest.model.Event;
import org.matrix.androidsdk.rest.model.TokensChunkEvents;
import org.matrix.androidsdk.rest.model.filter.RoomEventFilter;

import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -113,14 +113,14 @@ public void getEvent(final IMXStore store, final String roomId, final String eve
* @param roomId the room Id
* @param token the start token.
* @param limit the maximum number of messages to retrieve
* @param withLazyLoading true when lazy loading is enabled
* @param roomEventFilter the filter to use
* @param callback the callback
*/
public void backPaginate(final IMXStore store,
final String roomId,
final String token,
final int limit,
final boolean withLazyLoading,
final RoomEventFilter roomEventFilter,
final ApiCallback<TokensChunkEvents> callback) {
// reach the marker end
if (TextUtils.equals(token, Event.PAGINATE_BACK_TOKEN_END)) {
Expand Down Expand Up @@ -183,7 +183,7 @@ public void run() {
} else {
Log.d(LOG_TAG, "## backPaginate() : trigger a remote request");

mRestClient.getRoomMessagesFrom(roomId, token, EventTimeline.Direction.BACKWARDS, limit, FilterUtil.createRoomEventFilter(withLazyLoading),
mRestClient.getRoomMessagesFrom(roomId, token, EventTimeline.Direction.BACKWARDS, limit, roomEventFilter,
new SimpleApiCallback<TokensChunkEvents>(callback) {
@Override
public void onSuccess(TokensChunkEvents tokensChunkEvents) {
Expand Down Expand Up @@ -275,18 +275,17 @@ public void onUnexpectedError(Exception e) {
* @param store the store to use
* @param roomId the room Id
* @param token the start token.
* @param withLazyLoading true when lazy loading is enabled
* @param roomEventFilter the filter to use
* @param callback the callback
*/
private void forwardPaginate(final IMXStore store,
final String roomId,
final String token,
final boolean withLazyLoading,
final RoomEventFilter roomEventFilter,
final ApiCallback<TokensChunkEvents> callback) {
putPendingToken(mPendingForwardRequestTokenByRoomId, roomId, token);

mRestClient.getRoomMessagesFrom(roomId, token, EventTimeline.Direction.FORWARDS, RoomsRestClient.DEFAULT_MESSAGES_PAGINATION_LIMIT,
FilterUtil.createRoomEventFilter(withLazyLoading),
mRestClient.getRoomMessagesFrom(roomId, token, EventTimeline.Direction.FORWARDS, RoomsRestClient.DEFAULT_MESSAGES_PAGINATION_LIMIT, roomEventFilter,
new SimpleApiCallback<TokensChunkEvents>(callback) {
@Override
public void onSuccess(TokensChunkEvents tokensChunkEvents) {
Expand All @@ -306,19 +305,19 @@ public void onSuccess(TokensChunkEvents tokensChunkEvents) {
* @param roomId the room id
* @param token the token to go back from. Null to start from live.
* @param direction the pagination direction
* @param withLazyLoading true when lazy loading is enabled
* @param roomEventFilter the filter to use
* @param callback the onComplete callback
*/
public void paginate(final IMXStore store,
final String roomId,
final String token,
final EventTimeline.Direction direction,
final boolean withLazyLoading,
final RoomEventFilter roomEventFilter,
final ApiCallback<TokensChunkEvents> callback) {
if (direction == EventTimeline.Direction.BACKWARDS) {
backPaginate(store, roomId, token, RoomsRestClient.DEFAULT_MESSAGES_PAGINATION_LIMIT, withLazyLoading, callback);
backPaginate(store, roomId, token, RoomsRestClient.DEFAULT_MESSAGES_PAGINATION_LIMIT, roomEventFilter, callback);
} else {
forwardPaginate(store, roomId, token, withLazyLoading, callback);
forwardPaginate(store, roomId, token, roomEventFilter, callback);
}
}

Expand All @@ -329,17 +328,17 @@ public void paginate(final IMXStore store,
* @param roomId the room id
* @param token the token to go back from.
* @param paginationCount the number of events to retrieve.
* @param withLazyLoading true when lazy loading is enabled
* @param roomEventFilter the filter to use for pagination
* @param callback the onComplete callback
*/
public void requestServerRoomHistory(final String roomId,
final String token,
final int paginationCount,
final boolean withLazyLoading,
final RoomEventFilter roomEventFilter,
final ApiCallback<TokensChunkEvents> callback) {
putPendingToken(mPendingRemoteRequestTokenByRoomId, roomId, token);

mRestClient.getRoomMessagesFrom(roomId, token, EventTimeline.Direction.BACKWARDS, paginationCount, FilterUtil.createRoomEventFilter(withLazyLoading),
mRestClient.getRoomMessagesFrom(roomId, token, EventTimeline.Direction.BACKWARDS, paginationCount, roomEventFilter,
new SimpleApiCallback<TokensChunkEvents>(callback) {
@Override
public void onSuccess(TokensChunkEvents info) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ public void requestServerRoomHistory(final String token,
final int paginationCount,
final ApiCallback<TokensChunkEvents> callback) {
mDataHandler.getDataRetriever()
.requestServerRoomHistory(getRoomId(), token, paginationCount, mDataHandler.isLazyLoadingEnabled(),
.requestServerRoomHistory(getRoomId(), token, paginationCount, mDataHandler.getPaginationFilter(),
new SimpleApiCallback<TokensChunkEvents>(callback) {
@Override
public void onSuccess(TokensChunkEvents info) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ public void run() {
return true;
}

mDataHandler.getDataRetriever().backPaginate(mStore, mRoomId, getBackState().getToken(), eventCount, mDataHandler.isLazyLoadingEnabled(),
mDataHandler.getDataRetriever().backPaginate(mStore, mRoomId, getBackState().getToken(), eventCount, mDataHandler.getPaginationFilter(),
new SimpleApiCallback<TokensChunkEvents>(callback) {
@Override
public void onSuccess(TokensChunkEvents response) {
Expand Down Expand Up @@ -780,7 +780,7 @@ public boolean forwardPaginate(final ApiCallback<Integer> callback) {

mIsForwardPaginating = true;

mDataHandler.getDataRetriever().paginate(mStore, mRoomId, mForwardsPaginationToken, Direction.FORWARDS, mDataHandler.isLazyLoadingEnabled(),
mDataHandler.getDataRetriever().paginate(mStore, mRoomId, mForwardsPaginationToken, Direction.FORWARDS, mDataHandler.getPaginationFilter(),
new SimpleApiCallback<TokensChunkEvents>(callback) {
@Override
public void onSuccess(TokensChunkEvents response) {
Expand Down