import axios from 'axios'; import Cookies from 'universal-cookie'; const apiBaseUrl = 'https://juick.com'; /** * @typedef {Object} Token * @property {string} type * @property {string} token */ /** * @typedef {Object} User * @property {string=} uname * @property {number} uid * @property {number=} unreadCount * @property {string=} avatar * @property {User[]=} read * @property {User[]=} readers * @property {number=} statsMyBL * @property {string=} uri */ /** * @typedef {Object} SecureUserProperties * @property {string=} hash * @property {Token[]=} tokens * @property {string=} telegramName * @property {string=} twitterName * @property {string[]=} jids * @property {string[]=} emails * @property {string=} activeEmail * @property {{connected: boolean, crosspostEnabled: boolean}=} facebookStatus */ /** * @typedef {User & SecureUserProperties} SecureUser */ /** * @typedef {Object} ChatProperties * @property {number=} unreadCount * @property {string=} lastMessageText */ /** * @typedef {User & ChatProperties} Chat */ /** * @typedef {Object} Message * @property {string} body * @property {number=} mid * @property {number=} rid * @property {boolean=} service * @property {User} user * @property {User=} to * @property {string=} replyQuote * @property {string[]=} tags * @property {number=} likes * @property {number=} replies * @property {string=} photo * @property {string=} attach * @property {string=} timestamp * @property {boolean=} ReadOnly * @property {number=} replyto */ const client = axios.create({ baseURL: apiBaseUrl }); client.interceptors.request.use(config => { let cookies = new Cookies(); config.params = Object.assign(config.params || {}, { hash: cookies.get('hash') }); return config; }); /** * fetch my info * @param {string} username * @param {string} password * @return {Promise} me object */ export function me(username = '', password = '') { let cookies = new Cookies(); 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.set('hash', visitor.hash, { path: '/' }); resolve(visitor); }).catch(reason => { cookies.remove('hash', { path: '/' }); reject(reason); }); }); } /** * @param {string} username */ export function info(username) { return client.get(`/api/info/${username}`); } export function getChats() { return client.get('/api/groups_pms'); } /** * @param {string} userName */ export function getChat(userName) { return client.get('/api/pm', { params: { 'uname': userName } }); } /** * @param {string} userName * @param {string} body */ export function pm(userName, body) { let form = new FormData(); form.set('uname', userName); form.set('body', body); return client.post('/api/pm', form); } /** * @param {string} path * @param {{ mid: any; }} params */ export function getMessages(path, params) { return client.get(path, { params: params }); } /** * @param {string} body * @param {string} attach */ export function post(body, attach) { let form = new FormData(); form.append('attach', attach); form.append('body', body); return client.post('/api/post', form); } /** * @param {number} mid * @param {number} rid * @param {string} body * @param {string} attach */ export function comment(mid, rid, body, attach) { let form = new FormData(); form.append('mid', mid.toString()); form.append('rid', rid.toString()); form.append('body', body); form.append('attach', attach); return client.post('/api/comment', form); } export function update(mid, rid, body) { let form = new FormData(); form.append('mid', mid); form.append('rid', rid); form.append('body', body); return client.post('/api/update', form); } export function updateAvatar(newAvatar) { let form = new FormData(); form.append('avatar', newAvatar); return client.post('/api/me/upload', form); } /** * @param {string} network */ 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 appleLink() { return socialLink('apple'); } /** * @param {Message} msg * @param {SecureUser} visitor */ export function markReadTracker(msg, visitor) { return `${apiBaseUrl}/api/thread/mark_read/${msg.mid}-${msg.rid || 0}.gif?hash=${visitor.hash}`; } let profileCache = {}; /** * Fetch user profile * * @param {string} profileUrl User profile URL */ export function fetchUserUri(profileUrl) { return new Promise((resolve, reject) => { if (profileCache[profileUrl]) { resolve(profileCache[profileUrl]); } else { client.get(profileUrl, { headers: { 'Accept': 'application/ld+json' } }).then(response => { profileCache[profileUrl] = response.data; resolve(response.data); }).catch(reject); } }); }