NIMSDK-AOS  10.9.90
V2NIMConversationIdUtil.java
浏览该文件的文档.
1 package com.netease.nimlib.sdk.v2.utils;
2 
3 import android.text.TextUtils;
4 import android.util.Pair;
5 import androidx.annotation.Nullable;
6 import com.netease.nimlib.SDKCacheUI;
7 import com.netease.nimlib.log.NimLog;
10 import java.util.Locale;
11 /**
12  * 会话id工具类
13  */
14 public final class V2NIMConversationIdUtil {
15 
16  private V2NIMConversationIdUtil() {
17  }
18 
19  private static final String TAG = "V2NIMConversationIdUtil";
20  private static final String CONVERSATION_ID_FORMAT = "%s|%d|%s";
21  private static final String CONVERSATION_ID_SPLIT = "\\|";
22 
23  /**
24  * 生成会话id
25  * @param targetId 目标id
26  * @param type 会话类型
27  * @return 会话id
28  */
29  public static String conversationId(String targetId, V2NIMConversationType type) {
30  if (TextUtils.isEmpty(targetId) || type == null) {
31  return null;
32  }
33  String account = SDKCacheUI.getAccount();
34  if(TextUtils.isEmpty(account))
35  {
36  return null;
37  }
38  return String.format(Locale.ENGLISH,CONVERSATION_ID_FORMAT, account, type.getValue(), targetId);
39  }
40 
41  public static String conversationId(String sessionId, SessionTypeEnum type) {
42  switch (type){
43  case P2P:
44  return p2pConversationId(sessionId);
45  case Team:
46  return teamConversationId(sessionId);
47  case SUPER_TEAM:
48  return superTeamConversationId(sessionId);
49  default:
50  return null;
51  }
52  }
53 
54  /**
55  * 生成p2p会话id
56  * @param accountId 对方账号id
57  * @return 会话id
58  */
59  public static String p2pConversationId(String accountId){
61  }
62 
63  /**
64  * 生成team会话id
65  * @param teamId 群id
66  * @return 会话id
67  */
68  public static String teamConversationId(String teamId){
70  }
71 
72  /**
73  * 生成superTeam会话id
74  * @param superTeamId 超大群id
75  * @return 会话id
76  */
77  public static String superTeamConversationId(String superTeamId){
79  }
80 
81  /**
82  * 根据会话id返回会话类型
83  * @param conversationId 会话id
84  * @return 会话类型
85  */
87  if (TextUtils.isEmpty(conversationId)) {
89  }
90  String[] components = conversationId.split(CONVERSATION_ID_SPLIT);
91  // 3 parts
92  if (components.length != 3) {
94  }
95  // account and target
96 
97  if (TextUtils.isEmpty(components[0]) || TextUtils.isEmpty(components[2])) {
99  }
100  try {
101  int type = Integer.parseInt(components[1]);
102 
103  return V2NIMConversationType.typeOfValue(type);
104  }
105  catch (Throwable e) {
106  NimLog.e(TAG, "conversationType error, conversationId=" + conversationId + ", e=" + e.getMessage());
108  }
109  }
110 
111  /**
112  * 根据会话id返回会话对应目标 ID(accountId、teamId、superTeamId)
113  * @param conversationId 会话id
114  * @return 会话目标id
115  */
116  public static String conversationTargetId(String conversationId) {
117  if (TextUtils.isEmpty(conversationId)) {
118  return null;
119  }
120  String[] components = conversationId.split(CONVERSATION_ID_SPLIT);
121  // 3 parts
122  if (components.length != 3) {
123  return null;
124  }
125  // account and target
126 
127  if (TextUtils.isEmpty(components[0]) || TextUtils.isEmpty(components[2])) {
128  return null;
129  }
130 
131  return components[2];
132  }
133 
134  /**
135  * 根据会话id返回会话发送方账号id
136  * @param conversationId 会话id
137  * @return 发送方账号id
138  */
139  public static String conversationSenderId(String conversationId) {
140  if (TextUtils.isEmpty(conversationId)) {
141  return null;
142  }
143  String[] components = conversationId.split(CONVERSATION_ID_SPLIT);
144  // 3 parts
145  if (components.length != 3) {
146  return null;
147  }
148  // account and target
149  if (TextUtils.isEmpty(components[0]) || TextUtils.isEmpty(components[2])) {
150  return null;
151  }
152 
153  return components[0];
154  }
155 
156  /**
157  * 会话id是否合法
158  * @param conversationId 会话id
159  * @return 是否合法 true:合法 false:不合法
160  */
161  public static boolean isConversationIdValid(String conversationId){
162  if (TextUtils.isEmpty(conversationId)) {
163  return false;
164  }
165  String[] components = conversationId.split(CONVERSATION_ID_SPLIT);
166  // 3 parts
167  if (components.length != 3) {
168  return false;
169  }
170 
171  String account = SDKCacheUI.getAccount();
172  if(account != null && !account.equals(components[0]))
173  {
174  return false;
175  }
176 
177  if(TextUtils.isEmpty(components[0]))
178  {
179  return false;
180  }
181 
183  try {
184  int typeInt = Integer.parseInt(components[1]);
185  type = V2NIMConversationType.typeOfValue(typeInt);
186  }
187  catch (NumberFormatException e) {
188  return false;
189  }
191  {
192  return false;
193  }
194 
195  if(TextUtils.isEmpty(components[2]))
196  {
197  return false;
198  }
199 
200  return true;
201 
202  }
203 
204  @Nullable
206  if (conversationType == null) {
207  return null;
208  }
209  switch (conversationType) {
210  case V2NIM_CONVERSATION_TYPE_P2P:
211  return SessionTypeEnum.P2P;
212  case V2NIM_CONVERSATION_TYPE_TEAM:
213  return SessionTypeEnum.Team;
214  case V2NIM_CONVERSATION_TYPE_SUPER_TEAM:
216  default:
217  return SessionTypeEnum.None;
218  }
219  }
220 
222  if (sessionType == null) {
224  }
225  switch (sessionType) {
226  case P2P:
228  case Team:
230  case SUPER_TEAM:
232  default:
234  }
235  }
236 
237  /**
238  * 判断两个 P2P conversationId 是否代表同一个会话(双方视角不同但会话相同)。
239  * P2P conversationId 格式为 {account}|{type}|{targetId},
240  * A 视角:A|1|B,B 视角:B|1|A,两者代表同一个会话。
241  *
242  * @param conversationId1 第一个会话id
243  * @param conversationId2 第二个会话id
244  * @return 是否代表同一个 P2P 会话
245  */
246  public static boolean isSameP2PConversation(String conversationId1, String conversationId2) {
247  if (TextUtils.isEmpty(conversationId1) || TextUtils.isEmpty(conversationId2)) {
248  return false;
249  }
250  if (conversationId1.equals(conversationId2)) {
251  return true;
252  }
253  // 类型必须都是 P2P
256  return false;
257  }
258  // A|1|B 和 B|1|A 代表同一个会话:sender1 == target2 && target1 == sender2
259  String sender1 = conversationSenderId(conversationId1);
260  String target1 = conversationTargetId(conversationId1);
261  String sender2 = conversationSenderId(conversationId2);
262  String target2 = conversationTargetId(conversationId2);
263  if (TextUtils.isEmpty(sender1) || TextUtils.isEmpty(target1)
264  || TextUtils.isEmpty(sender2) || TextUtils.isEmpty(target2)) {
265  return false;
266  }
267  return sender1 != null && sender1.equals(target2) && target1 != null && target1.equals(sender2);
268  }
269 
270  public static Pair<String,SessionTypeEnum> parseConversationId(String conversationId) {
271  if (TextUtils.isEmpty(conversationId)) {
272  return null;
273  }
274  String[] components = conversationId.split(CONVERSATION_ID_SPLIT);
275  // 3 parts
276  if (components.length != 3) {
277  return null;
278  }
279 
280  String account = SDKCacheUI.getAccount();
281  if(account != null && !account.equals(components[0]))
282  {
283  return null;
284  }
285 
286  if(TextUtils.isEmpty(components[0]))
287  {
288  return null;
289  }
290 
291  if (TextUtils.isEmpty(components[2])) {
292  return null;
293  }
295  try {
296  int typeInt = Integer.parseInt(components[1]);
297  type = V2NIMConversationType.typeOfValue(typeInt);
298  }
299  catch (NumberFormatException e) {
300  return null;
301  }
302 
304  {
305  return null;
306  }
307 
308  SessionTypeEnum sessionTypeEnum = sessionTypeV1(type);
309  if (sessionTypeEnum == null || sessionTypeEnum == SessionTypeEnum.None) {
310  return null;
311  }
312 
313  return new Pair<>(components[2], sessionTypeEnum);
314  }
315 }
static boolean isSameP2PConversation(String conversationId1, String conversationId2)
判断两个 P2P conversationId 是否代表同一个会话(双方视角不同但会话相同)。 P2P conversationId 格式为 ...
static SessionTypeEnum sessionTypeV1(@Nullable V2NIMConversationType conversationType)
static Pair< String, SessionTypeEnum > parseConversationId(String conversationId)
static String conversationId(String sessionId, SessionTypeEnum type)
static V2NIMConversationType conversationType(String conversationId)
根据会话id返回会话类型
static String conversationTargetId(String conversationId)
根据会话id返回会话对应目标 ID(accountId、teamId、superTeamId)
static V2NIMConversationType conversationType(SessionTypeEnum sessionType)
static String teamConversationId(String teamId)
生成team会话id
static String p2pConversationId(String accountId)
生成p2p会话id
static boolean isConversationIdValid(String conversationId)
会话id是否合法
static String superTeamConversationId(String superTeamId)
生成superTeam会话id
static String conversationId(String targetId, V2NIMConversationType type)
生成会话id
static String conversationSenderId(String conversationId)
根据会话id返回会话发送方账号id