2026-06-08 00:57:42 +03:00
|
|
|
import { concat, each, last, map } from 'lodash'
|
2026-01-08 17:26:52 +02:00
|
|
|
|
2026-06-17 17:58:14 +03:00
|
|
|
import { paramsString, promisedRequest } from './helpers.js'
|
|
|
|
|
|
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-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
|
|
|
|
2018-08-02 18:34:12 +09:00
|
|
|
const SUGGESTIONS_URL = '/api/v1/suggestions'
|
2019-05-21 23:35:40 +03:00
|
|
|
const MASTODON_LOGIN_URL = '/api/v1/accounts/verify_credentials'
|
2019-05-22 19:13:41 +03:00
|
|
|
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 })}`
|
|
|
|
|
|
2019-01-12 23:33:45 +03:00
|
|
|
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-06-16 23:14:52 +03:00
|
|
|
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-17 14:26:41 +03:00
|
|
|
const MASTODON_USER_HOME_TIMELINE_URL = '/api/v1/timelines/home'
|
|
|
|
|
const MASTODON_LIST_TIMELINE_URL = (id) => `/api/v1/timelines/list/${id}`
|
|
|
|
|
const MASTODON_BOOKMARK_TIMELINE_URL = '/api/v1/bookmarks'
|
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'
|
2026-06-17 14:26:41 +03:00
|
|
|
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`
|
2019-03-08 22:40:57 +02:00
|
|
|
const MASTODON_USER_URL = '/api/v1/accounts'
|
2022-08-09 22:11:55 -04:00
|
|
|
const MASTODON_USER_LOOKUP_URL = '/api/v1/accounts/lookup'
|
2026-01-06 16:22:52 +02:00
|
|
|
const MASTODON_USER_TIMELINE_URL = (id) => `/api/v1/accounts/${id}/statuses`
|
|
|
|
|
const MASTODON_TAG_TIMELINE_URL = (tag) => `/api/v1/timelines/tag/${tag}`
|
2025-06-18 17:48:11 +03:00
|
|
|
const AKKOMA_BUBBLE_TIMELINE_URL = '/api/v1/timelines/bubble'
|
2026-01-06 16:22:52 +02:00
|
|
|
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`
|
2022-07-31 12:35:48 +03:00
|
|
|
const MASTODON_SEARCH_2 = '/api/v2/search'
|
2019-07-18 17:22:51 +03:00
|
|
|
const MASTODON_USER_SEARCH_URL = '/api/v1/accounts/search'
|
2019-11-24 18:50:28 +02:00
|
|
|
const MASTODON_STREAMING = '/api/v1/streaming'
|
2020-05-10 12:54:55 +02:00
|
|
|
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`
|
2026-06-16 23:14:52 +03:00
|
|
|
const PLEROMA_SCROBBLES_URL = (id, { maxId, sinceId, minId, limit, offset }) =>
|
|
|
|
|
`/api/v1/pleroma/accounts/${id}/scrobbles${paramsString({ maxId, sinceId, minId, limit, offset })}`
|
2026-01-06 16:22:52 +02:00
|
|
|
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`
|
2026-06-15 20:02:22 +03:00
|
|
|
|
|
|
|
|
const EMOJI_PACKS_URL = (page, pageSize) =>
|
2026-06-16 23:14:52 +03:00
|
|
|
`/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
|
2022-05-22 16:40:59 +00:00
|
|
|
// language
|
2026-06-13 03:10:00 +03:00
|
|
|
export const register = ({ params, credentials }) => {
|
2019-05-22 19:13:41 +03:00
|
|
|
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: {
|
2019-05-22 19:13:41 +03:00
|
|
|
nickname,
|
2019-06-13 00:47:06 +03:00
|
|
|
locale: 'en_US',
|
2019-05-22 19:13:41 +03:00
|
|
|
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,
|
2026-06-17 17:58:14 +03:00
|
|
|
}).then(({ data, ...rest }) => ({ ...rest, data: parseUser(data) }))
|
2017-11-14 18:08:03 +02:00
|
|
|
|
2026-06-13 03:10:00 +03:00
|
|
|
export const fetchUserByName = ({ name, credentials }) =>
|
|
|
|
|
promisedRequest({
|
2022-08-09 22:11:55 -04:00
|
|
|
url: MASTODON_USER_LOOKUP_URL,
|
|
|
|
|
credentials,
|
2026-01-06 16:22:52 +02:00
|
|
|
params: { acct: name },
|
2022-08-09 22:11:55 -04:00
|
|
|
})
|
2026-06-17 18:15:41 +03:00
|
|
|
.then(({ data }) => data.id)
|
2026-01-06 16:22:52 +02:00
|
|
|
.catch((error) => {
|
2022-08-09 22:11:55 -04:00
|
|
|
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 }))
|
2022-08-09 22:11:55 -04:00
|
|
|
|
2026-06-16 23:14:52 +03:00
|
|
|
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,
|
2026-06-17 17:58:14 +03:00
|
|
|
}).then(({ data, ...rest }) => ({ ...rest, data: data.map(parseUser) }))
|
2019-02-07 09:57:16 -05:00
|
|
|
|
2026-06-13 03:10:00 +03:00
|
|
|
export const fetchFollowers = ({
|
|
|
|
|
id,
|
|
|
|
|
maxId,
|
|
|
|
|
sinceId,
|
|
|
|
|
limit = 20,
|
|
|
|
|
credentials,
|
2026-06-16 23:14:52 +03:00
|
|
|
}) =>
|
|
|
|
|
promisedRequest({
|
|
|
|
|
url: MASTODON_FOLLOWERS_URL(id, {
|
|
|
|
|
maxId,
|
|
|
|
|
sinceId,
|
|
|
|
|
limit,
|
|
|
|
|
withRelationships: true,
|
|
|
|
|
}),
|
2026-06-13 03:10:00 +03:00
|
|
|
credentials,
|
2026-06-17 17:58:14 +03:00
|
|
|
}).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,
|
|
|
|
|
})
|
2026-06-17 17:58:14 +03:00
|
|
|
.then((result) => ({
|
|
|
|
|
...result,
|
|
|
|
|
data: {
|
|
|
|
|
...result.data,
|
|
|
|
|
ancestors: result.data.ancestors.map(parseStatus),
|
|
|
|
|
descendants: result.data.descendants.map(parseStatus),
|
|
|
|
|
},
|
2019-03-21 23:45:18 +02:00
|
|
|
}))
|
2026-06-16 16:37:47 +03:00
|
|
|
.catch((error) => {
|
|
|
|
|
throw new Error('Error fetching timeline', error)
|
|
|
|
|
})
|
2016-11-24 18:16:20 +01:00
|
|
|
|
2026-06-13 03:10:00 +03:00
|
|
|
export const fetchStatus = ({ id, credentials }) =>
|
|
|
|
|
promisedRequest({
|
|
|
|
|
url: MASTODON_STATUS_URL(id),
|
|
|
|
|
credentials,
|
|
|
|
|
})
|
2026-06-17 17:58:14 +03:00
|
|
|
.then(({ data, ...rest }) => ({ ...rest, data: parseStatus(data) }))
|
2026-06-16 16:37:47 +03:00
|
|
|
.catch((error) => {
|
|
|
|
|
throw new Error('Error fetching timeline', error)
|
|
|
|
|
})
|
2016-11-24 18:16:20 +01:00
|
|
|
|
2026-06-13 03:10:00 +03:00
|
|
|
export const fetchStatusSource = ({ id, credentials }) =>
|
|
|
|
|
promisedRequest({
|
|
|
|
|
url: MASTODON_STATUS_SOURCE_URL(id),
|
|
|
|
|
credentials,
|
|
|
|
|
})
|
2026-06-17 17:58:14 +03:00
|
|
|
.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-17 17:58:14 +03:00
|
|
|
}).then(({ data }) => {
|
|
|
|
|
return [...data].reverse().map((item) => {
|
2026-01-06 16:22:52 +02:00
|
|
|
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,
|
2026-06-16 23:14:52 +03:00
|
|
|
sinceId,
|
|
|
|
|
minId,
|
|
|
|
|
maxId,
|
|
|
|
|
userId,
|
|
|
|
|
listId,
|
|
|
|
|
statusId,
|
|
|
|
|
tag,
|
|
|
|
|
withMuted,
|
2021-02-01 12:55:23 +02:00
|
|
|
replyVisibility = 'all',
|
2024-09-23 23:10:32 +02:00
|
|
|
includeTypes = [],
|
2026-06-16 23:14:52 +03:00
|
|
|
bookmarkFolderId,
|
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,
|
2019-03-07 19:49:41 +02:00
|
|
|
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,
|
2019-01-12 23:33:45 +03:00
|
|
|
favorites: MASTODON_USER_FAVORITES_TIMELINE_URL,
|
2023-12-28 10:43:06 +01:00
|
|
|
publicFavorites: PLEROMA_USER_FAVORITES_TIMELINE_URL,
|
2020-07-03 19:45:49 +00:00
|
|
|
tag: MASTODON_TAG_TIMELINE_URL,
|
2024-01-04 22:46:04 +01:00
|
|
|
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
|
|
|
}
|
2026-06-16 23:14:52 +03:00
|
|
|
|
2019-01-17 21:46:03 +03:00
|
|
|
const isNotifications = timeline === 'notifications'
|
2026-06-16 23:14:52 +03:00
|
|
|
const params = {
|
|
|
|
|
minId,
|
|
|
|
|
sinceId,
|
|
|
|
|
maxId,
|
|
|
|
|
limit: 20,
|
|
|
|
|
}
|
2016-10-28 14:26:51 +02:00
|
|
|
|
2019-01-17 21:46:03 +03:00
|
|
|
let url = timelineUrls[timeline]
|
2017-06-12 16:00:46 +02:00
|
|
|
|
2023-12-28 10:43:06 +01: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)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-04 22:46:04 +01:00
|
|
|
if (timeline === 'quotes') {
|
|
|
|
|
url = url(statusId)
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-17 14:26:35 +03:00
|
|
|
if (tag) {
|
2019-03-26 11:40:34 -04:00
|
|
|
url = url(tag)
|
2017-09-17 14:26:35 +03:00
|
|
|
}
|
2026-06-16 23:14:52 +03:00
|
|
|
|
2019-01-24 10:47:49 +00:00
|
|
|
if (timeline === 'media') {
|
2026-06-16 23:14:52 +03:00
|
|
|
params.onlyMedia = 1
|
2019-01-24 10:47:49 +00:00
|
|
|
}
|
2019-03-07 20:16:35 +02:00
|
|
|
if (timeline === 'public') {
|
2026-06-16 23:14:52 +03:00
|
|
|
params.local = true
|
2019-03-07 20:16:35 +02:00
|
|
|
}
|
|
|
|
|
if (timeline === 'public' || timeline === 'publicAndExternal') {
|
2026-06-16 23:14:52 +03:00
|
|
|
params.onlyMedia = false
|
2019-03-07 20:16:35 +02:00
|
|
|
}
|
2020-07-03 19:45:49 +00:00
|
|
|
if (timeline !== 'favorites' && timeline !== 'bookmarks') {
|
2026-06-16 23:14:52 +03:00
|
|
|
params.withMuted = withMuted
|
2020-05-25 08:48:44 +03:00
|
|
|
}
|
2020-06-30 17:02:38 +03:00
|
|
|
if (replyVisibility !== 'all') {
|
2026-06-16 23:14:52 +03:00
|
|
|
params.replyVisibility = replyVisibility
|
2020-06-30 17:02:38 +03:00
|
|
|
}
|
2025-06-29 15:24:04 +03:00
|
|
|
if (includeTypes.size > 0) {
|
2026-06-16 23:14:52 +03:00
|
|
|
params.includeTypes = includeTypes
|
2021-02-01 12:55:23 +02:00
|
|
|
}
|
2024-09-23 23:10:32 +02:00
|
|
|
if (timeline === 'bookmarks' && bookmarkFolderId) {
|
2026-06-16 23:14:52 +03:00
|
|
|
params.folderId = bookmarkFolderId
|
2024-09-23 23:10:32 +02:00
|
|
|
}
|
2016-10-28 14:26:51 +02:00
|
|
|
|
2026-06-13 03:10:00 +03:00
|
|
|
return promisedRequest({
|
2026-06-16 23:14:52 +03:00
|
|
|
url: url + paramsString(params),
|
2026-06-13 03:10:00 +03:00
|
|
|
credentials,
|
2026-06-17 17:58:14 +03:00
|
|
|
}).then(async (result) => {
|
2026-06-13 03:10:00 +03:00
|
|
|
const pagination = parseLinkHeaderPagination(
|
2026-06-17 17:58:14 +03:00
|
|
|
result.response.headers.get('Link'),
|
2026-06-13 03:10:00 +03:00
|
|
|
{
|
|
|
|
|
flakeId: timeline !== 'bookmarks' && timeline !== 'notifications',
|
|
|
|
|
},
|
|
|
|
|
)
|
2023-01-14 22:17:21 -05:00
|
|
|
|
2026-06-13 03:10:00 +03:00
|
|
|
return {
|
2026-06-17 17:58:14 +03:00
|
|
|
...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({
|
|
|
|
|
url: MASTODON_USER_TIMELINE_URL(id) + '?pinned=true',
|
|
|
|
|
credentials,
|
2026-06-17 17:58:14 +03:00
|
|
|
}).then(({ data, ...rest }) => ({ ...rest, data: data.map(parseStatus) }))
|
2019-04-04 15:10:34 -04:00
|
|
|
|
2026-06-13 03:10:00 +03:00
|
|
|
export const verifyCredentials = ({ credentials }) =>
|
|
|
|
|
promisedRequest({
|
|
|
|
|
url: MASTODON_LOGIN_URL,
|
|
|
|
|
credentials,
|
2026-06-17 17:58:14 +03:00
|
|
|
}).then(({ data, ...rest }) => ({ ...rest, data: parseUser(data) }))
|
2016-10-28 14:26:51 +02:00
|
|
|
|
2026-06-17 19:09:55 +03:00
|
|
|
export const resetPassword = ({ instance, email }) => {
|
|
|
|
|
return promisedRequest({
|
|
|
|
|
url: MASTODON_PASSWORD_RESET_URL({ email }),
|
|
|
|
|
method: 'POST',
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
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 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({
|
2020-03-31 14:46:38 -05:00
|
|
|
url: MASTODON_STATUS_FAVORITEDBY_URL(id),
|
|
|
|
|
method: 'GET',
|
2026-01-06 16:22:52 +02:00
|
|
|
credentials,
|
2026-06-17 17:58:14 +03:00
|
|
|
}).then(({ data, ...rest }) => ({ ...rest, data: parseUser(data) }))
|
2019-04-01 22:29:45 -04:00
|
|
|
|
2026-06-13 03:10:00 +03:00
|
|
|
export const fetchRebloggedByUsers = ({ id, credentials }) =>
|
|
|
|
|
promisedRequest({
|
2020-03-31 14:46:38 -05:00
|
|
|
url: MASTODON_STATUS_REBLOGGEDBY_URL(id),
|
|
|
|
|
method: 'GET',
|
2026-01-06 16:22:52 +02:00
|
|
|
credentials,
|
2026-06-17 17:58:14 +03:00
|
|
|
}).then(({ data, ...rest }) => ({ ...rest, data: parseUser(data) }))
|
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,
|
2026-06-17 17:58:14 +03:00
|
|
|
}).then(({ data, ...rest }) => ({
|
|
|
|
|
...rest,
|
|
|
|
|
data: data.map((r) => {
|
2020-02-11 12:24:51 +00:00
|
|
|
r.accounts = r.accounts.map(parseUser)
|
|
|
|
|
return r
|
2026-01-06 16:22:52 +02:00
|
|
|
}),
|
2026-06-17 17:58:14 +03:00
|
|
|
}))
|
2019-11-15 08:39:21 +02:00
|
|
|
|
2026-06-13 03:10:00 +03:00
|
|
|
export const searchUsers = ({ credentials, query }) =>
|
|
|
|
|
promisedRequest({
|
2019-07-18 17:22:51 +03:00
|
|
|
url: MASTODON_USER_SEARCH_URL,
|
|
|
|
|
params: {
|
|
|
|
|
q: query,
|
2026-01-06 16:22:52 +02:00
|
|
|
resolve: true,
|
2019-07-18 17:22:51 +03:00
|
|
|
},
|
2026-01-06 16:22:52 +02:00
|
|
|
credentials,
|
2026-06-17 17:58:14 +03:00
|
|
|
}).then(({ data, ...rest }) => ({ ...rest, data: data.map(parseUser) }))
|
2019-07-18 17:22:51 +03:00
|
|
|
|
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) {
|
2025-04-09 20:16:52 -07:00
|
|
|
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,
|
|
|
|
|
})
|
2026-06-17 17:58:14 +03:00
|
|
|
.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))
|
2026-06-17 17:58:14 +03:00
|
|
|
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)
|
|
|
|
|
})
|
2019-07-10 16:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
2026-06-13 03:10:00 +03:00
|
|
|
export const fetchKnownDomains = ({ credentials }) =>
|
|
|
|
|
promisedRequest({ url: MASTODON_KNOWN_DOMAIN_LIST_URL, credentials })
|
2020-05-10 12:54:55 +02:00
|
|
|
|
2026-01-06 16:22:52 +02:00
|
|
|
export const getMastodonSocketURI = (
|
|
|
|
|
{ credentials, stream, args = {} },
|
|
|
|
|
base,
|
|
|
|
|
) => {
|
2024-10-19 13:42:04 -04:00
|
|
|
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([
|
2024-10-19 13:42:04 -04:00
|
|
|
'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,
|
2024-10-19 13:42:04 -04:00
|
|
|
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)
|
2024-10-19 13:42:04 -04:00
|
|
|
if (credentials) {
|
2026-01-06 16:22:52 +02:00
|
|
|
socket.send(
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
type: 'pleroma:authenticate',
|
|
|
|
|
token: credentials,
|
|
|
|
|
}),
|
|
|
|
|
)
|
2024-10-19 13:42:04 -04:00
|
|
|
}
|
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
|
|
|
|
2024-10-19 13:42:04 -04:00
|
|
|
const onAuthenticated = () => {
|
|
|
|
|
eventTarget.dispatchEvent(new CustomEvent('pleroma:authenticated'))
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 19:18:38 +02:00
|
|
|
proxy(socket, 'open')
|
|
|
|
|
proxy(socket, 'close')
|
2024-10-19 13:42:04 -04:00
|
|
|
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')
|
|
|
|
|
}
|
2021-01-13 21:33:20 +02:00
|
|
|
eventTarget.getState = () => socket.readyState
|
2024-10-19 13:42:04 -04:00
|
|
|
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,
|
|
|
|
|
}),
|
2024-10-19 13:42:04 -04:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
|
}),
|
2024-10-19 13:42:04 -04:00
|
|
|
)
|
|
|
|
|
}
|
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
|
2024-10-19 13:42:04 -04:00
|
|
|
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-16 23:14:52 +03:00
|
|
|
export const fetchScrobbles = ({ accountId, limit = 1 }) =>
|
|
|
|
|
promisedRequest({
|
|
|
|
|
url: PLEROMA_SCROBBLES_URL(accountId, { limit }),
|
|
|
|
|
})
|