pleroma-fe/src/services/api/api.service.js

1627 lines
41 KiB
JavaScript
Raw Normal View History

import { concat, each, last, map } from 'lodash'
2026-01-08 17:26:52 +02:00
2026-01-06 16:22:52 +02:00
import {
parseAttachment,
parseChat,
parseLinkHeaderPagination,
2026-01-06 16:23:17 +02:00
parseNotification,
parseSource,
parseStatus,
parseUser,
2026-01-06 16:22:52 +02:00
} from '../entity_normalizer/entity_normalizer.service.js'
2026-06-13 03:10:00 +03:00
import { promisedRequest } from './helpers.js'
import { RegistrationError, StatusCodeError } from 'src/services/errors/errors'
2019-06-18 20:28:31 +00:00
2016-10-27 18:03:14 +02:00
/* eslint-env browser */
2020-09-07 14:27:37 +03:00
const MUTES_IMPORT_URL = '/api/pleroma/mutes_import'
2019-03-30 07:27:53 -04:00
const BLOCKS_IMPORT_URL = '/api/pleroma/blocks_import'
2017-12-23 16:44:22 +02:00
const FOLLOW_IMPORT_URL = '/api/pleroma/follow_import'
const DELETE_ACCOUNT_URL = '/api/pleroma/delete_account'
2019-11-08 02:21:19 +00:00
const CHANGE_EMAIL_URL = '/api/pleroma/change_email'
2018-05-21 23:01:09 +01:00
const CHANGE_PASSWORD_URL = '/api/pleroma/change_password'
const MOVE_ACCOUNT_URL = '/api/pleroma/move_account'
const ALIASES_URL = '/api/pleroma/aliases'
2018-08-02 18:34:12 +09:00
const SUGGESTIONS_URL = '/api/v1/suggestions'
2019-05-25 07:01:02 +00:00
const NOTIFICATION_SETTINGS_URL = '/api/pleroma/notification_settings'
const NOTIFICATION_READ_URL = '/api/v1/pleroma/notifications/read'
2016-10-27 18:03:14 +02:00
const MFA_SETTINGS_URL = '/api/pleroma/accounts/mfa'
const MFA_BACKUP_CODES_URL = '/api/pleroma/accounts/mfa/backup_codes'
const MFA_SETUP_OTP_URL = '/api/pleroma/accounts/mfa/setup/totp'
const MFA_CONFIRM_OTP_URL = '/api/pleroma/accounts/mfa/confirm/totp'
2019-12-11 15:59:29 +03:00
const MFA_DISABLE_OTP_URL = '/api/pleroma/accounts/mfa/totp'
2019-05-21 23:35:40 +03:00
const MASTODON_LOGIN_URL = '/api/v1/accounts/verify_credentials'
const MASTODON_REGISTRATION_URL = '/api/v1/accounts'
const MASTODON_USER_FAVORITES_TIMELINE_URL = '/api/v1/favourites'
2019-03-12 17:16:57 -04:00
const MASTODON_USER_NOTIFICATIONS_URL = '/api/v1/notifications'
2026-01-06 16:22:52 +02:00
const MASTODON_DISMISS_NOTIFICATION_URL = (id) =>
`/api/v1/notifications/${id}/dismiss`
const MASTODON_FAVORITE_URL = (id) => `/api/v1/statuses/${id}/favourite`
const MASTODON_UNFAVORITE_URL = (id) => `/api/v1/statuses/${id}/unfavourite`
const MASTODON_RETWEET_URL = (id) => `/api/v1/statuses/${id}/reblog`
const MASTODON_UNRETWEET_URL = (id) => `/api/v1/statuses/${id}/unreblog`
const MASTODON_DELETE_URL = (id) => `/api/v1/statuses/${id}`
const MASTODON_FOLLOW_URL = (id) => `/api/v1/accounts/${id}/follow`
const MASTODON_UNFOLLOW_URL = (id) => `/api/v1/accounts/${id}/unfollow`
const MASTODON_FOLLOWING_URL = (id) => `/api/v1/accounts/${id}/following`
const MASTODON_FOLLOWERS_URL = (id) => `/api/v1/accounts/${id}/followers`
2019-09-03 13:38:52 +03:00
const MASTODON_FOLLOW_REQUESTS_URL = '/api/v1/follow_requests'
2026-01-06 16:22:52 +02:00
const MASTODON_APPROVE_USER_URL = (id) =>
`/api/v1/follow_requests/${id}/authorize`
const MASTODON_DENY_USER_URL = (id) => `/api/v1/follow_requests/${id}/reject`
2019-03-07 20:21:07 +02:00
const MASTODON_DIRECT_MESSAGES_TIMELINE_URL = '/api/v1/timelines/direct'
2019-03-07 20:16:35 +02:00
const MASTODON_PUBLIC_TIMELINE = '/api/v1/timelines/public'
const MASTODON_USER_HOME_TIMELINE_URL = '/api/v1/timelines/home'
2026-01-06 16:22:52 +02:00
const MASTODON_STATUS_URL = (id) => `/api/v1/statuses/${id}`
const MASTODON_STATUS_CONTEXT_URL = (id) => `/api/v1/statuses/${id}/context`
const MASTODON_STATUS_SOURCE_URL = (id) => `/api/v1/statuses/${id}/source`
const MASTODON_STATUS_HISTORY_URL = (id) => `/api/v1/statuses/${id}/history`
const MASTODON_USER_URL = '/api/v1/accounts'
const MASTODON_USER_LOOKUP_URL = '/api/v1/accounts/lookup'
const MASTODON_USER_RELATIONSHIPS_URL = '/api/v1/accounts/relationships'
2026-01-06 16:22:52 +02:00
const MASTODON_USER_TIMELINE_URL = (id) => `/api/v1/accounts/${id}/statuses`
const MASTODON_USER_IN_LISTS = (id) => `/api/v1/accounts/${id}/lists`
const MASTODON_LIST_URL = (id) => `/api/v1/lists/${id}`
const MASTODON_LIST_TIMELINE_URL = (id) => `/api/v1/timelines/list/${id}`
const MASTODON_LIST_ACCOUNTS_URL = (id) => `/api/v1/lists/${id}/accounts`
const MASTODON_TAG_TIMELINE_URL = (tag) => `/api/v1/timelines/tag/${tag}`
const MASTODON_BOOKMARK_TIMELINE_URL = '/api/v1/bookmarks'
2025-06-18 17:48:11 +03:00
const AKKOMA_BUBBLE_TIMELINE_URL = '/api/v1/timelines/bubble'
2019-03-21 21:27:10 -04:00
const MASTODON_USER_BLOCKS_URL = '/api/v1/blocks/'
const MASTODON_USER_MUTES_URL = '/api/v1/mutes/'
2026-01-06 16:22:52 +02:00
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_REMOVE_USER_FROM_FOLLOWERS = (id) =>
`/api/v1/accounts/${id}/remove_from_followers`
const MASTODON_USER_NOTE_URL = (id) => `/api/v1/accounts/${id}/note`
const MASTODON_BOOKMARK_STATUS_URL = (id) => `/api/v1/statuses/${id}/bookmark`
const MASTODON_UNBOOKMARK_STATUS_URL = (id) =>
`/api/v1/statuses/${id}/unbookmark`
const MASTODON_POST_STATUS_URL = '/api/v1/statuses'
const MASTODON_MEDIA_UPLOAD_URL = '/api/v1/media'
2026-01-06 16:22:52 +02:00
const MASTODON_VOTE_URL = (id) => `/api/v1/polls/${id}/votes`
const MASTODON_POLL_URL = (id) => `/api/v1/polls/${id}`
const MASTODON_STATUS_FAVORITEDBY_URL = (id) =>
`/api/v1/statuses/${id}/favourited_by`
const MASTODON_STATUS_REBLOGGEDBY_URL = (id) =>
`/api/v1/statuses/${id}/reblogged_by`
const MASTODON_PROFILE_UPDATE_URL = '/api/v1/accounts/update_credentials'
2019-04-24 22:40:37 -04:00
const MASTODON_REPORT_USER_URL = '/api/v1/reports'
2026-01-06 16:22:52 +02:00
const MASTODON_PIN_OWN_STATUS = (id) => `/api/v1/statuses/${id}/pin`
const MASTODON_UNPIN_OWN_STATUS = (id) => `/api/v1/statuses/${id}/unpin`
const MASTODON_MUTE_CONVERSATION = (id) => `/api/v1/statuses/${id}/mute`
const MASTODON_UNMUTE_CONVERSATION = (id) => `/api/v1/statuses/${id}/unmute`
2022-07-31 12:35:48 +03:00
const MASTODON_SEARCH_2 = '/api/v2/search'
const MASTODON_USER_SEARCH_URL = '/api/v1/accounts/search'
2020-01-15 20:22:54 +00:00
const MASTODON_DOMAIN_BLOCKS_URL = '/api/v1/domain_blocks'
2022-08-06 17:26:43 +03:00
const MASTODON_LISTS_URL = '/api/v1/lists'
2019-11-24 18:50:28 +02:00
const MASTODON_STREAMING = '/api/v1/streaming'
const MASTODON_KNOWN_DOMAIN_LIST_URL = '/api/v1/instance/peers'
2022-03-17 14:01:45 -04:00
const MASTODON_ANNOUNCEMENTS_URL = '/api/v1/announcements'
2026-01-06 16:22:52 +02:00
const MASTODON_ANNOUNCEMENTS_DISMISS_URL = (id) =>
`/api/v1/announcements/${id}/dismiss`
const PLEROMA_EMOJI_REACTIONS_URL = (id) =>
`/api/v1/pleroma/statuses/${id}/reactions`
const PLEROMA_EMOJI_REACT_URL = (id, emoji) =>
`/api/v1/pleroma/statuses/${id}/reactions/${emoji}`
const PLEROMA_EMOJI_UNREACT_URL = (id, emoji) =>
`/api/v1/pleroma/statuses/${id}/reactions/${emoji}`
2022-07-31 12:35:48 +03:00
const PLEROMA_CHATS_URL = '/api/v1/pleroma/chats'
2026-01-06 16:22:52 +02:00
const PLEROMA_CHAT_URL = (id) => `/api/v1/pleroma/chats/by-account-id/${id}`
const PLEROMA_CHAT_MESSAGES_URL = (id) => `/api/v1/pleroma/chats/${id}/messages`
const PLEROMA_CHAT_READ_URL = (id) => `/api/v1/pleroma/chats/${id}/read`
const PLEROMA_DELETE_CHAT_MESSAGE_URL = (chatId, messageId) =>
`/api/v1/pleroma/chats/${chatId}/messages/${messageId}`
2022-05-09 00:38:25 -04:00
const PLEROMA_BACKUP_URL = '/api/v1/pleroma/backups'
2026-01-06 16:22:52 +02:00
const PLEROMA_SCROBBLES_URL = (id) => `/api/v1/pleroma/accounts/${id}/scrobbles`
const PLEROMA_STATUS_QUOTES_URL = (id) =>
`/api/v1/pleroma/statuses/${id}/quotes`
const PLEROMA_USER_FAVORITES_TIMELINE_URL = (id) =>
`/api/v1/pleroma/accounts/${id}/favourites`
const PLEROMA_BOOKMARK_FOLDERS_URL = '/api/v1/pleroma/bookmark_folders'
2026-01-06 16:22:52 +02:00
const PLEROMA_BOOKMARK_FOLDER_URL = (id) =>
`/api/v1/pleroma/bookmark_folders/${id}`
2026-06-13 03:10:00 +03:00
export const updateNotificationSettings = ({ credentials, settings }) => {
2019-05-25 07:01:02 +00:00
const form = new FormData()
each(settings, (value, key) => {
form.append(key, value)
})
2026-06-13 03:10:00 +03:00
return promisedRequest({
url: `${NOTIFICATION_SETTINGS_URL}?${new URLSearchParams(settings)}`,
credentials,
method: 'PUT',
formData: form,
})
2019-05-25 07:01:02 +00:00
}
2026-06-13 03:10:00 +03:00
export const updateProfileImages = ({
2026-01-06 16:22:52 +02:00
credentials,
avatar = null,
avatarName = null,
banner = null,
background = null,
}) => {
2017-04-16 13:44:11 +02:00
const form = new FormData()
if (avatar !== null) {
if (avatarName !== null) {
form.append('avatar', avatar, avatarName)
} else {
form.append('avatar', avatar)
}
}
if (banner !== null) form.append('header', banner)
if (background !== null) form.append('pleroma_background_image', background)
2026-06-13 03:10:00 +03:00
return promisedRequest({
url: MASTODON_PROFILE_UPDATE_URL,
credentials,
method: 'PATCH',
2026-06-13 03:10:00 +03:00
formData: form,
}).then((data) => {
if (data.error) {
throw new Error(data.error)
}
return parseUser(data)
})
}
2026-06-13 03:10:00 +03:00
export const updateProfile = ({ credentials, params }) => {
2026-01-06 16:22:52 +02:00
const formData = new FormData()
2025-08-04 11:10:43 +03:00
2026-01-06 16:22:52 +02:00
for (const name in params) {
2025-08-04 11:10:43 +03:00
if (name === 'fields_attributes') {
params[name].forEach((param, i) => {
formData.append(name + `[${i}][name]`, param.name)
formData.append(name + `[${i}][value]`, param.value)
})
} else {
2025-08-10 17:42:37 +03:00
if (typeof params[name] === 'object') {
2026-01-06 16:22:52 +02:00
console.warn(
'Object detected in updateProfile API call. This will not work, use updateProfileJSON instead.',
)
2025-09-15 19:44:23 +03:00
console.warn('Object:\n' + JSON.stringify(params[name], null, 2))
2025-08-10 17:42:37 +03:00
}
2026-01-06 16:22:52 +02:00
formData.append(name, params[name])
2025-08-04 11:10:43 +03:00
}
}
2026-06-13 03:10:00 +03:00
return promisedRequest({
url: MASTODON_PROFILE_UPDATE_URL,
credentials,
method: 'PATCH',
2026-06-13 03:10:00 +03:00
formData,
}).then((data) => parseUser(data))
}
2026-06-13 03:10:00 +03:00
export const updateProfileJSON = ({ credentials, params }) =>
promisedRequest({
2019-04-30 16:38:34 -04:00
url: MASTODON_PROFILE_UPDATE_URL,
2025-08-10 17:42:37 +03:00
credentials,
2026-01-06 16:22:52 +02:00
payload: params,
method: 'PATCH',
2026-02-13 14:29:20 +02:00
}).then((data) => parseUser(data))
2017-04-15 18:12:23 +02:00
// Params needed:
// nickname
// email
// fullname
// password
// password_confirm
//
// Optional
// bio
// homepage
// location
2018-08-05 10:01:38 +03:00
// token
// language
2026-06-13 03:10:00 +03:00
export const register = ({ params, credentials }) => {
const { nickname, ...rest } = params
2026-06-13 03:10:00 +03:00
return promisedRequest({
url: MASTODON_REGISTRATION_URL,
2017-04-15 18:12:23 +02:00
method: 'POST',
2026-06-13 03:10:00 +03:00
credentials,
payload: {
nickname,
2019-06-13 00:47:06 +03:00
locale: 'en_US',
agreement: true,
2026-01-06 16:22:52 +02:00
...rest,
2026-06-13 03:10:00 +03:00
},
2017-04-15 18:12:23 +02:00
})
}
2026-06-13 03:10:00 +03:00
export const getCaptcha = () =>
promisedRequest({
url: '/api/pleroma/captcha',
})
2016-10-28 14:26:51 +02:00
2026-06-13 03:10:00 +03:00
export const followUser = ({ id, credentials, ...options }) => {
2019-10-08 10:21:48 +03:00
const form = {}
2026-06-13 03:10:00 +03:00
2026-01-06 16:22:52 +02:00
if (options.reblogs !== undefined) {
form.reblogs = options.reblogs
}
2026-06-13 03:10:00 +03:00
2026-01-06 16:22:52 +02:00
if (options.notify !== undefined) {
form.notify = options.notify
}
2026-06-13 03:10:00 +03:00
return promisedRequest({
url: MASTODON_FOLLOW_URL(id),
formData: form,
credentials,
2026-01-06 16:22:52 +02:00
method: 'POST',
2026-06-13 03:10:00 +03:00
})
2016-12-08 09:09:21 +01:00
}
2026-06-13 03:10:00 +03:00
export const unfollowUser = ({ id, credentials }) =>
promisedRequest({
url: MASTODON_UNFOLLOW_URL(id),
credentials,
2026-01-06 16:22:52 +02:00
method: 'POST',
2026-06-13 03:10:00 +03:00
})
2016-12-23 16:45:57 +01:00
2026-06-13 03:10:00 +03:00
export const fetchUserInLists = ({ id, credentials }) =>
promisedRequest({
url: MASTODON_USER_IN_LISTS(id),
credentials,
})
2026-06-13 03:10:00 +03:00
export const pinOwnStatus = ({ id, credentials }) =>
promisedRequest({
2026-01-06 16:22:52 +02:00
url: MASTODON_PIN_OWN_STATUS(id),
credentials,
method: 'POST',
}).then((data) => parseStatus(data))
2019-04-04 11:27:02 -04:00
2026-06-13 03:10:00 +03:00
export const unpinOwnStatus = ({ id, credentials }) =>
promisedRequest({
2026-01-06 16:22:52 +02:00
url: MASTODON_UNPIN_OWN_STATUS(id),
credentials,
method: 'POST',
}).then((data) => parseStatus(data))
2019-04-04 11:27:02 -04:00
2026-06-13 03:10:00 +03:00
export const muteConversation = ({ id, credentials }) =>
promisedRequest({
2026-01-06 16:22:52 +02:00
url: MASTODON_MUTE_CONVERSATION(id),
credentials,
method: 'POST',
}).then((data) => parseStatus(data))
2026-06-13 03:10:00 +03:00
export const unmuteConversation = ({ id, credentials }) =>
promisedRequest({
2026-01-06 16:22:52 +02:00
url: MASTODON_UNMUTE_CONVERSATION(id),
credentials,
method: 'POST',
}).then((data) => parseStatus(data))
2026-06-13 03:10:00 +03:00
export const blockUser = ({ id, expiresIn, credentials }) => {
const payload = {}
if (expiresIn) {
payload.duration = expiresIn
}
return promisedRequest({
url: MASTODON_BLOCK_USER_URL(id),
credentials,
method: 'POST',
2026-01-06 16:22:52 +02:00
payload,
})
2017-11-07 22:38:28 +02:00
}
2026-06-13 03:10:00 +03:00
export const unblockUser = ({ id, credentials }) =>
promisedRequest({
url: MASTODON_UNBLOCK_USER_URL(id),
credentials,
2026-01-06 16:22:52 +02:00
method: 'POST',
2026-06-13 03:10:00 +03:00
})
2017-11-07 22:38:28 +02:00
2026-06-13 03:10:00 +03:00
export const removeUserFromFollowers = ({ id, credentials }) =>
promisedRequest({
url: MASTODON_REMOVE_USER_FROM_FOLLOWERS(id),
credentials,
2026-01-06 16:22:52 +02:00
method: 'POST',
2026-06-13 03:10:00 +03:00
})
2026-06-13 03:10:00 +03:00
export const editUserNote = ({ id, credentials, comment }) =>
promisedRequest({
2022-08-20 13:18:57 -04:00
url: MASTODON_USER_NOTE_URL(id),
credentials,
payload: {
2026-01-06 16:22:52 +02:00
comment,
2022-08-20 13:18:57 -04:00
},
2026-01-06 16:22:52 +02:00
method: 'POST',
2022-08-20 13:18:57 -04:00
})
2026-06-13 03:10:00 +03:00
export const approveUser = ({ id, credentials }) =>
promisedRequest({
url: MASTODON_APPROVE_USER_URL(id),
credentials,
2026-01-06 16:22:52 +02:00
method: 'POST',
2026-06-13 03:10:00 +03:00
})
2026-06-13 03:10:00 +03:00
export const denyUser = ({ id, credentials }) =>
promisedRequest({
url: MASTODON_DENY_USER_URL(id),
credentials,
2026-01-06 16:22:52 +02:00
method: 'POST',
2026-06-13 03:10:00 +03:00
})
2026-06-13 03:10:00 +03:00
export const fetchUser = ({ id, credentials }) =>
promisedRequest({
url: `${MASTODON_USER_URL}/${id}`,
credentials,
}).then((data) => parseUser(data))
2026-06-13 03:10:00 +03:00
export const fetchUserByName = ({ name, credentials }) =>
promisedRequest({
url: MASTODON_USER_LOOKUP_URL,
credentials,
2026-01-06 16:22:52 +02:00
params: { acct: name },
})
2026-01-06 16:22:52 +02:00
.then((data) => data.id)
.catch((error) => {
if (error && error.statusCode === 404) {
// Either the backend does not support lookup endpoint,
// or there is no user with such name. Fallback and treat name as id.
return name
} else {
throw error
}
})
2026-01-06 16:22:52 +02:00
.then((id) => fetchUser({ id, credentials }))
2026-06-13 03:10:00 +03:00
export const fetchUserRelationship = ({ id, credentials }) =>
promisedRequest({
url: `${MASTODON_USER_RELATIONSHIPS_URL}/?id=${id}`,
credentials,
2026-01-06 16:22:52 +02:00
})
2026-06-13 03:10:00 +03:00
export const fetchFriends = ({
id,
maxId,
sinceId,
limit = 20,
credentials,
}) => {
let url = MASTODON_FOLLOWING_URL(id)
2019-03-25 21:04:52 +02:00
const args = [
maxId && `max_id=${maxId}`,
2019-03-27 22:02:46 +02:00
sinceId && `since_id=${sinceId}`,
2020-05-13 17:48:31 +03:00
limit && `limit=${limit}`,
2026-01-06 16:22:52 +02:00
'with_relationships=true',
]
.filter((_) => _)
.join('&')
2019-03-25 21:04:52 +02:00
url = url + (args ? '?' + args : '')
2026-06-13 03:10:00 +03:00
return promisedRequest({
url,
credentials,
}).then((data) => data.map(parseUser))
}
2026-06-13 03:10:00 +03:00
export const exportFriends = ({ id, credentials }) => {
2026-01-06 17:32:22 +02:00
// biome-ignore lint/suspicious/noAsyncPromiseExecutor: TODO refactor this
2019-04-19 00:27:06 -04:00
return new Promise(async (resolve, reject) => {
try {
let friends = []
let more = true
while (more) {
const maxId = friends.length > 0 ? last(friends).id : undefined
2019-07-05 10:02:14 +03:00
const users = await fetchFriends({ id, maxId, credentials })
2019-04-19 00:27:06 -04:00
friends = concat(friends, users)
if (users.length === 0) {
more = false
}
}
resolve(friends)
} catch (err) {
reject(err)
}
})
2017-08-21 20:25:01 +03:00
}
2026-06-13 03:10:00 +03:00
export const fetchFollowers = ({
id,
maxId,
sinceId,
limit = 20,
credentials,
}) => {
let url = MASTODON_FOLLOWERS_URL(id)
2019-03-25 21:04:52 +02:00
const args = [
maxId && `max_id=${maxId}`,
2019-03-27 22:02:46 +02:00
sinceId && `since_id=${sinceId}`,
2020-05-13 17:48:31 +03:00
limit && `limit=${limit}`,
2026-01-06 16:22:52 +02:00
'with_relationships=true',
]
.filter((_) => _)
.join('&')
2019-03-25 21:04:52 +02:00
2019-03-27 22:02:46 +02:00
url += args ? '?' + args : ''
2026-06-13 03:10:00 +03:00
return promisedRequest({
url,
credentials,
}).then((data) => data.map(parseUser))
2018-06-06 22:26:24 +00:00
}
2026-06-13 03:10:00 +03:00
export const fetchFollowRequests = ({ credentials }) =>
promisedRequest({
url: MASTODON_FOLLOW_REQUESTS_URL,
credentials,
}).then((data) => data.map(parseUser))
2022-08-06 17:26:43 +03:00
2026-06-13 03:10:00 +03:00
export const fetchLists = ({ credentials }) =>
promisedRequest({
url: MASTODON_LISTS_URL,
credentials,
})
2022-08-06 17:26:43 +03:00
2026-06-13 03:10:00 +03:00
export const createList = ({ title, credentials }) =>
promisedRequest({
url: MASTODON_LISTS_URL,
credentials,
2022-08-06 17:26:43 +03:00
method: 'POST',
2026-06-13 03:10:00 +03:00
payload: { title },
})
2022-08-06 17:26:43 +03:00
2026-06-13 03:10:00 +03:00
export const getList = ({ listId, credentials }) =>
promisedRequest({
url: MASTODON_LIST_URL(listId),
credentials,
})
2022-08-06 17:26:43 +03:00
2026-06-13 03:10:00 +03:00
export const updateList = ({ listId, title, credentials }) =>
promisedRequest({
url: MASTODON_LIST_URL(listId),
2022-08-06 17:26:43 +03:00
2026-06-13 03:10:00 +03:00
credentials,
2022-08-06 17:26:43 +03:00
method: 'PUT',
2026-06-13 03:10:00 +03:00
payload: { title },
2022-08-06 17:26:43 +03:00
})
2026-06-13 03:10:00 +03:00
export const getListAccounts = ({ listId, credentials }) =>
promisedRequest({
url: MASTODON_LIST_ACCOUNTS_URL(listId),
credentials,
}).then((data) => data.map(({ id }) => id))
2022-08-06 17:26:43 +03:00
2026-06-13 03:10:00 +03:00
export const addAccountsToList = ({ listId, accountIds, credentials }) =>
promisedRequest({
url: MASTODON_LIST_ACCOUNTS_URL(listId),
credentials,
2022-08-06 17:26:43 +03:00
method: 'POST',
2026-06-13 03:10:00 +03:00
payload: { account_ids: accountIds },
2022-08-06 17:26:43 +03:00
})
2026-06-13 03:10:00 +03:00
export const removeAccountsFromList = ({ listId, accountIds, credentials }) =>
promisedRequest({
url: MASTODON_LIST_ACCOUNTS_URL(listId),
credentials,
2022-08-06 17:26:43 +03:00
method: 'DELETE',
2026-06-13 03:10:00 +03:00
payload: { account_ids: accountIds },
2022-08-06 17:26:43 +03:00
})
2026-06-13 03:10:00 +03:00
export const deleteList = ({ listId, credentials }) =>
promisedRequest({
url: MASTODON_LIST_URL(listId),
2022-08-06 17:26:43 +03:00
method: 'DELETE',
2026-06-13 03:10:00 +03:00
credentials,
2022-08-06 17:26:43 +03:00
})
2026-06-13 03:10:00 +03:00
export const fetchConversation = ({ id, credentials }) =>
promisedRequest({
url: MASTODON_STATUS_CONTEXT_URL(id),
credentials,
})
2019-01-14 22:58:23 +03:00
.then((data) => {
if (data.ok) {
return data
}
2019-01-17 23:22:51 +03:00
throw new Error('Error fetching timeline', data)
2019-01-14 22:58:23 +03:00
})
2019-06-18 20:28:31 +00:00
.then(({ ancestors, descendants }) => ({
ancestors: ancestors.map(parseStatus),
2026-01-06 16:22:52 +02:00
descendants: descendants.map(parseStatus),
}))
2026-06-13 03:10:00 +03:00
export const fetchStatus = ({ id, credentials }) =>
promisedRequest({
url: MASTODON_STATUS_URL(id),
credentials,
})
2019-01-14 22:58:23 +03:00
.then((data) => {
if (data.ok) {
return data
}
2025-07-28 11:39:50 +03:00
throw new Error('Error fetching timeline', { cause: data })
2019-01-14 22:58:23 +03:00
})
.then((data) => parseStatus(data))
2026-06-13 03:10:00 +03:00
export const fetchStatusSource = ({ id, credentials }) =>
promisedRequest({
url: MASTODON_STATUS_SOURCE_URL(id),
credentials,
})
2022-06-07 21:31:48 -06:00
.then((data) => {
if (data.ok) {
return data
}
2025-07-28 11:39:50 +03:00
throw new Error('Error fetching source', { cause: data })
2022-06-07 21:31:48 -06:00
})
.then((data) => parseSource(data))
2026-06-13 03:10:00 +03:00
export const fetchStatusHistory = ({ status, credentials }) =>
promisedRequest({
url: MASTODON_STATUS_HISTORY_URL(status.id),
credentials,
}).then((data) => {
2026-01-06 16:22:52 +02:00
data.reverse()
return data.map((item) => {
item.originalStatus = status
return parseStatus(item)
2022-06-07 21:31:48 -06:00
})
2026-01-06 16:22:52 +02:00
})
2019-02-18 17:49:32 +03:00
2026-06-13 03:10:00 +03:00
export const fetchTimeline = ({
2019-06-18 20:28:31 +00:00
timeline,
credentials,
since = false,
minId = false,
2019-06-18 20:28:31 +00:00
until = false,
userId = false,
2022-08-06 17:26:43 +03:00
listId = false,
statusId = false,
2019-06-18 20:28:31 +00:00
tag = false,
2020-06-30 17:02:38 +03:00
withMuted = false,
replyVisibility = 'all',
includeTypes = [],
2026-01-06 16:22:52 +02:00
bookmarkFolderId = false,
2019-06-18 20:28:31 +00:00
}) => {
2016-10-28 14:26:51 +02:00
const timelineUrls = {
2019-03-07 20:16:35 +02:00
public: MASTODON_PUBLIC_TIMELINE,
friends: MASTODON_USER_HOME_TIMELINE_URL,
2019-03-07 20:21:07 +02:00
dms: MASTODON_DIRECT_MESSAGES_TIMELINE_URL,
2019-03-12 17:16:57 -04:00
notifications: MASTODON_USER_NOTIFICATIONS_URL,
2022-07-31 12:35:48 +03:00
publicAndExternal: MASTODON_PUBLIC_TIMELINE,
2019-03-08 00:50:58 +02:00
user: MASTODON_USER_TIMELINE_URL,
media: MASTODON_USER_TIMELINE_URL,
2022-08-06 17:26:43 +03:00
list: MASTODON_LIST_TIMELINE_URL,
favorites: MASTODON_USER_FAVORITES_TIMELINE_URL,
publicFavorites: PLEROMA_USER_FAVORITES_TIMELINE_URL,
tag: MASTODON_TAG_TIMELINE_URL,
bookmarks: MASTODON_BOOKMARK_TIMELINE_URL,
2025-06-18 17:48:11 +03:00
quotes: PLEROMA_STATUS_QUOTES_URL,
2026-01-06 16:22:52 +02:00
bubble: AKKOMA_BUBBLE_TIMELINE_URL,
2016-10-28 14:26:51 +02:00
}
const isNotifications = timeline === 'notifications'
const params = []
2016-10-28 14:26:51 +02:00
let url = timelineUrls[timeline]
2017-06-12 16:00:46 +02:00
if (timeline === 'favorites' && userId) {
url = timelineUrls.publicFavorites(userId)
}
2019-03-08 00:50:58 +02:00
if (timeline === 'user' || timeline === 'media') {
url = url(userId)
}
2022-08-06 17:26:43 +03:00
if (timeline === 'list') {
url = url(listId)
}
if (timeline === 'quotes') {
url = url(statusId)
}
if (minId) {
params.push(['min_id', minId])
}
2016-10-28 14:26:51 +02:00
if (since) {
2017-06-12 16:20:02 +02:00
params.push(['since_id', since])
2016-10-28 14:26:51 +02:00
}
if (until) {
2017-06-12 16:20:02 +02:00
params.push(['max_id', until])
2017-06-12 16:00:46 +02:00
}
2017-09-17 14:26:35 +03:00
if (tag) {
url = url(tag)
2017-09-17 14:26:35 +03:00
}
if (timeline === 'media') {
params.push(['only_media', 1])
}
2019-03-07 20:16:35 +02:00
if (timeline === 'public') {
params.push(['local', true])
}
if (timeline === 'public' || timeline === 'publicAndExternal') {
params.push(['only_media', false])
}
if (timeline !== 'favorites' && timeline !== 'bookmarks') {
params.push(['with_muted', withMuted])
}
2020-06-30 17:02:38 +03:00
if (replyVisibility !== 'all') {
params.push(['reply_visibility', replyVisibility])
}
2025-06-29 15:24:04 +03:00
if (includeTypes.size > 0) {
2026-01-06 16:22:52 +02:00
includeTypes.forEach((type) => {
params.push(['include_types[]', type])
})
}
if (timeline === 'bookmarks' && bookmarkFolderId) {
params.push(['folder_id', bookmarkFolderId])
}
2016-10-28 14:26:51 +02:00
2020-04-13 15:26:55 +04:00
params.push(['limit', 20])
2026-01-06 16:22:52 +02:00
const queryString = map(params, (param) => `${param[0]}=${param[1]}`).join(
'&',
)
2017-06-12 16:00:46 +02:00
url += `?${queryString}`
2020-09-29 10:18:37 +00:00
2026-06-13 03:10:00 +03:00
return promisedRequest({
url,
credentials,
}).then(async (data) => {
const pagination = parseLinkHeaderPagination(
data._response.headers.get('Link'),
{
flakeId: timeline !== 'bookmarks' && timeline !== 'notifications',
},
)
2026-06-13 03:10:00 +03:00
return {
data: data.map(isNotifications ? parseNotification : parseStatus),
pagination,
}
})
2016-10-28 14:26:51 +02:00
}
2026-06-13 03:10:00 +03:00
export const fetchPinnedStatuses = ({ id, credentials }) =>
promisedRequest({
url: MASTODON_USER_TIMELINE_URL(id) + '?pinned=true',
credentials,
}).then((data) => data.map(parseStatus))
2026-06-13 03:10:00 +03:00
export const verifyCredentials = ({ credentials }) =>
promisedRequest({
url: MASTODON_LOGIN_URL,
credentials,
}).then((data) => (data.error ? data : parseUser(data)))
2016-10-28 14:26:51 +02:00
2026-06-13 03:10:00 +03:00
export const favorite = ({ id, credentials }) =>
promisedRequest({
2026-01-06 16:22:52 +02:00
url: MASTODON_FAVORITE_URL(id),
method: 'POST',
credentials,
}).then((data) => parseStatus(data))
2016-10-30 16:12:35 +01:00
2026-06-13 03:10:00 +03:00
export const unfavorite = ({ id, credentials }) =>
promisedRequest({
2026-01-06 16:22:52 +02:00
url: MASTODON_UNFAVORITE_URL(id),
method: 'POST',
credentials,
}).then((data) => parseStatus(data))
2016-10-30 16:12:35 +01:00
2026-06-13 03:10:00 +03:00
export const retweet = ({ id, credentials }) =>
promisedRequest({
2026-01-06 16:22:52 +02:00
url: MASTODON_RETWEET_URL(id),
method: 'POST',
credentials,
}).then((data) => parseStatus(data))
2026-06-13 03:10:00 +03:00
export const unretweet = ({ id, credentials }) =>
promisedRequest({
2026-01-06 16:22:52 +02:00
url: MASTODON_UNRETWEET_URL(id),
method: 'POST',
credentials,
}).then((data) => parseStatus(data))
2018-06-14 05:00:11 -04:00
2026-06-13 03:10:00 +03:00
export const bookmarkStatus = ({ id, credentials, ...options }) =>
promisedRequest({
url: MASTODON_BOOKMARK_STATUS_URL(id),
2026-06-13 03:10:00 +03:00
credentials,
method: 'POST',
payload: {
2026-01-06 16:22:52 +02:00
folder_id: options.folder_id,
},
})
2026-06-13 03:10:00 +03:00
export const unbookmarkStatus = ({ id, credentials }) =>
promisedRequest({
url: MASTODON_UNBOOKMARK_STATUS_URL(id),
2026-06-13 03:10:00 +03:00
credentials,
2026-01-06 16:22:52 +02:00
method: 'POST',
})
2026-06-13 03:10:00 +03:00
export const postStatus = ({
2019-06-18 20:28:31 +00:00
credentials,
status,
spoilerText,
visibility,
sensitive,
poll,
mediaIds = [],
inReplyToStatusId,
2023-07-12 21:34:19 -04:00
quoteId,
2020-06-28 12:16:41 +03:00
contentType,
2020-07-15 16:19:57 +03:00
preview,
2026-01-06 16:22:52 +02:00
idempotencyKey,
2019-06-18 20:28:31 +00:00
}) => {
2016-10-30 16:53:58 +01:00
const form = new FormData()
2019-06-18 20:28:31 +00:00
const pollOptions = poll.options || []
2016-10-30 16:53:58 +01:00
form.append('status', status)
form.append('source', 'Pleroma FE')
2018-06-07 05:03:50 -04:00
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)
2026-01-06 16:22:52 +02:00
mediaIds.forEach((val) => {
2019-03-17 23:22:54 -04:00
form.append('media_ids[]', val)
})
2026-01-06 16:22:52 +02:00
if (pollOptions.some((option) => option !== '')) {
2019-06-18 20:28:31 +00:00
const normalizedPoll = {
expires_in: parseInt(poll.expiresIn, 10),
2026-01-06 16:22:52 +02:00
multiple: poll.multiple,
2019-06-18 20:28:31 +00:00
}
2026-01-06 16:22:52 +02:00
Object.keys(normalizedPoll).forEach((key) => {
2019-06-18 20:28:31 +00:00
form.append(`poll[${key}]`, normalizedPoll[key])
})
2026-01-06 16:22:52 +02:00
pollOptions.forEach((option) => {
2019-06-18 20:28:31 +00:00
form.append('poll[options][]', option)
})
}
2016-10-30 16:53:58 +01:00
if (inReplyToStatusId) {
form.append('in_reply_to_id', inReplyToStatusId)
2016-10-30 16:53:58 +01:00
}
2023-07-12 21:34:19 -04:00
if (quoteId) {
form.append('quote_id', quoteId)
}
2020-06-28 12:16:41 +03:00
if (preview) {
form.append('preview', 'true')
}
2016-10-30 16:53:58 +01:00
2026-06-13 03:10:00 +03:00
const headers = {}
2020-07-15 16:19:57 +03:00
if (idempotencyKey) {
2026-06-13 03:10:00 +03:00
headers['idempotency-key'] = idempotencyKey
2020-07-15 16:19:57 +03:00
}
2026-06-13 03:10:00 +03:00
return promisedRequest({
url: MASTODON_POST_STATUS_URL,
formData: form,
2016-10-30 16:53:58 +01:00
method: 'POST',
2026-06-13 03:10:00 +03:00
credentials,
headers,
}).then((data) => (data.error ? data : parseStatus(data)))
2016-10-30 16:53:58 +01:00
}
2026-06-13 03:10:00 +03:00
export const editStatus = ({
2022-06-07 21:31:48 -06:00
id,
credentials,
status,
spoilerText,
sensitive,
poll,
mediaIds = [],
2026-01-06 16:22:52 +02:00
contentType,
2022-06-07 21:31:48 -06:00
}) => {
const form = new FormData()
const pollOptions = poll.options || []
form.append('status', status)
if (spoilerText) form.append('spoiler_text', spoilerText)
if (sensitive) form.append('sensitive', sensitive)
if (contentType) form.append('content_type', contentType)
2026-01-06 16:22:52 +02:00
mediaIds.forEach((val) => {
2022-06-07 21:31:48 -06:00
form.append('media_ids[]', val)
})
2026-01-06 16:22:52 +02:00
if (pollOptions.some((option) => option !== '')) {
2022-06-07 21:31:48 -06:00
const normalizedPoll = {
expires_in: parseInt(poll.expiresIn, 10),
2026-01-06 16:22:52 +02:00
multiple: poll.multiple,
2022-06-07 21:31:48 -06:00
}
2026-01-06 16:22:52 +02:00
Object.keys(normalizedPoll).forEach((key) => {
2022-06-07 21:31:48 -06:00
form.append(`poll[${key}]`, normalizedPoll[key])
})
2026-01-06 16:22:52 +02:00
pollOptions.forEach((option) => {
2022-06-07 21:31:48 -06:00
form.append('poll[options][]', option)
})
}
2026-06-13 03:10:00 +03:00
return promisedRequest({
url: MASTODON_STATUS_URL(id),
formData: form,
2022-06-07 21:31:48 -06:00
method: 'PUT',
2026-06-13 03:10:00 +03:00
credentials,
}).then((data) => (data.error ? data : parseStatus(data)))
2022-06-07 21:31:48 -06:00
}
2026-06-13 03:10:00 +03:00
export const deleteStatus = ({ id, credentials }) =>
promisedRequest({
2023-01-20 12:33:19 -05:00
url: MASTODON_DELETE_URL(id),
credentials,
2026-01-06 16:22:52 +02:00
method: 'DELETE',
})
2026-06-13 03:10:00 +03:00
export const uploadMedia = ({ formData, credentials }) =>
promisedRequest({
url: MASTODON_MEDIA_UPLOAD_URL,
formData,
2016-11-06 19:29:41 +01:00
method: 'POST',
2026-06-13 03:10:00 +03:00
credentials,
}).then((data) => parseAttachment(data))
2016-11-06 19:29:41 +01:00
2026-06-13 03:10:00 +03:00
export const setMediaDescription = ({ id, description, credentials }) =>
promisedRequest({
2020-07-07 09:07:20 +03:00
url: `${MASTODON_MEDIA_UPLOAD_URL}/${id}`,
method: 'PUT',
2026-06-13 03:10:00 +03:00
credentials,
2020-07-07 09:07:20 +03:00
payload: {
2026-01-06 16:22:52 +02:00
description,
},
2020-07-07 09:07:20 +03:00
}).then((data) => parseAttachment(data))
2026-06-13 03:10:00 +03:00
export const importMutes = ({ file, credentials }) => {
2020-09-07 14:27:37 +03:00
const formData = new FormData()
formData.append('list', file)
2026-06-13 03:10:00 +03:00
return promisedRequest({
url: MUTES_IMPORT_URL,
formData,
2020-09-07 14:27:37 +03:00
method: 'POST',
2026-06-13 03:10:00 +03:00
credentials,
2026-01-06 16:22:52 +02:00
}).then((response) => response.ok)
2020-09-07 14:27:37 +03:00
}
2026-06-13 03:10:00 +03:00
export const importBlocks = ({ file, credentials }) => {
2019-03-30 07:27:53 -04:00
const formData = new FormData()
formData.append('list', file)
2026-06-13 03:10:00 +03:00
return promisedRequest({
url: BLOCKS_IMPORT_URL,
formData,
2019-03-30 07:27:53 -04:00
method: 'POST',
2026-06-13 03:10:00 +03:00
credentials,
2026-01-06 16:22:52 +02:00
}).then((response) => response.ok)
2019-03-30 07:27:53 -04:00
}
2026-06-13 03:10:00 +03:00
export const importFollows = ({ file, credentials }) => {
const formData = new FormData()
formData.append('list', file)
2026-06-13 03:10:00 +03:00
return promisedRequest({
url: FOLLOW_IMPORT_URL,
formData,
2017-12-23 16:44:22 +02:00
method: 'POST',
2026-06-13 03:10:00 +03:00
credentials,
2026-01-06 16:22:52 +02:00
}).then((response) => response.ok)
2017-12-23 16:44:22 +02:00
}
2026-06-13 03:10:00 +03:00
export const deleteAccount = ({ credentials, password }) => {
const formData = new FormData()
2026-06-13 03:10:00 +03:00
formData.append('password', password)
2026-06-13 03:10:00 +03:00
return promisedRequest({
url: DELETE_ACCOUNT_URL,
formData,
method: 'POST',
2026-06-13 03:10:00 +03:00
credentials,
})
}
2026-06-13 03:10:00 +03:00
export const changeEmail = ({ credentials, email, password }) => {
2019-11-08 02:21:19 +00:00
const form = new FormData()
form.append('email', email)
form.append('password', password)
2026-06-13 03:10:00 +03:00
return promisedRequest({
url: CHANGE_EMAIL_URL,
formData: form,
2019-11-08 02:21:19 +00:00
method: 'POST',
2026-06-13 03:10:00 +03:00
credentials,
})
2019-11-08 02:21:19 +00:00
}
2026-06-13 03:10:00 +03:00
export const moveAccount = ({ credentials, password, targetAccount }) => {
const form = new FormData()
form.append('password', password)
form.append('target_account', targetAccount)
2026-06-13 03:10:00 +03:00
return promisedRequest({
url: MOVE_ACCOUNT_URL,
formData: form,
method: 'POST',
2026-06-13 03:10:00 +03:00
credentials,
})
}
2026-06-13 03:10:00 +03:00
export const addAlias = ({ credentials, alias }) =>
promisedRequest({
url: ALIASES_URL,
method: 'PUT',
credentials,
2026-01-06 16:22:52 +02:00
payload: { alias },
})
2026-06-13 03:10:00 +03:00
export const deleteAlias = ({ credentials, alias }) =>
promisedRequest({
url: ALIASES_URL,
method: 'DELETE',
credentials,
2026-01-06 16:22:52 +02:00
payload: { alias },
})
2026-06-13 03:10:00 +03:00
export const listAliases = ({ credentials }) =>
promisedRequest({
url: ALIASES_URL,
method: 'GET',
credentials,
params: {
2026-01-06 16:22:52 +02:00
_cacheBooster: new Date().getTime(),
},
})
2026-06-13 03:10:00 +03:00
export const changePassword = ({
2026-01-06 16:22:52 +02:00
credentials,
password,
newPassword,
newPasswordConfirmation,
}) => {
2018-05-21 23:01:09 +01:00
const form = new FormData()
form.append('password', password)
form.append('new_password', newPassword)
form.append('new_password_confirmation', newPasswordConfirmation)
2026-06-13 03:10:00 +03:00
return promisedRequest({
url: CHANGE_PASSWORD_URL,
formData: form,
2018-05-21 23:01:09 +01:00
method: 'POST',
2026-06-13 03:10:00 +03:00
credentials,
})
2018-05-21 23:01:09 +01:00
}
2026-06-13 03:10:00 +03:00
export const settingsMFA = ({ credentials }) =>
promisedRequest({
url: MFA_SETTINGS_URL,
credentials,
2026-01-06 16:22:52 +02:00
method: 'GET',
2026-06-13 03:10:00 +03:00
})
2026-06-13 03:10:00 +03:00
export const mfaDisableOTP = ({ credentials, password }) => {
const form = new FormData()
form.append('password', password)
2026-06-13 03:10:00 +03:00
return promisedRequest({
url: MFA_DISABLE_OTP_URL,
formData: form,
method: 'DELETE',
2026-06-13 03:10:00 +03:00
credentials,
})
}
2026-06-13 03:10:00 +03:00
export const mfaConfirmOTP = ({ credentials, password, token }) => {
const form = new FormData()
form.append('password', password)
form.append('code', token)
2026-06-13 03:10:00 +03:00
return promisedRequest({
url: MFA_CONFIRM_OTP_URL,
formData: form,
credentials,
2026-01-06 16:22:52 +02:00
method: 'POST',
2026-06-13 03:10:00 +03:00
})
}
2026-06-13 03:10:00 +03:00
export const mfaSetupOTP = ({ credentials }) =>
promisedRequest({
url: MFA_SETUP_OTP_URL,
credentials,
2026-01-06 16:22:52 +02:00
method: 'GET',
2026-06-13 03:10:00 +03:00
})
export const generateMfaBackupCodes = ({ credentials }) =>
promisedRequest({
url: MFA_BACKUP_CODES_URL,
credentials,
2026-01-06 16:22:52 +02:00
method: 'GET',
2026-06-13 03:10:00 +03:00
})
2026-06-13 03:10:00 +03:00
export const fetchMutes = ({ maxId, credentials }) => {
const query = new URLSearchParams({ with_relationships: true })
if (maxId) {
query.append('max_id', maxId)
}
2026-01-06 16:22:52 +02:00
return promisedRequest({
url: `${MASTODON_USER_MUTES_URL}?${query.toString()}`,
credentials,
}).then((users) => users.map(parseUser))
}
2026-06-13 03:10:00 +03:00
export const muteUser = ({ id, expiresIn, credentials }) => {
2022-04-30 11:08:19 -04:00
const payload = {}
if (expiresIn) {
2022-08-01 11:08:32 -04:00
payload.expires_in = expiresIn
2022-04-30 11:08:19 -04:00
}
return promisedRequest({
url: MASTODON_MUTE_USER_URL(id),
credentials,
method: 'POST',
2026-01-06 16:22:52 +02:00
payload,
})
}
2026-06-13 03:10:00 +03:00
export const unmuteUser = ({ id, credentials }) =>
promisedRequest({
2026-01-06 16:22:52 +02:00
url: MASTODON_UNMUTE_USER_URL(id),
credentials,
method: 'POST',
})
2019-02-13 12:05:23 -05:00
2026-06-13 03:10:00 +03:00
export const fetchBlocks = ({ maxId, credentials }) => {
const query = new URLSearchParams({ with_relationships: true })
if (maxId) {
query.append('max_id', maxId)
}
2026-01-06 16:22:52 +02:00
return promisedRequest({
url: `${MASTODON_USER_BLOCKS_URL}?${query.toString()}`,
credentials,
}).then((users) => users.map(parseUser))
2019-02-13 12:05:23 -05:00
}
2026-06-13 03:10:00 +03:00
export const addBackup = ({ credentials }) =>
promisedRequest({
2022-05-09 00:38:25 -04:00
url: PLEROMA_BACKUP_URL,
method: 'POST',
2026-01-06 16:22:52 +02:00
credentials,
2022-05-09 00:38:25 -04:00
})
2026-06-13 03:10:00 +03:00
export const listBackups = ({ credentials }) =>
promisedRequest({
2022-05-09 00:38:25 -04:00
url: PLEROMA_BACKUP_URL,
method: 'GET',
credentials,
params: {
2026-01-06 16:22:52 +02:00
_cacheBooster: new Date().getTime(),
},
2022-05-09 00:38:25 -04:00
})
2026-06-13 03:10:00 +03:00
export const fetchOAuthTokens = ({ credentials }) =>
promisedRequest({
url: '/api/oauth_tokens.json',
credentials,
2019-03-21 12:04:57 -04:00
})
2026-06-13 03:10:00 +03:00
export const revokeOAuthToken = ({ id, credentials }) =>
promisedRequest({
url: `/api/oauth_tokens/${id}`,
credentials,
2026-01-06 16:22:52 +02:00
method: 'DELETE',
})
2026-06-13 03:10:00 +03:00
export const suggestions = ({ credentials }) =>
promisedRequest({
url: SUGGESTIONS_URL,
credentials,
})
2018-08-02 18:34:12 +09:00
2026-06-13 03:10:00 +03:00
export const markNotificationsAsSeen = ({
id,
credentials,
single = false,
}) => {
const formData = new FormData()
if (single) {
2026-06-13 03:10:00 +03:00
formData.append('id', id)
} else {
2026-06-13 03:10:00 +03:00
formData.append('max_id', id)
}
2026-06-13 03:10:00 +03:00
return promisedRequest({
url: NOTIFICATION_READ_URL,
formData,
credentials,
2026-01-06 16:22:52 +02:00
method: 'POST',
2026-06-13 03:10:00 +03:00
})
}
2026-06-13 03:10:00 +03:00
export const vote = ({ pollId, choices, credentials }) => {
2019-06-18 20:28:31 +00:00
const form = new FormData()
form.append('choices', choices)
return promisedRequest({
url: MASTODON_VOTE_URL(encodeURIComponent(pollId)),
method: 'POST',
credentials,
payload: {
2026-01-06 16:22:52 +02:00
choices,
},
2019-06-18 20:28:31 +00:00
})
}
2026-06-13 03:10:00 +03:00
export const fetchPoll = ({ pollId, credentials }) =>
promisedRequest({
2026-01-06 16:22:52 +02:00
url: MASTODON_POLL_URL(encodeURIComponent(pollId)),
method: 'GET',
credentials,
})
2019-06-18 20:28:31 +00:00
2026-06-13 03:10:00 +03:00
export const fetchFavoritedByUsers = ({ id, credentials }) =>
promisedRequest({
url: MASTODON_STATUS_FAVORITEDBY_URL(id),
method: 'GET',
2026-01-06 16:22:52 +02:00
credentials,
}).then((users) => users.map(parseUser))
2019-04-01 22:29:45 -04:00
2026-06-13 03:10:00 +03:00
export const fetchRebloggedByUsers = ({ id, credentials }) =>
promisedRequest({
url: MASTODON_STATUS_REBLOGGEDBY_URL(id),
method: 'GET',
2026-01-06 16:22:52 +02:00
credentials,
}).then((users) => users.map(parseUser))
2019-04-01 22:29:45 -04:00
2026-06-13 03:10:00 +03:00
export const fetchEmojiReactions = ({ id, credentials }) =>
promisedRequest({
2026-01-06 16:22:52 +02:00
url: PLEROMA_EMOJI_REACTIONS_URL(id),
credentials,
}).then((reactions) =>
reactions.map((r) => {
r.accounts = r.accounts.map(parseUser)
return r
2026-01-06 16:22:52 +02:00
}),
)
2026-06-13 03:10:00 +03:00
export const reactWithEmoji = ({ id, emoji, credentials }) =>
promisedRequest({
url: PLEROMA_EMOJI_REACT_URL(id, emoji),
method: 'PUT',
2026-01-06 16:22:52 +02:00
credentials,
2020-01-27 18:43:26 +02:00
}).then(parseStatus)
2026-06-13 03:10:00 +03:00
export const unreactWithEmoji = ({ id, emoji, credentials }) =>
promisedRequest({
url: PLEROMA_EMOJI_UNREACT_URL(id, emoji),
method: 'DELETE',
2026-01-06 16:22:52 +02:00
credentials,
}).then(parseStatus)
2026-06-13 03:10:00 +03:00
export const reportUser = ({
credentials,
userId,
statusIds,
comment,
forward,
}) =>
promisedRequest({
url: MASTODON_REPORT_USER_URL,
2019-03-20 11:54:16 -04:00
method: 'POST',
payload: {
2022-07-31 12:35:48 +03:00
account_id: userId,
status_ids: statusIds,
2019-03-20 11:54:16 -04:00
comment,
2026-01-06 16:22:52 +02:00
forward,
2019-03-20 11:45:19 -04:00
},
2026-01-06 16:22:52 +02:00
credentials,
2019-03-20 11:54:16 -04:00
})
2019-03-20 11:45:19 -04:00
2026-06-13 03:10:00 +03:00
export const searchUsers = ({ credentials, query }) =>
promisedRequest({
url: MASTODON_USER_SEARCH_URL,
params: {
q: query,
2026-01-06 16:22:52 +02:00
resolve: true,
},
2026-01-06 16:22:52 +02:00
credentials,
}).then((data) => data.map(parseUser))
2026-06-13 03:10:00 +03:00
export const search2 = ({
2026-01-06 16:22:52 +02:00
credentials,
q,
resolve,
limit,
offset,
following,
type,
}) => {
2019-07-15 16:42:27 +00:00
let url = MASTODON_SEARCH_2
2022-07-31 12:35:48 +03:00
const params = []
2019-07-15 16:42:27 +00:00
if (q) {
params.push(['q', encodeURIComponent(q)])
}
if (resolve) {
params.push(['resolve', resolve])
}
if (limit) {
params.push(['limit', limit])
}
if (offset) {
params.push(['offset', offset])
}
if (following) {
params.push(['following', true])
}
2022-07-15 20:22:39 -04:00
if (type) {
params.push(['type', type])
2022-07-15 20:22:39 -04:00
}
2020-05-13 17:48:31 +03:00
params.push(['with_relationships', true])
2026-01-06 16:22:52 +02:00
const queryString = map(params, (param) => `${param[0]}=${param[1]}`).join(
'&',
)
2019-07-15 16:42:27 +00:00
url += `?${queryString}`
2026-06-13 03:10:00 +03:00
return promisedRequest({
url,
credentials,
})
2019-07-15 16:42:27 +00:00
.then((data) => {
if (data.ok) {
return data
}
throw new Error('Error fetching search result', data)
})
2026-01-06 16:22:52 +02:00
.then((data) => {
data.accounts = data.accounts.slice(0, limit).map((u) => parseUser(u))
data.statuses = data.statuses.slice(0, limit).map((s) => parseStatus(s))
2019-07-15 16:42:27 +00:00
return data
})
}
2026-06-13 03:10:00 +03:00
export const fetchKnownDomains = ({ credentials }) =>
promisedRequest({ url: MASTODON_KNOWN_DOMAIN_LIST_URL, credentials })
2026-06-13 03:10:00 +03:00
export const fetchDomainMutes = ({ credentials }) =>
promisedRequest({ url: MASTODON_DOMAIN_BLOCKS_URL, credentials })
2020-01-15 20:22:54 +00:00
2026-06-13 03:10:00 +03:00
export const muteDomain = ({ domain, credentials }) =>
promisedRequest({
2020-01-15 20:22:54 +00:00
url: MASTODON_DOMAIN_BLOCKS_URL,
method: 'POST',
payload: { domain },
2026-01-06 16:22:52 +02:00
credentials,
2020-01-15 20:22:54 +00:00
})
2026-06-13 03:10:00 +03:00
export const unmuteDomain = ({ domain, credentials }) =>
promisedRequest({
2020-01-15 20:22:54 +00:00
url: MASTODON_DOMAIN_BLOCKS_URL,
method: 'DELETE',
payload: { domain },
2026-01-06 16:22:52 +02:00
credentials,
2020-01-15 20:22:54 +00:00
})
2026-06-13 03:10:00 +03:00
export const dismissNotification = ({ credentials, id }) =>
promisedRequest({
url: MASTODON_DISMISS_NOTIFICATION_URL(id),
method: 'POST',
payload: { id },
2026-01-06 16:22:52 +02:00
credentials,
})
2026-06-13 03:10:00 +03:00
export const getAnnouncements = ({ credentials }) =>
promisedRequest({ url: MASTODON_ANNOUNCEMENTS_URL, credentials })
2022-03-17 14:01:45 -04:00
2026-06-13 03:10:00 +03:00
export const dismissAnnouncement = ({ id, credentials }) =>
promisedRequest({
2022-03-17 14:01:45 -04:00
url: MASTODON_ANNOUNCEMENTS_DISMISS_URL(id),
credentials,
2026-01-06 16:22:52 +02:00
method: 'POST',
2022-03-17 14:01:45 -04:00
})
2022-03-17 15:07:04 -04:00
2026-01-06 16:22:52 +02:00
export const getMastodonSocketURI = (
{ credentials, stream, args = {} },
base,
) => {
const url = new URL(MASTODON_STREAMING, base)
if (credentials) {
url.searchParams.append('access_token', credentials)
}
if (stream) {
url.searchParams.append('stream', stream)
}
Object.entries(args).forEach(([key, val]) => {
url.searchParams.append(key, val)
})
return url
2019-11-24 18:50:28 +02:00
}
const MASTODON_STREAMING_EVENTS = new Set([
'update',
'notification',
'delete',
2022-06-07 21:31:48 -06:00
'filters_changed',
2026-01-06 16:22:52 +02:00
'status.update',
2019-11-24 18:50:28 +02:00
])
2020-05-07 16:10:53 +03:00
const PLEROMA_STREAMING_EVENTS = new Set([
'pleroma:chat_update',
2026-01-06 16:22:52 +02:00
'pleroma:respond',
2020-05-07 16:10:53 +03:00
])
2019-12-08 19:18:38 +02:00
// A thin wrapper around WebSocket API that allows adding a pre-processor to it
// Uses EventTarget and a CustomEvent to proxy events
export const ProcessedWS = ({
url,
preprocessor = handleMastoWS,
id = 'Unknown',
2026-01-06 16:22:52 +02:00
credentials,
2019-12-08 19:18:38 +02:00
}) => {
const eventTarget = new EventTarget()
const socket = new WebSocket(url)
if (!socket) throw new Error(`Failed to create socket ${id}`)
2026-01-06 16:22:52 +02:00
const proxy = (original, eventName, processor = (a) => a) => {
2019-12-08 19:18:38 +02:00
original.addEventListener(eventName, (eventData) => {
2026-01-06 16:22:52 +02:00
eventTarget.dispatchEvent(
new CustomEvent(eventName, { detail: processor(eventData) }),
)
2019-12-08 19:18:38 +02:00
})
}
socket.addEventListener('open', (wsEvent) => {
console.debug(`[WS][${id}] Socket connected`, wsEvent)
if (credentials) {
2026-01-06 16:22:52 +02:00
socket.send(
JSON.stringify({
type: 'pleroma:authenticate',
token: credentials,
}),
)
}
2019-12-08 19:18:38 +02:00
})
socket.addEventListener('error', (wsEvent) => {
console.debug(`[WS][${id}] Socket errored`, wsEvent)
})
socket.addEventListener('close', (wsEvent) => {
console.debug(
`[WS][${id}] Socket disconnected with code ${wsEvent.code}`,
2026-01-06 16:22:52 +02:00
wsEvent,
2019-12-08 19:18:38 +02:00
)
})
2019-12-10 21:30:27 +02:00
// Commented code reason: very spammy, uncomment to enable message debug logging
/*
2019-12-08 19:18:38 +02:00
socket.addEventListener('message', (wsEvent) => {
console.debug(
`[WS][${id}] Message received`,
wsEvent
)
})
2019-12-10 21:30:27 +02:00
/**/
2019-12-08 19:18:38 +02:00
const onAuthenticated = () => {
eventTarget.dispatchEvent(new CustomEvent('pleroma:authenticated'))
}
2019-12-08 19:18:38 +02:00
proxy(socket, 'open')
proxy(socket, 'close')
proxy(socket, 'message', (event) => preprocessor(event, { onAuthenticated }))
2019-12-08 19:18:38 +02:00
proxy(socket, 'error')
2019-12-10 21:30:27 +02:00
// 1000 = Normal Closure
2026-01-06 16:22:52 +02:00
eventTarget.close = () => {
socket.close(1000, 'Shutting down socket')
}
eventTarget.getState = () => socket.readyState
eventTarget.subscribe = (stream, args = {}) => {
2026-01-06 16:22:52 +02:00
console.debug(`[WS][${id}] Subscribing to stream ${stream} with args`, args)
socket.send(
JSON.stringify({
type: 'subscribe',
stream,
...args,
}),
)
}
eventTarget.unsubscribe = (stream, args = {}) => {
console.debug(
`[WS][${id}] Unsubscribing from stream ${stream} with args`,
2026-01-06 16:22:52 +02:00
args,
)
socket.send(
JSON.stringify({
type: 'unsubscribe',
stream,
...args,
}),
)
}
2019-12-10 21:30:27 +02:00
2019-12-08 19:18:38 +02:00
return eventTarget
}
2026-01-06 17:32:22 +02:00
export const handleMastoWS = (
wsEvent,
{
onAuthenticated = () => {
/* no-op */
},
} = {},
) => {
2019-11-24 18:50:28 +02:00
const { data } = wsEvent
if (!data) return
const parsedEvent = JSON.parse(data)
const { event, payload } = parsedEvent
2026-01-06 16:22:52 +02:00
if (
MASTODON_STREAMING_EVENTS.has(event) ||
PLEROMA_STREAMING_EVENTS.has(event)
) {
2019-12-11 18:20:43 +02:00
// MastoBE and PleromaBE both send payload for delete as a PLAIN string
if (event === 'delete') {
return { event, id: payload }
}
2019-11-24 18:50:28 +02:00
const data = payload ? JSON.parse(payload) : null
if (event === 'pleroma:respond') {
if (data.type === 'pleroma:authenticate') {
if (data.result === 'success') {
console.debug('[WS] Successfully authenticated')
onAuthenticated()
} else {
console.error('[WS] Unable to authenticate:', data.error)
wsEvent.target.close()
}
}
return null
} else if (event === 'update') {
2019-11-24 18:50:28 +02:00
return { event, status: parseStatus(data) }
2022-06-07 21:31:48 -06:00
} else if (event === 'status.update') {
return { event, status: parseStatus(data) }
2019-11-24 18:50:28 +02:00
} else if (event === 'notification') {
return { event, notification: parseNotification(data) }
2020-05-07 16:10:53 +03:00
} else if (event === 'pleroma:chat_update') {
return { event, chatUpdate: parseChat(data) }
2019-11-24 18:50:28 +02:00
}
} else {
console.warn('Unknown event', wsEvent)
return null
}
}
2020-05-07 16:10:53 +03:00
export const WSConnectionStatus = Object.freeze({
2022-07-31 12:35:48 +03:00
JOINED: 1,
CLOSED: 2,
ERROR: 3,
DISABLED: 4,
STARTING: 5,
2026-01-06 16:22:52 +02:00
STARTING_INITIAL: 6,
2020-05-07 16:10:53 +03:00
})
2026-06-13 03:10:00 +03:00
export const chats = ({ credentials }) =>
promisedRequest({
url: PLEROMA_CHATS_URL,
credentials,
2026-06-13 23:32:08 +03:00
}).then((data) => ({
chatList: data.map(parseChat).filter((c) => c),
}))
2020-05-07 16:10:53 +03:00
2026-06-13 03:10:00 +03:00
export const getOrCreateChat = ({ accountId, credentials }) =>
promisedRequest({
2020-05-07 16:10:53 +03:00
url: PLEROMA_CHAT_URL(accountId),
method: 'POST',
2026-01-06 16:22:52 +02:00
credentials,
2020-05-07 16:10:53 +03:00
})
2026-06-13 03:10:00 +03:00
export const chatMessages = ({
id,
credentials,
maxId,
sinceId,
limit = 20,
}) => {
2020-05-07 16:10:53 +03:00
let url = PLEROMA_CHAT_MESSAGES_URL(id)
const args = [
maxId && `max_id=${maxId}`,
sinceId && `since_id=${sinceId}`,
2026-01-06 16:22:52 +02:00
limit && `limit=${limit}`,
]
.filter((_) => _)
.join('&')
2020-05-07 16:10:53 +03:00
url = url + (args ? '?' + args : '')
return promisedRequest({
url,
method: 'GET',
2026-01-06 16:22:52 +02:00
credentials,
2020-05-07 16:10:53 +03:00
})
}
2026-06-13 03:10:00 +03:00
export const sendChatMessage = ({
2026-01-06 16:22:52 +02:00
id,
content,
mediaId = null,
idempotencyKey,
credentials,
}) => {
2020-05-07 16:10:53 +03:00
const payload = {
2026-01-06 16:22:52 +02:00
content,
2020-05-07 16:10:53 +03:00
}
if (mediaId) {
2022-07-31 12:35:48 +03:00
payload.media_id = mediaId
2020-05-07 16:10:53 +03:00
}
2020-10-29 13:33:06 +03:00
const headers = {}
if (idempotencyKey) {
headers['idempotency-key'] = idempotencyKey
}
2020-05-07 16:10:53 +03:00
return promisedRequest({
url: PLEROMA_CHAT_MESSAGES_URL(id),
method: 'POST',
2022-07-31 12:35:48 +03:00
payload,
2020-10-29 13:33:06 +03:00
credentials,
2026-01-06 16:22:52 +02:00
headers,
2020-05-07 16:10:53 +03:00
})
}
2026-06-13 03:10:00 +03:00
export const readChat = ({ id, lastReadId, credentials }) =>
promisedRequest({
2020-05-07 16:10:53 +03:00
url: PLEROMA_CHAT_READ_URL(id),
method: 'POST',
payload: {
2026-01-06 16:22:52 +02:00
last_read_id: lastReadId,
2020-05-07 16:10:53 +03:00
},
2026-01-06 16:22:52 +02:00
credentials,
2020-05-07 16:10:53 +03:00
})
2026-06-13 03:10:00 +03:00
export const deleteChatMessage = ({ chatId, messageId, credentials }) =>
promisedRequest({
2020-05-07 16:10:53 +03:00
url: PLEROMA_DELETE_CHAT_MESSAGE_URL(chatId, messageId),
method: 'DELETE',
2026-01-06 16:22:52 +02:00
credentials,
2020-05-07 16:10:53 +03:00
})
2026-06-13 03:10:00 +03:00
export const fetchScrobbles = ({ accountId, limit = 1 }) => {
let url = PLEROMA_SCROBBLES_URL(accountId)
const params = [['limit', limit]]
2026-01-06 16:22:52 +02:00
const queryString = map(params, (param) => `${param[0]}=${param[1]}`).join(
'&',
)
url += `?${queryString}`
2026-06-13 03:10:00 +03:00
return promisedRequest({ url })
}
2026-06-13 03:10:00 +03:00
export const fetchBookmarkFolders = ({ credentials }) =>
promisedRequest({
url: PLEROMA_BOOKMARK_FOLDERS_URL,
credentials,
2026-01-06 16:22:52 +02:00
})
2026-06-13 03:10:00 +03:00
export const createBookmarkFolder = ({ name, emoji, credentials }) =>
promisedRequest({
url: PLEROMA_BOOKMARK_FOLDERS_URL,
credentials,
2026-01-06 16:22:52 +02:00
method: 'POST',
2026-06-13 03:10:00 +03:00
payload: { name, emoji },
2026-01-06 16:22:52 +02:00
})
2026-06-13 03:10:00 +03:00
export const updateBookmarkFolder = ({ folderId, name, emoji, credentials }) =>
promisedRequest({
url: PLEROMA_BOOKMARK_FOLDER_URL(folderId),
credentials,
2026-01-06 16:22:52 +02:00
method: 'PATCH',
2026-06-13 03:10:00 +03:00
payload: { name, emoji },
2026-01-06 16:22:52 +02:00
})
2026-06-13 03:10:00 +03:00
export const deleteBookmarkFolder = ({ folderId, credentials }) =>
promisedRequest({
url: PLEROMA_BOOKMARK_FOLDER_URL(folderId),
method: 'DELETE',
2026-06-13 03:10:00 +03:00
credentials,
})