@@ -11,19 +11,28 @@ const {
1111 CREATE_CONVERSES_SUCCESS ,
1212 CREATE_CONVERSES_FAILED ,
1313 REMOVE_CONVERSES_SUCCESS ,
14+ REMOVE_USER_CONVERSE ,
1415 UPDATE_CONVERSES_INFO_SUCCESS ,
1516 UPDATE_CONVERSES_MSGLIST_SUCCESS ,
1617 SWITCH_CONVERSES ,
1718 SEND_MSG ,
1819 SEND_MSG_COMPLETED ,
1920 UPDATE_SYSTEM_CARD_CHAT_DATA ,
21+ UPDATE_WRITING_STATUS ,
2022} = constants ;
2123import * as trpgApi from '../../api/trpg.api.js' ;
2224const api = trpgApi . getInstance ( ) ;
2325import rnStorage from '../../api/rnStorage.api.js' ;
2426import { checkUser } from '../../shared/utils/cacheHelper' ;
2527import { hideProfileCard , switchMenuPannel } from './ui' ;
2628import uploadHelper from '../../shared/utils/uploadHelper' ;
29+ import { renewableDelayTimer } from '../../shared/utils/timer' ;
30+ import config from '../../../config/project.config' ;
31+ import _without from 'lodash/without' ;
32+
33+ const getUserConversesHash = ( userUUID ) => {
34+ return `userConverses#${ userUUID } ` ;
35+ } ;
2736
2837let localIndex = 0 ;
2938let getLocalUUID = function getLocalUUID ( ) {
@@ -157,6 +166,8 @@ export let createConverse = function createConverse(
157166 } ) ;
158167 } ;
159168} ;
169+
170+ // 移除多人会话
160171export let removeConverse = function removeConverse ( converseUUID ) {
161172 return function ( dispatch , getState ) {
162173 return api . emit ( 'chat::removeConverse' , { converseUUID } , function ( data ) {
@@ -169,6 +180,24 @@ export let removeConverse = function removeConverse(converseUUID) {
169180 } ;
170181} ;
171182
183+ export const removeUserConverse = ( userConverseUUID ) => {
184+ return ( dispatch , getState ) => {
185+ // 在当前删除
186+ dispatch ( { type : REMOVE_USER_CONVERSE , converseUUID : userConverseUUID } ) ;
187+
188+ // 在localStorage删除
189+ const userUUID = getState ( ) . getIn ( [ 'user' , 'info' , 'uuid' ] ) ;
190+ const converses = getState ( ) . getIn ( [ 'chat' , 'converses' ] ) ;
191+ const uuids = Object . keys (
192+ converses . filter ( ( c ) => c . get ( 'type' ) === 'user' ) . toJS ( )
193+ ) ;
194+ rnStorage . set (
195+ getUserConversesHash ( userUUID ) ,
196+ _without ( uuids , userConverseUUID )
197+ ) ;
198+ } ;
199+ } ;
200+
172201export let addUserConverse = function addUserConverse ( senders ) {
173202 return function ( dispatch , getState ) {
174203 if ( typeof senders === 'string' ) {
@@ -183,10 +212,10 @@ export let addUserConverse = function addUserConverse(senders) {
183212
184213 // 用户会话缓存
185214 let userUUID = getState ( ) . getIn ( [ 'user' , 'info' , 'uuid' ] ) ;
186- rnStorage . get ( 'userConverses#' + userUUID ) . then ( function ( converse ) {
215+ rnStorage . get ( getUserConversesHash ( userUUID ) ) . then ( function ( converse ) {
187216 converse = Array . from ( new Set ( [ ...converse , ...senders ] ) ) ;
188217 rnStorage
189- . set ( 'userConverses#' + userUUID , converse )
218+ . set ( getUserConversesHash ( userUUID ) , converse )
190219 . then ( ( data ) => console . log ( '用户会话缓存完毕:' , data ) ) ;
191220 } ) ;
192221
@@ -266,13 +295,15 @@ export let reloadConverseList = function reloadConverseList(cb) {
266295 let userInfo = getState ( ) . getIn ( [ 'user' , 'info' ] ) ;
267296 let userUUID = userInfo . get ( 'uuid' ) ;
268297
269- dispatch ( getConverses ( cb ) ) ;
270- rnStorage . get ( 'userConverses#' + userUUID ) . then ( function ( converse ) {
298+ dispatch ( getConverses ( cb ) ) ; // 从服务端获取多人会话列表
299+ rnStorage . get ( getUserConversesHash ( userUUID ) ) . then ( function ( converse ) {
271300 console . log ( '缓存中的用户会话列表:' , converse ) ;
272301 if ( converse && converse . length > 0 ) {
302+ // 如果本地缓存有存在用户会话,则根据上次登录时间获取这段时间内新建的用户会话
273303 dispatch ( addUserConverse ( converse ) ) ;
274304 dispatch ( getOfflineUserConverse ( userInfo . get ( 'last_login' ) ) ) ;
275305 } else {
306+ // 如果本地没有存在用户会话,则获取所有的用户会话
276307 dispatch ( getAllUserConverse ( ) ) ;
277308 }
278309 } ) ;
@@ -481,3 +512,37 @@ export let updateCardChatData = function(chatUUID, newData) {
481512 } ) ;
482513 } ;
483514} ;
515+
516+ const getWriteHash = ( type = 'user' , uuid ) => {
517+ return `${ type } #${ uuid } ` ;
518+ } ;
519+ export let startWriting = function ( type = 'user' , uuid ) {
520+ return function ( dispatch , getState ) {
521+ dispatch ( {
522+ type : UPDATE_WRITING_STATUS ,
523+ payload : {
524+ type,
525+ uuid,
526+ isWriting : true ,
527+ } ,
528+ } ) ;
529+
530+ renewableDelayTimer (
531+ getWriteHash ( type , uuid ) ,
532+ function ( ) {
533+ dispatch ( stopWriting ( ) ) ; // 如果10秒后没有再次收到正在输入的信号,则视为已经停止输入了
534+ } ,
535+ config . chat . isWriting . timeout
536+ ) ;
537+ } ;
538+ } ;
539+ export let stopWriting = function ( type = 'user' , uuid ) {
540+ return {
541+ type : UPDATE_WRITING_STATUS ,
542+ payload : {
543+ type,
544+ uuid,
545+ isWriting : false ,
546+ } ,
547+ } ;
548+ } ;
0 commit comments