File size: 5,052 Bytes
058cd84
8c4ec99
 
362ec6c
058cd84
 
6a6f6eb
6b8fc2c
362ec6c
 
8c4ec99
058cd84
 
 
6b8fc2c
 
362ec6c
 
 
 
8c4ec99
058cd84
 
 
362ec6c
 
 
 
 
 
 
6b8fc2c
8c4ec99
 
 
 
 
6b8fc2c
058cd84
 
 
 
 
 
 
 
 
 
 
 
 
aa396c5
 
 
 
 
058cd84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
362ec6c
8c4ec99
362ec6c
8c4ec99
 
058cd84
 
 
8c4ec99
 
 
 
 
 
 
058cd84
 
 
 
 
 
 
 
 
8c4ec99
 
 
 
 
 
 
058cd84
 
 
 
8c4ec99
 
6a6f6eb
 
 
 
 
 
 
 
 
 
058cd84
 
6a6f6eb
8c4ec99
 
 
058cd84
 
 
 
 
 
 
 
 
8c4ec99
 
 
058cd84
 
 
 
 
 
 
 
8c4ec99
6a6f6eb
 
 
 
 
 
 
 
 
 
 
 
 
362ec6c
6b8fc2c
 
362ec6c
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
import { IConversation, IDialog, Message } from '@/interfaces/database/chat';
import chatService from '@/services/chatService';
import { message } from 'antd';
import { DvaModel } from 'umi';
import { v4 as uuid } from 'uuid';
import { IClientConversation, IMessage } from './interface';
import { getDocumentIdsFromConversionReference } from './utils';

export interface ChatModelState {
  name: string;
  dialogList: IDialog[];
  currentDialog: IDialog;
  conversationList: IConversation[];
  currentConversation: IClientConversation;
}

const model: DvaModel<ChatModelState> = {
  namespace: 'chatModel',
  state: {
    name: 'kate',
    dialogList: [],
    currentDialog: <IDialog>{},
    conversationList: [],
    currentConversation: {} as IClientConversation,
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    setDialogList(state, { payload }) {
      return {
        ...state,
        dialogList: payload,
      };
    },
    setCurrentDialog(state, { payload }) {
      return {
        ...state,
        currentDialog: payload,
      };
    },
    setConversationList(state, { payload }) {
      return {
        ...state,
        conversationList: payload,
      };
    },
    setCurrentConversation(state, { payload }) {
      const messageList =
        payload?.message?.map((x: Message | IMessage) => ({
          ...x,
          id: 'id' in x ? x.id : uuid(),
        })) ?? [];
      return {
        ...state,
        currentConversation: { ...payload, message: messageList },
      };
    },
    addEmptyConversationToList(state, {}) {
      const list = [...state.conversationList];
      // if (list.every((x) => x.id !== 'empty')) {
      //   list.push({
      //     id: 'empty',
      //     name: 'New conversation',
      //     message: [],
      //   });
      // }
      return {
        ...state,
        conversationList: list,
      };
    },
  },

  effects: {
    *getDialog({ payload }, { call, put }) {
      const { data } = yield call(chatService.getDialog, payload);
      if (data.retcode === 0) {
        yield put({ type: 'setCurrentDialog', payload: data.data });
      }
    },
    *setDialog({ payload }, { call, put }) {
      const { data } = yield call(chatService.setDialog, payload);
      if (data.retcode === 0) {
        yield put({ type: 'listDialog' });
        message.success('Created successfully !');
      }
      return data.retcode;
    },
    *removeDialog({ payload }, { call, put }) {
      const { data } = yield call(chatService.removeDialog, payload);
      if (data.retcode === 0) {
        yield put({ type: 'listDialog' });
        message.success('Deleted successfully !');
      }
      return data.retcode;
    },
    *listDialog({ payload }, { call, put }) {
      const { data } = yield call(chatService.listDialog, payload);
      yield put({ type: 'setDialogList', payload: data.data });
    },
    *listConversation({ payload }, { call, put }) {
      const { data } = yield call(chatService.listConversation, payload);
      if (data.retcode === 0) {
        yield put({ type: 'setConversationList', payload: data.data });
      }
      return data.retcode;
    },
    *getConversation({ payload }, { call, put }) {
      const { data } = yield call(chatService.getConversation, {
        conversation_id: payload.conversation_id,
      });
      if (data.retcode === 0 && payload.needToBeSaved) {
        yield put({
          type: 'kFModel/fetch_document_thumbnails',
          payload: {
            doc_ids: getDocumentIdsFromConversionReference(data.data),
          },
        });
        yield put({ type: 'setCurrentConversation', payload: data.data });
      }
      return data;
    },
    *setConversation({ payload }, { call, put }) {
      const { data } = yield call(chatService.setConversation, payload);
      if (data.retcode === 0) {
        yield put({
          type: 'listConversation',
          payload: {
            dialog_id: data.data.dialog_id,
          },
        });
      }
      return data;
    },
    *completeConversation({ payload }, { call, put }) {
      const { data } = yield call(chatService.completeConversation, payload);
      if (data.retcode === 0) {
        yield put({
          type: 'getConversation',
          payload: {
            conversation_id: payload.conversation_id,
          },
        });
      }
    },
    *removeConversation({ payload }, { call, put }) {
      const { data } = yield call(chatService.removeConversation, {
        conversation_ids: payload.conversation_ids,
      });
      if (data.retcode === 0) {
        yield put({
          type: 'listConversation',
          payload: { dialog_id: payload.dialog_id },
        });
        message.success('Deleted successfully !');
      }
      return data.retcode;
    },
  },
};

export default model;