~cytrogen/masto-fe

ref: 20a89f1c8eefa0f766a4650be4d2e5f1fa92ee71 masto-fe/app/javascript/flavours/glitch/reducers/statuses.js -rw-r--r-- 5.0 KiB
20a89f1c — Cytrogen [feature] Bookmark folders UI 8 days ago
                                                                                
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { Map as ImmutableMap, fromJS } from "immutable";

import {
  REBLOG_REQUEST,
  REBLOG_FAIL,
  UNREBLOG_REQUEST,
  UNREBLOG_FAIL,
  FAVOURITE_REQUEST,
  FAVOURITE_FAIL,
  UNFAVOURITE_REQUEST,
  UNFAVOURITE_FAIL,
  BOOKMARK_REQUEST,
  BOOKMARK_FAIL,
  UNBOOKMARK_REQUEST,
  UNBOOKMARK_FAIL,
} from "flavours/glitch/actions/interactions";
import {
  STATUS_MUTE_SUCCESS,
  STATUS_UNMUTE_SUCCESS,
  STATUS_REVEAL,
  STATUS_HIDE,
  STATUS_COLLAPSE,
  STATUS_TRANSLATE_SUCCESS,
  STATUS_TRANSLATE_UNDO,
  STATUS_FETCH_REQUEST,
  STATUS_FETCH_FAIL,
} from "flavours/glitch/actions/statuses";
import {
  TIMELINE_DELETE,
} from "flavours/glitch/actions/timelines";

import { STATUS_IMPORT, STATUSES_IMPORT } from "../actions/importer";
import { normalizeStatusTranslation } from "../actions/importer/normalizer";

const importStatus = (state, status) => state.set(status.id, fromJS(status));

const importStatuses = (state, statuses) =>
  state.withMutations(mutable => statuses.forEach(status => importStatus(mutable, status)));

const deleteStatus = (state, id, references) => {
  references.forEach(ref => {
    state = deleteStatus(state, ref, []);
  });

  return state.delete(id);
};

const statusTranslateSuccess = (state, id, translation) => {
  return state.withMutations(map => {
    map.setIn([id, "translation"], fromJS(normalizeStatusTranslation(translation, map.get(id))));

    const list = map.getIn([id, "media_attachments"]);
    if (translation.media_attachments && list) {
      translation.media_attachments.forEach(item => {
        const index = list.findIndex(i => i.get("id") === item.id);
        map.setIn([id, "media_attachments", index, "translation"], fromJS({ description: item.description }));
      });
    }
  });
};

const statusTranslateUndo = (state, id) => {
  return state.withMutations(map => {
    map.deleteIn([id, "translation"]);
    map.getIn([id, "media_attachments"]).forEach((item, index) => map.deleteIn([id, "media_attachments", index, "translation"]));
  });
};

const initialState = ImmutableMap();

export default function statuses(state = initialState, action) {
  switch(action.type) {
    case STATUS_FETCH_REQUEST:
      return state.setIn([action.id, "isLoading"], true);
    case STATUS_FETCH_FAIL:
      return state.delete(action.id);
    case STATUS_IMPORT:
      return importStatus(state, action.status);
    case STATUSES_IMPORT:
      return importStatuses(state, action.statuses);
    case FAVOURITE_REQUEST:
      return state.setIn([action.status.get("id"), "favourited"], true);
    case FAVOURITE_FAIL:
      return state.get(action.status.get("id")) === undefined ? state : state.setIn([action.status.get("id"), "favourited"], false);
    case UNFAVOURITE_REQUEST:
      return state.setIn([action.status.get("id"), "favourited"], false);
    case UNFAVOURITE_FAIL:
      return state.get(action.status.get("id")) === undefined ? state : state.setIn([action.status.get("id"), "favourited"], true);
    case BOOKMARK_REQUEST:
      return state.get(action.status.get("id")) === undefined ? state : state.setIn([action.status.get("id"), "bookmarked"], true);
    case BOOKMARK_FAIL:
      return state.get(action.status.get("id")) === undefined ? state : state.setIn([action.status.get("id"), "bookmarked"], false);
    case UNBOOKMARK_REQUEST:
      return state.get(action.status.get("id")) === undefined ? state : state.setIn([action.status.get("id"), "bookmarked"], false);
    case UNBOOKMARK_FAIL:
      return state.get(action.status.get("id")) === undefined ? state : state.setIn([action.status.get("id"), "bookmarked"], true);
    case REBLOG_REQUEST:
      return state.setIn([action.status.get("id"), "reblogged"], true);
    case REBLOG_FAIL:
      return state.get(action.status.get("id")) === undefined ? state : state.setIn([action.status.get("id"), "reblogged"], false);
    case UNREBLOG_REQUEST:
      return state.setIn([action.status.get("id"), "reblogged"], false);
    case UNREBLOG_FAIL:
      return state.get(action.status.get("id")) === undefined ? state : state.setIn([action.status.get("id"), "reblogged"], true);
    case STATUS_MUTE_SUCCESS:
      return state.setIn([action.id, "muted"], true);
    case STATUS_UNMUTE_SUCCESS:
      return state.setIn([action.id, "muted"], false);
    case STATUS_REVEAL:
      return state.withMutations(map => {
        action.ids.forEach(id => {
          if (!(state.get(id) === undefined)) {
            map.setIn([id, "hidden"], false);
          }
        });
      });
    case STATUS_HIDE:
      return state.withMutations(map => {
        action.ids.forEach(id => {
          if (!(state.get(id) === undefined)) {
            map.setIn([id, "hidden"], true);
          }
        });
      });
    case STATUS_COLLAPSE:
      return state.setIn([action.id, "collapsed"], action.isCollapsed);
    case TIMELINE_DELETE:
      return deleteStatus(state, action.id, action.references);
    case STATUS_TRANSLATE_SUCCESS:
      return statusTranslateSuccess(state, action.id, action.translation);
    case STATUS_TRANSLATE_UNDO:
      return statusTranslateUndo(state, action.id);
    default:
      return state;
  }
}