Skip to content

Commit

Permalink
add push history persistency logic for message transmission
Browse files Browse the repository at this point in the history
  • Loading branch information
saviola1127 committed Jun 14, 2021
1 parent e20f858 commit d7863fd
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ public class MessageCreationModel {
@Expose
private int type;

//发送者的ID不能为空
@Expose
private String senderId;

//接收者,可为空(群组)
@Expose
private String receiverId;
Expand Down Expand Up @@ -69,14 +65,6 @@ public void setType(int type) {
this.type = type;
}

public String getSenderId() {
return senderId;
}

public void setSenderId(String senderId) {
this.senderId = senderId;
}

public String getReceiverId() {
return receiverId;
}
Expand All @@ -98,7 +86,6 @@ public static boolean check(MessageCreationModel model) {
// Model 不允许为null,
// 并且只需要具有一个及其以上的参数即可
return model != null && !(Strings.isNullOrEmpty(model.id)
|| Strings.isNullOrEmpty(model.senderId)
|| Strings.isNullOrEmpty(model.content)
|| Strings.isNullOrEmpty(model.receiverId))
&& (model.receiverType == Message.RECEIVE_TYPE_NONE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class Message {
@ManyToOne
@JoinColumn(name = "groupId")
private Group group;
@Column(nullable = false, insertable = false, updatable = false)
@Column(insertable = false, updatable = false)
private String groupId;


Expand Down Expand Up @@ -191,4 +191,12 @@ public String getGroupId() {
public void setGroupId(String groupId) {
this.groupId = groupId;
}

public Group getGroup() {
return group;
}

public void setGroup(Group group) {
this.group = group;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package net.qiujuer.web.italker.push.factory;

import com.google.common.base.Strings;
import net.qiujuer.web.italker.push.bean.api.base.PushModel;
import net.qiujuer.web.italker.push.bean.card.MessageCard;
import net.qiujuer.web.italker.push.bean.db.Message;
import net.qiujuer.web.italker.push.bean.db.PushHistory;
import net.qiujuer.web.italker.push.bean.db.User;
import net.qiujuer.web.italker.push.utils.Hib;
import net.qiujuer.web.italker.push.utils.PushDispatcher;
import net.qiujuer.web.italker.push.utils.TextUtil;

/**
* 消息存储与处理的工具类
Expand All @@ -12,5 +19,51 @@ public class PushFactory {

//发送一条消息 并在当前的发送历史记录中存储记录
public static void pushNewMessage(User sender, Message message) {
if (sender == null || message == null) {
return;
}

//构建消息卡片用于发送
MessageCard card = new MessageCard(message);
//即将推送的字符串
String entity = TextUtil.toJson(card);

//发送者
PushDispatcher dispatcher = new PushDispatcher();

if (message.getGroup() == null || Strings.isNullOrEmpty(message.getGroupId())) {
//这是一个发送给人的消息

User receiver = UserFactory.findById(message.getReceiverId());
if (receiver == null) {
return;
}

//存储历史记录表格字段
PushHistory history = new PushHistory();
history.setEntityType(PushModel.ENTITY_TYPE_MESSAGE); //普通消息类型
history.setEntity(entity);
history.setReceiver(receiver);
history.setReceiverPushId(receiver.getPushId()); //当前设备的推送ID

// 构建自己的PushModel
PushModel pushModel = new PushModel();
// 每一条历史记录都是独立的
pushModel.add(history.getEntityType(), history.getEntity());

//把需要发送的数据丢给发送者
dispatcher.add(receiver, pushModel);

// 保存到数据库
Hib.query(session -> {
session.save(history);
});

} else {

}

//发送者真实提交数据发送
dispatcher.submit();
}
}

0 comments on commit d7863fd

Please sign in to comment.