use of net.iGap.request.RequestClientGetRoom in project iGap-Android by KianIranian-STDG.
the class HelperMessageResponse method handleMessage.
public static void handleMessage(final long roomId, final ProtoGlobal.RoomMessage roomMessage, final ProtoGlobal.Room.Type roomType, final ProtoResponse.Response response, final String identity) {
DbManager.getInstance().doRealmTransaction(realm -> {
/**
* put message to realm
*/
FileLog.i("HelperMessageResponse roomType -> " + roomType.toString() + " messageId -> " + roomMessage.getMessageId() + " roomId -> " + roomId);
RealmRoomMessage realmRoomMessage = RealmRoomMessage.putOrUpdate(realm, roomId, roomMessage, new StructMessageOption().setGap());
final RealmRoom room = realm.where(RealmRoom.class).equalTo("id", roomId).findFirst();
/**
* because user may have more than one device, his another device should not
* be recipient but sender. so I check current userId with room message user id,
* and if not equals and response is null, so we sure recipient is another user
*/
if (!roomMessage.getAuthor().getHash().equals(RealmUserInfo.getCurrentUserAuthorHash())) {
/**
* i'm recipient
*
* if author has user check that client have latest info for this user or no
* if author don't have use this means that message is from channel so client
* don't have user id for message sender for get info
*/
if (roomMessage.getAuthor().hasUser()) {
RealmRegisteredInfo.needUpdateUser(roomMessage.getAuthor().getUser().getUserId(), roomMessage.getAuthor().getUser().getCacheId());
}
// } else if (!response.getId().isEmpty()) {
// /**
// * i'm the sender
// *
// * delete message that created with fake messageId as identity
// * because in new version of realm client can't update primary key
// */
// RealmRoomMessage.deleteMessage(realm, Long.parseLong(identity));
}
if (identity != null && identity.length() > 0) {
RealmRoomMessage.deleteMessage(realm, Long.parseLong(identity), roomId);
}
if (room == null) {
/**
* if first message received but the room doesn't exist, send request for create new room
*/
new RequestClientGetRoom().clientGetRoom(roomId, null);
} else {
if (room.getType() == ProtoGlobal.Room.Type.CHAT)
room.setDeleted(false);
if (!roomMessage.getAuthor().getHash().equals(RealmUserInfo.getCurrentUserAuthorHash()) && (room.getLastMessage() == null || (room.getLastMessage() != null && room.getLastMessage().getMessageId() < roomMessage.getMessageId()))) {
room.setUnreadCount(room.getUnreadCount() + 1);
}
if (!roomMessage.getAuthor().getHash().equals(RealmUserInfo.getCurrentUserAuthorHash())) {
if (room.getFirstUnreadMessage() == null) {
room.setFirstUnreadMessage(realmRoomMessage);
}
if (roomMessage.getStatus() != ProtoGlobal.RoomMessageStatus.SEEN && RealmNotificationRoomMessage.canShowNotif(realm, roomMessage.getMessageId(), roomId)) {
RealmNotificationRoomMessage.putToDataBase(realm, roomMessage.getMessageId(), roomId);
HelperNotification.getInstance().addMessage(roomId, roomMessage, roomType, room, realm, AccountManager.getInstance().getCurrentUser());
}
}
/**
* update last message sent/received in room table
*/
if (room.getLastMessage() != null) {
if (room.getLastMessage().getMessageId() <= roomMessage.getMessageId()) {
room.setLastMessage(realmRoomMessage);
}
} else {
room.setLastMessage(realmRoomMessage);
}
}
});
if (response.getId().isEmpty()) {
/**
* invoke following callback when i'm not the sender, because i already done everything after sending message
*/
ChatSendMessageUtil.getInstance(AccountManager.selectedAccount).onMessageReceive(roomId, roomMessage.getMessage(), roomMessage.getMessageType(), roomMessage, roomType);
} else {
/**
* invoke following callback when I'm the sender and the message has updated
*/
ChatSendMessageUtil.getInstance(AccountManager.selectedAccount).onMessageUpdate(roomId, roomMessage.getMessageId(), roomMessage.getStatus(), identity, roomMessage);
}
if ((roomMessage.getAuthor().getUser().getUserId() == AccountManager.getInstance().getCurrentUser().getId() || roomMessage.getAuthor().getUser().getUserId() == 0) && roomMessage.getAttachment() != null) {
G.runOnUiThread(() -> EventManager.getInstance(AccountManager.selectedAccount).postEvent(EventManager.ON_UPLOAD_COMPLETED, roomMessage.getMessageType(), roomMessage.getMessageId(), roomMessage.getAttachment().getCacheId(), roomMessage.getAttachment().getToken()));
}
}
use of net.iGap.request.RequestClientGetRoom in project iGap-Android by KianIranian-STDG.
the class BotInit method checkDrIgap.
public static void checkDrIgap() {
if (!BotInit.getCheckDrIgap())
return;
new RequestClientGetPromote().getPromote();
G.ipromote = new Ipromote() {
@Override
public void onGetPromoteResponse(ProtoClientGetPromote.ClientGetPromoteResponse.Builder builder) {
setCheckDrIgap(false);
ArrayList<Long> promoteIds = new ArrayList<>();
for (int i = 0; i < builder.getPromoteList().size(); i++) promoteIds.add(builder.getPromoteList().get(i).getId());
DbManager.getInstance().doRealmTask(realm -> {
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
RealmResults<RealmRoom> roomList = realm.where(RealmRoom.class).equalTo("isFromPromote", true).findAll();
for (RealmRoom room : roomList) {
if (!promoteIds.contains(room.getPromoteId())) {
room.setFromPromote(false);
RoomController.getInstance(AccountManager.selectedAccount).clientPinRoom(room.getId(), false);
}
}
}
});
for (int i = builder.getPromoteList().size() - 1; i >= 0; i--) {
ProtoClientGetPromote.ClientGetPromoteResponse.Promote.Type TYPE = builder.getPromoteList().get(i).getType();
RealmRoom realmRoom;
if (TYPE == ProtoClientGetPromote.ClientGetPromoteResponse.Promote.Type.USER) {
realmRoom = realm.where(RealmRoom.class).equalTo("chatRoom.peer_id", builder.getPromoteList().get(i).getId()).equalTo("isFromPromote", true).findFirst();
} else {
realmRoom = realm.where(RealmRoom.class).equalTo("id", builder.getPromoteList().get(i).getId()).equalTo("isFromPromote", true).findFirst();
}
if (realmRoom == null) {
if (TYPE == ProtoClientGetPromote.ClientGetPromoteResponse.Promote.Type.USER) {
// RealmRoom.setPromote(builder.getPromoteList().get(i).getId(), TYPE);
G.onChatGetRoom = new OnChatGetRoom() {
@Override
public void onChatGetRoom(final ProtoGlobal.Room room) {
G.onChatGetRoom = null;
DbManager.getInstance().doRealmTransaction(realm1 -> {
RealmRoom realmRoom1 = RealmRoom.putOrUpdate(room, realm1);
realmRoom1.setFromPromote(true);
realmRoom1.setPromoteId(realmRoom1.getChatRoom().getPeerId());
// RealmRoom.setPromote(2297310L, ProtoClientGetPromote.ClientGetPromoteResponse.Promote.Type.USER);
});
RoomController.getInstance(AccountManager.selectedAccount).clientPinRoom(room.getId(), true);
ActivityPopUpNotification.sendMessage("/start", room.getId(), ProtoGlobal.Room.Type.CHAT);
}
@Override
public void onChatGetRoomTimeOut() {
}
@Override
public void onChatGetRoomError(int majorCode, int minorCode) {
}
};
new RequestChatGetRoom().chatGetRoom(builder.getPromoteList().get(i).getId());
} else {
new RequestClientGetRoom().clientGetRoom(builder.getPromoteList().get(i).getId(), RequestClientGetRoom.CreateRoomMode.getPromote);
}
} else {
RoomController.getInstance(AccountManager.selectedAccount).clientPinRoom(realmRoom.getId(), true);
}
}
});
}
};
}
use of net.iGap.request.RequestClientGetRoom in project iGap-Android by KianIranian-STDG.
the class RealmRoom method needUpdateRoomInfo.
/**
* if room isn't exist get info from server
*/
public static boolean needUpdateRoomInfo(long roomId) {
return DbManager.getInstance().doRealmTask(realm -> {
RealmRoom realmRoom = realm.where(RealmRoom.class).equalTo("id", roomId).findFirst();
if (realmRoom != null) {
return false;
}
new RequestClientGetRoom().clientGetRoom(roomId, RequestClientGetRoom.CreateRoomMode.justInfo);
return true;
});
}
use of net.iGap.request.RequestClientGetRoom in project iGap-Android by KianIranian-STDG.
the class HelperGetOwnerInfo method checkRoomExist.
private static void checkRoomExist(long id, final Listener listener) {
DbManager.getInstance().doRealmTask(realm -> {
RealmRoom realmRoom = realm.where(RealmRoom.class).equalTo("id", id).findFirst();
if (realmRoom == null) {
G.onClientGetRoomResponse = new OnClientGetRoomResponse() {
@Override
public void onClientGetRoomResponse(ProtoGlobal.Room room, ProtoClientGetRoom.ClientGetRoomResponse.Builder builder, RequestClientGetRoom.IdentityClientGetRoom identity) {
if (identity.createRoomMode == RequestClientGetRoom.CreateRoomMode.requestFromOwner) {
if (listener != null) {
listener.OnResponse();
}
}
}
@Override
public void onError(int majorCode, int minorCode) {
}
@Override
public void onTimeOut() {
}
};
new RequestClientGetRoom().clientGetRoom(id, RequestClientGetRoom.CreateRoomMode.requestFromOwner);
} else {
if (listener != null) {
listener.OnResponse();
}
}
});
}
use of net.iGap.request.RequestClientGetRoom in project iGap-Android by KianIranian-STDG.
the class HelperLogMessage method getAuthorName.
private static String getAuthorName(StructMyLog log, Realm realm) throws InvalidProtocolBufferException {
String result = "";
if (ProtoGlobal.RoomMessage.Author.parseFrom(log.author).hasUser()) {
result = MessageDataStorage.getInstance(AccountManager.selectedAccount).getDisplayNameWithUserId(ProtoGlobal.RoomMessage.Author.parseFrom(log.author).getUser().getUserId());
if (result != null) {
result = " " + result + " ";
} else {
logMessageUpdateList.put(ProtoGlobal.RoomMessage.Author.parseFrom(log.author).getUser().getUserId(), log);
new RequestUserInfo().userInfoAvoidDuplicate(ProtoGlobal.RoomMessage.Author.parseFrom(log.author).getUser().getUserId());
}
} else if (ProtoGlobal.RoomMessage.Author.parseFrom(log.author).hasRoom()) {
RealmRoom realmRoom = MessageDataStorage.getInstance(AccountManager.selectedAccount).getRoom(ProtoGlobal.RoomMessage.Author.parseFrom(log.author).getRoom().getRoomId());
;
if (realmRoom != null) {
result = " " + realmRoom.getTitle() + " ";
} else {
logMessageUpdateList.put(ProtoGlobal.RoomMessage.Author.parseFrom(log.author).getRoom().getRoomId(), log);
RealmRoom.needUpdateRoomInfo(ProtoGlobal.RoomMessage.Author.parseFrom(log.author).getRoom().getRoomId());
new RequestClientGetRoom().clientGetRoom(ProtoGlobal.RoomMessage.Author.parseFrom(log.author).getRoom().getRoomId(), RequestClientGetRoom.CreateRoomMode.justInfo);
}
}
return result;
}
Aggregations