NIMSDK-AOS  9.16.0
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 
17  public static final int MAX_LIMIT = 100;
21  @NonNull
22  private final String sessionId;
26  @NonNull
27  private final SessionTypeEnum sessionType;
31  private long fromTime;
35  private long toTime;
36 
43  private long anchorServerId;
48  private String anchorClientId;
53  private int limit;
54 
58  @Nullable
59  private GetMessageDirectionEnum direction;
60 
61  public GetMessagesDynamicallyParam(@NonNull String sessionId, @NonNull SessionTypeEnum sessionType) {
62  this(sessionId, sessionType, 0, 0, 0, null, 0, GetMessageDirectionEnum.FORWARD);
63  }
64 
65  public GetMessagesDynamicallyParam(@NonNull String sessionId, @NonNull SessionTypeEnum sessionType, long fromTime, long toTime, long anchorServerId, String anchorClientId, int limit, @Nullable GetMessageDirectionEnum direction) {
66  this.sessionId = sessionId;
67  this.sessionType = sessionType;
68  this.fromTime = fromTime;
69  this.toTime = toTime;
70  this.anchorServerId = anchorServerId;
71  this.anchorClientId = anchorClientId;
72  this.limit = limit;
73  this.direction = direction;
74  }
75 
76  @NonNull
77  public String getSessionId() {
78  return sessionId;
79  }
80 
81  @NonNull
83  return sessionType;
84  }
85 
86  public long getFromTime() {
87  return fromTime;
88  }
89 
90  public void setFromTime(long fromTime) {
91  this.fromTime = fromTime;
92  }
93 
94  public long getToTime() {
95  return toTime;
96  }
97 
98  public void setToTime(long toTime) {
99  this.toTime = toTime;
100  }
101 
102  public long getAnchorServerId() {
103  return anchorServerId;
104  }
105 
106  public void setAnchorServerId(long anchorServerId) {
107  this.anchorServerId = anchorServerId;
108  }
109 
110  public String getAnchorClientId() {
111  return anchorClientId;
112  }
113 
114  public void setAnchorClientId(String anchorClientId) {
115  this.anchorClientId = anchorClientId;
116  }
117 
118  public int getLimit() {
119  return limit;
120  }
121 
122  public void setLimit(int limit) {
123  this.limit = limit;
124  }
125 
127  return direction;
128  }
129 
130  public void setDirection(GetMessageDirectionEnum direction) {
131  this.direction = direction;
132  }
133 
137  @NonNull
138  public IMMessage getAnchor() {
139  String clientId = this.getAnchorClientId();
140  if (StringUtil.isNotEmpty(clientId)) {
141  IMMessage anchor = MsgDBHelper.queryMessageByUuid(clientId);
142  if (anchor instanceof IMMessageImpl) {
143  return anchor;
144  }
145  }
146  long anchorTime = this.getDirection() == GetMessageDirectionEnum.BACKWARD ? this.getFromTime() : this.getToTime();
147  IMMessageImpl anchor = (IMMessageImpl) MessageBuilder.createEmptyMessage(this.getSessionId(), this.getSessionType(), anchorTime);
148  anchor.setUuid(this.getAnchorClientId());
149  anchor.setServerId(this.getAnchorServerId());
150  return anchor;
151  }
152 
154  String formedSessionId = this.sessionId;
155  SessionTypeEnum formedSessionType = this.sessionType == null ? SessionTypeEnum.None : this.sessionType;
156  long formedFromTime = this.fromTime;
157  long formedToTime = this.toTime;
158  long formedExcludeServerId = this.anchorServerId;
159  String formedExcludeClientId = this.anchorClientId;
160  int formedLimit = this.limit;
161  formedLimit = formedLimit <= 0 ? GetMessagesDynamicallyParam.MAX_LIMIT : formedLimit;
162  formedLimit = Math.min(formedLimit, GetMessagesDynamicallyParam.MAX_LIMIT);
163  GetMessageDirectionEnum formedDirection = this.direction == null ? GetMessageDirectionEnum.FORWARD : this.direction;
164  return new GetMessagesDynamicallyParam(formedSessionId, formedSessionType, formedFromTime, formedToTime, formedExcludeServerId, formedExcludeClientId, formedLimit, formedDirection);
165  }
166 
172  public boolean isValid() {
173  return StringUtil.isNotEmpty(sessionId)
174  && (sessionType == SessionTypeEnum.P2P || sessionType == SessionTypeEnum.Team || sessionType == SessionTypeEnum.SUPER_TEAM)
175  && fromTime >= 0
176  && toTime >= 0
177  && limit >= 0 && limit <= MAX_LIMIT
178  && (anchorServerId == 0 || StringUtil.isNotEmpty(anchorClientId));
179  }
180 
181  @Override
182  public String toString() {
183  return "GetMessagesDynamicallyParam{" +
184  "sessionId='" + sessionId + '\'' +
185  ", sessionType=" + sessionType +
186  ", fromTime=" + fromTime +
187  ", toTime=" + toTime +
188  ", excludeServerId=" + anchorServerId +
189  ", excludeClientId='" + anchorClientId + '\'' +
190  ", limit=" + limit +
191  ", direction=" + direction +
192  '}';
193  }
194 }
云信 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)