diff options
| author | shpuld <shp@cock.li> | 2019-01-26 17:50:41 +0200 |
|---|---|---|
| committer | shpuld <shp@cock.li> | 2019-01-26 17:50:41 +0200 |
| commit | 0ab828bb30e70e190c1b26eda658ea1d14cc129f (patch) | |
| tree | 5b6816dbc839a0cf821a6b4945fb0a81c109b0f2 /src/services/api/api.service.js | |
| parent | 3978aaef84cc023908155343af76983f2715cf90 (diff) | |
| parent | 8197c77f75929aad557c98a5f698159b1a6d0c90 (diff) | |
Merge develop and fix conflict
Diffstat (limited to 'src/services/api/api.service.js')
| -rw-r--r-- | src/services/api/api.service.js | 57 |
1 files changed, 52 insertions, 5 deletions
diff --git a/src/services/api/api.service.js b/src/services/api/api.service.js index 4ee95bd1..776d8dae 100644 --- a/src/services/api/api.service.js +++ b/src/services/api/api.service.js @@ -41,7 +41,10 @@ const APPROVE_USER_URL = '/api/pleroma/friendships/approve' const DENY_USER_URL = '/api/pleroma/friendships/deny' const SUGGESTIONS_URL = '/api/v1/suggestions' +const MASTODON_USER_FAVORITES_TIMELINE_URL = '/api/v1/favourites' + import { each, map } from 'lodash' +import { parseStatus, parseUser, parseNotification } from '../entity_normalizer/entity_normalizer.service.js' import 'whatwg-fetch' const oldfetch = window.fetch @@ -70,6 +73,7 @@ const updateAvatar = ({credentials, params}) => { form.append(key, value) } }) + return fetch(url, { headers: authHeaders(credentials), method: 'POST', @@ -87,6 +91,7 @@ const updateBg = ({credentials, params}) => { form.append(key, value) } }) + return fetch(url, { headers: authHeaders(credentials), method: 'POST', @@ -110,6 +115,7 @@ const updateBanner = ({credentials, params}) => { form.append(key, value) } }) + return fetch(url, { headers: authHeaders(credentials), method: 'POST', @@ -123,13 +129,14 @@ const updateBanner = ({credentials, params}) => { // location // description const updateProfile = ({credentials, params}) => { + // Always include these fields, because they might be empty or false + const fields = ['description', 'locked', 'no_rich_text', 'hide_network'] let url = PROFILE_UPDATE_URL const form = new FormData() each(params, (value, key) => { - /* Always include description, no_rich_text and locked, because it might be empty or false */ - if (key === 'description' || key === 'locked' || key === 'no_rich_text' || value) { + if (fields.includes(key) || value) { form.append(key, value) } }) @@ -237,24 +244,28 @@ const fetchUser = ({id, credentials}) => { let url = `${USER_URL}?user_id=${id}` return fetch(url, { headers: authHeaders(credentials) }) .then((data) => data.json()) + .then((data) => parseUser(data)) } const fetchFriends = ({id, credentials}) => { let url = `${FRIENDS_URL}?user_id=${id}` return fetch(url, { headers: authHeaders(credentials) }) .then((data) => data.json()) + .then((data) => data.map(parseUser)) } const fetchFollowers = ({id, credentials}) => { let url = `${FOLLOWERS_URL}?user_id=${id}` return fetch(url, { headers: authHeaders(credentials) }) .then((data) => data.json()) + .then((data) => data.map(parseUser)) } const fetchAllFollowing = ({username, credentials}) => { const url = `${ALL_FOLLOWING_URL}/${username}.json` return fetch(url, { headers: authHeaders(credentials) }) .then((data) => data.json()) + .then((data) => data.map(parseUser)) } const fetchFollowRequests = ({credentials}) => { @@ -266,13 +277,27 @@ const fetchFollowRequests = ({credentials}) => { const fetchConversation = ({id, credentials}) => { let url = `${CONVERSATION_URL}/${id}.json?count=100` return fetch(url, { headers: authHeaders(credentials) }) + .then((data) => { + if (data.ok) { + return data + } + throw new Error('Error fetching timeline', data) + }) .then((data) => data.json()) + .then((data) => data.map(parseStatus)) } const fetchStatus = ({id, credentials}) => { let url = `${STATUS_URL}/${id}.json` return fetch(url, { headers: authHeaders(credentials) }) + .then((data) => { + if (data.ok) { + return data + } + throw new Error('Error fetching timeline', data) + }) .then((data) => data.json()) + .then((data) => parseStatus(data)) } const setUserMute = ({id, credentials, muted = true}) => { @@ -300,13 +325,14 @@ const fetchTimeline = ({timeline, credentials, since = false, until = false, use notifications: QVITTER_USER_NOTIFICATIONS_URL, 'publicAndExternal': PUBLIC_AND_EXTERNAL_TIMELINE_URL, user: QVITTER_USER_TIMELINE_URL, + favorites: MASTODON_USER_FAVORITES_TIMELINE_URL, tag: TAG_TIMELINE_URL } + const isNotifications = timeline === 'notifications' + const params = [] let url = timelineUrls[timeline] - let params = [] - if (since) { params.push(['since_id', since]) } @@ -330,9 +356,10 @@ const fetchTimeline = ({timeline, credentials, since = false, until = false, use if (data.ok) { return data } - throw new Error('Error fetching timeline') + throw new Error('Error fetching timeline', data) }) .then((data) => data.json()) + .then((data) => data.map(isNotifications ? parseNotification : parseStatus)) } const verifyCredentials = (user) => { @@ -340,6 +367,16 @@ const verifyCredentials = (user) => { method: 'POST', headers: authHeaders(user) }) + .then((response) => { + if (response.ok) { + return response.json() + } else { + return { + error: response + } + } + }) + .then((data) => data.error ? data : parseUser(data)) } const favorite = ({ id, credentials }) => { @@ -391,6 +428,16 @@ const postStatus = ({credentials, status, spoilerText, visibility, sensitive, me method: 'POST', headers: authHeaders(credentials) }) + .then((response) => { + if (response.ok) { + return response.json() + } else { + return { + error: response + } + } + }) + .then((data) => data.error ? data : parseStatus(data)) } const deleteStatus = ({ id, credentials }) => { |
