NIMSDK-AOS  9.20.15
GetMessagesDynamicallyParam.java
浏览该文件的文档.
1 package com.netease.nimlib.sdk.msg.model;
2 
3 import androidx.annotation.NonNull;
4 import androidx.annotation.Nullable;
5 
8 import com.netease.nimlib.session.IMMessageImpl;
9 import com.netease.nimlib.session.MsgDBHelper;
10 import com.netease.nimlib.util.StringUtil;
11 
12 import java.io.Serializable;
13 
14 public class GetMessagesDynamicallyParam implements Serializable {
15 
16  /** 最大数量限制 */
17  public static final int MAX_LIMIT = 100;
18  /**
19  * 会话ID
20  */
21  @NonNull
22  private final String sessionId;
23  /**
24  * 会话类型
25  */
26  @NonNull
27  private final SessionTypeEnum sessionType;
28  /**
29  * 开始时间(时间戳小)
30  */
31  private long fromTime;
32  /**
33  * 结束时间(时间戳大)
34  */
35  private long toTime;
36 
37  /**
38  * 要排除的最后一条消息的服务器ID,用于翻页。
39  * 要从A页翻到B页时,传A页中最接近B页的消息
40  *
41  * 设置此参数,需要同时传{@link GetMessagesDynamicallyParam#anchorClientId}
42  */
43  private long anchorServerId;
44  /**
45  * 要排除的最后一条消息的客户端ID,用于翻页。
46  * 要从A页翻到B页时,传A页中最接近B页的消息
47  */
48  private String anchorClientId;
49  /**
50  * 条数限制
51  * 限制0~100,否则414。其中0会被转化为100
52  */
53  private int limit;
54 
55  /**
56  * 查询方向
57  */
58  @Nullable
59  private GetMessageDirectionEnum direction;
60 
61  private GetMessageModeEnum mode;
62 
63  public GetMessagesDynamicallyParam(@NonNull String sessionId, @NonNull SessionTypeEnum sessionType) {
64  this(sessionId, sessionType, 0, 0, 0, null, 0, GetMessageDirectionEnum.FORWARD, GetMessageModeEnum.DEFAULT);
65  }
66 
67  public GetMessagesDynamicallyParam(@NonNull String sessionId, @NonNull SessionTypeEnum sessionType, long fromTime, long toTime, long anchorServerId, String anchorClientId, int limit, @Nullable GetMessageDirectionEnum direction) {
68  this(sessionId, sessionType, fromTime, toTime, anchorServerId, anchorClientId, limit, direction, GetMessageModeEnum.DEFAULT);
69  }
70 
71  private GetMessagesDynamicallyParam(@NonNull String sessionId, @NonNull SessionTypeEnum sessionType, long fromTime, long toTime, long anchorServerId, String anchorClientId, int limit, @Nullable GetMessageDirectionEnum direction, @Nullable GetMessageModeEnum mode) {
72  this.sessionId = sessionId;
73  this.sessionType = sessionType;
74  this.fromTime = fromTime;
75  this.toTime = toTime;
76  this.anchorServerId = anchorServerId;
77  this.anchorClientId = anchorClientId;
78  this.limit = limit;
79  this.direction = direction;
80  this.mode = mode == null ? GetMessageModeEnum.DEFAULT : mode;
81  }
82 
83  @NonNull
84  public String getSessionId() {
85  return sessionId;
86  }
87 
88  @NonNull
90  return sessionType;
91  }
92 
93  public long getFromTime() {
94  return fromTime;
95  }
96 
97  public void setFromTime(long fromTime) {
98  this.fromTime = fromTime;
99  }
100 
101  public long getToTime() {
102  return toTime;
103  }
104 
105  public void setToTime(long toTime) {
106  this.toTime = toTime;
107  }
108 
109  public long getAnchorServerId() {
110  return anchorServerId;
111  }
112 
113  public void setAnchorServerId(long anchorServerId) {
114  this.anchorServerId = anchorServerId;
115  }
116 
117  public String getAnchorClientId() {
118  return anchorClientId;
119  }
120 
121  public void setAnchorClientId(String anchorClientId) {
122  this.anchorClientId = anchorClientId;
123  }
124 
125  public int getLimit() {
126  return limit;
127  }
128 
129  public void setLimit(int limit) {
130  this.limit = limit;
131  }
132 
134  return direction;
135  }
136 
137  public void setDirection(GetMessageDirectionEnum direction) {
138  this.direction = direction;
139  }
140 
142  return mode;
143  }
144 
145  public void setMode(GetMessageModeEnum mode) {
146  this.mode = mode == null ? GetMessageModeEnum.DEFAULT : mode;
147  }
148 
149  /**
150  * 可能会查询数据库
151  */
152  @NonNull
153  public IMMessage getAnchor() {
154  String clientId = this.getAnchorClientId();
155  if (StringUtil.isNotEmpty(clientId)) {
156  IMMessage anchor = MsgDBHelper.queryMessageByUuid(clientId);
157  if (anchor instanceof IMMessageImpl) {
158  return anchor;
159  }
160  }
161  long anchorTime = this.getDirection() == GetMessageDirectionEnum.BACKWARD ? this.getFromTime() : this.getToTime();
162  IMMessageImpl anchor = (IMMessageImpl) MessageBuilder.createEmptyMessage(this.getSessionId(), this.getSessionType(), anchorTime);
163  anchor.setUuid(this.getAnchorClientId());
164  anchor.setServerId(this.getAnchorServerId());
165  return anchor;
166  }
167 
169  String formedSessionId = this.sessionId;
170  SessionTypeEnum formedSessionType = this.sessionType == null ? SessionTypeEnum.None : this.sessionType;
171  long formedFromTime = this.fromTime;
172  long formedToTime = this.toTime;
173  long formedExcludeServerId = this.anchorServerId;
174  String formedExcludeClientId = this.anchorClientId;
175  int formedLimit = this.limit;
176  formedLimit = formedLimit <= 0 ? GetMessagesDynamicallyParam.MAX_LIMIT : formedLimit;
177  formedLimit = Math.min(formedLimit, GetMessagesDynamicallyParam.MAX_LIMIT);
178  GetMessageDirectionEnum formedDirection = this.direction == null ? GetMessageDirectionEnum.FORWARD : this.direction;
179  GetMessageModeEnum mode = this.mode == null ? GetMessageModeEnum.DEFAULT : this.mode;
180  return new GetMessagesDynamicallyParam(formedSessionId, formedSessionType, formedFromTime, formedToTime, formedExcludeServerId, formedExcludeClientId, formedLimit, formedDirection, mode);
181  }
182 
183  /**
184  * 参数合法的初阶判断函数
185  *
186  * @return 初步筛查是否合法的结果
187  */
188  public boolean isValid() {
189  return StringUtil.isNotEmpty(sessionId)
190  && (sessionType == SessionTypeEnum.P2P || sessionType == SessionTypeEnum.Team || sessionType == SessionTypeEnum.SUPER_TEAM)
191  && fromTime >= 0
192  && toTime >= 0
193  && limit >= 0 && limit <= MAX_LIMIT
194  && (anchorServerId == 0 || StringUtil.isNotEmpty(anchorClientId));
195  }
196 
197  @Override
198  public String toString() {
199  return "GetMessagesDynamicallyParam{" +
200  "sessionId='" + sessionId + '\'' +
201  ", sessionType=" + sessionType +
202  ", fromTime=" + fromTime +
203  ", toTime=" + toTime +
204  ", excludeServerId=" + anchorServerId +
205  ", excludeClientId='" + anchorClientId + '\'' +
206  ", limit=" + limit +
207  ", direction=" + direction +
208  ", mode=" + mode +
209  '}';
210  }
211 }
云信 IM 消息构造器,提供构建各类型消息的接口。
NIM消息实体数据结构。 第三方APP不要调用设置类接口,调用之后不会被持久化
Definition: IMMessage.java:10
static IMMessage createEmptyMessage(String sessionId, SessionTypeEnum sessionType, long time)
创建一条空消息。
GetMessagesDynamicallyParam(@NonNull String sessionId,@NonNull SessionTypeEnum sessionType, long fromTime, long toTime, long anchorServerId, String anchorClientId, int limit,@Nullable GetMessageDirectionEnum direction)
GetMessagesDynamicallyParam(@NonNull String sessionId,@NonNull SessionTypeEnum sessionType)