File size: 4,676 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { ACTION, ChatActionTypes } from '../actions/types';

export interface ChatStore {
  servers: {
    [serverName: string]: {
      channels: {
        [channelName: string]: { from: string; msg: string; date: Date }[];
      };
    };
  };
  privateMessages: {
    [userPM: string]: { from: string; to: string; msg: string; date: Date }[];
  };
  activeServer: string;
  activeChannel: string;
  activeUserList: { user_name: string }[];
  activeView: string;
  activePMUser: string;
  voiceClients: { userId: string; userName: string }[];
  voiceJoinUserId: string;
  voiceLeaveUserId: string;
  rtcSignalData: { userId: string; ice?: any; sdp?: any };
}

const initialState = {
  servers: {
    'Default-FANfDprXmt': {
      channels: {
        'general-0m5vBsRnfd': []
      }
    }
  },
  privateMessages: {},
  activeServer: 'Default-FANfDprXmt',
  activeChannel: 'general-0m5vBsRnfd',
  activeUserList: [],
  activeView: 'servers',
  activePMUser: 'none',
  voiceClients: [{ userId: '', userName: '' }],
  voiceJoinUserId: '',
  voiceLeaveUserId: '',
  rtcSignalData: { userId: '' }
};

export const chatReducer = (state: ChatStore = initialState, action: ChatActionTypes): ChatStore => {
  switch (action.type) {
    case ACTION.RECEIVE_SOCKET_MESSAGE:
      let { server, channel, from, msg, date } = action.payload;
      return {
        ...state,
        servers: {
          ...state.servers,
          [server]: {
            ...state.servers[server],
            channels: {
              ...state.servers[server].channels,
              [channel]: [...state.servers[server]['channels'][channel], { from: from, msg: msg, date: date }]
            }
          }
        }
      };
    case ACTION.RECEIVE_SOCKET_PRIVATE_MESSAGE:
      if (state.privateMessages[action.payload.user]) {
        return {
          ...state,
          privateMessages: {
            ...state.privateMessages,
            [action.payload.user]: [
              ...state.privateMessages[action.payload.user],
              { from: action.payload.from, to: action.payload.to, msg: action.payload.msg, date: action.payload.date }
            ]
          }
        };
      } else
        return {
          ...state,
          privateMessages: {
            ...state.privateMessages,
            [action.payload.user]: [
              { from: action.payload.from, to: action.payload.to, msg: action.payload.msg, date: action.payload.date }
            ]
          }
        };
    case ACTION.RECEIVE_SOCKET_JOIN_VOICE:
      return {
        ...state,
        voiceClients: action.payload.clients,
        voiceJoinUserId: action.payload.userId,
        voiceLeaveUserId: ''
      };
    case ACTION.RECEIVE_SOCKET_RTC_SIGNAL:
      return { ...state, rtcSignalData: action.payload };
    case ACTION.RECEIVE_SOCKET_LEAVE_VOICE:
      return { ...state, voiceLeaveUserId: action.payload.userId, voiceClients: action.payload.clients };
    case ACTION.CLEAR_VOICE_CONNECTION:
      return { ...state, voiceClients: [], voiceLeaveUserId: '', voiceJoinUserId: '' };
    case ACTION.ADD_CHANNEL:
      return {
        ...state,
        servers: {
          ...state.servers,
          [action.payload.server]: {
            ...state.servers[action.payload.server],
            channels: {
              ...state.servers[action.payload.server].channels,
              [action.payload.channel]: []
            }
          }
        }
      };
    case ACTION.ADD_SERVER:
      return {
        ...state,
        servers: {
          ...state.servers,
          [action.payload.server]: {
            channels: {
              [action.payload.channel]: []
            }
          }
        }
      };
    case ACTION.GET_INITIAL_DATA:
      return {
        ...state,
        servers: action.payload.servers,
        privateMessages: action.payload.privateMessages,
        activeServer: Object.keys(action.payload.servers)[0],
        activeChannel: Object.keys(state.servers[Object.keys(action.payload.servers)[0]].channels)[0]
      };
    case ACTION.CHANGE_SERVER:
      return {
        ...state,
        activeServer: action.payload,
        activeChannel: Object.keys(state.servers[action.payload].channels)[0]
      };
    case ACTION.CHANGE_CHANNEL:
      return { ...state, activeChannel: action.payload };
    case ACTION.CHANGE_VIEW:
      return { ...state, activeView: action.payload, activePMUser: Object.keys(state.privateMessages)[0] };
    case ACTION.CHANGE_PM_USER:
      return { ...state, activePMUser: action.payload };
    case ACTION.UPDATE_ACTIVE_USERS:
      return { ...state, activeUserList: action.payload };
    default:
      return { ...state };
  }
};