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

Commit

Permalink
Add "server_name" parameter to the join room request (element-hq/riot…
Browse files Browse the repository at this point in the history
  • Loading branch information
bmarty committed Jul 17, 2019
1 parent 53d59a4 commit 2977fbc
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 100 deletions.
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.24 (2019-XX-XX)
=======================================================

Features:
-
- Add "server_name" parameter to the join room request (vector-im/riot-android#3204)

Improvements:
- RoomSummary: Add a listener to override the method used to handle the last message of the rooms.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,24 @@ public static boolean isEventId(@Nullable final String str) {
public static boolean isGroupId(@Nullable final String str) {
return str != null && PATTERN_CONTAIN_MATRIX_GROUP_IDENTIFIER.matcher(str).matches();
}

/**
* Extract server name from a matrix id
*
* @param matrixId
* @return null if not found or if matrixId is null
*/
public static String extractServerNameFromId(@Nullable String matrixId) {
if (matrixId == null) {
return null;
}

int index = matrixId.lastIndexOf(":");

if (index == -1) {
return null;
}

return matrixId.substring(index + 1);
}
}
51 changes: 32 additions & 19 deletions matrix-sdk/src/main/java/org/matrix/androidsdk/MXSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -1417,31 +1417,44 @@ public void onSuccess(Void info) {
* @param callback the async callback once the room is joined. The RoomId is provided.
*/
public void joinRoom(String roomIdOrAlias, final ApiCallback<String> callback) {
joinRoom(roomIdOrAlias, null, callback);
}

/**
* Join a room by its roomAlias
*
* @param roomIdOrAlias the room alias
* @param callback the async callback once the room is joined. The RoomId is provided.
*/
public void joinRoom(String roomIdOrAlias, List<String> viaServers, final ApiCallback<String> callback) {
checkIfAlive();

// sanity check
if ((null != mDataHandler) && (null != roomIdOrAlias)) {
mDataRetriever.getRoomsRestClient().joinRoom(roomIdOrAlias, new SimpleApiCallback<RoomResponse>(callback) {
@Override
public void onSuccess(final RoomResponse roomResponse) {
final String roomId = roomResponse.roomId;
Room joinedRoom = mDataHandler.getRoom(roomId);

// wait until the initial sync is done
if (!joinedRoom.isJoined()) {
joinedRoom.setOnInitialSyncCallback(new SimpleApiCallback<Void>(callback) {
@Override
public void onSuccess(Void info) {
mDataRetriever.getRoomsRestClient().joinRoom(roomIdOrAlias,
viaServers,
null,
new SimpleApiCallback<RoomResponse>(callback) {
@Override
public void onSuccess(final RoomResponse roomResponse) {
final String roomId = roomResponse.roomId;
Room joinedRoom = mDataHandler.getRoom(roomId);

// wait until the initial sync is done
if (!joinedRoom.isJoined()) {
joinedRoom.setOnInitialSyncCallback(new SimpleApiCallback<Void>(callback) {
@Override
public void onSuccess(Void info) {
callback.onSuccess(roomId);
}
});
} else {
// to initialise the notification counters
joinedRoom.markAllAsRead(null);
callback.onSuccess(roomId);
}
});
} else {
// to initialise the notification counters
joinedRoom.markAllAsRead(null);
callback.onSuccess(roomId);
}
}
});
}
});
}
}

Expand Down
115 changes: 61 additions & 54 deletions matrix-sdk/src/main/java/org/matrix/androidsdk/data/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ public void onSucceed(JsonObject object) {
if (null != map) {
Map<String, Object> joinMap = new HashMap<>();
joinMap.put("third_party_signed", map);
join(alias, joinMap, callback);
join(alias, null, joinMap, callback);
} else {
join(callback);
}
Expand Down Expand Up @@ -739,7 +739,7 @@ public void run() {
* @param callback the callback for when done
*/
public void join(final ApiCallback<Void> callback) {
join(null, null, callback);
join(null, null, null, callback);
}

/**
Expand All @@ -749,76 +749,83 @@ public void join(final ApiCallback<Void> callback) {
* @param callback the callback for when done
*/
private void join(String roomAlias, ApiCallback<Void> callback) {
join(roomAlias, null, callback);
join(roomAlias, null, null, callback);
}

/**
* Join the room. If successful, the room's current state will be loaded before calling back onComplete.
*
* @param roomAlias the room alias
* @param viaServers The servers to attempt to join the room through. One of the servers must be participating in the room.
* @param extraParams the join extra params
* @param callback the callback for when done
*/
private void join(final String roomAlias, final Map<String, Object> extraParams, final ApiCallback<Void> callback) {
public void join(final String roomAlias,
final List<String> viaServers,
final Map<String, Object> extraParams,
final ApiCallback<Void> callback) {
Log.d(LOG_TAG, "Join the room " + getRoomId() + " with alias " + roomAlias);

mDataHandler.getDataRetriever().getRoomsRestClient()
.joinRoom((null != roomAlias) ? roomAlias : getRoomId(), extraParams, new SimpleApiCallback<RoomResponse>(callback) {
@Override
public void onSuccess(final RoomResponse aResponse) {
try {
// the join request did not get the room initial history
if (!isJoined()) {
Log.d(LOG_TAG, "the room " + getRoomId() + " is joined but wait after initial sync");

// wait the server sends the events chunk before calling the callback
setOnInitialSyncCallback(callback);
} else {
Log.d(LOG_TAG, "the room " + getRoomId() + " is joined : the initial sync has been done");
// to initialise the notification counters
markAllAsRead(null);
// already got the initial sync
callback.onSuccess(null);
.joinRoom((null != roomAlias) ? roomAlias : getRoomId(),
viaServers,
extraParams,
new SimpleApiCallback<RoomResponse>(callback) {
@Override
public void onSuccess(final RoomResponse aResponse) {
try {
// the join request did not get the room initial history
if (!isJoined()) {
Log.d(LOG_TAG, "the room " + getRoomId() + " is joined but wait after initial sync");

// wait the server sends the events chunk before calling the callback
setOnInitialSyncCallback(callback);
} else {
Log.d(LOG_TAG, "the room " + getRoomId() + " is joined : the initial sync has been done");
// to initialise the notification counters
markAllAsRead(null);
// already got the initial sync
callback.onSuccess(null);
}
} catch (Exception e) {
Log.e(LOG_TAG, "join exception " + e.getMessage(), e);
}
}
} catch (Exception e) {
Log.e(LOG_TAG, "join exception " + e.getMessage(), e);
}
}

@Override
public void onNetworkError(Exception e) {
Log.e(LOG_TAG, "join onNetworkError " + e.getMessage(), e);
callback.onNetworkError(e);
}
@Override
public void onNetworkError(Exception e) {
Log.e(LOG_TAG, "join onNetworkError " + e.getMessage(), e);
callback.onNetworkError(e);
}

@Override
public void onMatrixError(MatrixError e) {
Log.e(LOG_TAG, "join onMatrixError " + e.getMessage());

if (MatrixError.UNKNOWN.equals(e.errcode) && TextUtils.equals("No known servers", e.error)) {
// It can happen when user wants to join a room he was invited to, but the inviter has left
// minging kludge until https://matrix.org/jira/browse/SYN-678 is fixed
// 'Error when trying to join an empty room should be more explicit
e.error = getStore().getContext().getString(R.string.room_error_join_failed_empty_room);
}
@Override
public void onMatrixError(MatrixError e) {
Log.e(LOG_TAG, "join onMatrixError " + e.getMessage());

if (MatrixError.UNKNOWN.equals(e.errcode) && TextUtils.equals("No known servers", e.error)) {
// It can happen when user wants to join a room he was invited to, but the inviter has left
// minging kludge until https://matrix.org/jira/browse/SYN-678 is fixed
// 'Error when trying to join an empty room should be more explicit
e.error = getStore().getContext().getString(R.string.room_error_join_failed_empty_room);
}

// if the alias is not found
// try with the room id
if ((e.mStatus == 404) && !TextUtils.isEmpty(roomAlias)) {
Log.e(LOG_TAG, "Retry without the room alias");
join(null, extraParams, callback);
return;
}
// if the alias is not found
// try with the room id
if ((e.mStatus == 404) && !TextUtils.isEmpty(roomAlias)) {
Log.e(LOG_TAG, "Retry without the room alias");
join(null, viaServers, extraParams, callback);
return;
}

callback.onMatrixError(e);
}
callback.onMatrixError(e);
}

@Override
public void onUnexpectedError(Exception e) {
Log.e(LOG_TAG, "join onUnexpectedError " + e.getMessage(), e);
callback.onUnexpectedError(e);
}
});
@Override
public void onUnexpectedError(Exception e) {
Log.e(LOG_TAG, "join onUnexpectedError " + e.getMessage(), e);
callback.onUnexpectedError(e);
}
});
}

//================================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ private void joinRoom() {

Log.d(LOG_TAG, "joinRoom " + mRoom.getRoomId());

mRoom.join(new SimpleApiCallback<Void>(getActivity()) {
mRoom.join(mRoom.getRoomId(), null, null, new SimpleApiCallback<Void>(getActivity()) {
@Override
public void onSuccess(Void info) {
Log.d(LOG_TAG, "joinRoom succeeds");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.matrix.androidsdk.rest.model.message.Message;
import org.matrix.androidsdk.rest.model.sync.RoomResponse;

import java.util.List;
import java.util.Map;

import retrofit2.Call;
Expand Down Expand Up @@ -146,23 +147,17 @@ Call<Void> sendStateEvent(@Path("roomId") String roomId,
@POST("rooms/{roomId}/invite")
Call<Void> invite(@Path("roomId") String roomId, @Body Map<String, String> params);

/**
* Join the given room.
*
* @param roomId the room id
* @param content the request body
*/
@POST("rooms/{roomId}/join")
Call<Void> join(@Path("roomId") String roomId, @Body JsonObject content);

/**
* Join the room with a room id or an alias.
*
* @param roomAliasOrId a room alias (or room id)
* @param viaServers The servers to attempt to join the room through. One of the servers must be participating in the room.
* @param params the extra join param
*/
@POST("join/{roomAliasOrId}")
Call<RoomResponse> joinRoomByAliasOrId(@Path("roomAliasOrId") String roomAliasOrId, @Body Map<String, Object> params);
Call<RoomResponse> joinRoomByAliasOrId(@Path("roomAliasOrId") String roomAliasOrId,
@Query("server_name") List<String> viaServers,
@Body Map<String, Object> params);

/**
* Leave the given room.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@
import org.matrix.androidsdk.rest.model.sync.AccountDataElement;
import org.matrix.androidsdk.rest.model.sync.RoomResponse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -230,31 +232,27 @@ public void onRetry() {
}));
}

/**
* Join a room by its roomAlias or its roomId.
*
* @param roomIdOrAlias the room id or the room alias
* @param callback the async callback
*/
public void joinRoom(final String roomIdOrAlias, final ApiCallback<RoomResponse> callback) {
joinRoom(roomIdOrAlias, null, callback);
}

/**
* Join a room by its roomAlias or its roomId with some parameters.
*
* @param roomIdOrAlias the room id or the room alias
* @param params the joining parameters.
* @param viaServers The servers to attempt to join the room through. One of the servers must be participating in the room. Can be null.
* @param params the joining parameters. Can be null.
* @param callback the async callback
*/
public void joinRoom(final String roomIdOrAlias, final Map<String, Object> params, final ApiCallback<RoomResponse> callback) {
public void joinRoom(final String roomIdOrAlias,
@Nullable final List<String> viaServers,
@Nullable final Map<String, Object> params,
final ApiCallback<RoomResponse> callback) {
final String description = "joinRoom : roomId " + roomIdOrAlias;

mApi.joinRoomByAliasOrId(roomIdOrAlias, (null == params) ? new HashMap<String, Object>() : params)
.enqueue(new RestAdapterCallback<RoomResponse>(description, mUnsentEventsManager, callback, new RestAdapterCallback.RequestRetryCallBack() {
mApi.joinRoomByAliasOrId(roomIdOrAlias,
(null == viaServers) ? new ArrayList<>() : viaServers,
(null == params) ? new HashMap<>() : params)
.enqueue(new RestAdapterCallback<>(description, mUnsentEventsManager, callback, new RestAdapterCallback.RequestRetryCallBack() {
@Override
public void onRetry() {
joinRoom(roomIdOrAlias, params, callback);
joinRoom(roomIdOrAlias, viaServers, params, callback);
}
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ public void MxPatterns_groupId_ok() {
}
}

@Test
public void MxPatterns_extractServer() {
Assert.assertNull(MXPatterns.extractServerNameFromId(null));
Assert.assertNull(MXPatterns.extractServerNameFromId("!GptcCLkPQiNdyDSgbJ"));
Assert.assertEquals("vector.modular.im", MXPatterns.extractServerNameFromId("!GptcCLkPQiNdyDSgbJ:vector.modular.im"));
}

/* ==========================================================================================
* Private methods
* ========================================================================================== */
Expand Down

0 comments on commit 2977fbc

Please sign in to comment.