ConversationUtil.js
4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import StorageUtil from './StorageUtil';
import TimeUtil from './TimeUtil';
import Utils from './Utils';
import UserInfoUtil from './UserInfoUtil';
// 会话工具列
export default class ConversationUtil {
// 获取所有会话,username为null的话是获取所有的会话,username为当前登录用户,可以获取与该用户有关的所有会话
static getConversations(username, callback) {
StorageUtil.get('conversations', (error, object) => {
if (!error && object && object.conversations) {
let result = object.conversations;
if (username != null) {
result = object.conversations.filter(function (con) {
return Utils.endWith(con.conversationId, username) || Utils.startWith(con.conversationId, username);
});
}
callback && callback(result);
} else {
let conversations = [];
StorageUtil.set('conversations', {'conversations': conversations});
callback(conversations);
}
})
}
static getConversation(conversationId, callback) {
this.getConversations(null, (result) => {
let conversation = this.isConversationExists(result, conversationId);
callback(conversation);
});
}
// 根据用户名生成会话id,规则是将较小的用户名排在前面
static generateConversationId(username1, username2) {
if (username1 < username2) {
return username1 + username2;
}
return username2 + username1;
}
// 判断某个会话是否存在
static isConversationExists(conversations, conversationId) {
for (let i = 0; i < conversations.length; i++) {
if (conversations[i].conversationId == conversationId) {
return conversations[i];
}
}
return null;
}
static showConversations() {
this.getConversations(null, (result) => {
console.log('-----------show conversations-----------')
console.log(result);
console.log('-----------show conversations-----------')
})
}
// 添加一条message
static addMessage(message, callback) {
let conversationId = message.conversationId;
this.getConversations(null, (result) => {
let conversation = this.isConversationExists(result, conversationId);
if (conversation) {
if (conversation.unreadCount) {
conversation.unreadCount = conversation.unreadCount + 1;
} else {
conversation.unreadCount = 1;
}
conversation.messages.push(message);
conversation.lastTime = TimeUtil.currentTime();
} else {
conversation = {
'conversationId': conversationId,
'messages': [message],
'lastTime': TimeUtil.currentTime(),
'avatar': null,
'unreadCount': 1
};
result.push(conversation);
}
result.sort(function (a, b) {
if (a.lastTime > b.lastTime) {
return -1;
} else if (a.lastTime < b.lastTime) {
return 1;
}
return 0;
});
StorageUtil.get('username', (error, object) => {
if (!error && object) {
let username = object.username;
let chatWithUsername = message.from;
if (chatWithUsername == username) {
chatWithUsername = message.to;
}
UserInfoUtil.getUserInfo(chatWithUsername, (userInfo) => {
if (userInfo != null) {
conversation['avatar'] = userInfo.avatar;
conversation['nick'] = userInfo.nick;
}
StorageUtil.set('conversations', {'conversations': result}, (error) => {
callback && callback(error);
});
})
}
});
})
}
// 将conversationId对应的会话未读书置为0
static clearUnreadCount(conversationId, callback) {
this.getConversations(null, (conversations)=>{
if (conversations) {
for (let i = 0; i < conversations.length; i++) {
if (conversations[i].conversationId == conversationId) {
conversations[i].unreadCount = 0;
break;
}
}
StorageUtil.set('conversations', {'conversations': conversations}, ()=>{
callback && callback();
});
}
})
}
}