diff --git a/CHANGES.rst b/CHANGES.rst index ee1ab37ab..62fcc327d 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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: - diff --git a/matrix-sdk/src/main/java/org/matrix/androidsdk/MXDataHandler.java b/matrix-sdk/src/main/java/org/matrix/androidsdk/MXDataHandler.java index f9e0c9af7..d8bbf9c82 100644 --- a/matrix-sdk/src/main/java/org/matrix/androidsdk/MXDataHandler.java +++ b/matrix-sdk/src/main/java/org/matrix/androidsdk/MXDataHandler.java @@ -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. * @@ -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 diff --git a/matrix-sdk/src/main/java/org/matrix/androidsdk/MXSession.java b/matrix-sdk/src/main/java/org/matrix/androidsdk/MXSession.java index 71d647c8d..a8216b38c 100644 --- a/matrix-sdk/src/main/java/org/matrix/androidsdk/MXSession.java +++ b/matrix-sdk/src/main/java/org/matrix/androidsdk/MXSession.java @@ -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; @@ -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 diff --git a/matrix-sdk/src/main/java/org/matrix/androidsdk/core/FilterUtil.java b/matrix-sdk/src/main/java/org/matrix/androidsdk/core/FilterUtil.java index 9368e2103..15547c167 100644 --- a/matrix-sdk/src/main/java/org/matrix/androidsdk/core/FilterUtil.java +++ b/matrix-sdk/src/main/java/org/matrix/androidsdk/core/FilterUtil.java @@ -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 * diff --git a/matrix-sdk/src/main/java/org/matrix/androidsdk/data/DataRetriever.java b/matrix-sdk/src/main/java/org/matrix/androidsdk/data/DataRetriever.java index 7fde21c9b..fa4f4d948 100644 --- a/matrix-sdk/src/main/java/org/matrix/androidsdk/data/DataRetriever.java +++ b/matrix-sdk/src/main/java/org/matrix/androidsdk/data/DataRetriever.java @@ -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; @@ -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; @@ -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 callback) { // reach the marker end if (TextUtils.equals(token, Event.PAGINATE_BACK_TOKEN_END)) { @@ -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(callback) { @Override public void onSuccess(TokensChunkEvents tokensChunkEvents) { @@ -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 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(callback) { @Override public void onSuccess(TokensChunkEvents tokensChunkEvents) { @@ -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 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); } } @@ -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 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(callback) { @Override public void onSuccess(TokensChunkEvents info) { diff --git a/matrix-sdk/src/main/java/org/matrix/androidsdk/data/Room.java b/matrix-sdk/src/main/java/org/matrix/androidsdk/data/Room.java index 1b1e05680..9fe2dca0e 100644 --- a/matrix-sdk/src/main/java/org/matrix/androidsdk/data/Room.java +++ b/matrix-sdk/src/main/java/org/matrix/androidsdk/data/Room.java @@ -407,7 +407,7 @@ public void requestServerRoomHistory(final String token, final int paginationCount, final ApiCallback callback) { mDataHandler.getDataRetriever() - .requestServerRoomHistory(getRoomId(), token, paginationCount, mDataHandler.isLazyLoadingEnabled(), + .requestServerRoomHistory(getRoomId(), token, paginationCount, mDataHandler.getPaginationFilter(), new SimpleApiCallback(callback) { @Override public void onSuccess(TokensChunkEvents info) { diff --git a/matrix-sdk/src/main/java/org/matrix/androidsdk/data/timeline/MXEventTimeline.java b/matrix-sdk/src/main/java/org/matrix/androidsdk/data/timeline/MXEventTimeline.java index de8a60393..cf5280447 100644 --- a/matrix-sdk/src/main/java/org/matrix/androidsdk/data/timeline/MXEventTimeline.java +++ b/matrix-sdk/src/main/java/org/matrix/androidsdk/data/timeline/MXEventTimeline.java @@ -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(callback) { @Override public void onSuccess(TokensChunkEvents response) { @@ -780,7 +780,7 @@ public boolean forwardPaginate(final ApiCallback 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(callback) { @Override public void onSuccess(TokensChunkEvents response) {