about summary refs log tree commit diff
path: root/app/javascript/flavours/glitch/reducers/contexts.js
blob: effd70756d170cc92ba318226679afdf8007c886 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import {
  ACCOUNT_BLOCK_SUCCESS,
  ACCOUNT_MUTE_SUCCESS,
} from 'flavours/glitch/actions/accounts';
import { CONTEXT_FETCH_SUCCESS } from 'flavours/glitch/actions/statuses';
import { TIMELINE_DELETE, TIMELINE_CONTEXT_UPDATE } from 'flavours/glitch/actions/timelines';
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';

const initialState = ImmutableMap({
  ancestors: ImmutableMap(),
  descendants: ImmutableMap(),
});

const normalizeContext = (state, id, ancestors, descendants) => {
  const ancestorsIds   = ImmutableList(ancestors.map(ancestor => ancestor.id));
  const descendantsIds = ImmutableList(descendants.map(descendant => descendant.id));

  return state.withMutations(map => {
    map.setIn(['ancestors', id], ancestorsIds);
    map.setIn(['descendants', id], descendantsIds);
  });
};

const deleteFromContexts = (immutableState, ids) => immutableState.withMutations(state => {
  state.update('ancestors', immutableAncestors => immutableAncestors.withMutations(ancestors => {
    state.update('descendants', immutableDescendants => immutableDescendants.withMutations(descendants => {
      ids.forEach(id => {
        descendants.get(id, ImmutableList()).forEach(descendantId => {
          ancestors.update(descendantId, ImmutableList(), list => list.filterNot(itemId => itemId === id));
        });

        ancestors.get(id, ImmutableList()).forEach(ancestorId => {
          descendants.update(ancestorId, ImmutableList(), list => list.filterNot(itemId => itemId === id));
        });

        descendants.delete(id);
        ancestors.delete(id);
      });
    }));
  }));
});

const filterContexts = (state, relationship, statuses) => {
  const ownedStatusIds = statuses.filter(status => status.get('account') === relationship.id)
                                 .map(status => status.get('id'));

  return deleteFromContexts(state, ownedStatusIds);
};

const updateContext = (state, status, references) => {
  return state.update('descendants', map => {
    references.forEach(parentId => {
      map = map.update(parentId, ImmutableList(), list => {
        if (list.includes(status.id)) {
          return list;
        }

        return list.push(status.id);
      });
    });

    return map;
  });
};

export default function contexts(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:
    return deleteFromContexts(state, [action.id]);
  case TIMELINE_CONTEXT_UPDATE:
    return updateContext(state, action.status, action.references);
  default:
    return state;
  }
};