2020-03-27 13:59:38 -07:00
|
|
|
import {
|
|
|
|
ACCOUNT_BLOCK_SUCCESS,
|
|
|
|
ACCOUNT_MUTE_SUCCESS,
|
|
|
|
} from '../actions/accounts';
|
|
|
|
import { CONTEXT_FETCH_SUCCESS } from '../actions/statuses';
|
2020-09-27 14:17:21 -07:00
|
|
|
import { TIMELINE_DELETE } from '../actions/timelines';
|
|
|
|
import { STATUS_IMPORT, STATUSES_IMPORT } from 'soapbox/actions/importer';
|
2020-09-18 14:16:56 -07:00
|
|
|
import { Map as ImmutableMap, OrderedSet as ImmutableOrderedSet } from 'immutable';
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
const initialState = ImmutableMap({
|
|
|
|
inReplyTos: ImmutableMap(),
|
|
|
|
replies: ImmutableMap(),
|
|
|
|
});
|
|
|
|
|
2021-04-21 14:40:32 -07:00
|
|
|
const importStatus = (state, { id, in_reply_to_id }) => {
|
|
|
|
if (!in_reply_to_id) return state;
|
|
|
|
|
2021-04-21 15:35:01 -07:00
|
|
|
return state.withMutations(state => {
|
|
|
|
state.setIn(['inReplyTos', id], in_reply_to_id);
|
2021-04-21 14:40:32 -07:00
|
|
|
|
|
|
|
state.updateIn(['replies', in_reply_to_id], ImmutableOrderedSet(), ids => {
|
|
|
|
return ids.add(id).sort();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const importStatuses = (state, statuses) => {
|
|
|
|
return state.withMutations(state => {
|
|
|
|
statuses.forEach(status => importStatus(state, status));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-04-21 15:35:01 -07:00
|
|
|
const insertTombstone = (state, ancestorId, descendantId) => {
|
2021-04-21 16:28:43 -07:00
|
|
|
const tombstoneId = `${descendantId}-tombstone`;
|
2021-04-21 15:35:01 -07:00
|
|
|
return state.withMutations(state => {
|
|
|
|
importStatus(state, { id: tombstoneId, in_reply_to_id: ancestorId });
|
|
|
|
importStatus(state, { id: descendantId, in_reply_to_id: tombstoneId });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-04-21 16:14:20 -07:00
|
|
|
const importBranch = (state, statuses, rootId) => {
|
|
|
|
return state.withMutations(state => {
|
|
|
|
statuses.forEach((status, i) => {
|
|
|
|
const lastId = rootId && i === 0 ? rootId : (statuses[i - 1] || {}).id;
|
2021-04-21 15:35:01 -07:00
|
|
|
|
2021-04-21 16:14:20 -07:00
|
|
|
if (status.in_reply_to_id) {
|
|
|
|
importStatus(state, status);
|
|
|
|
} else if (lastId) {
|
|
|
|
insertTombstone(state, lastId, status.id);
|
|
|
|
}
|
|
|
|
});
|
2021-04-21 15:35:01 -07:00
|
|
|
});
|
2021-04-21 16:14:20 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const normalizeContext = (state, id, ancestors, descendants) => state.withMutations(state => {
|
|
|
|
importBranch(state, ancestors);
|
|
|
|
importBranch(state, descendants, id);
|
2021-04-21 15:35:01 -07:00
|
|
|
|
|
|
|
if (ancestors.length > 0 && !state.getIn(['inReplyTos', id])) {
|
|
|
|
insertTombstone(state, ancestors[ancestors.length - 1].id, id);
|
|
|
|
}
|
2020-03-27 13:59:38 -07:00
|
|
|
});
|
|
|
|
|
2021-04-21 15:35:01 -07:00
|
|
|
const deleteStatus = (state, id) => {
|
|
|
|
return state.withMutations(state => {
|
|
|
|
const parentId = state.getIn(['inReplyTos', id]);
|
|
|
|
const replies = state.getIn(['replies', id], ImmutableOrderedSet());
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2021-04-21 15:35:01 -07:00
|
|
|
// Delete from its parent's tree
|
|
|
|
state.updateIn(['replies', parentId], ImmutableOrderedSet(), ids => ids.delete(id));
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2021-04-21 15:35:01 -07:00
|
|
|
// Dereference children
|
|
|
|
replies.forEach(reply => state.deleteIn(['inReplyTos', reply]));
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2021-04-21 15:35:01 -07:00
|
|
|
state.deleteIn(['inReplyTos', id]);
|
|
|
|
state.deleteIn(['replies', id]);
|
|
|
|
});
|
|
|
|
};
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2021-04-21 15:35:01 -07:00
|
|
|
const deleteStatuses = (state, ids) => {
|
|
|
|
return state.withMutations(state => {
|
|
|
|
ids.forEach(id => deleteStatus(state, id));
|
|
|
|
});
|
|
|
|
};
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
const filterContexts = (state, relationship, statuses) => {
|
|
|
|
const ownedStatusIds = statuses
|
|
|
|
.filter(status => status.get('account') === relationship.id)
|
|
|
|
.map(status => status.get('id'));
|
|
|
|
|
2021-04-21 15:35:01 -07:00
|
|
|
return deleteStatuses(state, ownedStatusIds);
|
2020-03-27 13:59:38 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
export default function replies(state = initialState, action) {
|
|
|
|
switch(action.type) {
|
|
|
|
case ACCOUNT_BLOCK_SUCCESS:
|
|
|
|
case ACCOUNT_MUTE_SUCCESS:
|
|
|
|
return filterContexts(state, action.relationship, action.statuses);
|
|
|
|
case CONTEXT_FETCH_SUCCESS:
|
|
|
|
return normalizeContext(state, action.id, action.ancestors, action.descendants);
|
|
|
|
case TIMELINE_DELETE:
|
2021-04-21 15:35:01 -07:00
|
|
|
return deleteStatuses(state, [action.id]);
|
2020-09-27 14:17:21 -07:00
|
|
|
case STATUS_IMPORT:
|
2021-04-21 14:40:32 -07:00
|
|
|
return importStatus(state, action.status);
|
2020-09-27 14:17:21 -07:00
|
|
|
case STATUSES_IMPORT:
|
2021-04-21 14:40:32 -07:00
|
|
|
return importStatuses(state, action.statuses);
|
2020-03-27 13:59:38 -07:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|