2018-10-07 21:44:58 +00:00
|
|
|
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
|
2023-05-23 15:15:17 +00:00
|
|
|
|
2023-11-03 15:00:03 +00:00
|
|
|
import { blockAccountSuccess, muteAccountSuccess } from 'mastodon/actions/accounts';
|
|
|
|
import { blockDomainSuccess } from 'mastodon/actions/domain_blocks';
|
2023-05-23 15:15:17 +00:00
|
|
|
|
2018-10-07 21:44:58 +00:00
|
|
|
import {
|
2018-10-10 23:31:03 +00:00
|
|
|
CONVERSATIONS_MOUNT,
|
|
|
|
CONVERSATIONS_UNMOUNT,
|
2018-10-07 21:44:58 +00:00
|
|
|
CONVERSATIONS_FETCH_REQUEST,
|
|
|
|
CONVERSATIONS_FETCH_SUCCESS,
|
|
|
|
CONVERSATIONS_FETCH_FAIL,
|
|
|
|
CONVERSATIONS_UPDATE,
|
2018-10-18 23:47:29 +00:00
|
|
|
CONVERSATIONS_READ,
|
2019-10-01 02:54:17 +00:00
|
|
|
CONVERSATIONS_DELETE_SUCCESS,
|
2018-10-07 21:44:58 +00:00
|
|
|
} from '../actions/conversations';
|
2023-05-09 01:11:56 +00:00
|
|
|
import { compareId } from '../compare_id';
|
2018-10-07 21:44:58 +00:00
|
|
|
|
|
|
|
const initialState = ImmutableMap({
|
|
|
|
items: ImmutableList(),
|
|
|
|
isLoading: false,
|
|
|
|
hasMore: true,
|
2018-10-10 23:31:03 +00:00
|
|
|
mounted: false,
|
2018-10-07 21:44:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const conversationToMap = item => ImmutableMap({
|
|
|
|
id: item.id,
|
2018-10-18 23:47:29 +00:00
|
|
|
unread: item.unread,
|
2018-10-07 21:44:58 +00:00
|
|
|
accounts: ImmutableList(item.accounts.map(a => a.id)),
|
2018-10-30 23:55:20 +00:00
|
|
|
last_status: item.last_status ? item.last_status.id : null,
|
2018-10-07 21:44:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const updateConversation = (state, item) => state.update('items', list => {
|
|
|
|
const index = list.findIndex(x => x.get('id') === item.id);
|
|
|
|
const newItem = conversationToMap(item);
|
|
|
|
|
|
|
|
if (index === -1) {
|
|
|
|
return list.unshift(newItem);
|
|
|
|
} else {
|
|
|
|
return list.set(index, newItem);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-02-27 18:13:16 +00:00
|
|
|
const expandNormalizedConversations = (state, conversations, next, isLoadingRecent) => {
|
2018-10-07 21:44:58 +00:00
|
|
|
let items = ImmutableList(conversations.map(conversationToMap));
|
|
|
|
|
|
|
|
return state.withMutations(mutable => {
|
|
|
|
if (!items.isEmpty()) {
|
|
|
|
mutable.update('items', list => {
|
|
|
|
list = list.map(oldItem => {
|
|
|
|
const newItemIndex = items.findIndex(x => x.get('id') === oldItem.get('id'));
|
|
|
|
|
|
|
|
if (newItemIndex === -1) {
|
|
|
|
return oldItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newItem = items.get(newItemIndex);
|
|
|
|
items = items.delete(newItemIndex);
|
|
|
|
|
|
|
|
return newItem;
|
|
|
|
});
|
|
|
|
|
|
|
|
list = list.concat(items);
|
|
|
|
|
2018-11-05 17:51:43 +00:00
|
|
|
return list.sortBy(x => x.get('last_status'), (a, b) => {
|
|
|
|
if(a === null || b === null) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return compareId(a, b) * -1;
|
|
|
|
});
|
2018-10-07 21:44:58 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-27 18:13:16 +00:00
|
|
|
if (!next && !isLoadingRecent) {
|
2018-10-07 21:44:58 +00:00
|
|
|
mutable.set('hasMore', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
mutable.set('isLoading', false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-07-25 02:17:35 +00:00
|
|
|
const filterConversations = (state, accountIds) => {
|
|
|
|
return state.update('items', list => list.filterNot(item => item.get('accounts').some(accountId => accountIds.includes(accountId))));
|
|
|
|
};
|
|
|
|
|
2018-10-07 21:44:58 +00:00
|
|
|
export default function conversations(state = initialState, action) {
|
|
|
|
switch (action.type) {
|
|
|
|
case CONVERSATIONS_FETCH_REQUEST:
|
|
|
|
return state.set('isLoading', true);
|
|
|
|
case CONVERSATIONS_FETCH_FAIL:
|
|
|
|
return state.set('isLoading', false);
|
|
|
|
case CONVERSATIONS_FETCH_SUCCESS:
|
2019-02-27 18:13:16 +00:00
|
|
|
return expandNormalizedConversations(state, action.conversations, action.next, action.isLoadingRecent);
|
2018-10-07 21:44:58 +00:00
|
|
|
case CONVERSATIONS_UPDATE:
|
|
|
|
return updateConversation(state, action.conversation);
|
2018-10-10 23:31:03 +00:00
|
|
|
case CONVERSATIONS_MOUNT:
|
|
|
|
return state.update('mounted', count => count + 1);
|
|
|
|
case CONVERSATIONS_UNMOUNT:
|
|
|
|
return state.update('mounted', count => count - 1);
|
2018-10-18 23:47:29 +00:00
|
|
|
case CONVERSATIONS_READ:
|
|
|
|
return state.update('items', list => list.map(item => {
|
|
|
|
if (item.get('id') === action.id) {
|
|
|
|
return item.set('unread', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}));
|
2023-11-03 15:00:03 +00:00
|
|
|
case blockAccountSuccess.type:
|
|
|
|
case muteAccountSuccess.type:
|
|
|
|
return filterConversations(state, [action.payload.relationship.id]);
|
|
|
|
case blockDomainSuccess.type:
|
|
|
|
return filterConversations(state, action.payload.accounts);
|
2019-10-01 02:54:17 +00:00
|
|
|
case CONVERSATIONS_DELETE_SUCCESS:
|
|
|
|
return state.update('items', list => list.filterNot(item => item.get('id') === action.id));
|
2018-10-07 21:44:58 +00:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2022-12-18 15:51:37 +00:00
|
|
|
}
|