[mastodon-client] Use structuredClone for id converters

This commit is contained in:
Laura Hausmann 2023-10-07 00:27:18 +02:00
parent 3bc4afec77
commit 9dd3277ddb
No known key found for this signature in database
GPG key ID: D044E84C5BE01605

View file

@ -1,10 +1,12 @@
import { convertId, IdType } from "../index.js"; import { convertId, IdType } from "../index.js";
// It's *very* important to put `param = structuredClone(param)` at the top of each function that doesn't simply call simpleConvertId on the param object directly.
function simpleConvertId(data: any) { function simpleConvertId(data: any) {
// copy the object to bypass weird pass by reference bugs // copy the object to bypass weird pass by reference bugs
const result = Object.assign({}, data); data = structuredClone(data);
result.id = convertId(data.id, IdType.MastodonId); data.id = convertId(data.id, IdType.MastodonId);
return result; return data;
} }
export function convertAccountId(account: MastodonEntity.Account | MastodonEntity.MutedAccount) { export function convertAccountId(account: MastodonEntity.Account | MastodonEntity.MutedAccount) {
@ -23,12 +25,26 @@ export function convertListId(list: MastodonEntity.List) {
return simpleConvertId(list); return simpleConvertId(list);
} }
export function convertPollId(poll: MastodonEntity.Poll) {
return simpleConvertId(poll);
}
export function convertRelationshipId(relationship: MastodonEntity.Relationship) {
return simpleConvertId(relationship);
}
export function convertStatusSourceId(statusSource: MastodonEntity.StatusSource) {
return simpleConvertId(statusSource);
}
export function convertSuggestionIds(suggestion: MastodonEntity.SuggestedAccount) { export function convertSuggestionIds(suggestion: MastodonEntity.SuggestedAccount) {
suggestion = structuredClone(suggestion);
suggestion.account = convertAccountId(suggestion.account) suggestion.account = convertAccountId(suggestion.account)
return suggestion return suggestion
} }
export function convertNotificationIds(notification: MastodonEntity.Notification) { export function convertNotificationIds(notification: MastodonEntity.Notification) {
notification = structuredClone(notification);
notification.account = convertAccountId(notification.account); notification.account = convertAccountId(notification.account);
notification.id = convertId(notification.id, IdType.MastodonId); notification.id = convertId(notification.id, IdType.MastodonId);
if (notification.status) if (notification.status)
@ -38,32 +54,23 @@ export function convertNotificationIds(notification: MastodonEntity.Notification
return notification; return notification;
} }
export function convertPollId(poll: MastodonEntity.Poll) {
return simpleConvertId(poll);
}
export function convertReactionIds(reaction: MastodonEntity.Reaction) { export function convertReactionIds(reaction: MastodonEntity.Reaction) {
reaction = structuredClone(reaction);
if (reaction.accounts) { if (reaction.accounts) {
reaction.accounts = reaction.accounts.map(convertAccountId); reaction.accounts = reaction.accounts.map(convertAccountId);
} }
return reaction; return reaction;
} }
export function convertRelationshipId(relationship: MastodonEntity.Relationship) {
return simpleConvertId(relationship);
}
export function convertSearchIds(search: MastodonEntity.Search) { export function convertSearchIds(search: MastodonEntity.Search) {
search = structuredClone(search);
search.accounts = search.accounts.map(p => convertAccountId(p)); search.accounts = search.accounts.map(p => convertAccountId(p));
search.statuses = search.statuses.map(p => convertStatusIds(p)); search.statuses = search.statuses.map(p => convertStatusIds(p));
return search; return search;
} }
export function convertStatusSourceId(statusSource: MastodonEntity.StatusSource) {
return simpleConvertId(statusSource);
}
export function convertStatusIds(status: MastodonEntity.Status) { export function convertStatusIds(status: MastodonEntity.Status) {
status = structuredClone(status);
status.account = convertAccountId(status.account); status.account = convertAccountId(status.account);
status.id = convertId(status.id, IdType.MastodonId); status.id = convertId(status.id, IdType.MastodonId);
if (status.in_reply_to_account_id) if (status.in_reply_to_account_id)
@ -89,6 +96,7 @@ export function convertStatusIds(status: MastodonEntity.Status) {
} }
export function convertStatusEditIds(edit: MastodonEntity.StatusEdit) { export function convertStatusEditIds(edit: MastodonEntity.StatusEdit) {
edit = structuredClone(edit);
edit.account = convertAccountId(edit.account); edit.account = convertAccountId(edit.account);
edit.media_attachments = edit.media_attachments.map((attachment) => edit.media_attachments = edit.media_attachments.map((attachment) =>
convertAttachmentId(attachment), convertAttachmentId(attachment),
@ -98,6 +106,7 @@ export function convertStatusEditIds(edit: MastodonEntity.StatusEdit) {
} }
export function convertConversationIds(conversation: MastodonEntity.Conversation) { export function convertConversationIds(conversation: MastodonEntity.Conversation) {
conversation = structuredClone(conversation);
conversation.id = convertId(conversation.id, IdType.MastodonId); conversation.id = convertId(conversation.id, IdType.MastodonId);
conversation.accounts = conversation.accounts.map(convertAccountId); conversation.accounts = conversation.accounts.map(convertAccountId);
if (conversation.last_status) { if (conversation.last_status) {