use of org.matrix.androidsdk.rest.model.RoomMember in project matrix-android-sdk by matrix-org.
the class RoomState method getDisplayableMembers.
/**
* Provides a list of displayable members.
* Some dummy members are created to internal stuff.
*
* @return a copy of the displayable room members list.
*/
public Collection<RoomMember> getDisplayableMembers() {
Collection<RoomMember> members = getMembers();
RoomMember conferenceUserId = getMember(MXCallsManager.getConferenceUserId(roomId));
if (null != conferenceUserId) {
ArrayList<RoomMember> membersList = new ArrayList<>(members);
membersList.remove(conferenceUserId);
members = membersList;
}
return members;
}
use of org.matrix.androidsdk.rest.model.RoomMember in project matrix-android-sdk by matrix-org.
the class RoomState method readExternal.
@Override
public void readExternal(ObjectInput input) throws IOException, ClassNotFoundException {
if (input.readBoolean()) {
roomId = input.readUTF();
}
if (input.readBoolean()) {
powerLevels = (PowerLevels) input.readObject();
}
if (input.readBoolean()) {
aliases = (List<String>) input.readObject();
}
List<Event> roomAliasesEvents = (List<Event>) input.readObject();
for (Event e : roomAliasesEvents) {
mRoomAliases.put(e.stateKey, e);
}
mAliasesByDomain = (Map<String, List<String>>) input.readObject();
if (input.readBoolean()) {
mMergedAliasesList = (List<String>) input.readObject();
}
Map<String, List<Event>> stateEvents = (Map<String, List<Event>>) input.readObject();
if (null != stateEvents) {
mStateEvents = new HashMap<>(stateEvents);
}
if (input.readBoolean()) {
alias = input.readUTF();
}
if (input.readBoolean()) {
name = input.readUTF();
}
if (input.readBoolean()) {
topic = input.readUTF();
}
if (input.readBoolean()) {
url = input.readUTF();
}
if (input.readBoolean()) {
avatar_url = input.readUTF();
}
if (input.readBoolean()) {
creator = input.readUTF();
}
if (input.readBoolean()) {
join_rule = input.readUTF();
}
if (input.readBoolean()) {
guest_access = input.readUTF();
}
if (input.readBoolean()) {
history_visibility = input.readUTF();
}
if (input.readBoolean()) {
roomAliasName = input.readUTF();
}
if (input.readBoolean()) {
visibility = input.readUTF();
}
if (input.readBoolean()) {
algorithm = input.readUTF();
}
mNotificationCount = input.readInt();
mHighlightCount = input.readInt();
if (input.readBoolean()) {
token = input.readUTF();
}
List<RoomMember> members = (List<RoomMember>) input.readObject();
for (RoomMember r : members) {
mMembers.put(r.getUserId(), r);
}
List<RoomThirdPartyInvite> invites = (List<RoomThirdPartyInvite>) input.readObject();
for (RoomThirdPartyInvite i : invites) {
mThirdPartyInvites.put(i.token, i);
}
List<RoomMember> inviteTokens = (List<RoomMember>) input.readObject();
for (RoomMember r : inviteTokens) {
mMembersWithThirdPartyInviteTokenCache.put(r.getThirdPartyInviteToken(), r);
}
if (input.readBoolean()) {
mMembership = input.readUTF();
}
mIsLive = input.readBoolean();
if (input.readBoolean()) {
mIsConferenceUserRoom = input.readBoolean();
}
if (input.readBoolean()) {
groups = (List<String>) input.readObject();
}
}
use of org.matrix.androidsdk.rest.model.RoomMember in project matrix-android-sdk by matrix-org.
the class RoomState method getDisplayName.
/**
* Build and return the room's display name.
*
* @param selfUserId this user's user id (to exclude from members)
* @return the display name
*/
public String getDisplayName(String selfUserId) {
String displayName = null;
String alias = getAlias();
synchronized (this) {
if (name != null) {
displayName = name;
} else if (!TextUtils.isEmpty(alias)) {
displayName = getAlias();
} else // compute a name
if (mMembers.size() > 0) {
Iterator it = mMembers.entrySet().iterator();
Map.Entry<String, RoomMember> otherUserPair = null;
if ((mMembers.size() >= 3) && (selfUserId != null)) {
// this is a group chat and should have the names of participants
// according to "(<num> <name1>, <name2>, <name3> ..."
int count = 0;
displayName = "";
while (it.hasNext()) {
Map.Entry<String, RoomMember> pair = (Map.Entry<String, RoomMember>) it.next();
if (!selfUserId.equals(pair.getKey())) {
otherUserPair = pair;
if (count > 0) {
displayName += ", ";
}
if (otherUserPair.getValue().getName() != null) {
// The member name
displayName += getMemberName(otherUserPair.getValue().getUserId());
} else {
// The user id
displayName += getMemberName(otherUserPair.getKey());
}
count++;
}
}
displayName = "(" + count + ") " + displayName;
} else {
// by default, it is oneself name
displayName = getMemberName(selfUserId);
// A One2One private room can default to being called like the other guy
if (selfUserId != null) {
while (it.hasNext()) {
Map.Entry<String, RoomMember> pair = (Map.Entry<String, RoomMember>) it.next();
if (!selfUserId.equals(pair.getKey())) {
otherUserPair = pair;
break;
}
}
}
if (otherUserPair != null) {
if (otherUserPair.getValue().getName() != null) {
// The member name
displayName = getMemberName(otherUserPair.getValue().getUserId());
} else {
// The user id
displayName = getMemberName(otherUserPair.getKey());
}
}
}
}
}
if ((displayName != null) && (alias != null) && !displayName.equals(alias)) {
if (TextUtils.isEmpty(displayName)) {
displayName = alias;
} else {
displayName += " (" + alias + ")";
}
}
if (displayName == null) {
displayName = roomId;
}
return displayName;
}
use of org.matrix.androidsdk.rest.model.RoomMember in project matrix-android-sdk by matrix-org.
the class RoomSummary method setLatestRoomState.
/**
* Set the latest tracked event (e.g. the latest m.room.message)
*
* @param roomState The room state of the latest event.
* @return This summary for chaining calls.
*/
public RoomSummary setLatestRoomState(RoomState roomState) {
mLatestRoomState = roomState;
// check for the invitation status
if (null != mLatestRoomState) {
RoomMember member = mLatestRoomState.getMember(mMatrixId);
mIsInvited = (null != member) && RoomMember.MEMBERSHIP_INVITE.equals(member.membership);
}
// when invited, the only received message should be the invitation one
if (mIsInvited) {
mInviterName = null;
if (null != mLatestReceivedEvent) {
mInviterName = mInviterUserId = mLatestReceivedEvent.getSender();
// try to retrieve a display name
if (null != mLatestRoomState) {
mInviterName = mLatestRoomState.getMemberName(mLatestReceivedEvent.getSender());
}
}
} else {
mInviterUserId = mInviterName = null;
}
return this;
}
use of org.matrix.androidsdk.rest.model.RoomMember in project matrix-android-sdk by matrix-org.
the class EventTimeline method handleJoinedRoomSync.
/**
* Manage the joined room events.
*
* @param roomSync the roomSync.
* @param isGlobalInitialSync true if the sync has been triggered by a global initial sync
*/
public void handleJoinedRoomSync(RoomSync roomSync, boolean isGlobalInitialSync) {
String membership = null;
String myUserId = mDataHandler.getMyUser().user_id;
RoomSummary currentSummary = null;
RoomMember selfMember = mState.getMember(mDataHandler.getMyUser().user_id);
if (null != selfMember) {
membership = selfMember.membership;
}
boolean isRoomInitialSync = (null == membership) || TextUtils.equals(membership, RoomMember.MEMBERSHIP_INVITE);
// Check whether the room was pending on an invitation.
if (TextUtils.equals(membership, RoomMember.MEMBERSHIP_INVITE)) {
// Reset the storage of this room. An initial sync of the room will be done with the provided 'roomSync'.
Log.d(LOG_TAG, "handleJoinedRoomSync: clean invited room from the store " + mRoomId);
mStore.deleteRoomData(mRoomId);
// clear the states
RoomState state = new RoomState();
state.roomId = mRoomId;
state.setDataHandler(mDataHandler);
this.mBackState = this.mState = state;
}
if ((null != roomSync.state) && (null != roomSync.state.events) && (roomSync.state.events.size() > 0)) {
if (isRoomInitialSync) {
Log.d(LOG_TAG, "## handleJoinedRoomSync() : " + roomSync.state.events.size() + " events for room " + mRoomId + " in store " + getStore());
}
// Note: We consider it is not required to clone the existing room state here, because no notification is posted for these events.
if (mDataHandler.isAlive()) {
for (Event event : roomSync.state.events) {
try {
processStateEvent(event, Direction.FORWARDS);
} catch (Exception e) {
Log.e(LOG_TAG, "processStateEvent failed " + e.getMessage());
}
}
mRoom.setReadyState(true);
} else {
Log.e(LOG_TAG, "## handleJoinedRoomSync() : mDataHandler.isAlive() is false");
}
// so the back state must also be initialized
if (isRoomInitialSync) {
Log.d(LOG_TAG, "## handleJoinedRoomSync() : retrieve " + this.mState.getMembers().size() + " members for room " + mRoomId);
this.mBackState = this.mState.deepCopy();
}
}
// Handle now timeline.events, the room state is updated during this step too (Note: timeline events are in chronological order)
if (null != roomSync.timeline) {
if (roomSync.timeline.limited) {
if (!isRoomInitialSync) {
currentSummary = mStore.getSummary(mRoomId);
// define a summary if some messages are left
// the unsent messages are often displayed messages.
Event oldestEvent = mStore.getOldestEvent(mRoomId);
// Flush the existing messages for this room by keeping state events.
mStore.deleteAllRoomMessages(mRoomId, true);
if (oldestEvent != null) {
if (RoomSummary.isSupportedEvent(oldestEvent)) {
if (null != currentSummary) {
currentSummary.setLatestReceivedEvent(oldestEvent, mState);
mStore.storeSummary(currentSummary);
} else {
mStore.storeSummary(new RoomSummary(null, oldestEvent, mState, myUserId));
}
}
}
}
// it implies there is no more data on server side.
if (null == roomSync.timeline.prevBatch) {
roomSync.timeline.prevBatch = Event.PAGINATE_BACK_TOKEN_END;
}
// In case of limited timeline, update token where to start back pagination
mStore.storeBackToken(mRoomId, roomSync.timeline.prevBatch);
// reset the state back token
// because it does not make anymore sense
// by setting at null, the events cache will be cleared when a requesthistory will be called
mBackState.setToken(null);
// reset the back paginate lock
mCanBackPaginate = true;
}
// any event ?
if ((null != roomSync.timeline.events) && (roomSync.timeline.events.size() > 0)) {
List<Event> events = roomSync.timeline.events;
// save the back token
events.get(0).mToken = roomSync.timeline.prevBatch;
// They will be added at the end of the stored events, so we keep the chronological order.
for (Event event : events) {
// the roomId is not defined.
event.roomId = mRoomId;
try {
boolean isLimited = (null != roomSync.timeline) && roomSync.timeline.limited;
// digest the forward event
handleLiveEvent(event, !isLimited && !isGlobalInitialSync, !isGlobalInitialSync && !isRoomInitialSync);
} catch (Exception e) {
Log.e(LOG_TAG, "timeline event failed " + e.getMessage());
}
}
}
}
if (isRoomInitialSync) {
// any request history can be triggered by now.
mRoom.setReadyState(true);
} else // Finalize initial sync
{
if ((null != roomSync.timeline) && roomSync.timeline.limited) {
// The room has been synced with a limited timeline
mDataHandler.onRoomFlush(mRoomId);
}
}
// so, the following items should only be called when it is a live one.
if (mIsLiveTimeline) {
// after a sync, the room summary might not be defined because the latest message did not generate a room summary/
if (null != mStore.getRoom(mRoomId)) {
RoomSummary summary = mStore.getSummary(mRoomId);
// we have to create a new one
if (null == summary) {
// define a summary if some messages are left
// the unsent messages are often displayed messages.
Event oldestEvent = mStore.getOldestEvent(mRoomId);
// if there is an oldest event, use it to set a summary
if (oldestEvent != null) {
// always defined a room summary else the room won't be displayed in the recents
mStore.storeSummary(new RoomSummary(null, oldestEvent, mState, myUserId));
mStore.commit();
// back paginate until to find a valid one
if (!RoomSummary.isSupportedEvent(oldestEvent)) {
Log.e(LOG_TAG, "the room " + mRoomId + " has no valid summary, back paginate once to find a valid one");
}
} else // use the latest known event
if (null != currentSummary) {
currentSummary.setLatestReceivedEvent(currentSummary.getLatestReceivedEvent(), mState);
mStore.storeSummary(currentSummary);
mStore.commit();
} else // try to build a summary from the state events
if ((null != roomSync.state) && (null != roomSync.state.events) && (roomSync.state.events.size() > 0)) {
ArrayList<Event> events = new ArrayList<>(roomSync.state.events);
Collections.reverse(events);
for (Event event : events) {
event.roomId = mRoomId;
if (RoomSummary.isSupportedEvent(event)) {
if (null == summary) {
summary = new RoomSummary(mStore.getSummary(mRoomId), event, mState, myUserId);
} else {
summary.setLatestReceivedEvent(event, mState);
}
mStore.storeSummary(summary);
String eventType = event.getType();
// Watch for potential room name changes
if (Event.EVENT_TYPE_STATE_ROOM_NAME.equals(eventType) || Event.EVENT_TYPE_STATE_ROOM_ALIASES.equals(eventType) || Event.EVENT_TYPE_STATE_ROOM_MEMBER.equals(eventType)) {
if (null != summary) {
summary.setName(mRoom.getName(myUserId));
}
}
mStore.commit();
break;
}
}
}
}
}
if (null != roomSync.unreadNotifications) {
int notifCount = 0;
int highlightCount = 0;
if (null != roomSync.unreadNotifications.highlightCount) {
highlightCount = roomSync.unreadNotifications.highlightCount;
}
if (null != roomSync.unreadNotifications.notificationCount) {
notifCount = roomSync.unreadNotifications.notificationCount;
}
if ((notifCount != mState.getNotificationCount()) || (mState.getHighlightCount() != highlightCount)) {
Log.d(LOG_TAG, "## handleJoinedRoomSync() : update room state notifs count for room id " + getRoom().getRoomId() + ": highlightCount " + highlightCount + " - notifCount " + notifCount);
mState.setNotificationCount(notifCount);
mState.setHighlightCount(highlightCount);
mStore.storeLiveStateForRoom(mRoomId);
mDataHandler.onNotificationCountUpdate(mRoomId);
}
// some users reported that the summary notification counts were sometimes invalid
// so check roomstates and summaries separately
RoomSummary summary = mStore.getSummary(mRoomId);
if ((null != summary) && ((notifCount != summary.getNotificationCount()) || (summary.getHighlightCount() != highlightCount))) {
Log.d(LOG_TAG, "## handleJoinedRoomSync() : update room summary notifs count for room id " + getRoom().getRoomId() + ": highlightCount " + highlightCount + " - notifCount " + notifCount);
summary.setNotificationCount(notifCount);
summary.setHighlightCount(highlightCount);
mStore.flushSummary(summary);
mDataHandler.onNotificationCountUpdate(mRoomId);
}
}
}
}
Aggregations