aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/api/index.js
blob: 2332ed4817945051c9448b426e04f06073a0b8c6 (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
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
import axios from 'axios';
import cookies from 'react-cookies';

const apiBaseUrl = 'https://juick.com';

const client = axios.create({
    baseURL: apiBaseUrl
});
client.interceptors.request.use(config => {
    config.params = Object.assign(config.params || {}, {
        hash: cookies.load('hash')
    });
    return config;
});

export function me(username = '', password = '') {
    return new Promise((resolve, reject) => {
        client.get('/api/me', {
            headers: username ? {
                'Authorization': 'Basic ' + window.btoa(unescape(encodeURIComponent(username + ':' + password)))
            } : {}
        }).then(response => {
            let visitor = response.data;
            cookies.save('hash', visitor.hash, { path: '/' });
            resolve(visitor);
        }).catch(reason => {
            cookies.remove('hash', { path: '/'});
            reject(reason);
        });
    });
}

export function info(username) {
    return client.get(`/api/info/${username}`);
}

export function getChats() {
    return client.get('/api/groups_pms');
}

export function getChat(userName) {
    return client.get('/api/pm', {
        params: {
            'uname': userName
        }
    });
}

export function pm(userName, body) {
    let form = new FormData();
    form.set('uname', userName);
    form.set('body', body);
    return client.post('/api/pm', form);
}

export function getMessages(path, params) {
    return client.get(path, {
        params: params
    });
}

export function post(body, attach) {
    let form = new FormData();
    form.append('attach', attach);
    form.append('body', body);
    return client.post('/api/post', form);
}

export function comment(mid, rid, body, attach) {
    let form = new FormData();
    form.append('mid', mid);
    form.append('rid', rid);
    form.append('body', body);
    form.append('attach', attach);
    return client.post('/api/comment', form);
}

export function updateAvatar(newAvatar) {
    let form = new FormData();
    form.append('avatar', newAvatar);
    return client.post('/api/me/upload', form);
}

function socialLink(network) {
    return `${apiBaseUrl}/api/_${network}login?state=${window.location.protocol}//${window.location.host}${window.location.pathname}`;
}

export function facebookLink() {
    return socialLink('fb');
}

export function vkLink() {
    return socialLink('vk');
}

export function markReadTracker(msg, visitor) {
    return `${apiBaseUrl}/api/thread/mark_read/${msg.mid}-${msg.rid || 0}.gif?hash=${visitor.hash}`;
}

export function fetchUserUri(dataUri) {
    return new Promise((resolve, reject) => {
        let form = new FormData();
        form.append('uri', dataUri);
        client.post('/u/', form).then(response => resolve(response));
    });
}