@ -9,17 +9,13 @@ const FAVORITE_URL = '/api/favorites/create'
const UNFAVORITE _URL = '/api/favorites/destroy'
const UNFAVORITE _URL = '/api/favorites/destroy'
const RETWEET _URL = '/api/statuses/retweet'
const RETWEET _URL = '/api/statuses/retweet'
const UNRETWEET _URL = '/api/statuses/unretweet'
const UNRETWEET _URL = '/api/statuses/unretweet'
const STATUS _UPDATE _URL = '/api/statuses/update.json'
const STATUS _DELETE _URL = '/api/statuses/destroy'
const STATUS _DELETE _URL = '/api/statuses/destroy'
const MEDIA _UPLOAD _URL = '/api/statusnet/media/upload'
const MENTIONS _URL = '/api/statuses/mentions.json'
const MENTIONS _URL = '/api/statuses/mentions.json'
const DM _TIMELINE _URL = '/api/statuses/dm_timeline.json'
const DM _TIMELINE _URL = '/api/statuses/dm_timeline.json'
const FOLLOWERS _URL = '/api/statuses/followers.json'
const FOLLOWERS _URL = '/api/statuses/followers.json'
const FRIENDS _URL = '/api/statuses/friends.json'
const FRIENDS _URL = '/api/statuses/friends.json'
const BLOCKS _URL = '/api/statuses/blocks.json'
const FOLLOWING _URL = '/api/friendships/create.json'
const FOLLOWING _URL = '/api/friendships/create.json'
const UNFOLLOWING _URL = '/api/friendships/destroy.json'
const UNFOLLOWING _URL = '/api/friendships/destroy.json'
const QVITTER _USER _PREF _URL = '/api/qvitter/set_profile_pref.json'
const REGISTRATION _URL = '/api/account/register.json'
const REGISTRATION _URL = '/api/account/register.json'
const AVATAR _UPDATE _URL = '/api/qvitter/update_avatar.json'
const AVATAR _UPDATE _URL = '/api/qvitter/update_avatar.json'
const BG _UPDATE _URL = '/api/qvitter/update_background_image.json'
const BG _UPDATE _URL = '/api/qvitter/update_background_image.json'
@ -28,8 +24,6 @@ const PROFILE_UPDATE_URL = '/api/account/update_profile.json'
const EXTERNAL _PROFILE _URL = '/api/externalprofile/show.json'
const EXTERNAL _PROFILE _URL = '/api/externalprofile/show.json'
const QVITTER _USER _NOTIFICATIONS _URL = '/api/qvitter/statuses/notifications.json'
const QVITTER _USER _NOTIFICATIONS _URL = '/api/qvitter/statuses/notifications.json'
const QVITTER _USER _NOTIFICATIONS _READ _URL = '/api/qvitter/statuses/notifications/read.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 FOLLOW _IMPORT _URL = '/api/pleroma/follow_import'
const FOLLOW _IMPORT _URL = '/api/pleroma/follow_import'
const DELETE _ACCOUNT _URL = '/api/pleroma/delete_account'
const DELETE _ACCOUNT _URL = '/api/pleroma/delete_account'
const CHANGE _PASSWORD _URL = '/api/pleroma/change_password'
const CHANGE _PASSWORD _URL = '/api/pleroma/change_password'
@ -44,9 +38,17 @@ const MASTODON_STATUS_CONTEXT_URL = id => `/api/v1/statuses/${id}/context`
const MASTODON _USER _URL = '/api/v1/accounts'
const MASTODON _USER _URL = '/api/v1/accounts'
const MASTODON _USER _RELATIONSHIPS _URL = '/api/v1/accounts/relationships'
const MASTODON _USER _RELATIONSHIPS _URL = '/api/v1/accounts/relationships'
const MASTODON _USER _TIMELINE _URL = id => ` /api/v1/accounts/ ${ id } /statuses `
const MASTODON _USER _TIMELINE _URL = id => ` /api/v1/accounts/ ${ id } /statuses `
const MASTODON _USER _BLOCKS _URL = '/api/v1/blocks/'
const MASTODON _USER _MUTES _URL = '/api/v1/mutes/'
const MASTODON _BLOCK _USER _URL = id => ` /api/v1/accounts/ ${ id } /block `
const MASTODON _UNBLOCK _USER _URL = id => ` /api/v1/accounts/ ${ id } /unblock `
const MASTODON _MUTE _USER _URL = id => ` /api/v1/accounts/ ${ id } /mute `
const MASTODON _UNMUTE _USER _URL = id => ` /api/v1/accounts/ ${ id } /unmute `
const MASTODON _POST _STATUS _URL = '/api/v1/statuses'
const MASTODON _MEDIA _UPLOAD _URL = '/api/v1/media'
import { each , map } from 'lodash'
import { each , map } from 'lodash'
import { parseStatus , parseUser , parseNotification } from '../entity_normalizer/entity_normalizer.service.js'
import { parseStatus , parseUser , parseNotification , parseAttachment } from '../entity_normalizer/entity_normalizer.service.js'
import 'whatwg-fetch'
import 'whatwg-fetch'
import { StatusCodeError } from '../errors/errors'
import { StatusCodeError } from '../errors/errors'
@ -60,6 +62,19 @@ let fetch = (url, options) => {
return oldfetch ( fullUrl , options )
return oldfetch ( fullUrl , options )
}
}
const promisedRequest = ( url , options ) => {
return fetch ( url , options )
. then ( ( response ) => {
return new Promise ( ( resolve , reject ) => response . json ( )
. then ( ( json ) => {
if ( ! response . ok ) {
return reject ( new StatusCodeError ( response . status , json , { url , options } , response ) )
}
return resolve ( json )
} ) )
} )
}
// Params
// Params
// cropH
// cropH
// cropW
// cropW
@ -212,16 +227,14 @@ const unfollowUser = ({id, credentials}) => {
}
}
const blockUser = ( { id , credentials } ) => {
const blockUser = ( { id , credentials } ) => {
let url = ` ${ BLOCKING _URL } ?user_id= ${ id } `
return fetch ( MASTODON _BLOCK _USER _URL ( id ) , {
return fetch ( url , {
headers : authHeaders ( credentials ) ,
headers : authHeaders ( credentials ) ,
method : 'POST'
method : 'POST'
} ) . then ( ( data ) => data . json ( ) )
} ) . then ( ( data ) => data . json ( ) )
}
}
const unblockUser = ( { id , credentials } ) => {
const unblockUser = ( { id , credentials } ) => {
let url = ` ${ UNBLOCKING _URL } ?user_id= ${ id } `
return fetch ( MASTODON _UNBLOCK _USER _URL ( id ) , {
return fetch ( url , {
headers : authHeaders ( credentials ) ,
headers : authHeaders ( credentials ) ,
method : 'POST'
method : 'POST'
} ) . then ( ( data ) => data . json ( ) )
} ) . then ( ( data ) => data . json ( ) )
@ -245,16 +258,7 @@ const denyUser = ({id, credentials}) => {
const fetchUser = ( { id , credentials } ) => {
const fetchUser = ( { id , credentials } ) => {
let url = ` ${ MASTODON _USER _URL } / ${ id } `
let url = ` ${ MASTODON _USER _URL } / ${ id } `
return fetch ( url , { headers : authHeaders ( credentials ) } )
return promisedRequest ( url , { headers : authHeaders ( credentials ) } )
. then ( ( response ) => {
return new Promise ( ( resolve , reject ) => response . json ( )
. then ( ( json ) => {
if ( ! response . ok ) {
return reject ( new StatusCodeError ( response . status , json , { url } , response ) )
}
return resolve ( json )
} ) )
} )
. then ( ( data ) => parseUser ( data ) )
. then ( ( data ) => parseUser ( data ) )
}
}
@ -341,23 +345,7 @@ const fetchStatus = ({id, credentials}) => {
. then ( ( data ) => parseStatus ( data ) )
. then ( ( data ) => parseStatus ( data ) )
}
}
const setUserMute = ( { id , credentials , muted = true } ) => {
const fetchTimeline = ( { timeline , credentials , since = false , until = false , userId = false , tag = false , withMuted = false } ) => {
const form = new FormData ( )
const muteInteger = muted ? 1 : 0
form . append ( 'namespace' , 'qvitter' )
form . append ( 'data' , muteInteger )
form . append ( 'topic' , ` mute: ${ id } ` )
return fetch ( QVITTER _USER _PREF _URL , {
method : 'POST' ,
headers : authHeaders ( credentials ) ,
body : form
} )
}
const fetchTimeline = ( { timeline , credentials , since = false , until = false , userId = false , tag = false } ) => {
const timelineUrls = {
const timelineUrls = {
public : PUBLIC _TIMELINE _URL ,
public : PUBLIC _TIMELINE _URL ,
friends : FRIENDS _TIMELINE _URL ,
friends : FRIENDS _TIMELINE _URL ,
@ -393,6 +381,7 @@ const fetchTimeline = ({timeline, credentials, since = false, until = false, use
}
}
params . push ( [ 'count' , 20 ] )
params . push ( [ 'count' , 20 ] )
params . push ( [ 'with_muted' , withMuted ] )
const queryString = map ( params , ( param ) => ` ${ param [ 0 ] } = ${ param [ 1 ] } ` ) . join ( '&' )
const queryString = map ( params , ( param ) => ` ${ param [ 0 ] } = ${ param [ 1 ] } ` ) . join ( '&' )
url += ` ? ${ queryString } `
url += ` ? ${ queryString } `
@ -453,23 +442,23 @@ const unretweet = ({ id, credentials }) => {
} )
} )
}
}
const postStatus = ( { credentials , status , spoilerText , visibility , sensitive , mediaIds , inReplyToStatusId , contentType , noAttachmentLinks } ) => {
const postStatus = ( { credentials , status , spoilerText , visibility , sensitive , mediaIds = [ ] , inReplyToStatusId , contentType } ) => {
const idsText = mediaIds . join ( ',' )
const form = new FormData ( )
const form = new FormData ( )
form . append ( 'status' , status )
form . append ( 'status' , status )
form . append ( 'source' , 'Pleroma FE' )
form . append ( 'source' , 'Pleroma FE' )
if ( noAttachmentLinks ) form . append ( 'no_attachment_links' , noAttachmentLinks )
if ( spoilerText ) form . append ( 'spoiler_text' , spoilerText )
if ( spoilerText ) form . append ( 'spoiler_text' , spoilerText )
if ( visibility ) form . append ( 'visibility' , visibility )
if ( visibility ) form . append ( 'visibility' , visibility )
if ( sensitive ) form . append ( 'sensitive' , sensitive )
if ( sensitive ) form . append ( 'sensitive' , sensitive )
if ( contentType ) form . append ( 'content_type' , contentType )
if ( contentType ) form . append ( 'content_type' , contentType )
form . append ( 'media_ids' , idsText )
mediaIds . forEach ( val => {
form . append ( 'media_ids[]' , val )
} )
if ( inReplyToStatusId ) {
if ( inReplyToStatusId ) {
form . append ( 'in_reply_to_status_id' , inReplyToStatusId )
form . append ( 'in_reply_to_ id', inReplyToStatusId )
}
}
return fetch ( STATUS_UPDATE _URL , {
return fetch ( MASTODON_POST _STATUS _URL , {
body : form ,
body : form ,
method : 'POST' ,
method : 'POST' ,
headers : authHeaders ( credentials )
headers : authHeaders ( credentials )
@ -494,13 +483,13 @@ const deleteStatus = ({ id, credentials }) => {
}
}
const uploadMedia = ( { formData , credentials } ) => {
const uploadMedia = ( { formData , credentials } ) => {
return fetch ( M EDIA_UPLOAD _URL , {
return fetch ( M ASTODON_M EDIA_UPLOAD _URL , {
body : formData ,
body : formData ,
method : 'POST' ,
method : 'POST' ,
headers : authHeaders ( credentials )
headers : authHeaders ( credentials )
} )
} )
. then ( ( response) => response . text ( ) )
. then ( ( data) => data . json ( ) )
. then ( ( text) => ( new DOMParser ( ) ) . parseFromString ( text , 'application/xml' ) )
. then ( ( data) => parseAttachment ( data ) )
}
}
const followImport = ( { params , credentials } ) => {
const followImport = ( { params , credentials } ) => {
@ -541,24 +530,29 @@ const changePassword = ({credentials, password, newPassword, newPasswordConfirma
}
}
const fetchMutes = ( { credentials } ) => {
const fetchMutes = ( { credentials } ) => {
const url = '/api/qvitter/mutes.json'
return promisedRequest ( MASTODON _USER _MUTES _URL , { headers : authHeaders ( credentials ) } )
. then ( ( users ) => users . map ( parseUser ) )
}
return fetch ( url , {
const muteUser = ( { id , credentials } ) => {
headers : authHeaders ( credentials )
return promisedRequest ( MASTODON _MUTE _USER _URL ( id ) , {
} ) . then ( ( data ) => data . json ( ) )
headers : authHeaders ( credentials ) ,
method : 'POST'
} )
}
}
const fetchBlocks = ( { page , credentials } ) => {
const unmuteUser = ( { id , credentials } ) => {
return fetch ( BLOCKS _URL , {
return promisedRequest ( MASTODON _UNMUTE _USER _URL ( id ) , {
headers : authHeaders ( credentials )
headers : authHeaders ( credentials ) ,
} ) . then ( ( data ) => {
method : 'POST'
if ( data . ok ) {
return data . json ( )
}
throw new Error ( 'Error fetching blocks' , data )
} )
} )
}
}
const fetchBlocks = ( { credentials } ) => {
return promisedRequest ( MASTODON _USER _BLOCKS _URL , { headers : authHeaders ( credentials ) } )
. then ( ( users ) => users . map ( parseUser ) )
}
const fetchOAuthTokens = ( { credentials } ) => {
const fetchOAuthTokens = ( { credentials } ) => {
const url = '/api/oauth_tokens.json'
const url = '/api/oauth_tokens.json'
@ -621,8 +615,9 @@ const apiService = {
deleteStatus ,
deleteStatus ,
uploadMedia ,
uploadMedia ,
fetchAllFollowing ,
fetchAllFollowing ,
setUserMute ,
fetchMutes ,
fetchMutes ,
muteUser ,
unmuteUser ,
fetchBlocks ,
fetchBlocks ,
fetchOAuthTokens ,
fetchOAuthTokens ,
revokeOAuthToken ,
revokeOAuthToken ,