1 package com.netease.nimlib.sdk.v2.utils;
3 import android.text.TextUtils;
4 import android.util.Pair;
5 import androidx.annotation.Nullable;
10 import java.util.Locale;
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 =
"\\|";
30 if (TextUtils.isEmpty(targetId) || type == null) {
33 String account = SDKCacheUI.getAccount();
34 if(TextUtils.isEmpty(account))
38 return String.format(Locale.ENGLISH,CONVERSATION_ID_FORMAT, account, type.
getValue(), targetId);
87 if (TextUtils.isEmpty(conversationId)) {
90 String[] components = conversationId.split(CONVERSATION_ID_SPLIT);
92 if (components.length != 3) {
97 if (TextUtils.isEmpty(components[0]) || TextUtils.isEmpty(components[2])) {
101 int type = Integer.parseInt(components[1]);
105 catch (Throwable e) {
106 NimLog.e(TAG,
"conversationType error, conversationId=" + conversationId +
", e=" + e.getMessage());
117 if (TextUtils.isEmpty(conversationId)) {
120 String[] components = conversationId.split(CONVERSATION_ID_SPLIT);
122 if (components.length != 3) {
127 if (TextUtils.isEmpty(components[0]) || TextUtils.isEmpty(components[2])) {
131 return components[2];
140 if (TextUtils.isEmpty(conversationId)) {
143 String[] components = conversationId.split(CONVERSATION_ID_SPLIT);
145 if (components.length != 3) {
149 if (TextUtils.isEmpty(components[0]) || TextUtils.isEmpty(components[2])) {
153 return components[0];
162 if (TextUtils.isEmpty(conversationId)) {
165 String[] components = conversationId.split(CONVERSATION_ID_SPLIT);
167 if (components.length != 3) {
171 String account = SDKCacheUI.getAccount();
172 if(account != null && !account.equals(components[0]))
177 if(TextUtils.isEmpty(components[0]))
184 int typeInt = Integer.parseInt(components[1]);
187 catch (NumberFormatException e) {
195 if(TextUtils.isEmpty(components[2]))
206 if (conversationType == null) {
209 switch (conversationType) {
210 case V2NIM_CONVERSATION_TYPE_P2P:
212 case V2NIM_CONVERSATION_TYPE_TEAM:
214 case V2NIM_CONVERSATION_TYPE_SUPER_TEAM:
222 if (sessionType == null) {
225 switch (sessionType) {
247 if (TextUtils.isEmpty(conversationId1) || TextUtils.isEmpty(conversationId2)) {
250 if (conversationId1.equals(conversationId2)) {
263 if (TextUtils.isEmpty(sender1) || TextUtils.isEmpty(target1)
264 || TextUtils.isEmpty(sender2) || TextUtils.isEmpty(target2)) {
267 return sender1 != null && sender1.equals(target2) && target1 != null && target1.equals(sender2);
271 if (TextUtils.isEmpty(conversationId)) {
274 String[] components = conversationId.split(CONVERSATION_ID_SPLIT);
276 if (components.length != 3) {
280 String account = SDKCacheUI.getAccount();
281 if(account != null && !account.equals(components[0]))
286 if(TextUtils.isEmpty(components[0]))
291 if (TextUtils.isEmpty(components[2])) {
296 int typeInt = Integer.parseInt(components[1]);
299 catch (NumberFormatException e) {
313 return new Pair<>(components[2], sessionTypeEnum);
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返回会话类型
V2NIM_CONVERSATION_TYPE_UNKNOWN
未知
static String conversationTargetId(String conversationId)
根据会话id返回会话对应目标 ID(accountId、teamId、superTeamId)
V2NIM_CONVERSATION_TYPE_SUPER_TEAM
超大群
static V2NIMConversationType conversationType(SessionTypeEnum sessionType)
V2NIM_CONVERSATION_TYPE_TEAM
群聊
static V2NIMConversationType typeOfValue(int value)
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
V2NIM_CONVERSATION_TYPE_P2P
单聊