From 495e6143d4acf5b94981f74fcfb412e13538cae5 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Thu, 25 Jun 2026 13:28:05 +0300 Subject: [PATCH] split timelines part into its own file --- src/api/public.js | 193 +---------------- src/api/timelines.js | 198 ++++++++++++++++++ src/modules/api.js | 2 +- .../notifications_fetcher.service.js | 2 +- .../timeline_fetcher.service.js | 2 +- 5 files changed, 202 insertions(+), 195 deletions(-) create mode 100644 src/api/timelines.js diff --git a/src/api/public.js b/src/api/public.js index bd3f56fce..cf8d082e2 100644 --- a/src/api/public.js +++ b/src/api/public.js @@ -1,8 +1,7 @@ import { paramsString, promisedRequest } from './helpers.js' +import { MASTODON_USER_TIMELINE_URL } from './timelines.js' import { - parseLinkHeaderPagination, - parseNotification, parseSource, parseStatus, parseUser, @@ -15,15 +14,6 @@ const MASTODON_REGISTRATION_URL = '/api/v1/accounts' const MASTODON_PASSWORD_RESET_URL = ({ email }) => `/auth/password${paramsString({ email })}` -const MASTODON_USER_NOTIFICATIONS_URL = ({ - minId, - sinceId, - maxId, - limit, - includeTypes, - replyVisibility, -}) => - `/api/v1/notifications${paramsString({ minId, sinceId, maxId, limit, includeTypes, replyVisibility })}` const MASTODON_FOLLOWING_URL = ( id, { minId, maxId, sinceId, limit, withRelationships }, @@ -35,84 +25,6 @@ const MASTODON_FOLLOWERS_URL = ( ) => `/api/v1/accounts/${id}/followers${paramsString({ minId, maxId, sinceId, limit, withRelationships })}` -const MASTODON_USER_HOME_TIMELINE_URL = ({ - minId, - sinceId, - maxId, - limit, - replyVisibility, -}) => - `/api/v1/timelines/home${paramsString({ minId, sinceId, maxId, limit, replyVisibility })}` -const MASTODON_LIST_TIMELINE_URL = ( - id, - { minId, sinceId, maxId, limit, replyVisibility }, -) => - `/api/v1/timelines/list/${id}${paramsString({ minId, sinceId, maxId, limit, replyVisibility })}` -const MASTODON_DIRECT_MESSAGES_TIMELINE_URL = ({ - minId, - sinceId, - maxId, - limit, - replyVisibility, -}) => - `/api/v1/timelines/direct${paramsString({ minId, sinceId, maxId, limit, replyVisibility })}` -const MASTODON_PUBLIC_TIMELINE = ({ - minId, - sinceId, - maxId, - limit, - replyVisibility, - local, - remote, - onlyMedia, -}) => - `/api/v1/timelines/public${paramsString({ minId, sinceId, maxId, limit, replyVisibility, local, remote, onlyMedia })}` -const MASTODON_TAG_TIMELINE_URL = ( - tag, - { minId, sinceId, maxId, limit, replyVisibility }, -) => - `/api/v1/timelines/tag/${tag}${paramsString({ minId, sinceId, maxId, limit, replyVisibility })}` -const MASTODON_USER_TIMELINE_URL = ( - id, - { minId, sinceId, maxId, limit, replyVisibility, pinned, onlyMedia }, -) => - `/api/v1/accounts/${id}/statuses${paramsString({ minId, sinceId, maxId, limit, replyVisibility, pinned, onlyMedia })}` -const MASTODON_USER_FAVORITES_TIMELINE_URL = ({ - minId, - sinceId, - maxId, - limit, - replyVisibility, -}) => - `/api/v1/favourites${paramsString({ minId, sinceId, maxId, limit, replyVisibility })}` -const MASTODON_BOOKMARK_TIMELINE_URL = ({ - minId, - sinceId, - maxId, - limit, - replyVisibility, - folderId, -}) => - `/api/v1/bookmarks${paramsString({ minId, sinceId, maxId, limit, replyVisibility, folderId })}` -const PLEROMA_STATUS_QUOTES_URL = ( - id, - { minId, sinceId, maxId, limit, replyVisibility }, -) => - `/api/v1/pleroma/statuses/${id}/quotes${paramsString({ minId, sinceId, maxId, limit, replyVisibility })}` -const PLEROMA_USER_FAVORITES_TIMELINE_URL = ( - id, - { minId, sinceId, maxId, limit, replyVisibility }, -) => - `/api/v1/pleroma/accounts/${id}/favourites${paramsString({ minId, sinceId, maxId, limit, replyVisibility })}` -const AKKOMA_BUBBLE_TIMELINE_URL = ({ - minId, - sinceId, - maxId, - limit, - replyVisibility, -}) => - `/api/v1/timelines/bubble${paramsString({ minId, sinceId, maxId, limit, replyVisibility })}` - export 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` @@ -275,109 +187,6 @@ export const fetchStatusHistory = ({ status, credentials }) => }) }) -export const fetchTimeline = ({ - timeline, - credentials, - sinceId, - minId, - maxId, - userId, - listId, - statusId, - tag, - withMuted, - replyVisibility = 'all', - includeTypes = [], - bookmarkFolderId, -}) => { - const timelineUrls = { - friends: MASTODON_USER_HOME_TIMELINE_URL, - public: MASTODON_PUBLIC_TIMELINE, - publicAndExternal: MASTODON_PUBLIC_TIMELINE, - dms: MASTODON_DIRECT_MESSAGES_TIMELINE_URL, - user: MASTODON_USER_TIMELINE_URL, - media: MASTODON_USER_TIMELINE_URL, - list: MASTODON_LIST_TIMELINE_URL, - favorites: MASTODON_USER_FAVORITES_TIMELINE_URL, - publicFavorites: PLEROMA_USER_FAVORITES_TIMELINE_URL, - bookmarks: MASTODON_BOOKMARK_TIMELINE_URL, - bubble: AKKOMA_BUBBLE_TIMELINE_URL, - tag: MASTODON_TAG_TIMELINE_URL, - quotes: PLEROMA_STATUS_QUOTES_URL, - - notifications: MASTODON_USER_NOTIFICATIONS_URL, - } - const urlFunc = timelineUrls[timeline] - - const twoArgs = new Set([ - 'user', - 'media', - 'list', - 'publicFavorites', - 'tag', - 'quotes', - ]) - - const params = { - minId, - sinceId, - maxId, - limit: 20, - } - - const id = (() => { - switch (timeline) { - case 'user': - case 'media': - return userId - case 'list': - return listId - case 'quotes': - return statusId - case 'tag': - return tag - } - })() - - const isNotifications = timeline === 'notifications' - - if (timeline === 'media') { - params.onlyMedia = true - } - if (timeline === 'public') { - params.local = true - } - if (timeline !== 'favorites' && timeline !== 'bookmarks') { - params.withMuted = withMuted - } - if (replyVisibility !== 'all') { - params.replyVisibility = replyVisibility - } - if (timeline === 'bookmarks' && bookmarkFolderId) { - params.folderId = bookmarkFolderId - } - - if (isNotifications && includeTypes.length > 0) { - params.includeTypes = includeTypes - } - - const url = twoArgs.has(timeline) ? urlFunc(id, params) : urlFunc(params) - return promisedRequest({ url, credentials }).then((result) => { - const pagination = parseLinkHeaderPagination( - result.response.headers.get('Link'), - { - flakeId: timeline !== 'bookmarks' && timeline !== 'notifications', - }, - ) - - return { - ...result, - data: result.data.map(isNotifications ? parseNotification : parseStatus), - pagination, - } - }) -} - export const listEmojiPacks = ({ page, pageSize, credentials }) => promisedRequest({ url: EMOJI_PACKS_URL(page, pageSize), diff --git a/src/api/timelines.js b/src/api/timelines.js new file mode 100644 index 000000000..0679b1eec --- /dev/null +++ b/src/api/timelines.js @@ -0,0 +1,198 @@ +import { paramsString, promisedRequest } from './helpers.js' + +import { + parseLinkHeaderPagination, + parseNotification, + parseStatus, +} from 'src/services/entity_normalizer/entity_normalizer.service.js' + +const MASTODON_USER_HOME_TIMELINE_URL = ({ + minId, + sinceId, + maxId, + limit, + replyVisibility, +}) => + `/api/v1/timelines/home${paramsString({ minId, sinceId, maxId, limit, replyVisibility })}` +const MASTODON_LIST_TIMELINE_URL = ( + id, + { minId, sinceId, maxId, limit, replyVisibility }, +) => + `/api/v1/timelines/list/${id}${paramsString({ minId, sinceId, maxId, limit, replyVisibility })}` +const MASTODON_DIRECT_MESSAGES_TIMELINE_URL = ({ + minId, + sinceId, + maxId, + limit, + replyVisibility, +}) => + `/api/v1/timelines/direct${paramsString({ minId, sinceId, maxId, limit, replyVisibility })}` +const MASTODON_PUBLIC_TIMELINE = ({ + minId, + sinceId, + maxId, + limit, + replyVisibility, + local, + remote, + onlyMedia, +}) => + `/api/v1/timelines/public${paramsString({ minId, sinceId, maxId, limit, replyVisibility, local, remote, onlyMedia })}` +const MASTODON_TAG_TIMELINE_URL = ( + tag, + { minId, sinceId, maxId, limit, replyVisibility }, +) => + `/api/v1/timelines/tag/${tag}${paramsString({ minId, sinceId, maxId, limit, replyVisibility })}` +export const MASTODON_USER_TIMELINE_URL = ( + id, + { minId, sinceId, maxId, limit, replyVisibility, pinned, onlyMedia }, +) => + `/api/v1/accounts/${id}/statuses${paramsString({ minId, sinceId, maxId, limit, replyVisibility, pinned, onlyMedia })}` +const MASTODON_USER_FAVORITES_TIMELINE_URL = ({ + minId, + sinceId, + maxId, + limit, + replyVisibility, +}) => + `/api/v1/favourites${paramsString({ minId, sinceId, maxId, limit, replyVisibility })}` +const MASTODON_BOOKMARK_TIMELINE_URL = ({ + minId, + sinceId, + maxId, + limit, + replyVisibility, + folderId, +}) => + `/api/v1/bookmarks${paramsString({ minId, sinceId, maxId, limit, replyVisibility, folderId })}` +const PLEROMA_STATUS_QUOTES_URL = ( + id, + { minId, sinceId, maxId, limit, replyVisibility }, +) => + `/api/v1/pleroma/statuses/${id}/quotes${paramsString({ minId, sinceId, maxId, limit, replyVisibility })}` +const PLEROMA_USER_FAVORITES_TIMELINE_URL = ( + id, + { minId, sinceId, maxId, limit, replyVisibility }, +) => + `/api/v1/pleroma/accounts/${id}/favourites${paramsString({ minId, sinceId, maxId, limit, replyVisibility })}` +const AKKOMA_BUBBLE_TIMELINE_URL = ({ + minId, + sinceId, + maxId, + limit, + replyVisibility, +}) => + `/api/v1/timelines/bubble${paramsString({ minId, sinceId, maxId, limit, replyVisibility })}` + +const MASTODON_USER_NOTIFICATIONS_URL = ({ + minId, + sinceId, + maxId, + limit, + includeTypes, + replyVisibility, +}) => + `/api/v1/notifications${paramsString({ minId, sinceId, maxId, limit, includeTypes, replyVisibility })}` + +export const fetchTimeline = ({ + timeline, + credentials, + sinceId, + minId, + maxId, + userId, + listId, + statusId, + tag, + withMuted, + replyVisibility = 'all', + includeTypes = [], + bookmarkFolderId, +}) => { + const timelineUrls = { + friends: MASTODON_USER_HOME_TIMELINE_URL, + public: MASTODON_PUBLIC_TIMELINE, + publicAndExternal: MASTODON_PUBLIC_TIMELINE, + dms: MASTODON_DIRECT_MESSAGES_TIMELINE_URL, + user: MASTODON_USER_TIMELINE_URL, + media: MASTODON_USER_TIMELINE_URL, + list: MASTODON_LIST_TIMELINE_URL, + favorites: MASTODON_USER_FAVORITES_TIMELINE_URL, + publicFavorites: PLEROMA_USER_FAVORITES_TIMELINE_URL, + bookmarks: MASTODON_BOOKMARK_TIMELINE_URL, + bubble: AKKOMA_BUBBLE_TIMELINE_URL, + tag: MASTODON_TAG_TIMELINE_URL, + quotes: PLEROMA_STATUS_QUOTES_URL, + + notifications: MASTODON_USER_NOTIFICATIONS_URL, + } + const urlFunc = timelineUrls[timeline] + + const twoArgs = new Set([ + 'user', + 'media', + 'list', + 'publicFavorites', + 'tag', + 'quotes', + ]) + + const params = { + minId, + sinceId, + maxId, + limit: 20, + } + + const id = (() => { + switch (timeline) { + case 'user': + case 'media': + return userId + case 'list': + return listId + case 'quotes': + return statusId + case 'tag': + return tag + } + })() + + const isNotifications = timeline === 'notifications' + + if (timeline === 'media') { + params.onlyMedia = true + } + if (timeline === 'public') { + params.local = true + } + if (timeline !== 'favorites' && timeline !== 'bookmarks') { + params.withMuted = withMuted + } + if (replyVisibility !== 'all') { + params.replyVisibility = replyVisibility + } + if (timeline === 'bookmarks' && bookmarkFolderId) { + params.folderId = bookmarkFolderId + } + + if (isNotifications && includeTypes.length > 0) { + params.includeTypes = includeTypes + } + + const url = twoArgs.has(timeline) ? urlFunc(id, params) : urlFunc(params) + return promisedRequest({ url, credentials }).then((result) => { + const pagination = parseLinkHeaderPagination( + result.response.headers.get('Link'), + { + flakeId: timeline !== 'bookmarks' && timeline !== 'notifications', + }, + ) + + return { + ...result, + data: result.data.map(isNotifications ? parseNotification : parseStatus), + pagination, + } + }) +} diff --git a/src/modules/api.js b/src/modules/api.js index ca1aacf05..03d184160 100644 --- a/src/modules/api.js +++ b/src/modules/api.js @@ -8,7 +8,7 @@ import { useInterfaceStore } from 'src/stores/interface.js' import { useOAuthStore } from 'src/stores/oauth.js' import { useShoutStore } from 'src/stores/shout.js' -import { fetchTimeline } from 'src/api/public.js' +import { fetchTimeline } from 'src/api/timelines.js' import { getMastodonSocketURI, ProcessedWS, diff --git a/src/services/notifications_fetcher/notifications_fetcher.service.js b/src/services/notifications_fetcher/notifications_fetcher.service.js index f787b92e4..46101db7d 100644 --- a/src/services/notifications_fetcher/notifications_fetcher.service.js +++ b/src/services/notifications_fetcher/notifications_fetcher.service.js @@ -5,7 +5,7 @@ import { useInstanceCapabilitiesStore } from 'src/stores/instance_capabilities.j import { useInterfaceStore } from 'src/stores/interface.js' import { useMergedConfigStore } from 'src/stores/merged_config.js' -import { fetchTimeline } from 'src/api/public.js' +import { fetchTimeline } from 'src/api/timelines.js' const update = ({ store, notifications, older }) => { store.dispatch('addNewNotifications', { notifications, older }) diff --git a/src/services/timeline_fetcher/timeline_fetcher.service.js b/src/services/timeline_fetcher/timeline_fetcher.service.js index ff75ea704..c006ae172 100644 --- a/src/services/timeline_fetcher/timeline_fetcher.service.js +++ b/src/services/timeline_fetcher/timeline_fetcher.service.js @@ -7,7 +7,7 @@ import { useInstanceCapabilitiesStore } from 'src/stores/instance_capabilities.j import { useInterfaceStore } from 'src/stores/interface.js' import { useMergedConfigStore } from 'src/stores/merged_config.js' -import { fetchTimeline } from 'src/api/public.js' +import { fetchTimeline } from 'src/api/timelines.js' const update = ({ store,