2020-08-24 19:26:42 -07:00
|
|
|
import api from '../api';
|
2020-08-25 15:54:10 -07:00
|
|
|
import { getSettings, changeSetting } from 'soapbox/actions/settings';
|
2020-08-26 22:06:27 -07:00
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
2020-08-25 15:54:10 -07:00
|
|
|
import { Map as ImmutableMap } from 'immutable';
|
2020-08-24 19:26:42 -07:00
|
|
|
|
|
|
|
export const CHATS_FETCH_REQUEST = 'CHATS_FETCH_REQUEST';
|
|
|
|
export const CHATS_FETCH_SUCCESS = 'CHATS_FETCH_SUCCESS';
|
|
|
|
export const CHATS_FETCH_FAIL = 'CHATS_FETCH_FAIL';
|
|
|
|
|
2020-08-25 18:33:49 -07:00
|
|
|
export const CHAT_MESSAGES_FETCH_REQUEST = 'CHAT_MESSAGES_FETCH_REQUEST';
|
|
|
|
export const CHAT_MESSAGES_FETCH_SUCCESS = 'CHAT_MESSAGES_FETCH_SUCCESS';
|
|
|
|
export const CHAT_MESSAGES_FETCH_FAIL = 'CHAT_MESSAGES_FETCH_FAIL';
|
|
|
|
|
2020-08-25 19:31:34 -07:00
|
|
|
export const CHAT_MESSAGE_SEND_REQUEST = 'CHAT_MESSAGE_SEND_REQUEST';
|
|
|
|
export const CHAT_MESSAGE_SEND_SUCCESS = 'CHAT_MESSAGE_SEND_SUCCESS';
|
|
|
|
export const CHAT_MESSAGE_SEND_FAIL = 'CHAT_MESSAGE_SEND_FAIL';
|
|
|
|
|
2020-08-26 17:46:23 -07:00
|
|
|
export const CHAT_FETCH_REQUEST = 'CHAT_FETCH_REQUEST';
|
|
|
|
export const CHAT_FETCH_SUCCESS = 'CHAT_FETCH_SUCCESS';
|
|
|
|
export const CHAT_FETCH_FAIL = 'CHAT_FETCH_FAIL';
|
|
|
|
|
2020-08-27 14:09:03 -07:00
|
|
|
export const CHAT_READ_REQUEST = 'CHAT_READ_REQUEST';
|
|
|
|
export const CHAT_READ_SUCCESS = 'CHAT_READ_SUCCESS';
|
|
|
|
export const CHAT_READ_FAIL = 'CHAT_READ_FAIL';
|
|
|
|
|
2020-09-04 19:43:14 -07:00
|
|
|
export const STREAMING_CHAT_UPDATE = 'STREAMING_CHAT_UPDATE';
|
|
|
|
|
2020-08-24 19:26:42 -07:00
|
|
|
export function fetchChats() {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
dispatch({ type: CHATS_FETCH_REQUEST });
|
|
|
|
return api(getState).get('/api/v1/pleroma/chats').then(({ data }) => {
|
2020-08-26 17:53:54 -07:00
|
|
|
dispatch({ type: CHATS_FETCH_SUCCESS, chats: data });
|
2020-08-24 19:26:42 -07:00
|
|
|
}).catch(error => {
|
|
|
|
dispatch({ type: CHATS_FETCH_FAIL, error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2020-08-25 15:54:10 -07:00
|
|
|
|
2020-09-03 17:23:00 -07:00
|
|
|
export function fetchChatMessages(chatId, maxId = null) {
|
2020-08-25 18:33:49 -07:00
|
|
|
return (dispatch, getState) => {
|
2020-09-03 17:23:00 -07:00
|
|
|
dispatch({ type: CHAT_MESSAGES_FETCH_REQUEST, chatId, maxId });
|
|
|
|
return api(getState).get(`/api/v1/pleroma/chats/${chatId}/messages`, { params: { max_id: maxId } }).then(({ data }) => {
|
|
|
|
dispatch({ type: CHAT_MESSAGES_FETCH_SUCCESS, chatId, maxId, chatMessages: data });
|
2020-08-25 18:33:49 -07:00
|
|
|
}).catch(error => {
|
2020-09-03 17:23:00 -07:00
|
|
|
dispatch({ type: CHAT_MESSAGES_FETCH_FAIL, chatId, maxId, error });
|
2020-08-25 18:33:49 -07:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-25 19:31:34 -07:00
|
|
|
export function sendChatMessage(chatId, params) {
|
|
|
|
return (dispatch, getState) => {
|
2020-09-04 16:03:38 -07:00
|
|
|
const uuid = `末_${Date.now()}_${uuidv4()}`;
|
2020-08-26 22:06:27 -07:00
|
|
|
const me = getState().get('me');
|
|
|
|
dispatch({ type: CHAT_MESSAGE_SEND_REQUEST, chatId, params, uuid, me });
|
2020-08-25 19:31:34 -07:00
|
|
|
return api(getState).post(`/api/v1/pleroma/chats/${chatId}/messages`, params).then(({ data }) => {
|
2020-08-26 22:06:27 -07:00
|
|
|
dispatch({ type: CHAT_MESSAGE_SEND_SUCCESS, chatId, chatMessage: data, uuid });
|
2020-08-25 19:31:34 -07:00
|
|
|
}).catch(error => {
|
2020-08-26 22:06:27 -07:00
|
|
|
dispatch({ type: CHAT_MESSAGE_SEND_FAIL, chatId, error, uuid });
|
2020-08-25 19:31:34 -07:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-25 15:54:10 -07:00
|
|
|
export function openChat(chatId) {
|
|
|
|
return (dispatch, getState) => {
|
2020-08-27 14:09:03 -07:00
|
|
|
const state = getState();
|
|
|
|
const panes = getSettings(state).getIn(['chats', 'panes']);
|
2020-08-25 15:54:10 -07:00
|
|
|
const idx = panes.findIndex(pane => pane.get('chat_id') === chatId);
|
|
|
|
|
2020-08-27 14:09:03 -07:00
|
|
|
dispatch(markChatRead(chatId));
|
|
|
|
|
2020-08-25 15:54:10 -07:00
|
|
|
if (idx > -1) {
|
|
|
|
return dispatch(changeSetting(['chats', 'panes', idx, 'state'], 'open'));
|
|
|
|
} else {
|
|
|
|
const newPane = ImmutableMap({ chat_id: chatId, state: 'open' });
|
|
|
|
return dispatch(changeSetting(['chats', 'panes'], panes.push(newPane)));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2020-08-25 16:11:48 -07:00
|
|
|
|
|
|
|
export function closeChat(chatId) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const panes = getSettings(getState()).getIn(['chats', 'panes']);
|
|
|
|
const idx = panes.findIndex(pane => pane.get('chat_id') === chatId);
|
|
|
|
|
|
|
|
if (idx > -1) {
|
|
|
|
return dispatch(changeSetting(['chats', 'panes'], panes.delete(idx)));
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2020-08-25 16:45:05 -07:00
|
|
|
|
|
|
|
export function toggleChat(chatId) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const panes = getSettings(getState()).getIn(['chats', 'panes']);
|
|
|
|
const [idx, pane] = panes.findEntry(pane => pane.get('chat_id') === chatId);
|
|
|
|
|
|
|
|
if (idx > -1) {
|
|
|
|
const state = pane.get('state') === 'minimized' ? 'open' : 'minimized';
|
2020-08-27 14:09:03 -07:00
|
|
|
if (state === 'open') dispatch(markChatRead(chatId));
|
2020-08-25 16:45:05 -07:00
|
|
|
return dispatch(changeSetting(['chats', 'panes', idx, 'state'], state));
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2020-08-25 16:53:36 -07:00
|
|
|
|
|
|
|
export function toggleMainWindow() {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const main = getSettings(getState()).getIn(['chats', 'mainWindow']);
|
|
|
|
const state = main === 'minimized' ? 'open' : 'minimized';
|
|
|
|
return dispatch(changeSetting(['chats', 'mainWindow'], state));
|
|
|
|
};
|
|
|
|
}
|
2020-08-26 17:46:23 -07:00
|
|
|
|
2020-08-28 11:17:19 -07:00
|
|
|
export function fetchChat(chatId) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
dispatch({ type: CHAT_FETCH_REQUEST, chatId });
|
|
|
|
return api(getState).get(`/api/v1/pleroma/chats/${chatId}`).then(({ data }) => {
|
|
|
|
dispatch({ type: CHAT_FETCH_SUCCESS, chat: data });
|
|
|
|
}).catch(error => {
|
|
|
|
dispatch({ type: CHAT_FETCH_FAIL, chatId, error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-26 17:46:23 -07:00
|
|
|
export function startChat(accountId) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
dispatch({ type: CHAT_FETCH_REQUEST, accountId });
|
|
|
|
return api(getState).post(`/api/v1/pleroma/chats/by-account-id/${accountId}`).then(({ data }) => {
|
|
|
|
dispatch({ type: CHAT_FETCH_SUCCESS, chat: data });
|
2020-08-28 13:06:55 -07:00
|
|
|
return data;
|
2020-08-26 17:46:23 -07:00
|
|
|
}).catch(error => {
|
|
|
|
dispatch({ type: CHAT_FETCH_FAIL, accountId, error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2020-08-27 14:09:03 -07:00
|
|
|
|
|
|
|
export function markChatRead(chatId, lastReadId) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const chat = getState().getIn(['chats', chatId]);
|
|
|
|
if (!lastReadId) lastReadId = chat.get('last_message');
|
|
|
|
|
|
|
|
if (chat.get('unread') < 1) return;
|
|
|
|
if (!lastReadId) return;
|
|
|
|
|
|
|
|
dispatch({ type: CHAT_READ_REQUEST, chatId, lastReadId });
|
|
|
|
api(getState).post(`/api/v1/pleroma/chats/${chatId}/read`, { last_read_id: lastReadId }).then(({ data }) => {
|
|
|
|
dispatch({ type: CHAT_READ_SUCCESS, chat: data, lastReadId });
|
|
|
|
}).catch(error => {
|
|
|
|
dispatch({ type: CHAT_READ_FAIL, chatId, error, lastReadId });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2020-09-04 19:43:14 -07:00
|
|
|
|
|
|
|
export function updateChatsQueue(chat) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
if (chat.type === 'pleroma:chat_mention') return; // Drop chat notifications, handle them per-chat
|
|
|
|
|
|
|
|
// const showAlert = getSettings(getState()).getIn(['notifications', 'alerts', chat.type]);
|
|
|
|
// const filters = getFilters(getState(), { contextType: 'notifications' });
|
|
|
|
// const playSound = getSettings(getState()).getIn(['notifications', 'sounds', chat.type]);
|
|
|
|
//
|
|
|
|
// let filtered = false;
|
|
|
|
//
|
|
|
|
// const isOnNotificationsPage = curPath === '/notifications';
|
|
|
|
//
|
|
|
|
// if (chat.type === 'mention') {
|
|
|
|
// const regex = regexFromFilters(filters);
|
|
|
|
// const searchIndex = chat.status.spoiler_text + '\n' + unescapeHTML(chat.status.content);
|
|
|
|
// filtered = regex && regex.test(searchIndex);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // Desktop notifications
|
|
|
|
// if (typeof window.Notification !== 'undefined' && showAlert && !filtered) {
|
|
|
|
// const title = new IntlMessageFormat(intlMessages[`notification.${chat.type}`], intlLocale).format({ name: chat.account.display_name.length > 0 ? chat.account.display_name : notification.account.username });
|
|
|
|
// const body = (chat.status && chat.status.spoiler_text.length > 0) ? chat.status.spoiler_text : unescapeHTML(chat.status ? chat.status.content : '');
|
|
|
|
//
|
|
|
|
// const notify = new Notification(title, { body, icon: chat.account.avatar, tag: chat.id });
|
|
|
|
//
|
|
|
|
// notify.addEventListener('click', () => {
|
|
|
|
// window.focus();
|
|
|
|
// notify.close();
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (playSound && !filtered) {
|
|
|
|
// dispatch({
|
|
|
|
// type: CHATS_UPDATE_NOOP,
|
|
|
|
// meta: { sound: 'boop' },
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
|
|
|
|
dispatch({ type: STREAMING_CHAT_UPDATE, chat: chat, me: getState().get('me') });
|
|
|
|
// if (isOnNotificationsPage) {
|
|
|
|
// dispatch({
|
|
|
|
// type: CHATS_UPDATE_QUEUE,
|
|
|
|
// chat,
|
|
|
|
// intlMessages,
|
|
|
|
// intlLocale,
|
|
|
|
// });
|
|
|
|
// } else {
|
|
|
|
// dispatch(updateChats(chat, intlMessages, intlLocale));
|
|
|
|
// }
|
|
|
|
};
|
|
|
|
};
|