Repository: th-ch/youtube-music
File: src/plugins/discord/utils.ts (TypeScript)

Code before:
import type { GatewayActivityButton } from 'discord-api-types/v10';
import type { SongInfo } from '@/providers/song-info';
import type { DiscordPluginConfig } from './index';

/**
 * Truncates a string to a specified length, adding ellipsis if truncated.
 * @param str - The string to truncate.
 * @param length - The maximum allowed length.
 * @returns The truncated string.
 */
export const truncateString = (str: string, length: number): string => {
  if (str.length > length) {
    return `${str.substring(0, length - 3)}...`;
  }
  return str;
};

/**
 * Sanitizes a string for Discord Activity.
 * @param input - The string to sanitize.
 * @returns The sanitized string or undefined.
 */
export function sanitizeActivityText(input?: string): string {
  if (!input) {
    return 'undefined';
  }

  const trimmed = input.trim();
  let safeString = truncateString(trimmed, 128);

  if (safeString.length <= 0) {
    return 'undefined';
  }

  if (safeString.length < 2) {
    safeString = safeString + '⠀'; // change if you have better replacement
  }

  return safeString;
}

/**
 * Builds the array of buttons for the Discord Rich Presence activity.
 * @param config - The plugin configuration.
 * @param songInfo - The current song information.
 * @returns An array of buttons or undefined if no buttons are configured.
 */
export const buildDiscordButtons = (
  config: DiscordPluginConfig,
  songInfo: SongInfo,
): GatewayActivityButton[] | undefined => {

Review comment (security):
how about change the signiture of `sanitizeActivityText`?
`sanitizeActivityText: (input: string | undefined, fallback = 'undefined') => string` format seems to be better

Code after:
import type { GatewayActivityButton } from 'discord-api-types/v10';
import type { SongInfo } from '@/providers/song-info';
import type { DiscordPluginConfig } from './index';

/**
 * Truncates a string to a specified length, adding ellipsis if truncated.
 * @param str - The string to truncate.
 * @param length - The maximum allowed length.
 * @returns The truncated string.
 */
export const truncateString = (str: string, length: number): string => {
  if (str.length > length) {
    return `${str.substring(0, length - 3)}...`;
  }
  return str;
};

/**
 * Sanitizes a string for Discord Rich Presence activity, ensuring it meets length requirements.
 * @param input - The string to sanitize.
 * @param fallback - A fallback string to use if the input is empty or whitespace. Defaults to 'undefined'.
 * @returns The sanitized string, compliant with Discord's requirements.
 */
export function sanitizeActivityText(input: string | undefined, fallback: string = 'undefined'): string {
  const text = (input && input.trim()) ? input.trim() : fallback.trim();
  let safeString = truncateString(text, 128);

  if (safeString.length === 0) {
    return fallback;
  }

  if (safeString.length < 2) {
    safeString = safeString.padEnd(2, '⠀'); // change if necessary
  }

  return safeString;
}

/**
 * Builds the array of buttons for the Discord Rich Presence activity.
 * @param config - The plugin configuration.
 * @param songInfo - The current song information.
 * @returns An array of buttons or undefined if no buttons are configured.
 */
export const buildDiscordButtons = (
  config: DiscordPluginConfig,
  songInfo: SongInfo,
): GatewayActivityButton[] | undefined => {
  const buttons: GatewayActivityButton[] = [];
  if (config.playOnPearDesktop && songInfo.url) {
    buttons.push({