From f5e28fc27a99dcdd8bbdc2436de47b2a0742845d Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Wed, 4 Oct 2023 23:54:45 +0200 Subject: [PATCH] [mastodon-client] Refactor object id converters --- .../src/server/api/mastodon/converters.ts | 85 +++++++++---------- .../server/api/mastodon/endpoints/account.ts | 46 +++++----- .../server/api/mastodon/endpoints/filter.ts | 10 +-- .../src/server/api/mastodon/endpoints/list.ts | 12 +-- .../server/api/mastodon/endpoints/media.ts | 8 +- .../src/server/api/mastodon/endpoints/misc.ts | 6 +- .../api/mastodon/endpoints/notifications.ts | 6 +- .../server/api/mastodon/endpoints/search.ts | 10 +-- .../server/api/mastodon/endpoints/status.ts | 46 +++++----- .../server/api/mastodon/endpoints/timeline.ts | 12 +-- .../src/server/api/mastodon/helpers/misc.ts | 4 +- 11 files changed, 120 insertions(+), 125 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/converters.ts b/packages/backend/src/server/api/mastodon/converters.ts index 8a77bf363..ff0b7b654 100644 --- a/packages/backend/src/server/api/mastodon/converters.ts +++ b/packages/backend/src/server/api/mastodon/converters.ts @@ -1,74 +1,69 @@ -import { Entity } from "megalodon"; import { convertId, IdType } from "../index.js"; -function simpleConvert(data: any) { +function simpleConvertId(data: any) { // copy the object to bypass weird pass by reference bugs const result = Object.assign({}, data); result.id = convertId(data.id, IdType.MastodonId); return result; } -export function convertAccount(account: Entity.Account | MastodonEntity.MutedAccount) { - return simpleConvert(account); +export function convertAccountId(account: MastodonEntity.Account | MastodonEntity.MutedAccount) { + return simpleConvertId(account); } -export function convertAnnouncement(announcement: Entity.Announcement) { - return simpleConvert(announcement); +export function convertAnnouncementId(announcement: MastodonEntity.Announcement) { + return simpleConvertId(announcement); } -export function convertAttachment(attachment: Entity.Attachment) { - return simpleConvert(attachment); +export function convertAttachmentId(attachment: MastodonEntity.Attachment) { + return simpleConvertId(attachment); } -export function convertFilter(filter: Entity.Filter) { - return simpleConvert(filter); +export function convertFilterId(filter: MastodonEntity.Filter) { + return simpleConvertId(filter); } -export function convertList(list: MastodonEntity.List) { - return simpleConvert(list); +export function convertListId(list: MastodonEntity.List) { + return simpleConvertId(list); } -export function convertFeaturedTag(tag: Entity.FeaturedTag) { - return simpleConvert(tag); -} - -export function convertNotification(notification: MastodonEntity.Notification) { - notification.account = convertAccount(notification.account); +export function convertNotificationIds(notification: MastodonEntity.Notification) { + notification.account = convertAccountId(notification.account); notification.id = convertId(notification.id, IdType.MastodonId); if (notification.status) - notification.status = convertStatus(notification.status); + notification.status = convertStatusIds(notification.status); if (notification.reaction) - notification.reaction = convertReaction(notification.reaction); + notification.reaction = convertReactionIds(notification.reaction); return notification; } -export function convertPoll(poll: Entity.Poll) { - return simpleConvert(poll); +export function convertPollId(poll: MastodonEntity.Poll) { + return simpleConvertId(poll); } -export function convertReaction(reaction: MastodonEntity.Reaction) { +export function convertReactionIds(reaction: MastodonEntity.Reaction) { if (reaction.accounts) { - reaction.accounts = reaction.accounts.map(convertAccount); + reaction.accounts = reaction.accounts.map(convertAccountId); } return reaction; } -export function convertRelationship(relationship: Entity.Relationship) { - return simpleConvert(relationship); +export function convertRelationshipId(relationship: MastodonEntity.Relationship) { + return simpleConvertId(relationship); } -export function convertSearch(search: MastodonEntity.Search) { - search.accounts = search.accounts.map(p => convertAccount(p)); - search.statuses = search.statuses.map(p => convertStatus(p)); +export function convertSearchIds(search: MastodonEntity.Search) { + search.accounts = search.accounts.map(p => convertAccountId(p)); + search.statuses = search.statuses.map(p => convertStatusIds(p)); return search; } -export function convertStatusSource(statusSource: MastodonEntity.StatusSource) { - return simpleConvert(statusSource); +export function convertStatusSourceId(statusSource: MastodonEntity.StatusSource) { + return simpleConvertId(statusSource); } -export function convertStatus(status: MastodonEntity.Status) { - status.account = convertAccount(status.account); +export function convertStatusIds(status: MastodonEntity.Status) { + status.account = convertAccountId(status.account); status.id = convertId(status.id, IdType.MastodonId); if (status.in_reply_to_account_id) status.in_reply_to_account_id = convertId( @@ -78,34 +73,34 @@ export function convertStatus(status: MastodonEntity.Status) { if (status.in_reply_to_id) status.in_reply_to_id = convertId(status.in_reply_to_id, IdType.MastodonId); status.media_attachments = status.media_attachments.map((attachment) => - convertAttachment(attachment), + convertAttachmentId(attachment), ); status.mentions = status.mentions.map((mention) => ({ ...mention, id: convertId(mention.id, IdType.MastodonId), })); - if (status.poll) status.poll = convertPoll(status.poll); - if (status.reblog) status.reblog = convertStatus(status.reblog); - if (status.quote) status.quote = convertStatus(status.quote); - status.reactions = status.reactions.map(convertReaction); + if (status.poll) status.poll = convertPollId(status.poll); + if (status.reblog) status.reblog = convertStatusIds(status.reblog); + if (status.quote) status.quote = convertStatusIds(status.quote); + status.reactions = status.reactions.map(convertReactionIds); return status; } -export function convertStatusEdit(edit: MastodonEntity.StatusEdit) { - edit.account = convertAccount(edit.account); +export function convertStatusEditIds(edit: MastodonEntity.StatusEdit) { + edit.account = convertAccountId(edit.account); edit.media_attachments = edit.media_attachments.map((attachment) => - convertAttachment(attachment), + convertAttachmentId(attachment), ); - if (edit.poll) edit.poll = convertPoll(edit.poll); + if (edit.poll) edit.poll = convertPollId(edit.poll); return edit; } -export function convertConversation(conversation: MastodonEntity.Conversation) { +export function convertConversationIds(conversation: MastodonEntity.Conversation) { conversation.id = convertId(conversation.id, IdType.MastodonId); - conversation.accounts = conversation.accounts.map(convertAccount); + conversation.accounts = conversation.accounts.map(convertAccountId); if (conversation.last_status) { - conversation.last_status = convertStatus(conversation.last_status); + conversation.last_status = convertStatusIds(conversation.last_status); } return conversation; diff --git a/packages/backend/src/server/api/mastodon/endpoints/account.ts b/packages/backend/src/server/api/mastodon/endpoints/account.ts index f574f8af4..c2007b1d3 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/account.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/account.ts @@ -1,7 +1,7 @@ import Router from "@koa/router"; import { argsToBools, convertPaginationArgsIds, limitToInt, normalizeUrlQuery } from "./timeline.js"; import { convertId, IdType } from "../../index.js"; -import { convertAccount, convertList, convertRelationship, convertStatus, } from "../converters.js"; +import { convertAccountId, convertListId, convertRelationshipId, convertStatusIds, } from "../converters.js"; import { getUser } from "@/server/api/common/getters.js"; import { UserConverter } from "@/server/api/mastodon/converters/user.js"; import authenticate from "@/server/api/authenticate.js"; @@ -22,7 +22,7 @@ export function setupEndpointsAccount(router: Router): void { } const acct = await UserHelpers.verifyCredentials(user); - ctx.body = convertAccount(acct); + ctx.body = convertAccountId(acct); } catch (e: any) { console.error(e); console.error(e.response.data); @@ -41,7 +41,7 @@ export function setupEndpointsAccount(router: Router): void { } const acct = await UserHelpers.updateCredentials(user, (ctx.request as any).body as any); - ctx.body = convertAccount(acct) + ctx.body = convertAccountId(acct) } catch (e: any) { console.error(e); console.error(e.response.data); @@ -58,7 +58,7 @@ export function setupEndpointsAccount(router: Router): void { return; } const account = await UserConverter.encode(user); - ctx.body = convertAccount(account); + ctx.body = convertAccountId(account); } catch (e: any) { console.error(e); console.error(e.response.data); @@ -79,7 +79,7 @@ export function setupEndpointsAccount(router: Router): void { const ids = (normalizeUrlQuery(ctx.query, ['id[]'])['id[]'] ?? []) .map((id: string) => convertId(id, IdType.IceshrimpId)); const result = await UserHelpers.getUserRelationhipToMany(ids, user.id); - ctx.body = result.map(rel => convertRelationship(rel)); + ctx.body = result.map(rel => convertRelationshipId(rel)); } catch (e: any) { console.error(e); console.error(e.response.data); @@ -91,7 +91,7 @@ export function setupEndpointsAccount(router: Router): void { try { const userId = convertId(ctx.params.id, IdType.IceshrimpId); const account = await UserConverter.encode(await getUser(userId)); - ctx.body = convertAccount(account); + ctx.body = convertAccountId(account); } catch (e: any) { console.error(e); console.error(e.response.data); @@ -113,7 +113,7 @@ export function setupEndpointsAccount(router: Router): void { const tl = await UserHelpers.getUserStatuses(query, user, args.max_id, args.since_id, args.min_id, args.limit, args['only_media'], args['exclude_replies'], args['exclude_reblogs'], args.pinned, args.tagged) .then(n => NoteConverter.encodeMany(n, user, cache)); - ctx.body = tl.map(s => convertStatus(s)); + ctx.body = tl.map(s => convertStatusIds(s)); } catch (e: any) { console.error(e); console.error(e.response.data); @@ -148,7 +148,7 @@ export function setupEndpointsAccount(router: Router): void { const res = await UserHelpers.getUserFollowers(query, user, args.max_id, args.since_id, args.min_id, args.limit); const followers = await UserConverter.encodeMany(res.data, cache); - ctx.body = followers.map((account) => convertAccount(account)); + ctx.body = followers.map((account) => convertAccountId(account)); PaginationHelpers.appendLinkPaginationHeader(args, ctx, res, 40); } catch (e: any) { console.error(e); @@ -173,7 +173,7 @@ export function setupEndpointsAccount(router: Router): void { const res = await UserHelpers.getUserFollowing(query, user, args.max_id, args.since_id, args.min_id, args.limit); const following = await UserConverter.encodeMany(res.data, cache); - ctx.body = following.map((account) => convertAccount(account)); + ctx.body = following.map((account) => convertAccountId(account)); PaginationHelpers.appendLinkPaginationHeader(args, ctx, res, 40); } catch (e: any) { console.error(e); @@ -197,7 +197,7 @@ export function setupEndpointsAccount(router: Router): void { const member = await UserHelpers.getUserCached(convertId(ctx.params.id, IdType.IceshrimpId)); const results = await ListHelpers.getListsByMember(user, member); - ctx.body = results.map(p => convertList(p)); + ctx.body = results.map(p => convertListId(p)); } catch (e: any) { ctx.status = 400; ctx.body = { error: e.message }; @@ -219,7 +219,7 @@ export function setupEndpointsAccount(router: Router): void { const target = await UserHelpers.getUserCached(convertId(ctx.params.id, IdType.IceshrimpId)); //FIXME: Parse form data const result = await UserHelpers.followUser(target, user, true, false); - ctx.body = convertRelationship(result); + ctx.body = convertRelationshipId(result); } catch (e: any) { console.error(e); console.error(e.response.data); @@ -242,7 +242,7 @@ export function setupEndpointsAccount(router: Router): void { const target = await UserHelpers.getUserCached(convertId(ctx.params.id, IdType.IceshrimpId)); const result = await UserHelpers.unfollowUser(target, user); - ctx.body = convertRelationship(result); + ctx.body = convertRelationshipId(result); } catch (e: any) { console.error(e); console.error(e.response.data); @@ -265,7 +265,7 @@ export function setupEndpointsAccount(router: Router): void { const target = await UserHelpers.getUserCached(convertId(ctx.params.id, IdType.IceshrimpId)); const result = await UserHelpers.blockUser(target, user); - ctx.body = convertRelationship(result); + ctx.body = convertRelationshipId(result); } catch (e: any) { console.error(e); console.error(e.response.data); @@ -288,7 +288,7 @@ export function setupEndpointsAccount(router: Router): void { const target = await UserHelpers.getUserCached(convertId(ctx.params.id, IdType.IceshrimpId)); const result = await UserHelpers.unblockUser(target, user); - ctx.body = convertRelationship(result) + ctx.body = convertRelationshipId(result) } catch (e: any) { console.error(e); console.error(e.response.data); @@ -313,7 +313,7 @@ export function setupEndpointsAccount(router: Router): void { const args = normalizeUrlQuery(argsToBools(limitToInt(ctx.query, ['duration']), ['notifications'])); const target = await UserHelpers.getUserCached(convertId(ctx.params.id, IdType.IceshrimpId)); const result = await UserHelpers.muteUser(target, user, args.notifications, args.duration); - ctx.body = convertRelationship(result) + ctx.body = convertRelationshipId(result) } catch (e: any) { console.error(e); console.error(e.response.data); @@ -336,7 +336,7 @@ export function setupEndpointsAccount(router: Router): void { const target = await UserHelpers.getUserCached(convertId(ctx.params.id, IdType.IceshrimpId)); const result = await UserHelpers.unmuteUser(target, user); - ctx.body = convertRelationship(result) + ctx.body = convertRelationshipId(result) } catch (e: any) { console.error(e); console.error(e.response.data); @@ -376,7 +376,7 @@ export function setupEndpointsAccount(router: Router): void { const res = await UserHelpers.getUserBookmarks(user, args.max_id, args.since_id, args.min_id, args.limit); const bookmarks = await NoteConverter.encodeMany(res.data, user, cache); - ctx.body = bookmarks.map(s => convertStatus(s)); + ctx.body = bookmarks.map(s => convertStatusIds(s)); PaginationHelpers.appendLinkPaginationHeader(args, ctx, res, 20); } catch (e: any) { console.error(e); @@ -400,7 +400,7 @@ export function setupEndpointsAccount(router: Router): void { const res = await UserHelpers.getUserFavorites(user, args.max_id, args.since_id, args.min_id, args.limit); const favorites = await NoteConverter.encodeMany(res.data, user, cache); - ctx.body = favorites.map(s => convertStatus(s)); + ctx.body = favorites.map(s => convertStatusIds(s)); PaginationHelpers.appendLinkPaginationHeader(args, ctx, res, 20); } catch (e: any) { console.error(e); @@ -422,7 +422,7 @@ export function setupEndpointsAccount(router: Router): void { const cache = UserHelpers.getFreshAccountCache(); const args = normalizeUrlQuery(convertPaginationArgsIds(limitToInt(ctx.query as any))); const res = await UserHelpers.getUserMutes(user, args.max_id, args.since_id, args.min_id, args.limit, cache); - ctx.body = res.data.map(m => convertAccount(m)); + ctx.body = res.data.map(m => convertAccountId(m)); PaginationHelpers.appendLinkPaginationHeader(args, ctx, res, 40); } catch (e: any) { console.error(e); @@ -445,7 +445,7 @@ export function setupEndpointsAccount(router: Router): void { const args = normalizeUrlQuery(convertPaginationArgsIds(limitToInt(ctx.query as any))); const res = await UserHelpers.getUserBlocks(user, args.max_id, args.since_id, args.min_id, args.limit); const blocks = await UserConverter.encodeMany(res.data, cache); - ctx.body = blocks.map(b => convertAccount(b)); + ctx.body = blocks.map(b => convertAccountId(b)); PaginationHelpers.appendLinkPaginationHeader(args, ctx, res, 40); } catch (e: any) { console.error(e); @@ -468,7 +468,7 @@ export function setupEndpointsAccount(router: Router): void { const args = normalizeUrlQuery(convertPaginationArgsIds(limitToInt(ctx.query as any))); const res = await UserHelpers.getUserFollowRequests(user, args.max_id, args.since_id, args.min_id, args.limit); const requests = await UserConverter.encodeMany(res.data, cache); - ctx.body = requests.map(b => convertAccount(b)); + ctx.body = requests.map(b => convertAccountId(b)); PaginationHelpers.appendLinkPaginationHeader(args, ctx, res, 40); } catch (e: any) { console.error(e); @@ -491,7 +491,7 @@ export function setupEndpointsAccount(router: Router): void { const target = await UserHelpers.getUserCached(convertId(ctx.params.id, IdType.IceshrimpId)); const result = await UserHelpers.acceptFollowRequest(target, user); - ctx.body = convertRelationship(result); + ctx.body = convertRelationshipId(result); } catch (e: any) { console.error(e); console.error(e.response.data); @@ -514,7 +514,7 @@ export function setupEndpointsAccount(router: Router): void { const target = await UserHelpers.getUserCached(convertId(ctx.params.id, IdType.IceshrimpId)); const result = await UserHelpers.rejectFollowRequest(target, user); - ctx.body = convertRelationship(result); + ctx.body = convertRelationshipId(result); } catch (e: any) { console.error(e); console.error(e.response.data); diff --git a/packages/backend/src/server/api/mastodon/endpoints/filter.ts b/packages/backend/src/server/api/mastodon/endpoints/filter.ts index f4700f9e3..e90f007fa 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/filter.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/filter.ts @@ -1,7 +1,7 @@ import Router from "@koa/router"; import { getClient } from "../index.js"; import { convertId, IdType } from "../../index.js"; -import { convertFilter } from "../converters.js"; +import { convertFilterId } from "../converters.js"; export function setupEndpointsFilter(router: Router): void { router.get("/v1/filters", async (ctx) => { @@ -11,7 +11,7 @@ export function setupEndpointsFilter(router: Router): void { const body: any = ctx.request.body; try { const data = await client.getFilters(); - ctx.body = data.data.map((filter) => convertFilter(filter)); + ctx.body = data.data.map((filter) => convertFilterId(filter)); } catch (e: any) { console.error(e); ctx.status = 401; @@ -28,7 +28,7 @@ export function setupEndpointsFilter(router: Router): void { const data = await client.getFilter( convertId(ctx.params.id, IdType.IceshrimpId), ); - ctx.body = convertFilter(data.data); + ctx.body = convertFilterId(data.data); } catch (e: any) { console.error(e); ctx.status = 401; @@ -43,7 +43,7 @@ export function setupEndpointsFilter(router: Router): void { const body: any = ctx.request.body; try { const data = await client.createFilter(body.phrase, body.context, body); - ctx.body = convertFilter(data.data); + ctx.body = convertFilterId(data.data); } catch (e: any) { console.error(e); ctx.status = 401; @@ -62,7 +62,7 @@ export function setupEndpointsFilter(router: Router): void { body.phrase, body.context, ); - ctx.body = convertFilter(data.data); + ctx.body = convertFilterId(data.data); } catch (e: any) { console.error(e); ctx.status = 401; diff --git a/packages/backend/src/server/api/mastodon/endpoints/list.ts b/packages/backend/src/server/api/mastodon/endpoints/list.ts index 053f617a8..9f1531f34 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/list.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/list.ts @@ -1,5 +1,5 @@ import Router from "@koa/router"; -import { convertAccount, convertList, } from "../converters.js"; +import { convertAccountId, convertListId, } from "../converters.js"; import { convertId, IdType } from "../../index.js"; import authenticate from "@/server/api/authenticate.js"; import { convertPaginationArgsIds, limitToInt, normalizeUrlQuery } from "@/server/api/mastodon/endpoints/timeline.js"; @@ -23,7 +23,7 @@ export function setupEndpointsList(router: Router): void { } ctx.body = await ListHelpers.getLists(user) - .then(p => p.map(list => convertList(list))); + .then(p => p.map(list => convertListId(list))); } catch (e: any) { console.error(e); console.error(e.response.data); @@ -46,7 +46,7 @@ export function setupEndpointsList(router: Router): void { const id = convertId(ctx.params.id, IdType.IceshrimpId); ctx.body = await ListHelpers.getList(user, id) - .then(p => convertList(p)); + .then(p => convertListId(p)); } catch (e: any) { ctx.status = 404; } @@ -71,7 +71,7 @@ export function setupEndpointsList(router: Router): void { } ctx.body = await ListHelpers.createList(user, title) - .then(p => convertList(p)); + .then(p => convertListId(p)); } catch (e: any) { ctx.status = 400; ctx.body = { error: e.message }; @@ -106,7 +106,7 @@ export function setupEndpointsList(router: Router): void { } ctx.body = await ListHelpers.updateList(user, list, title) - .then(p => convertList(p)); + .then(p => convertListId(p)); } catch (e: any) { console.error(e); console.error(e.response.data); @@ -159,7 +159,7 @@ export function setupEndpointsList(router: Router): void { const args = normalizeUrlQuery(convertPaginationArgsIds(limitToInt(ctx.query))); const res = await ListHelpers.getListUsers(user, id, args.max_id, args.since_id, args.min_id, args.limit); const accounts = await UserConverter.encodeMany(res.data); - ctx.body = accounts.map(account => convertAccount(account)); + ctx.body = accounts.map(account => convertAccountId(account)); PaginationHelpers.appendLinkPaginationHeader(args, ctx, res, 40); } catch (e: any) { ctx.status = 404; diff --git a/packages/backend/src/server/api/mastodon/endpoints/media.ts b/packages/backend/src/server/api/mastodon/endpoints/media.ts index 08d528001..3b4ac7c00 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/media.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/media.ts @@ -1,6 +1,6 @@ import Router from "@koa/router"; import { convertId, IdType } from "@/misc/convert-id.js"; -import { convertAttachment } from "@/server/api/mastodon/converters.js"; +import { convertAttachmentId } from "@/server/api/mastodon/converters.js"; import multer from "@koa/multer"; import authenticate from "@/server/api/authenticate.js"; import { MediaHelpers } from "@/server/api/mastodon/helpers/media.js"; @@ -27,7 +27,7 @@ export function setupEndpointsMedia(router: Router, fileRouter: Router, upload: } const attachment = FileConverter.encode(file); - ctx.body = convertAttachment(attachment); + ctx.body = convertAttachmentId(attachment); } catch (e: any) { console.error(e); ctx.status = 500; @@ -55,7 +55,7 @@ export function setupEndpointsMedia(router: Router, fileRouter: Router, upload: const result = await MediaHelpers.updateMedia(user, file, ctx.request.body) .then(p => FileConverter.encode(p)); - ctx.body = convertAttachment(result); + ctx.body = convertAttachmentId(result); } catch (e: any) { console.error(e); ctx.status = 401; @@ -81,7 +81,7 @@ export function setupEndpointsMedia(router: Router, fileRouter: Router, upload: } const result = await MediaHelpers.uploadMedia(user, file, ctx.request.body) .then(p => FileConverter.encode(p)); - ctx.body = convertAttachment(result); + ctx.body = convertAttachmentId(result); } catch (e: any) { console.error(e); ctx.status = 500; diff --git a/packages/backend/src/server/api/mastodon/endpoints/misc.ts b/packages/backend/src/server/api/mastodon/endpoints/misc.ts index f0011eaaa..52c8a1a5c 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/misc.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/misc.ts @@ -1,7 +1,7 @@ import Router from "@koa/router"; import { getClient } from "@/server/api/mastodon/index.js"; import { convertId, IdType } from "@/misc/convert-id.js"; -import { convertAccount, convertAnnouncement, convertFilter } from "@/server/api/mastodon/converters.js"; +import { convertAccountId, convertAnnouncementId, convertFilterId } from "@/server/api/mastodon/converters.js"; import { Users } from "@/models/index.js"; import { getInstance } from "@/server/api/mastodon/endpoints/meta.js"; import { IsNull } from "typeorm"; @@ -39,7 +39,7 @@ export function setupEndpointsMisc(router: Router): void { try { const data = await client.getInstanceAnnouncements(); ctx.body = data.data.map((announcement) => - convertAnnouncement(announcement), + convertAnnouncementId(announcement), ); } catch (e: any) { console.error(e); @@ -74,7 +74,7 @@ export function setupEndpointsMisc(router: Router): void { // displayed without being logged in try { const data = await client.getFilters(); - ctx.body = data.data.map((filter) => convertFilter(filter)); + ctx.body = data.data.map((filter) => convertFilterId(filter)); } catch (e: any) { console.error(e); ctx.status = 401; diff --git a/packages/backend/src/server/api/mastodon/endpoints/notifications.ts b/packages/backend/src/server/api/mastodon/endpoints/notifications.ts index 4c27ef53c..7133a6817 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/notifications.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/notifications.ts @@ -1,7 +1,7 @@ import Router from "@koa/router"; import { convertId, IdType } from "../../index.js"; import { convertPaginationArgsIds, limitToInt, normalizeUrlQuery } from "./timeline.js"; -import { convertConversation, convertNotification } from "../converters.js"; +import { convertConversationIds, convertNotificationIds } from "../converters.js"; import authenticate from "@/server/api/authenticate.js"; import { UserHelpers } from "@/server/api/mastodon/helpers/user.js"; import { NotificationHelpers } from "@/server/api/mastodon/helpers/notification.js"; @@ -24,7 +24,7 @@ export function setupEndpointsNotifications(router: Router): void { const args = normalizeUrlQuery(convertPaginationArgsIds(limitToInt(ctx.query)), ['types[]', 'exclude_types[]']); const data = NotificationHelpers.getNotifications(user, args.max_id, args.since_id, args.min_id, args.limit, args['types[]'], args['exclude_types[]'], args.account_id) .then(p => NotificationConverter.encodeMany(p, user, cache)) - .then(p => p.map(n => convertNotification(n))); + .then(p => p.map(n => convertNotificationIds(n))); ctx.body = await data; } catch (e: any) { @@ -50,7 +50,7 @@ export function setupEndpointsNotifications(router: Router): void { return; } - ctx.body = convertNotification(await NotificationConverter.encode(notification, user)); + ctx.body = convertNotificationIds(await NotificationConverter.encode(notification, user)); } catch (e: any) { console.error(e); ctx.status = 401; diff --git a/packages/backend/src/server/api/mastodon/endpoints/search.ts b/packages/backend/src/server/api/mastodon/endpoints/search.ts index f230d0429..630ea777f 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/search.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/search.ts @@ -2,7 +2,7 @@ import { Converter } from "megalodon"; import Router from "@koa/router"; import axios from "axios"; import { argsToBools, convertPaginationArgsIds, limitToInt, normalizeUrlQuery } from "./timeline.js"; -import { convertAccount, convertSearch, convertStatus } from "../converters.js"; +import { convertAccountId, convertSearchIds, convertStatusIds } from "../converters.js"; import authenticate from "@/server/api/authenticate.js"; import { UserHelpers } from "@/server/api/mastodon/helpers/user.js"; import { SearchHelpers } from "@/server/api/mastodon/helpers/search.js"; @@ -23,7 +23,7 @@ export function setupEndpointsSearch(router: Router): void { const result = await SearchHelpers.search(user, args.q, args.type, args.resolve, args.following, args.account_id, args['exclude_unreviewed'], args.max_id, args.min_id, args.limit, args.offset, cache); ctx.body = { - ...convertSearch(result), + ...convertSearchIds(result), hashtags: result.hashtags.map(p => p.name), }; } catch (e: any) { @@ -46,7 +46,7 @@ export function setupEndpointsSearch(router: Router): void { const cache = UserHelpers.getFreshAccountCache(); const result = await SearchHelpers.search(user, args.q, args.type, args.resolve, args.following, args.account_id, args['exclude_unreviewed'], args.max_id, args.min_id, args.limit, args.offset, cache); - ctx.body = convertSearch(result); + ctx.body = convertSearchIds(result); } catch (e: any) { console.error(e); ctx.status = 400; @@ -62,7 +62,7 @@ export function setupEndpointsSearch(router: Router): void { ctx.request.hostname, accessTokens, ); - ctx.body = data.map((status) => convertStatus(status)); + ctx.body = data.map((status) => convertStatusIds(status)); } catch (e: any) { console.error(e); ctx.status = 401; @@ -81,7 +81,7 @@ export function setupEndpointsSearch(router: Router): void { query.limit || 20, ); data = data.map((suggestion) => { - suggestion.account = convertAccount(suggestion.account); + suggestion.account = convertAccountId(suggestion.account); return suggestion; }); console.log(data); diff --git a/packages/backend/src/server/api/mastodon/endpoints/status.ts b/packages/backend/src/server/api/mastodon/endpoints/status.ts index 776de78a6..3d2318af2 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/status.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/status.ts @@ -1,6 +1,6 @@ import Router from "@koa/router"; import { convertId, IdType } from "../../index.js"; -import { convertAccount, convertPoll, convertStatus, convertStatusEdit, convertStatusSource, } from "../converters.js"; +import { convertAccountId, convertPollId, convertStatusIds, convertStatusEditIds, convertStatusSourceId, } from "../converters.js"; import { NoteConverter } from "@/server/api/mastodon/converters/note.js"; import { getNote } from "@/server/api/common/getters.js"; import authenticate from "@/server/api/authenticate.js"; @@ -42,7 +42,7 @@ export function setupEndpointsStatus(router: Router): void { let request = NoteHelpers.normalizeComposeOptions(ctx.request.body); ctx.body = await NoteHelpers.createNote(request, user) .then(p => NoteConverter.encode(p, user)) - .then(p => convertStatus(p)); + .then(p => convertStatusIds(p)); if (key !== null) postIdempotencyCache.set(key, {status: ctx.body}); } catch (e: any) { @@ -76,7 +76,7 @@ export function setupEndpointsStatus(router: Router): void { let request = NoteHelpers.normalizeEditOptions(ctx.request.body); ctx.body = await NoteHelpers.editNote(request, note, user) .then(p => NoteConverter.encode(p, user)) - .then(p => convertStatus(p)); + .then(p => convertStatusIds(p)); } catch (e: any) { console.error(e); ctx.status = ctx.status == 404 ? 404 : 401; @@ -97,7 +97,7 @@ export function setupEndpointsStatus(router: Router): void { } const status = await NoteConverter.encode(note, user); - ctx.body = convertStatus(status); + ctx.body = convertStatusIds(status); } catch (e: any) { console.error(e); ctx.status = ctx.status == 404 ? 404 : 401; @@ -134,7 +134,7 @@ export function setupEndpointsStatus(router: Router): void { } ctx.body = await NoteHelpers.deleteNote(note, user) - .then(p => convertStatus(p)); + .then(p => convertStatusIds(p)); } catch (e: any) { console.error(`Error processing ${ctx.method} /api${ctx.path}: ${e.message}`); ctx.status = 500; @@ -163,10 +163,10 @@ export function setupEndpointsStatus(router: Router): void { const ancestors = await NoteHelpers.getNoteAncestors(note, user, user ? 4096 : 60) .then(n => NoteConverter.encodeMany(n, user, cache)) - .then(n => n.map(s => convertStatus(s))); + .then(n => n.map(s => convertStatusIds(s))); const descendants = await NoteHelpers.getNoteDescendants(note, user, user ? 4096 : 40, user ? 4096 : 20) .then(n => NoteConverter.encodeMany(n, user, cache)) - .then(n => n.map(s => convertStatus(s))); + .then(n => n.map(s => convertStatusIds(s))); ctx.body = { ancestors, @@ -195,7 +195,7 @@ export function setupEndpointsStatus(router: Router): void { } const res = await NoteHelpers.getNoteEditHistory(note); - ctx.body = res.map(p => convertStatusEdit(p)); + ctx.body = res.map(p => convertStatusEditIds(p)); } catch (e: any) { console.error(e); ctx.status = 401; @@ -219,7 +219,7 @@ export function setupEndpointsStatus(router: Router): void { } const src = NoteHelpers.getNoteSource(note); - ctx.body = convertStatusSource(src); + ctx.body = convertStatusSourceId(src); } catch (e: any) { console.error(e); ctx.status = 401; @@ -246,7 +246,7 @@ export function setupEndpointsStatus(router: Router): void { const args = normalizeUrlQuery(convertPaginationArgsIds(limitToInt(ctx.query as any))); const res = await NoteHelpers.getNoteRebloggedBy(note, user, args.max_id, args.since_id, args.min_id, args.limit); const users = await UserConverter.encodeMany(res.data, cache); - ctx.body = users.map(m => convertAccount(m)); + ctx.body = users.map(m => convertAccountId(m)); PaginationHelpers.appendLinkPaginationHeader(args, ctx, res, 40); } catch (e: any) { console.error(e); @@ -274,7 +274,7 @@ export function setupEndpointsStatus(router: Router): void { const args = normalizeUrlQuery(convertPaginationArgsIds(limitToInt(ctx.query as any))); const res = await NoteHelpers.getNoteFavoritedBy(note, args.max_id, args.since_id, args.min_id, args.limit); const users = await UserConverter.encodeMany(res.data, cache); - ctx.body = users.map(m => convertAccount(m)); + ctx.body = users.map(m => convertAccountId(m)); PaginationHelpers.appendLinkPaginationHeader(args, ctx, res, 40); } catch (e: any) { console.error(e); @@ -312,7 +312,7 @@ export function setupEndpointsStatus(router: Router): void { ctx.body = await NoteHelpers.reactToNote(note, user, reaction) .then(p => NoteConverter.encode(p, user)) - .then(p => convertStatus(p)); + .then(p => convertStatusIds(p)); } catch (e: any) { console.error(e); console.error(e.response.data); @@ -343,7 +343,7 @@ export function setupEndpointsStatus(router: Router): void { ctx.body = await NoteHelpers.removeReactFromNote(note, user) .then(p => NoteConverter.encode(p, user)) - .then(p => convertStatus(p)); + .then(p => convertStatusIds(p)); } catch (e: any) { console.error(e); ctx.status = 401; @@ -374,7 +374,7 @@ export function setupEndpointsStatus(router: Router): void { ctx.body = await NoteHelpers.reblogNote(note, user) .then(p => NoteConverter.encode(p, user)) - .then(p => convertStatus(p)); + .then(p => convertStatusIds(p)); } catch (e: any) { console.error(e); ctx.status = 401; @@ -405,7 +405,7 @@ export function setupEndpointsStatus(router: Router): void { ctx.body = await NoteHelpers.unreblogNote(note, user) .then(p => NoteConverter.encode(p, user)) - .then(p => convertStatus(p)); + .then(p => convertStatusIds(p)); } catch (e: any) { console.error(e); ctx.status = 401; @@ -436,7 +436,7 @@ export function setupEndpointsStatus(router: Router): void { ctx.body = await NoteHelpers.bookmarkNote(note, user) .then(p => NoteConverter.encode(p, user)) - .then(p => convertStatus(p)); + .then(p => convertStatusIds(p)); } catch (e: any) { console.error(e); ctx.status = 401; @@ -467,7 +467,7 @@ export function setupEndpointsStatus(router: Router): void { ctx.body = await NoteHelpers.unbookmarkNote(note, user) .then(p => NoteConverter.encode(p, user)) - .then(p => convertStatus(p)); + .then(p => convertStatusIds(p)); } catch (e: any) { console.error(e); ctx.status = 401; @@ -498,7 +498,7 @@ export function setupEndpointsStatus(router: Router): void { ctx.body = await NoteHelpers.pinNote(note, user) .then(p => NoteConverter.encode(p, user)) - .then(p => convertStatus(p)); + .then(p => convertStatusIds(p)); } catch (e: any) { console.error(e); ctx.status = 401; @@ -529,7 +529,7 @@ export function setupEndpointsStatus(router: Router): void { ctx.body = await NoteHelpers.unpinNote(note, user) .then(p => NoteConverter.encode(p, user)) - .then(p => convertStatus(p)); + .then(p => convertStatusIds(p)); } catch (e: any) { console.error(e); ctx.status = 401; @@ -560,7 +560,7 @@ export function setupEndpointsStatus(router: Router): void { ctx.body = await NoteHelpers.reactToNote(note, user, ctx.params.name) .then(p => NoteConverter.encode(p, user)) - .then(p => convertStatus(p)); + .then(p => convertStatusIds(p)); } catch (e: any) { console.error(e); ctx.status = 401; @@ -591,7 +591,7 @@ export function setupEndpointsStatus(router: Router): void { ctx.body = await NoteHelpers.removeReactFromNote(note, user) .then(p => NoteConverter.encode(p, user)) - .then(p => convertStatus(p)); + .then(p => convertStatusIds(p)); } catch (e: any) { console.error(e); ctx.status = 401; @@ -613,7 +613,7 @@ export function setupEndpointsStatus(router: Router): void { } const data = await PollHelpers.getPoll(note, user); - ctx.body = convertPoll(data); + ctx.body = convertPollId(data); } catch (e: any) { console.error(e); ctx.status = 401; @@ -649,7 +649,7 @@ export function setupEndpointsStatus(router: Router): void { } const data = await PollHelpers.voteInPoll(choices, note, user); - ctx.body = convertPoll(data); + ctx.body = convertPollId(data); } catch (e: any) { console.error(e); ctx.status = 401; diff --git a/packages/backend/src/server/api/mastodon/endpoints/timeline.ts b/packages/backend/src/server/api/mastodon/endpoints/timeline.ts index b6ed146b5..9740b4cc9 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/timeline.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/timeline.ts @@ -1,7 +1,7 @@ import Router from "@koa/router"; import { getClient } from "../index.js"; import { ParsedUrlQuery } from "querystring"; -import { convertConversation, convertStatus, } from "../converters.js"; +import { convertConversationIds, convertStatusIds, } from "../converters.js"; import { convertId, IdType } from "../../index.js"; import authenticate from "@/server/api/authenticate.js"; import { TimelineHelpers } from "@/server/api/mastodon/helpers/timeline.js"; @@ -79,7 +79,7 @@ export function setupEndpointsTimeline(router: Router): void { const tl = await TimelineHelpers.getPublicTimeline(user, args.max_id, args.since_id, args.min_id, args.limit, args.only_media, args.local, args.remote) .then(n => NoteConverter.encodeMany(n, user, cache)); - ctx.body = tl.map(s => convertStatus(s)); + ctx.body = tl.map(s => convertStatusIds(s)); } catch (e: any) { console.error(e); console.error(e.response.data); @@ -111,7 +111,7 @@ export function setupEndpointsTimeline(router: Router): void { const tl = await TimelineHelpers.getTagTimeline(user, tag, args.max_id, args.since_id, args.min_id, args.limit, args['any[]'] ?? [], args['all[]'] ?? [], args['none[]'] ?? [], args.only_media, args.local, args.remote) .then(n => NoteConverter.encodeMany(n, user, cache)); - ctx.body = tl.map(s => convertStatus(s)); + ctx.body = tl.map(s => convertStatusIds(s)); } catch (e: any) { ctx.status = 400; ctx.body = { error: e.message }; @@ -133,7 +133,7 @@ export function setupEndpointsTimeline(router: Router): void { const tl = await TimelineHelpers.getHomeTimeline(user, args.max_id, args.since_id, args.min_id, args.limit) .then(n => NoteConverter.encodeMany(n, user, cache)); - ctx.body = tl.map(s => convertStatus(s)); + ctx.body = tl.map(s => convertStatusIds(s)); } catch (e: any) { console.error(e); console.error(e.response.data); @@ -166,7 +166,7 @@ export function setupEndpointsTimeline(router: Router): void { const tl = await TimelineHelpers.getListTimeline(user, list, args.max_id, args.since_id, args.min_id, args.limit) .then(n => NoteConverter.encodeMany(n, user, cache)); - ctx.body = tl.map(s => convertStatus(s)); + ctx.body = tl.map(s => convertStatusIds(s)); } catch (e: any) { console.error(e); @@ -189,7 +189,7 @@ export function setupEndpointsTimeline(router: Router): void { const args = normalizeUrlQuery(convertPaginationArgsIds(limitToInt(ctx.query))); const res = await TimelineHelpers.getConversations(user, args.max_id, args.since_id, args.min_id, args.limit); - ctx.body = res.data.map(c => convertConversation(c)); + ctx.body = res.data.map(c => convertConversationIds(c)); PaginationHelpers.appendLinkPaginationHeader(args, ctx, res, 20); } catch (e: any) { console.error(e); diff --git a/packages/backend/src/server/api/mastodon/helpers/misc.ts b/packages/backend/src/server/api/mastodon/helpers/misc.ts index eec930e3e..51666b68c 100644 --- a/packages/backend/src/server/api/mastodon/helpers/misc.ts +++ b/packages/backend/src/server/api/mastodon/helpers/misc.ts @@ -5,7 +5,7 @@ import { Instances, Notes, Users } from "@/models/index.js"; import { IsNull } from "typeorm"; import { awaitAll } from "@/prelude/await-all.js"; import { UserConverter } from "@/server/api/mastodon/converters/user.js"; -import { convertAccount } from "@/server/api/mastodon/converters.js"; +import { convertAccountId } from "@/server/api/mastodon/converters.js"; export class MiscHelpers { public static async getInstance(): Promise { @@ -22,7 +22,7 @@ export class MiscHelpers { order: {id: "ASC"}, }) .then(p => p ? UserConverter.encode(p) : null) - .then(p => p ? convertAccount(p) : null); + .then(p => p ? convertAccountId(p) : null); const meta = await fetchMeta(true); const res = {