~cytrogen/masto-fe

ref: 6d0b964e2bab79ee0cb448c7772883f57b2818ec masto-fe/app/javascript/mastodon/actions/server.js -rw-r--r-- 2.2 KiB
6d0b964e — Sqx. Flann [feature] Query media description limit (#62) 10 months 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
import api from '../api';

import { importFetchedAccount } from './importer';

export const SERVER_FETCH_REQUEST = 'Server_FETCH_REQUEST';
export const SERVER_FETCH_SUCCESS = 'Server_FETCH_SUCCESS';
export const SERVER_FETCH_FAIL    = 'Server_FETCH_FAIL';

export const SERVER_DOMAIN_BLOCKS_FETCH_REQUEST = 'SERVER_DOMAIN_BLOCKS_FETCH_REQUEST';
export const SERVER_DOMAIN_BLOCKS_FETCH_SUCCESS = 'SERVER_DOMAIN_BLOCKS_FETCH_SUCCESS';
export const SERVER_DOMAIN_BLOCKS_FETCH_FAIL    = 'SERVER_DOMAIN_BLOCKS_FETCH_FAIL';

export const fetchServer = () => (dispatch, getState) => {
  if (getState().getIn(['server', 'server', 'isLoading'])) {
    return;
  }

  dispatch(fetchServerRequest());

  try {
    api(getState)
      .get('/api/v2/instance').then({ data });
      if (data.contact.account) dispatch(importFetchedAccount(data.contact.account));
      dispatch(fetchServerSuccess(data));
  } catch (e) {
    api(getState)
      .get('/api/v1/instance').then(({ data }) => {
        if (data.contact.account) dispatch(importFetchedAccount(data.contact.account));
        dispatch(fetchServerSuccess(data));
    }).catch(err => dispatch(fetchServerFail(err)));
  }
};

const fetchServerRequest = () => ({
  type: SERVER_FETCH_REQUEST,
});

const fetchServerSuccess = server => ({
  type: SERVER_FETCH_SUCCESS,
  server,
});

const fetchServerFail = error => ({
  type: SERVER_FETCH_FAIL,
  error,
});

export const fetchDomainBlocks = () => (dispatch, getState) => {
  if (getState().getIn(['server', 'domainBlocks', 'isLoading'])) {
    return;
  }

  dispatch(fetchDomainBlocksRequest());

  api(getState)
    .get('/api/v1/instance/domain_blocks')
    .then(({ data }) => dispatch(fetchDomainBlocksSuccess(true, data)))
    .catch(err => {
      if (err.response.status === 404) {
        dispatch(fetchDomainBlocksSuccess(false, []));
      } else {
        dispatch(fetchDomainBlocksFail(err));
      }
    });
};

const fetchDomainBlocksRequest = () => ({
  type: SERVER_DOMAIN_BLOCKS_FETCH_REQUEST,
});

const fetchDomainBlocksSuccess = (isAvailable, blocks) => ({
  type: SERVER_DOMAIN_BLOCKS_FETCH_SUCCESS,
  isAvailable,
  blocks,
});

const fetchDomainBlocksFail = error => ({
  type: SERVER_DOMAIN_BLOCKS_FETCH_FAIL,
  error,
});