diff options
Diffstat (limited to 'src/services/api/api.service.js')
| -rw-r--r-- | src/services/api/api.service.js | 136 |
1 files changed, 118 insertions, 18 deletions
diff --git a/src/services/api/api.service.js b/src/services/api/api.service.js index f14bfd6d..ae876b7f 100644 --- a/src/services/api/api.service.js +++ b/src/services/api/api.service.js @@ -8,12 +8,14 @@ const TAG_TIMELINE_URL = '/api/statusnet/tags/timeline' const FAVORITE_URL = '/api/favorites/create' const UNFAVORITE_URL = '/api/favorites/destroy' const RETWEET_URL = '/api/statuses/retweet' +const UNRETWEET_URL = '/api/statuses/unretweet' const STATUS_UPDATE_URL = '/api/statuses/update.json' const STATUS_DELETE_URL = '/api/statuses/destroy' const STATUS_URL = '/api/statuses/show' const MEDIA_UPLOAD_URL = '/api/statusnet/media/upload' const CONVERSATION_URL = '/api/statusnet/conversation' const MENTIONS_URL = '/api/statuses/mentions.json' +const DM_TIMELINE_URL = '/api/statuses/dm_timeline.json' const FOLLOWERS_URL = '/api/statuses/followers.json' const FRIENDS_URL = '/api/statuses/friends.json' const FOLLOWING_URL = '/api/friendships/create.json' @@ -26,10 +28,18 @@ const BANNER_UPDATE_URL = '/api/account/update_profile_banner.json' const PROFILE_UPDATE_URL = '/api/account/update_profile.json' const EXTERNAL_PROFILE_URL = '/api/externalprofile/show.json' const QVITTER_USER_TIMELINE_URL = '/api/qvitter/statuses/user_timeline.json' +const QVITTER_USER_NOTIFICATIONS_URL = '/api/qvitter/statuses/notifications.json' +const QVITTER_USER_NOTIFICATIONS_READ_URL = '/api/qvitter/statuses/notifications/read.json' const BLOCKING_URL = '/api/blocks/create.json' const UNBLOCKING_URL = '/api/blocks/destroy.json' const USER_URL = '/api/users/show.json' const FOLLOW_IMPORT_URL = '/api/pleroma/follow_import' +const DELETE_ACCOUNT_URL = '/api/pleroma/delete_account' +const CHANGE_PASSWORD_URL = '/api/pleroma/change_password' +const FOLLOW_REQUESTS_URL = '/api/pleroma/friend_requests' +const APPROVE_USER_URL = '/api/pleroma/friendships/approve' +const DENY_USER_URL = '/api/pleroma/friendships/deny' +const SUGGESTIONS_URL = '/api/v1/suggestions' import { each, map } from 'lodash' import 'whatwg-fetch' @@ -44,16 +54,6 @@ let fetch = (url, options) => { return oldfetch(fullUrl, options) } -// from https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding -let utoa = (str) => { - // first we use encodeURIComponent to get percent-encoded UTF-8, - // then we convert the percent encodings into raw bytes which - // can be fed into btoa. - return btoa(encodeURIComponent(str) - .replace(/%([0-9A-F]{2})/g, - (match, p1) => { return String.fromCharCode('0x' + p1) })) -} - // Params // cropH // cropW @@ -128,8 +128,8 @@ const updateProfile = ({credentials, params}) => { const form = new FormData() each(params, (value, key) => { - if (key === 'description' || /* Always include description, because it might be empty */ - value) { + /* 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) { form.append(key, value) } }) @@ -151,6 +151,7 @@ const updateProfile = ({credentials, params}) => { // bio // homepage // location +// token const register = (params) => { const form = new FormData() @@ -166,9 +167,9 @@ const register = (params) => { }) } -const authHeaders = (user) => { - if (user && user.username && user.password) { - return { 'Authorization': `Basic ${utoa(`${user.username}:${user.password}`)}` } +const authHeaders = (accessToken) => { + if (accessToken) { + return { 'Authorization': `Bearer ${accessToken}` } } else { return { } } @@ -214,6 +215,22 @@ const unblockUser = ({id, credentials}) => { }).then((data) => data.json()) } +const approveUser = ({id, credentials}) => { + let url = `${APPROVE_USER_URL}?user_id=${id}` + return fetch(url, { + headers: authHeaders(credentials), + method: 'POST' + }).then((data) => data.json()) +} + +const denyUser = ({id, credentials}) => { + let url = `${DENY_USER_URL}?user_id=${id}` + return fetch(url, { + headers: authHeaders(credentials), + method: 'POST' + }).then((data) => data.json()) +} + const fetchUser = ({id, credentials}) => { let url = `${USER_URL}?user_id=${id}` return fetch(url, { headers: authHeaders(credentials) }) @@ -238,6 +255,12 @@ const fetchAllFollowing = ({username, credentials}) => { .then((data) => data.json()) } +const fetchFollowRequests = ({credentials}) => { + const url = FOLLOW_REQUESTS_URL + return fetch(url, { headers: authHeaders(credentials) }) + .then((data) => data.json()) +} + const fetchConversation = ({id, credentials}) => { let url = `${CONVERSATION_URL}/${id}.json?count=100` return fetch(url, { headers: authHeaders(credentials) }) @@ -271,8 +294,13 @@ const fetchTimeline = ({timeline, credentials, since = false, until = false, use public: PUBLIC_TIMELINE_URL, friends: FRIENDS_TIMELINE_URL, mentions: MENTIONS_URL, + dms: DM_TIMELINE_URL, + notifications: QVITTER_USER_NOTIFICATIONS_URL, 'publicAndExternal': PUBLIC_AND_EXTERNAL_TIMELINE_URL, user: QVITTER_USER_TIMELINE_URL, + // separate timeline for own posts, so it won't break due to user timeline bugs + // really needed only for broken favorites + own: QVITTER_USER_TIMELINE_URL, tag: TAG_TIMELINE_URL } @@ -298,7 +326,14 @@ const fetchTimeline = ({timeline, credentials, since = false, until = false, use const queryString = map(params, (param) => `${param[0]}=${param[1]}`).join('&') url += `?${queryString}` - return fetch(url, { headers: authHeaders(credentials) }).then((data) => data.json()) + return fetch(url, { headers: authHeaders(credentials) }) + .then((data) => { + if (data.ok) { + return data + } + throw new Error('Error fetching timeline') + }) + .then((data) => data.json()) } const verifyCredentials = (user) => { @@ -329,12 +364,23 @@ const retweet = ({ id, credentials }) => { }) } -const postStatus = ({credentials, status, mediaIds, inReplyToStatusId}) => { +const unretweet = ({ id, credentials }) => { + return fetch(`${UNRETWEET_URL}/${id}.json`, { + headers: authHeaders(credentials), + method: 'POST' + }) +} + +const postStatus = ({credentials, status, spoilerText, visibility, sensitive, mediaIds, inReplyToStatusId, contentType}) => { const idsText = mediaIds.join(',') const form = new FormData() form.append('status', status) form.append('source', 'Pleroma FE') + if (spoilerText) form.append('spoiler_text', spoilerText) + if (visibility) form.append('visibility', visibility) + if (sensitive) form.append('sensitive', sensitive) + if (contentType) form.append('content_type', contentType) form.append('media_ids', idsText) if (inReplyToStatusId) { form.append('in_reply_to_status_id', inReplyToStatusId) @@ -373,6 +419,34 @@ const followImport = ({params, credentials}) => { .then((response) => response.ok) } +const deleteAccount = ({credentials, password}) => { + const form = new FormData() + + form.append('password', password) + + return fetch(DELETE_ACCOUNT_URL, { + body: form, + method: 'POST', + headers: authHeaders(credentials) + }) + .then((response) => response.json()) +} + +const changePassword = ({credentials, password, newPassword, newPasswordConfirmation}) => { + const form = new FormData() + + form.append('password', password) + form.append('new_password', newPassword) + form.append('new_password_confirmation', newPasswordConfirmation) + + return fetch(CHANGE_PASSWORD_URL, { + body: form, + method: 'POST', + headers: authHeaders(credentials) + }) + .then((response) => response.json()) +} + const fetchMutes = ({credentials}) => { const url = '/api/qvitter/mutes.json' @@ -381,6 +455,24 @@ const fetchMutes = ({credentials}) => { }).then((data) => data.json()) } +const suggestions = ({credentials}) => { + return fetch(SUGGESTIONS_URL, { + headers: authHeaders(credentials) + }).then((data) => data.json()) +} + +const markNotificationsAsSeen = ({id, credentials}) => { + const body = new FormData() + + body.append('latest_id', id) + + return fetch(QVITTER_USER_NOTIFICATIONS_READ_URL, { + body, + headers: authHeaders(credentials), + method: 'POST' + }).then((data) => data.json()) +} + const apiService = { verifyCredentials, fetchTimeline, @@ -396,6 +488,7 @@ const apiService = { favorite, unfavorite, retweet, + unretweet, postStatus, deleteStatus, uploadMedia, @@ -408,7 +501,14 @@ const apiService = { updateProfile, updateBanner, externalProfile, - followImport + followImport, + deleteAccount, + changePassword, + fetchFollowRequests, + approveUser, + denyUser, + suggestions, + markNotificationsAsSeen } export default apiService |
