Skip to content

Commit

Permalink
direct message in conversation.
Browse files Browse the repository at this point in the history
  • Loading branch information
imndx committed Apr 14, 2019
1 parent 2ac7dc4 commit 4c1f27c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import cn.wildfirechat.message.StickerMessageContent;
import cn.wildfirechat.message.TextMessageContent;
import cn.wildfirechat.message.VideoMessageContent;
import cn.wildfirechat.message.core.MessageDirection;
import cn.wildfirechat.model.Conversation;
import cn.wildfirechat.model.ConversationInfo;
import cn.wildfirechat.remote.ChatManager;
Expand All @@ -50,14 +49,11 @@ public class ConversationViewModel extends ViewModel implements OnReceiveMessage
private MutableLiveData<UiMessage> messageRemovedLiveData;
private MutableLiveData<Map<String, String>> mediaUploadedLiveData;
private Conversation conversation;
// channel主发起和某个channel听众的私聊会话时,才有效
//仅限于在Channel内使用。Channel的owner对订阅Channel单个用户发起一对一私聊
private String channelPrivateChatUser;

private Message toPlayAudioMessage;

private static final String audioDir = "wfc/audio";
private static final String videoDir = "wfc/video";

public ConversationViewModel(Conversation conversation, String channelPrivateChatUser) {
this.conversation = conversation;
this.channelPrivateChatUser = channelPrivateChatUser;
Expand Down Expand Up @@ -271,6 +267,9 @@ public void sendMessage(MessageContent content) {
Message msg = new Message();
msg.conversation = conversation;
msg.content = content;
if (!TextUtils.isEmpty(channelPrivateChatUser)) {
msg.toUsers = new String[]{channelPrivateChatUser};
}
sendMessage(msg);
}

Expand Down Expand Up @@ -448,12 +447,18 @@ public void onFail() {
}

private void postMessageUpdate(UiMessage message) {
if (message == null || message.message == null) {
return;
}
if (messageUpdateLiveData != null) {
UIUtils.postTaskSafely(() -> messageUpdateLiveData.setValue(message));
}
}

private void postNewMessage(UiMessage message) {
if (message == null || message.message == null) {
return;
}
if (messageLiveData != null) {
UIUtils.postTaskSafely(() -> messageLiveData.setValue(message));
}
Expand Down Expand Up @@ -509,20 +514,6 @@ public void onMessageUpdate(Message message) {
}

private boolean isMessageInCurrentConversation(Message message) {
if (!message.conversation.equals(conversation)) {
return false;
}
if (message.conversation.type == Conversation.ConversationType.Channel && !TextUtils.isEmpty(channelPrivateChatUser)) {
if (message.direction == MessageDirection.Receive && message.sender.equals(channelPrivateChatUser)) {
return true;
} else if (message.direction == MessageDirection.Send && (message.to == null || message.to.equals(channelPrivateChatUser))) {
return true;
} else {
return false;
}

} else {
return true;
}
return message.conversation.equals(conversation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private ProtoMessage convertMessage(cn.wildfirechat.message.Message msg) {
protoMessage.setLine(msg.conversation.line);
}
protoMessage.setFrom(msg.sender);
protoMessage.setTo(TextUtils.isEmpty(msg.to) ? "" : msg.to);
protoMessage.setTos(msg.toUsers);
MessagePayload payload = msg.content.encode();
payload.contentType = msg.content.getClass().getAnnotation(ContentTag.class).type();
protoMessage.setContent(payload.toProtoContent());
Expand Down Expand Up @@ -1530,7 +1530,7 @@ private List<cn.wildfirechat.message.Message> convertProtoMessages(List<ProtoMes
List<cn.wildfirechat.message.Message> out = new ArrayList<>();
for (ProtoMessage protoMessage : protoMessages) {
cn.wildfirechat.message.Message msg = convertProtoMessage(protoMessage);
if (msg != null) {
if (msg != null && msg.content != null) {
out.add(msg);
}
}
Expand All @@ -1545,7 +1545,7 @@ private cn.wildfirechat.message.Message convertProtoMessage(ProtoMessage protoMe
msg.messageId = protoMessage.getMessageId();
msg.conversation = new Conversation(Conversation.ConversationType.values()[protoMessage.getConversationType()], protoMessage.getTarget(), protoMessage.getLine());
msg.sender = protoMessage.getFrom();
msg.to = TextUtils.isEmpty(protoMessage.getTo()) ? null : protoMessage.getTo();
msg.toUsers = protoMessage.getTos();

msg.content = contentOfType(protoMessage.getContent().getType());
MessagePayload payload = new MessagePayload(protoMessage.getContent());
Expand Down
46 changes: 29 additions & 17 deletions client/src/main/java/cn/wildfirechat/message/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.os.Parcel;
import android.os.Parcelable;

import java.util.Objects;
import java.util.Arrays;

import cn.wildfirechat.message.core.MessageDirection;
import cn.wildfirechat.message.core.MessageStatus;
Expand All @@ -20,9 +20,9 @@ public class Message implements Parcelable {
public Conversation conversation;
public String sender;
/**
* 消息在会话中定向发送给该用户的
* 消息在会话中定向发送给指定用户
*/
public String to;
public String[] toUsers;
public MessageContent content;
public MessageDirection direction;
public MessageStatus status;
Expand All @@ -32,6 +32,7 @@ public class Message implements Parcelable {
public Message() {
}


@Override
public int describeContents() {
return 0;
Expand All @@ -42,8 +43,8 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(this.messageId);
dest.writeParcelable(this.conversation, flags);
dest.writeString(this.sender);
dest.writeString(this.to);
dest.writeParcelable(content, flags);
dest.writeStringArray(this.toUsers);
dest.writeParcelable(this.content, flags);
dest.writeInt(this.direction == null ? -1 : this.direction.ordinal());
dest.writeInt(this.status == null ? -1 : this.status.ordinal());
dest.writeLong(this.messageUid);
Expand All @@ -54,7 +55,7 @@ protected Message(Parcel in) {
this.messageId = in.readLong();
this.conversation = in.readParcelable(Conversation.class.getClassLoader());
this.sender = in.readString();
this.to = in.readString();
this.toUsers = in.createStringArray();
this.content = in.readParcelable(MessageContent.class.getClassLoader());
int tmpDirection = in.readInt();
this.direction = tmpDirection == -1 ? null : MessageDirection.values()[tmpDirection];
Expand All @@ -80,21 +81,32 @@ public Message[] newArray(int size) {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Message message = (Message) o;
return messageId == message.messageId &&
messageUid == message.messageUid &&
serverTime == message.serverTime &&
Objects.equals(conversation, message.conversation) &&
Objects.equals(sender, message.sender) &&
Objects.equals(to, message.to) &&
Objects.equals(content, message.content) &&
direction == message.direction &&
status == message.status;

if (messageId != message.messageId) return false;
if (messageUid != message.messageUid) return false;
if (serverTime != message.serverTime) return false;
if (!conversation.equals(message.conversation)) return false;
if (!sender.equals(message.sender)) return false;
// Probably incorrect - comparing Object[] arrays with Arrays.equals
if (!Arrays.equals(toUsers, message.toUsers)) return false;
if (!content.equals(message.content)) return false;
if (direction != message.direction) return false;
return status == message.status;
}

@Override
public int hashCode() {

return Objects.hash(messageId, conversation, sender, to, content, direction, status, messageUid, serverTime);
int result = (int) (messageId ^ (messageId >>> 32));
result = 31 * result + conversation.hashCode();
result = 31 * result + sender.hashCode();
result = 31 * result + Arrays.hashCode(toUsers);
result = 31 * result + content.hashCode();
result = 31 * result + direction.hashCode();
result = 31 * result + status.hashCode();
result = 31 * result + (int) (messageUid ^ (messageUid >>> 32));
result = 31 * result + (int) (serverTime ^ (serverTime >>> 32));
return result;
}
}
21 changes: 19 additions & 2 deletions client/src/main/java/cn/wildfirechat/remote/ChatManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1120,14 +1120,31 @@ public void disconnect(boolean cleanSession) {
}
}

/**
* 发送消息
*
* @param conversation
* @param content
* @param toUsers 定向发送给会话中的某些用户;为空,则发给所有人
* @param expireDuration
* @param callback
*/
public void sendMessage(Conversation conversation, MessageContent content, String[] toUsers, int expireDuration, SendMessageCallback callback) {
Message msg = new Message();
msg.conversation = conversation;
msg.content = content;
msg.toUsers = toUsers;
sendMessage(msg, expireDuration, callback);
}

/**
* 发送消息
*
* @param msg
* @param callback 发送消息状态回调
*/
public void sendMessage(final Message msg, final SendMessageCallback callback) {
sendMessage(msg, callback, 0);
sendMessage(msg, 0, callback);
}

/**
Expand All @@ -1137,7 +1154,7 @@ public void sendMessage(final Message msg, final SendMessageCallback callback) {
* @param callback 发送状态回调
* @param expireDuration 0, 永不过期;否则,规定时间内,对方未收到,则丢弃;单位是毫秒
*/
public void sendMessage(final Message msg, final SendMessageCallback callback, final int expireDuration) {
public void sendMessage(final Message msg, final int expireDuration, final SendMessageCallback callback) {
msg.direction = MessageDirection.Send;
msg.status = MessageStatus.Sending;
msg.sender = userId;
Expand Down

0 comments on commit 4c1f27c

Please sign in to comment.