bigbuffet-rw/app/soapbox/actions/chats.js

206 lines
7.5 KiB
JavaScript

import api from '../api';
import { getSettings, changeSetting } from 'soapbox/actions/settings';
import { v4 as uuidv4 } from 'uuid';
import { Map as ImmutableMap } from 'immutable';
export const CHATS_FETCH_REQUEST = 'CHATS_FETCH_REQUEST';
export const CHATS_FETCH_SUCCESS = 'CHATS_FETCH_SUCCESS';
export const CHATS_FETCH_FAIL = 'CHATS_FETCH_FAIL';
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';
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';
export const CHAT_FETCH_REQUEST = 'CHAT_FETCH_REQUEST';
export const CHAT_FETCH_SUCCESS = 'CHAT_FETCH_SUCCESS';
export const CHAT_FETCH_FAIL = 'CHAT_FETCH_FAIL';
export const CHAT_READ_REQUEST = 'CHAT_READ_REQUEST';
export const CHAT_READ_SUCCESS = 'CHAT_READ_SUCCESS';
export const CHAT_READ_FAIL = 'CHAT_READ_FAIL';
export const STREAMING_CHAT_UPDATE = 'STREAMING_CHAT_UPDATE';
export function fetchChats() {
return (dispatch, getState) => {
dispatch({ type: CHATS_FETCH_REQUEST });
return api(getState).get('/api/v1/pleroma/chats').then(({ data }) => {
dispatch({ type: CHATS_FETCH_SUCCESS, chats: data });
}).catch(error => {
dispatch({ type: CHATS_FETCH_FAIL, error });
});
};
}
export function fetchChatMessages(chatId, maxId = null) {
return (dispatch, getState) => {
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 });
}).catch(error => {
dispatch({ type: CHAT_MESSAGES_FETCH_FAIL, chatId, maxId, error });
});
};
}
export function sendChatMessage(chatId, params) {
return (dispatch, getState) => {
const uuid = `末_${Date.now()}_${uuidv4()}`;
const me = getState().get('me');
dispatch({ type: CHAT_MESSAGE_SEND_REQUEST, chatId, params, uuid, me });
return api(getState).post(`/api/v1/pleroma/chats/${chatId}/messages`, params).then(({ data }) => {
dispatch({ type: CHAT_MESSAGE_SEND_SUCCESS, chatId, chatMessage: data, uuid });
}).catch(error => {
dispatch({ type: CHAT_MESSAGE_SEND_FAIL, chatId, error, uuid });
});
};
}
export function openChat(chatId) {
return (dispatch, getState) => {
const state = getState();
const panes = getSettings(state).getIn(['chats', 'panes']);
const idx = panes.findIndex(pane => pane.get('chat_id') === chatId);
dispatch(markChatRead(chatId));
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)));
}
};
}
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;
}
};
}
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';
if (state === 'open') dispatch(markChatRead(chatId));
return dispatch(changeSetting(['chats', 'panes', idx, 'state'], state));
} else {
return false;
}
};
}
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));
};
}
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 });
});
};
}
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 });
return data;
}).catch(error => {
dispatch({ type: CHAT_FETCH_FAIL, accountId, error });
});
};
}
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 });
});
};
}
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));
// }
};
};