File size: 5,420 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
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
import axios from '../components/Api/api';
import { ThunkDispatch } from 'redux-thunk';
import { AnyAction } from 'redux';

import {
  ACTION,
  UpdateActiveStateAction,
  ChangeServerAction,
  ChangeChannelAction,
  ChangeViewAction,
  ChangePmUserAction,
  LoadUserDataAction,
  LoadInitialData,
  SignInAction,
  SignInData,
  SignOutAction,
  SendJoinVoiceData,
  SendJoinVoiceAction,
  ReceiveJoinVoiceData,
  ReceiveJoinVoiceAction,
  SendRtcSignalData,
  ReceiveRtcSignalData,
  ReceiveRtcSignalAction,
  SendRtcSignalAction,
  SendLeaveVoiceAction,
  ReceiveLeaveVoiceAction,
  SendLeaveVoiceData,
  ReceiveLeaveVoiceData,
  ClearVoiceConnectionAction
} from './types';
import {
  SendMessageData,
  ReceiveMessageData,
  SendPrivateMessageData,
  ReceivePrivateMessageData,
  AddChannelData,
  AddServerData
} from './types';
import {
  SendMessageAction,
  ReceiveMessageAction,
  SendPrivateMessageAction,
  ReceivePrivateMessageAction,
  AddChannelAction,
  AddServerAction
} from './types';

// Action to send a message (Handled by socket middleware)
export const sendMessage = (message: SendMessageData): SendMessageAction => ({
  type: ACTION.SEND_SOCKET_MESSAGE,
  payload: message
});

// Action to add message to a channel (Handled by socket middleware)
export const receiveMessage = (message: ReceiveMessageData): ReceiveMessageAction => ({
  type: ACTION.RECEIVE_SOCKET_MESSAGE,
  payload: message
});

// Action to send new private message (Handled by socket middleware)
export const sendPrivateMessage = (message: SendPrivateMessageData): SendPrivateMessageAction => ({
  type: ACTION.SEND_SOCKET_PRIVATE_MESSAGE,
  payload: message
});

// Action to send new private message (Handled by socket middleware)
export const receivePrivateMessage = (message: ReceivePrivateMessageData): ReceivePrivateMessageAction => ({
  type: ACTION.RECEIVE_SOCKET_PRIVATE_MESSAGE,
  payload: message
});

// Action to send a join voice channel message (Handles by socket middleware)
export const sendJoinVoice = (data: SendJoinVoiceData): SendJoinVoiceAction => ({
  type: ACTION.SEND_SOCKET_JOIN_VOICE,
  payload: data
});

// Action to receive join voice channel message (Handles by socket middlware)
export const receiveJoinVoice = (data: ReceiveJoinVoiceData): ReceiveJoinVoiceAction => ({
  type: ACTION.RECEIVE_SOCKET_JOIN_VOICE,
  payload: data
});

export const sendRtcSignal = (data: SendRtcSignalData): SendRtcSignalAction => ({
  type: ACTION.SEND_SOCKET_RTC_SIGNAL,
  payload: data
});

export const sendLeaveVoice = (data: SendLeaveVoiceData): SendLeaveVoiceAction => ({
  type: ACTION.SEND_SOCKET_LEAVE_VOICE,
  payload: data
});

export const receiveLeaveVoice = (data: ReceiveLeaveVoiceData): ReceiveLeaveVoiceAction => ({
  type: ACTION.RECEIVE_SOCKET_LEAVE_VOICE,
  payload: data
});

export const clearVoiceConnection = (): ClearVoiceConnectionAction => ({
  type: ACTION.CLEAR_VOICE_CONNECTION,
  payload: null
});

export const receiveRtcSignal = (data: ReceiveRtcSignalData): ReceiveRtcSignalAction => ({
  type: ACTION.RECEIVE_SOCKET_RTC_SIGNAL,
  payload: data
});

// Action to add Channel to a Server
export const addChannel = (channel: AddChannelData): AddChannelAction => ({
  type: ACTION.ADD_CHANNEL,
  payload: channel
});

// Action to add Server to server list
export const addServer = (server: AddServerData): AddServerAction => ({
  type: ACTION.ADD_SERVER,
  payload: server
});

// Get active user list in given server
export const updateActiveUserList = (serverId: string) => async (dispatch: ThunkDispatch<{}, {}, AnyAction>) => {
  const response = await axios.get(`/server/activeusers?serverId=${serverId}`);
  dispatch({ type: ACTION.UPDATE_ACTIVE_USERS, payload: response.data });
};

// Action creator to update active state (socket middleware)
export const updateActiveState = (): UpdateActiveStateAction => ({
  type: ACTION.UPDATE_ACTIVE_STATE,
  payload: null
});

// Action to change the current Active Server
export const changeServer = (server: string) => (dispatch: ThunkDispatch<{}, {}, AnyAction>) => {
  dispatch(updateActiveUserList(server.split('-')[1]));
  dispatch<ChangeServerAction>({ type: ACTION.CHANGE_SERVER, payload: server });
};

// Action to change the current Active Channel
export const changeChannel = (channel: string): ChangeChannelAction => ({
  type: ACTION.CHANGE_CHANNEL,
  payload: channel
});

// Action to change the current active view
export const changeView = (view: string): ChangeViewAction => ({
  type: ACTION.CHANGE_VIEW,
  payload: view
});

// Action to change active user we have private message open with
export const changePMUser = (user: string): ChangePmUserAction => ({
  type: ACTION.CHANGE_PM_USER,
  payload: user
});

// Loads user Data. Gets all Servers + Channel History
export const loadUserData = (userId: string) => async (dispatch: ThunkDispatch<{}, {}, AnyAction>) => {
  let url = `/user/data?userId=${userId}`;
  const res = await axios.get<LoadInitialData>(url);
  // get active user list for first server
  dispatch(updateActiveUserList(Object.keys(res.data.servers)[0].split('-')[1]));
  dispatch<LoadUserDataAction>({ type: ACTION.GET_INITIAL_DATA, payload: res.data });
};

// On sign in
export const signIn = (user: SignInData): SignInAction => ({
  type: ACTION.SIGN_IN,
  payload: user
});

// On sign out
export const signOut = (): SignOutAction => ({
  type: ACTION.SIGN_OUT,
  payload: null
});