-
Notifications
You must be signed in to change notification settings - Fork 67
/
message.js
320 lines (315 loc) · 11.9 KB
/
message.js
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import { EMClient } from '@/IM'
import { setMessageKey } from '@/utils/handleSomeData'
import _ from 'lodash'
import {
MESSAGE_STATUS_TYPE,
CUSTOM_MESSAGE_TYPE,
CHANGE_MESSAGE_BODAY_TYPE,
CHAT_TYPE,
MAX_MESSAGE_LIST_COUNT
} from '@/constant'
import { usePlayRing } from '@/hooks'
const Message = {
state: {
messageList: {},
messageIdsCollection: {
// 'pfh':new Map(),
// 'pfh1':new Map(),
}
},
mutations: {
UPDATE_MESSAGE_LIST: (state, msgBody) => {
const { id: serverMsgId } = msgBody
const listKey = setMessageKey(msgBody)
if (!state.messageList[listKey]) {
state.messageList[listKey] = []
}
state.messageList[listKey] = _.unionBy(
state.messageList[listKey],
[msgBody],
(m) => m.id
)
// 限制数组的长度为 MAX_MESSAGE_LIST_COUNT
if (state.messageList[listKey].length > MAX_MESSAGE_LIST_COUNT) {
state.messageList[listKey] = state.messageList[listKey].slice(
-MAX_MESSAGE_LIST_COUNT
)
}
/**
* 暂只实现以单对单已读回执
* 群组已读回执可通过Reaction方案实现
*/
if (
!state.messageIdsCollection[listKey] &&
msgBody.chatType === CHAT_TYPE.SINGLE
) {
state.messageIdsCollection[listKey] = new Map()
}
if (
msgBody.from === EMClient.user &&
msgBody.chatType === CHAT_TYPE.SINGLE
) {
state.messageIdsCollection[listKey].set(serverMsgId, {
[MESSAGE_STATUS_TYPE.READ_STATUS]: false
})
}
},
UPDATE_HISTORY_MESSAGE: (state, payload) => {
const { listKey, historyMessageList } = payload
if (!state.messageList[listKey]) {
state.messageList[listKey] = []
}
state.messageList[listKey] = _.unionBy(
historyMessageList,
state.messageList[listKey],
(m) => m.id
)
},
UPDATE_MESSAGE_IDS_COLLECTION: (state, payload) => {
const { id: serverMsgId, key, type } = payload
switch (type) {
case MESSAGE_STATUS_TYPE.READ_STATUS:
{
if (state.messageIdsCollection[key]) {
state.messageIdsCollection[key].set(serverMsgId, {
[MESSAGE_STATUS_TYPE.READ_STATUS]: true
})
}
}
break
case MESSAGE_STATUS_TYPE.CHANLE_STATUS:
{
if (state.messageIdsCollection[key]) {
const READ_STATUS_KEY =
MESSAGE_STATUS_TYPE.READ_STATUS
// 直接使用Map的forEach方法
state.messageIdsCollection[key].forEach(
(value, key) => {
if (value[READ_STATUS_KEY] !== true) {
value[READ_STATUS_KEY] = true
}
}
)
}
}
break
default:
break
}
},
//清除某条会话消息
CLEAR_SOMEONE_MESSAGE: (state, payload) => {
state.messageList[payload] = []
},
//修改本地原消息【撤回、删除、编辑】
CHANGE_MESSAGE_BODAY: (state, payload) => {
const { type, key, mid } = payload
switch (type) {
case CHANGE_MESSAGE_BODAY_TYPE.RECALL:
{
if (state.messageList[key]) {
const res = _.find(
state.messageList[key],
(o) => o.id === mid
)
res.isRecall = true
}
}
break
case CHANGE_MESSAGE_BODAY_TYPE.DELETE:
{
if (state.messageList[key]) {
const sourceData = state.messageList[key]
const index = _.findIndex(
state.messageList[key],
(o) => o.id === mid
)
sourceData.splice(index, 1)
state.messageList[key] = _.assign([], sourceData)
}
}
break
case CHANGE_MESSAGE_BODAY_TYPE.MODIFY:
{
if (state.messageList[key]) {
const res = _.find(
state.messageList[key],
(o) => o.id === mid
)
_.assign(res, payload?.message)
}
}
break
default:
break
}
}
},
actions: {
//添加新消息
createNewMessage: ({ dispatch, commit }, params) => {
const { isOpenPlayRing, playRing } = usePlayRing()
const key = setMessageKey(params)
commit('UPDATE_MESSAGE_LIST', params)
//目前根据全局配置进行新消息声音提示,后续计划根据会话级别可进行设置是否声音提示,比如设定免打扰。
if (isOpenPlayRing.value) playRing()
dispatch('updateLocalConversation', {
conversationId: key,
chatType: params.chatType
})
},
//获取历史消息
getHistoryMessage: async ({ state, dispatch, commit }, params) => {
const { id, chatType, cursor } = params
return new Promise((resolve, reject) => {
const options = {
targetId: id,
pageSize: 10,
cursor: cursor,
chatType: chatType,
searchDirection: 'up'
}
EMClient.getHistoryMessages(options)
.then((res) => {
const { cursor, messages } = res
messages.length > 0 &&
messages.forEach((item) => {
item.read = true
})
resolve({ messages, cursor })
commit('UPDATE_HISTORY_MESSAGE', {
listKey: id,
historyMessageList: _.reverse(messages)
})
if (!state.messageList[id]) {
//提示会话列表更新
dispatch('updateLocalConversation', {
conversationId: id,
chatType: chatType
})
}
})
.catch((error) => {
reject(error)
})
})
},
//已发送展示类型消息
senedShowTypeMessage: async ({ dispatch, commit }, message) => {
commit('UPDATE_MESSAGE_LIST', message)
// 提示会话列表更新
dispatch('updateLocalConversation', {
conversationId: message.to,
chatType: message.chatType
})
},
//添加通知类消息
createInformMessage: ({ dispatch, commit }, params) => {
/**
const params = {
from: '',
to: '',
chatType: '',
msg:''
}
*/
console.log('first', params)
const msgBody = _.cloneDeep(params)
msgBody.type = CUSTOM_MESSAGE_TYPE.INFORM
const key = setMessageKey(params)
commit('UPDATE_MESSAGE_LIST', msgBody)
dispatch('updateLocalConversation', {
conversationId: key,
chatType: msgBody.chatType
})
},
//删除消息
removeMessage: ({ dispatch, commit }, params) => {
const { id: mid, chatType } = params
const key = setMessageKey(params)
return new Promise((resolve, reject) => {
EMClient.removeHistoryMessages({
targetId: key,
chatType: chatType,
messageIds: [mid]
})
.then((res) => {
commit('CHANGE_MESSAGE_BODAY', {
type: CHANGE_MESSAGE_BODAY_TYPE.DELETE,
key: key,
mid
})
dispatch('updateLocalConversation', {
conversationId: key,
chatType
})
resolve('OK')
})
.catch((error) => {
reject(error)
})
})
},
//撤回消息
recallMessage: async ({ dispatch, commit }, params) => {
const { mid, to, chatType } = params
return new Promise((resolve, reject) => {
EMClient.recallMessage({ mid, to, chatType })
.then(() => {
commit('CHANGE_MESSAGE_BODAY', {
type: CHANGE_MESSAGE_BODAY_TYPE.RECALL,
key: to,
mid
})
dispatch('updateLocalConversation', {
conversationId: to,
chatType
})
resolve('OK')
})
.catch((error) => {
reject(error)
})
})
},
//修改(编辑)消息
modifyMessage: async ({ dispatch, commit }, params) => {
const { id: mid, to, chatType, msg } = params
return new Promise((resolve, reject) => {
const textMessage = EMClient.Message.create({
type: 'txt',
msg: msg,
to: to,
chatType: chatType
})
EMClient.modifyMessage({
messageId: mid,
modifiedMessage: textMessage
})
.then((res) => {
const { message } = res || {}
commit('CHANGE_MESSAGE_BODAY', {
type: CHANGE_MESSAGE_BODAY_TYPE.MODIFY,
key: to,
mid,
message
})
dispatch('updateLocalConversation', {
conversationId: to,
chatType
})
resolve(res)
})
.catch((e) => {
reject(e)
})
})
}
},
getters: {
getMessageIdsCollectionMap: (state) => (messageIdsCollectionKey) => {
return state.messageIdsCollection[messageIdsCollectionKey]
}
}
}
export default Message