pleroma-fe/src/api/public.js

487 lines
13 KiB
JavaScript
Raw Normal View History

import { paramsString, promisedRequest } from './helpers.js'
2026-01-06 16:22:52 +02:00
import {
parseLinkHeaderPagination,
2026-01-06 16:23:17 +02:00
parseNotification,
parseSource,
parseStatus,
parseUser,
2026-06-17 14:36:45 +03:00
} from 'src/services/entity_normalizer/entity_normalizer.service.js'
2026-06-13 03:10:00 +03:00
import { RegistrationError, StatusCodeError } from 'src/services/errors/errors'
2019-06-18 20:28:31 +00:00
2026-06-19 14:43:09 +03:00
const MASTODON_SUGGESTIONS_URL = '/api/v1/suggestions'
2019-05-21 23:35:40 +03:00
const MASTODON_LOGIN_URL = '/api/v1/accounts/verify_credentials'
const MASTODON_REGISTRATION_URL = '/api/v1/accounts'
2026-06-17 19:09:55 +03:00
const MASTODON_PASSWORD_RESET_URL = ({ email }) =>
`/auth/password${paramsString({ email })}`
2026-06-19 15:50:58 +03:00
const MASTODON_USER_NOTIFICATIONS_URL = ({
minId,
sinceId,
maxId,
limit,
includeTypes,
replyVisibility,
}) =>
2026-06-19 14:43:09 +03:00
`/api/v1/notifications${paramsString({ minId, sinceId, maxId, limit, includeTypes, replyVisibility })}`
const MASTODON_FOLLOWING_URL = (
id,
{ minId, maxId, sinceId, limit, withRelationships },
) =>
`/api/v1/accounts/${id}/following${paramsString({ minId, maxId, sinceId, limit, withRelationships })}`
const MASTODON_FOLLOWERS_URL = (
id,
{ minId, maxId, sinceId, limit, withRelationships },
) =>
`/api/v1/accounts/${id}/followers${paramsString({ minId, maxId, sinceId, limit, withRelationships })}`
2026-06-19 14:43:09 +03:00
2026-06-19 15:50:58 +03:00
const MASTODON_USER_HOME_TIMELINE_URL = ({
minId,
sinceId,
maxId,
limit,
replyVisibility,
}) =>
2026-06-19 14:43:09 +03:00
`/api/v1/timelines/home${paramsString({ minId, sinceId, maxId, limit, replyVisibility })}`
2026-06-19 15:50:58 +03:00
const MASTODON_LIST_TIMELINE_URL = (
id,
{ minId, sinceId, maxId, limit, replyVisibility },
) =>
2026-06-19 14:43:09 +03:00
`/api/v1/timelines/list/${id}${paramsString({ minId, sinceId, maxId, limit, replyVisibility })}`
2026-06-19 15:50:58 +03:00
const MASTODON_DIRECT_MESSAGES_TIMELINE_URL = ({
minId,
sinceId,
maxId,
limit,
replyVisibility,
}) =>
2026-06-19 14:43:09 +03:00
`/api/v1/timelines/direct${paramsString({ minId, sinceId, maxId, limit, replyVisibility })}`
2026-06-19 15:50:58 +03:00
const MASTODON_PUBLIC_TIMELINE = ({
minId,
sinceId,
maxId,
limit,
replyVisibility,
local,
remote,
onlyMedia,
}) =>
2026-06-19 14:43:09 +03:00
`/api/v1/timelines/public${paramsString({ minId, sinceId, maxId, limit, replyVisibility, local, remote, onlyMedia })}`
2026-06-19 15:50:58 +03:00
const MASTODON_TAG_TIMELINE_URL = (
tag,
{ minId, sinceId, maxId, limit, replyVisibility },
) =>
2026-06-19 14:43:09 +03:00
`/api/v1/timelines/tag/${tag}${paramsString({ minId, sinceId, maxId, limit, replyVisibility })}`
2026-06-19 15:50:58 +03:00
const MASTODON_USER_TIMELINE_URL = (
id,
{ minId, sinceId, maxId, limit, replyVisibility, pinned, onlyMedia },
) =>
2026-06-19 14:43:09 +03:00
`/api/v1/accounts/${id}/statuses${paramsString({ minId, sinceId, maxId, limit, replyVisibility, pinned, onlyMedia })}`
2026-06-19 15:50:58 +03:00
const MASTODON_USER_FAVORITES_TIMELINE_URL = ({
minId,
sinceId,
maxId,
limit,
replyVisibility,
}) =>
2026-06-19 14:43:09 +03:00
`/api/v1/favourites${paramsString({ minId, sinceId, maxId, limit, replyVisibility })}`
2026-06-19 15:50:58 +03:00
const MASTODON_BOOKMARK_TIMELINE_URL = ({
minId,
sinceId,
maxId,
limit,
replyVisibility,
folderId,
}) =>
2026-06-19 14:43:09 +03:00
`/api/v1/bookmarks${paramsString({ minId, sinceId, maxId, limit, replyVisibility, folderId })}`
2026-06-19 15:50:58 +03:00
const PLEROMA_STATUS_QUOTES_URL = (
id,
{ minId, sinceId, maxId, limit, replyVisibility },
) =>
2026-06-19 14:43:09 +03:00
`/api/v1/pleroma/statuses/${id}/quotes${paramsString({ minId, sinceId, maxId, limit, replyVisibility })}`
2026-06-19 15:50:58 +03:00
const PLEROMA_USER_FAVORITES_TIMELINE_URL = (
id,
{ minId, sinceId, maxId, limit, replyVisibility },
) =>
2026-06-19 14:43:09 +03:00
`/api/v1/pleroma/accounts/${id}/favourites${paramsString({ minId, sinceId, maxId, limit, replyVisibility })}`
2026-06-19 15:50:58 +03:00
const AKKOMA_BUBBLE_TIMELINE_URL = ({
minId,
sinceId,
maxId,
limit,
replyVisibility,
}) =>
2026-06-19 14:43:09 +03:00
`/api/v1/timelines/bubble${paramsString({ minId, sinceId, maxId, limit, replyVisibility })}`
export const MASTODON_STATUS_URL = (id) => `/api/v1/statuses/${id}`
2026-01-06 16:22:52 +02:00
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'
2026-06-22 20:54:34 +03:00
const MASTODON_USER_LOOKUP_URL = ({ acct }) =>
`/api/v1/accounts/lookup${paramsString({ acct })}`
2026-06-19 14:43:09 +03:00
const MASTODON_POLL_URL = (id = '') => `/api/v1/polls/${id}`
2026-01-06 16:22:52 +02:00
const MASTODON_STATUS_FAVORITEDBY_URL = (id) =>
`/api/v1/statuses/${id}/favourited_by`
const MASTODON_STATUS_REBLOGGEDBY_URL = (id) =>
`/api/v1/statuses/${id}/reblogged_by`
2026-06-19 15:50:58 +03:00
const MASTODON_SEARCH_2 = ({
q,
resolve,
limit,
offset,
following,
type,
withRelationships,
accountId,
excludeUnreviewed,
}) =>
2026-06-19 14:43:09 +03:00
`/api/v2/search${paramsString({ q, resolve, limit, offset, following, type, withRelationships, accountId, excludeUnreviewed })}`
2026-06-19 15:50:58 +03:00
const MASTODON_USER_SEARCH_URL = ({ q, resolve }) =>
`/api/v1/accounts/search${paramsString({ q, resolve })}`
const MASTODON_KNOWN_DOMAIN_LIST_URL = '/api/v1/instance/peers'
2026-01-06 16:22:52 +02:00
const PLEROMA_EMOJI_REACTIONS_URL = (id) =>
`/api/v1/pleroma/statuses/${id}/reactions`
const PLEROMA_SCROBBLES_URL = (id, { maxId, sinceId, minId, limit, offset }) =>
`/api/v1/pleroma/accounts/${id}/scrobbles${paramsString({ maxId, sinceId, minId, limit, offset })}`
2026-06-15 20:02:22 +03:00
const EMOJI_PACKS_URL = (page, pageSize) =>
`/api/v1/pleroma/emoji/packs${paramsString({ page, pageSize })}`
2026-06-15 20:02:22 +03:00
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 fetchUser = ({ id, credentials }) =>
promisedRequest({
url: `${MASTODON_USER_URL}/${id}`,
credentials,
}).then(({ data, ...rest }) => ({ ...rest, data: parseUser(data) }))
2026-06-13 03:10:00 +03:00
export const fetchUserByName = ({ name, credentials }) =>
promisedRequest({
2026-06-22 19:53:36 +03:00
url: MASTODON_USER_LOOKUP_URL({ acct: name }),
credentials,
})
2026-06-17 18:15:41 +03:00
.then(({ data }) => data.id)
2026-01-06 16:22:52 +02:00
.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 }))
export const fetchFriends = ({ id, maxId, sinceId, limit = 20, credentials }) =>
promisedRequest({
url: MASTODON_FOLLOWING_URL(id, { maxId, sinceId, limit }),
2026-06-13 03:10:00 +03:00
credentials,
}).then(({ data, ...rest }) => ({ ...rest, data: data.map(parseUser) }))
2026-06-13 03:10:00 +03:00
export const fetchFollowers = ({
id,
maxId,
sinceId,
limit = 20,
credentials,
}) =>
promisedRequest({
url: MASTODON_FOLLOWERS_URL(id, {
maxId,
sinceId,
limit,
withRelationships: true,
}),
2026-06-13 03:10:00 +03:00
credentials,
}).then(({ data, ...rest }) => ({ ...rest, data: data.map(parseUser) }))
2018-06-06 22:26:24 +00:00
2026-06-13 03:10:00 +03:00
export const fetchConversation = ({ id, credentials }) =>
promisedRequest({
url: MASTODON_STATUS_CONTEXT_URL(id),
credentials,
})
.then((result) => ({
...result,
data: {
...result.data,
ancestors: result.data.ancestors.map(parseStatus),
descendants: result.data.descendants.map(parseStatus),
},
}))
2026-06-16 16:37:47 +03:00
.catch((error) => {
throw new Error('Error fetching timeline', error)
})
2026-06-13 03:10:00 +03:00
export const fetchStatus = ({ id, credentials }) =>
promisedRequest({
url: MASTODON_STATUS_URL(id),
credentials,
})
.then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) }))
2026-06-16 16:37:47 +03:00
.catch((error) => {
throw new Error('Error fetching timeline', error)
})
2026-06-13 03:10:00 +03:00
export const fetchStatusSource = ({ id, credentials }) =>
promisedRequest({
url: MASTODON_STATUS_SOURCE_URL(id),
credentials,
})
.then(({ data, ...rest }) => ({ ...rest, data: parseSource(data) }))
2026-06-16 16:37:47 +03:00
.catch((error) => {
throw new Error('Error fetching timeline', error)
})
2022-06-07 21:31:48 -06:00
2026-06-13 03:10:00 +03:00
export const fetchStatusHistory = ({ status, credentials }) =>
promisedRequest({
url: MASTODON_STATUS_HISTORY_URL(status.id),
credentials,
2026-06-22 15:25:37 +03:00
}).then(({ data, ...rest }) => {
return [...data].reverse().map((item) => {
2026-01-06 16:22:52 +02:00
item.originalStatus = status
2026-06-22 15:25:37 +03:00
return { ...rest, data: 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,
sinceId,
minId,
maxId,
userId,
listId,
statusId,
tag,
withMuted,
replyVisibility = 'all',
includeTypes = [],
bookmarkFolderId,
2019-06-18 20:28:31 +00:00
}) => {
2016-10-28 14:26:51 +02:00
const timelineUrls = {
friends: MASTODON_USER_HOME_TIMELINE_URL,
2026-06-19 14:43:09 +03:00
public: MASTODON_PUBLIC_TIMELINE,
2022-07-31 12:35:48 +03:00
publicAndExternal: MASTODON_PUBLIC_TIMELINE,
2026-06-19 14:43:09 +03:00
dms: MASTODON_DIRECT_MESSAGES_TIMELINE_URL,
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,
bookmarks: MASTODON_BOOKMARK_TIMELINE_URL,
2026-01-06 16:22:52 +02:00
bubble: AKKOMA_BUBBLE_TIMELINE_URL,
2026-06-19 14:43:09 +03:00
tag: MASTODON_TAG_TIMELINE_URL,
quotes: PLEROMA_STATUS_QUOTES_URL,
notifications: MASTODON_USER_NOTIFICATIONS_URL,
2016-10-28 14:26:51 +02:00
}
2026-06-22 20:15:35 +03:00
const urlFunc = timelineUrls[timeline]
2026-06-19 14:43:09 +03:00
const twoArgs = new Set([
'user',
'media',
'list',
'publicFavorites',
'tag',
2026-06-19 15:50:58 +03:00
'quotes',
2026-06-19 14:43:09 +03:00
])
const params = {
minId,
sinceId,
maxId,
limit: 20,
}
2016-10-28 14:26:51 +02:00
2026-06-19 14:43:09 +03:00
const id = (() => {
switch (timeline) {
case 'user':
case 'media':
return userId
case 'list':
return listId
case 'quotes':
return statusId
case 'tag':
return tag
}
})()
2022-08-06 17:26:43 +03:00
2026-06-19 14:43:09 +03:00
const isNotifications = timeline === 'notifications'
if (timeline === 'media') {
2026-06-19 14:43:09 +03:00
params.onlyMedia = true
}
2019-03-07 20:16:35 +02:00
if (timeline === 'public') {
params.local = true
2019-03-07 20:16:35 +02:00
}
if (timeline !== 'favorites' && timeline !== 'bookmarks') {
params.withMuted = withMuted
}
2020-06-30 17:02:38 +03:00
if (replyVisibility !== 'all') {
params.replyVisibility = replyVisibility
2020-06-30 17:02:38 +03:00
}
if (timeline === 'bookmarks' && bookmarkFolderId) {
params.folderId = bookmarkFolderId
}
2016-10-28 14:26:51 +02:00
2026-06-22 20:15:35 +03:00
if (isNotifications && includeTypes.length > 0) {
2026-06-19 14:43:09 +03:00
params.includeTypes = includeTypes
}
const url = twoArgs.has(timeline) ? urlFunc(id, params) : urlFunc(params)
return promisedRequest({ url, credentials }).then((result) => {
2026-06-13 03:10:00 +03:00
const pagination = parseLinkHeaderPagination(
result.response.headers.get('Link'),
2026-06-13 03:10:00 +03:00
{
flakeId: timeline !== 'bookmarks' && timeline !== 'notifications',
},
)
2026-06-13 03:10:00 +03:00
return {
...result,
data: result.data.map(isNotifications ? parseNotification : parseStatus),
2026-06-13 03:10:00 +03:00
pagination,
}
})
2016-10-28 14:26:51 +02:00
}
2026-06-15 20:02:22 +03:00
export const listEmojiPacks = ({ page, pageSize, credentials }) =>
promisedRequest({
url: EMOJI_PACKS_URL(page, pageSize),
})
2026-06-13 03:10:00 +03:00
export const fetchPinnedStatuses = ({ id, credentials }) =>
promisedRequest({
2026-06-19 14:43:09 +03:00
url: MASTODON_USER_TIMELINE_URL(id, { pinned: true }),
2026-06-13 03:10:00 +03:00
credentials,
}).then(({ data, ...rest }) => ({ ...rest, data: data.map(parseStatus) }))
2026-06-13 03:10:00 +03:00
export const verifyCredentials = ({ credentials }) =>
promisedRequest({
url: MASTODON_LOGIN_URL,
credentials,
}).then(({ data, ...rest }) => ({ ...rest, data: parseUser(data) }))
2016-10-28 14:26:51 +02:00
2026-06-17 19:45:26 +03:00
export const resetPassword = ({ email }) => {
2026-06-17 19:09:55 +03:00
return promisedRequest({
url: MASTODON_PASSWORD_RESET_URL({ email }),
method: 'POST',
})
}
2026-06-13 03:10:00 +03:00
export const suggestions = ({ credentials }) =>
promisedRequest({
2026-06-19 14:43:09 +03:00
url: MASTODON_SUGGESTIONS_URL,
2026-06-13 03:10:00 +03:00
credentials,
})
2018-08-02 18:34:12 +09: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,
2026-06-18 20:53:21 +03:00
}).then(({ data, ...rest }) => ({ ...rest, data: data.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,
2026-06-18 20:53:21 +03:00
}).then(({ data, ...rest }) => ({ ...rest, data: data.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(({ data, ...rest }) => ({
...rest,
data: data.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 searchUsers = ({ credentials, query }) =>
promisedRequest({
2026-06-19 14:59:16 +03:00
url: MASTODON_USER_SEARCH_URL({ q: query, resolve: true }),
2026-01-06 16:22:52 +02:00
credentials,
}).then(({ data, ...rest }) => ({ ...rest, 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,
}) => {
2026-06-13 03:10:00 +03:00
return promisedRequest({
2026-06-19 14:43:09 +03:00
url: MASTODON_SEARCH_2({
q,
resolve,
limit,
offset,
following,
type,
withRelationships: true,
}),
2026-06-13 03:10:00 +03:00
credentials,
})
.then(({ data, ...rest }) => {
2026-01-06 16:22:52 +02:00
data.accounts = data.accounts.slice(0, limit).map((u) => parseUser(u))
data.statuses = data.statuses.slice(0, limit).map((s) => parseStatus(s))
return { ...rest, data }
2019-07-15 16:42:27 +00:00
})
2026-06-16 16:37:47 +03:00
.catch((error) => {
throw new Error('Error fetching timeline', error)
})
}
2026-06-13 03:10:00 +03:00
export const fetchKnownDomains = ({ credentials }) =>
promisedRequest({ url: MASTODON_KNOWN_DOMAIN_LIST_URL, credentials })
export const fetchScrobbles = ({ accountId, limit = 1 }) =>
promisedRequest({
url: PLEROMA_SCROBBLES_URL(accountId, { limit }),
})