pleroma/app/soapbox/reducers/contexts.js

190 lines
5.6 KiB
JavaScript
Raw Normal View History

2022-01-10 14:01:24 -08:00
import { Map as ImmutableMap, OrderedSet as ImmutableOrderedSet } from 'immutable';
2022-01-10 14:01:24 -08:00
import { STATUS_IMPORT, STATUSES_IMPORT } from 'soapbox/actions/importer';
2020-03-27 13:59:38 -07:00
import {
ACCOUNT_BLOCK_SUCCESS,
ACCOUNT_MUTE_SUCCESS,
} from '../actions/accounts';
2021-10-09 15:47:25 -07:00
import {
STATUS_CREATE_REQUEST,
STATUS_CREATE_SUCCESS,
} from '../actions/statuses';
2020-03-27 13:59:38 -07:00
import { CONTEXT_FETCH_SUCCESS } from '../actions/statuses';
import { TIMELINE_DELETE } from '../actions/timelines';
2020-03-27 13:59:38 -07:00
const initialState = ImmutableMap({
inReplyTos: ImmutableMap(),
replies: ImmutableMap(),
});
const importStatus = (state, status, idempotencyKey) => {
const { id, in_reply_to_id } = status;
2021-04-21 14:40:32 -07:00
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();
});
if (idempotencyKey) {
deletePendingStatus(state, status, idempotencyKey);
}
2021-04-21 14:40:32 -07:00
});
};
const importStatuses = (state, statuses) => {
return state.withMutations(state => {
statuses.forEach(status => importStatus(state, status));
});
};
const isReplyTo = (state, childId, parentId, initialId = null) => {
if (!childId) return false;
// Prevent cycles
if (childId === initialId) return false;
initialId = initialId || childId;
if (childId === parentId) {
return true;
} else {
const nextId = state.getIn(['inReplyTos', childId]);
return isReplyTo(state, nextId, parentId, initialId);
}
};
2021-04-21 15:35:01 -07:00
const insertTombstone = (state, ancestorId, descendantId) => {
// Prevent infinite loop if the API returns a bogus response
if (isReplyTo(state, ancestorId, descendantId)) return state;
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 });
});
};
/** Find the highest level status from this statusId. */
const getRootNode = (state, statusId, initialId = statusId) => {
const parent = state.getIn(['inReplyTos', statusId]);
if (!parent) {
return statusId;
} else if (parent === initialId) {
// Prevent cycles
return parent;
} else {
return getRootNode(state, parent, initialId);
}
};
/** Route fromId to toId by inserting tombstones. */
const connectNodes = (state, fromId, toId) => {
const root = getRootNode(state, fromId);
if (root !== toId) {
return insertTombstone(state, toId, fromId);
} else {
return state;
}
};
const importBranch = (state, statuses, statusId) => {
return state.withMutations(state => {
statuses.forEach((status, i) => {
const prevId = statusId && i === 0 ? statusId : (statuses[i - 1] || {}).id;
2021-04-21 15:35:01 -07:00
if (status.in_reply_to_id) {
importStatus(state, status);
connectNodes(state, status.id, statusId);
} else if (prevId) {
insertTombstone(state, prevId, status.id);
}
});
2021-04-21 15:35:01 -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
};
2021-10-09 15:47:25 -07:00
const importPendingStatus = (state, params, idempotencyKey) => {
const id = `末pending-${idempotencyKey}`;
2021-10-09 15:47:25 -07:00
const { in_reply_to_id } = params;
return importStatus(state, { id, in_reply_to_id });
};
const deletePendingStatus = (state, { in_reply_to_id }, idempotencyKey) => {
const id = `末pending-${idempotencyKey}`;
2021-10-09 15:47:25 -07:00
return state.withMutations(state => {
state.deleteIn(['inReplyTos', id]);
if (in_reply_to_id) {
state.updateIn(['replies', in_reply_to_id], ImmutableOrderedSet(), ids => {
return ids.delete(id).sort();
});
}
});
};
2020-03-27 13:59:38 -07:00
export default function replies(state = initialState, action) {
switch (action.type) {
2022-05-11 14:06:35 -07:00
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:
return deleteStatuses(state, [action.id]);
case STATUS_CREATE_REQUEST:
return importPendingStatus(state, action.params, action.idempotencyKey);
case STATUS_CREATE_SUCCESS:
return deletePendingStatus(state, action.status, action.idempotencyKey);
case STATUS_IMPORT:
return importStatus(state, action.status, action.idempotencyKey);
case STATUSES_IMPORT:
return importStatuses(state, action.statuses);
default:
return state;
2020-03-27 13:59:38 -07:00
}
2021-08-03 12:22:51 -07:00
}