Files
Sable/src/app/hooks/usePerMessageProfile.ts
T

569 lines
21 KiB
TypeScript

import type { AccountDataCompatVersion } from '$types/matrix/accountData';
import type { PronounSet } from '$utils/pronouns';
import type { MatrixClient } from '$types/matrix-sdk';
import { CustomAccountDataEvent } from '$types/matrix/accountData';
import { MATRIX_UNSTABLE_PROFILE_PRONOUNS_PROPERTY_NAME } from '$unstable/prefixes';
const ACCOUNT_DATA_PREFIX = CustomAccountDataEvent.SablePerProfileMessageProfiles;
/**
* a per message profile
*/
export type PerMessageProfile = {
/**
* a unique id for this profile, can be generated using something like nanoid.
* This is used to identify the profile when applying it to a message, and also used as the key when storing the profile in account data.
*/
id: string;
/**
* the display name to use for messages using this profile.
* This is required because otherwise the profile would have no effect on the message.
*/
name: string;
/**
* the avatar url to use for messages using this profile.
*/
avatarUrl?: string;
/**
* a per message profile can also include pronouns
* @see PronounSet for the format of the pronouns, and how to parse them from a string input
*/
pronouns?: PronounSet[];
compat?: AccountDataCompatVersion;
};
/**
* the format used by Beeper for per message profiles
* This is the format that Beeper expects when applying a profile to a message before sending it
*/
export type PerMessageProfileBeeperFormat = {
/**
* the unique id for this profile, which is used to identify the profile when applying it to a message, and also used as the key when storing the profile in account data.
*/
id: string;
/**
* the display name to use for messages using this profile. This is required because otherwise the profile would have no effect on the message.
*/
displayname?: string;
/**
* the avatar url to use for messages using this profile.
* Beeper expects this to be a mxc url.
*/
avatar_url?: string;
/**
* using the unstable prefix for pronouns, under which it is also stored in profiles
*/
[MATRIX_UNSTABLE_PROFILE_PRONOUNS_PROPERTY_NAME]?: PronounSet[];
has_fallback?: boolean;
};
/**
* converts a per message profile from our format to the format used by Beeper, which is used when applying the profile to a message before sending it.
* We have out own format because we want to have more control over the data and how it's stored in account data.
* @export
* @param {PerMessageProfile} profile the per message profile in our format
* @return {*} {PerMessageProfileBeeperFormat} the per message profile in Beeper's format, which can be applied to a message before sending it
*/
export function convertPerMessageProfileToBeeperFormat(
profile: PerMessageProfile,
has_fallback: boolean
): PerMessageProfileBeeperFormat {
const beeperPMP: PerMessageProfileBeeperFormat = {
id: profile.id,
displayname: profile.name,
avatar_url: profile.avatarUrl,
[MATRIX_UNSTABLE_PROFILE_PRONOUNS_PROPERTY_NAME]: profile.pronouns,
has_fallback,
};
// delete empty fields
// to-do maybe find a better way of doing it
if (!profile.name || profile?.name.trim().length === 0) delete beeperPMP.displayname;
if (!profile.avatarUrl) delete beeperPMP.avatar_url;
if (!profile.pronouns || profile.pronouns?.length === 0)
delete beeperPMP[MATRIX_UNSTABLE_PROFILE_PRONOUNS_PROPERTY_NAME];
if (!has_fallback) delete beeperPMP.has_fallback;
return beeperPMP;
}
/**
* converts a per message profile from Beeper's format to our format, which is used when storing the profile in account data and using it in the app.
* We have our own format because we want to have more control over the data and how it's stored in account data.
*
* @export
* @param {PerMessageProfileBeeperFormat} beeperProfile the per message profile in Beeper's format
* @return {*} {PerMessageProfile} the per message profile in our format, which can be stored in account data and used in the app
*/
export function convertBeeperFormatToOurPerMessageProfile(
beeperProfile: PerMessageProfileBeeperFormat
): PerMessageProfile {
return {
id: beeperProfile.id,
name: beeperProfile.displayname ?? '',
avatarUrl: beeperProfile.avatar_url,
pronouns: beeperProfile[MATRIX_UNSTABLE_PROFILE_PRONOUNS_PROPERTY_NAME],
};
}
type PerMessageProfileIndex = {
/**
* a list of all profile ids, used to list all profiles when the user wants to manage them.
*/
profileIds: string[];
compat: AccountDataCompatVersion;
};
/**
* how we will store room associations in the account data :3
*/
type PerMessageProfileRoomAssociation = {
profileId: string;
validUntil?: number;
};
/**
* associating a profile by proxy
* @author Rye
*/
export type PerMessageProfileProxyAssociation = {
/**
* the profile associated with the proxy
*/
profileId: string;
/**
* regex (string representation of it) to handle the proxy
*/
regexString: string;
/**
* optional parameter to save when the proxy was added
*/
setAt?: number;
};
export type InternalPerMessageProfileProxyAssociation = {
/**
* the profile associated with the proxy
*/
profileId: string;
/**
* regex to handle the proxy
*/
regex: RegExp;
/**
* optional parameter to save when the proxy was added
*/
setAt?: number;
};
export function parsePerMessageProfileProxyAssociation(
assoc: PerMessageProfileProxyAssociation
): InternalPerMessageProfileProxyAssociation {
return {
profileId: assoc.profileId,
// we need to remove artifacts from the toString conversion
regex: new RegExp(assoc.regexString.slice(1, -1)),
setAt: assoc.setAt,
} satisfies InternalPerMessageProfileProxyAssociation;
}
type PerMessageProfileProxyAssociationWrapper = {
/**
* the associations saved in the wrapper
*/
associations:
| Map<string, PerMessageProfileProxyAssociation>
| Record<string, PerMessageProfileProxyAssociation>;
/**
* optional parameter to save compatibility information
*/
compat?: AccountDataCompatVersion;
};
/**
* the shape of the account data for room associations, which is a wrapper around a list of associations.
* This is used to store the associations in account data, and allows us to easily add additional fields in the future if needed without breaking the existing data structure.
*/
type PerMessageProfileRoomAssociationWrapper = {
/**
* Key-Value pairs of room ids and profile ids, used to apply a profile to all messages in a room without having to set the profile for each message individually.
* The key is the room id, and the value is the profile id. The profile id can then be used to fetch the profile data when applying the profile to a message before sending it.
*
* @type {Map<string, PerMessageProfileRoomAssociation>}
*/
associations:
| Map<string, PerMessageProfileRoomAssociation>
| Record<string, PerMessageProfileRoomAssociation>;
compat?: AccountDataCompatVersion;
};
/**
* unwrap a profile-room-associations-wrapper
* @param wrapper the wrapper to unwrap
* @returns unwrapped map for profile-room-associations
*/
function getAssociationsMap(
wrapper?: PerMessageProfileRoomAssociationWrapper
): Map<string, PerMessageProfileRoomAssociation> {
if (!wrapper?.associations) return new Map();
if (wrapper.associations instanceof Map) return wrapper.associations;
return new Map(Object.entries(wrapper.associations));
}
// Helper to always get a plain object from a Map
function associationsMapToObject(
map: Map<string, PerMessageProfileRoomAssociation>
): Record<string, PerMessageProfileRoomAssociation> {
return Object.fromEntries(map);
}
/**
* helper function (similar to getAssociationsMap for Room associations)
* @param wrapper the wrapper to unwrap
* @returns unwrapped map of proxy associations
*/
function getProxyAssociationMap(
wrapper?: PerMessageProfileProxyAssociationWrapper
): Map<string, PerMessageProfileProxyAssociation> {
if (!wrapper?.associations) return new Map();
if (wrapper.associations instanceof Map) return wrapper.associations;
return new Map(Object.entries(wrapper.associations));
}
function proxyAssociationsMapToObject(
map: Map<string, PerMessageProfileProxyAssociation>
): Record<string, PerMessageProfileProxyAssociation> {
return Object.fromEntries(map);
}
/**
* getting a profile from the account data where the profile matches a given id
*
* @export
* @param {MatrixClient} mx the matrix client
* @param {string} id the profile id
* @return {*} {(Promise<PerMessageProfile | undefined>)} the profile, with the profile Id, if it exists
*/
export async function getPerMessageProfileById(
mx: MatrixClient,
id: string
): Promise<PerMessageProfile | undefined> {
const profile = mx.getAccountData(
`${ACCOUNT_DATA_PREFIX}.${id}` as Parameters<typeof mx.getAccountData>[0]
);
return profile ? (profile.getContent() as unknown as PerMessageProfile) : undefined;
}
/**
* getting an array of all PerMessageProfile's saved in the account data
*
* @export
* @param {MatrixClient} mx the matrix client
* @return {*} {Promise<PerMessageProfile[]>} a array containing all per-message-profiles saved
*/
export async function getAllPerMessageProfiles(mx: MatrixClient): Promise<PerMessageProfile[]> {
const profileData = mx.getAccountData(
`${ACCOUNT_DATA_PREFIX}.index` as Parameters<typeof mx.getAccountData>[0]
);
const profileIds = (profileData?.getContent() as PerMessageProfileIndex)?.profileIds || [];
const profiles = await Promise.all(profileIds.map((id) => getPerMessageProfileById(mx, id)));
return profiles.filter((profile): profile is PerMessageProfile => profile !== undefined);
}
/**
* add or update a pmp
* @param mx the matrix client
* @param profile the profile to add/update
* @returns void
*/
export function addOrUpdatePerMessageProfile(mx: MatrixClient, profile: PerMessageProfile) {
const profileListIndex = mx.getAccountData(
`${ACCOUNT_DATA_PREFIX}.index` as Parameters<typeof mx.getAccountData>[0]
);
const profileWithCompat = {
...profile,
compat: {
version: 1,
compatDate: '2026-03-26',
} satisfies AccountDataCompatVersion,
} satisfies PerMessageProfile;
if (profileListIndex?.getContent()?.profileIds.includes(profile.id)) {
return mx.setAccountData(
`${ACCOUNT_DATA_PREFIX}.${profile.id}` as Parameters<typeof mx.setAccountData>[0],
profileWithCompat as Parameters<typeof mx.setAccountData>[1]
);
}
const newProfileIds = [...(profileListIndex?.getContent()?.profileIds || []), profile.id];
return Promise.all([
mx.setAccountData(
`${ACCOUNT_DATA_PREFIX}.index` as Parameters<typeof mx.setAccountData>[0],
{ profileIds: newProfileIds } as Parameters<typeof mx.setAccountData>[1]
),
mx.setAccountData(
`${ACCOUNT_DATA_PREFIX}.${profile.id}` as Parameters<typeof mx.setAccountData>[0],
profileWithCompat as Parameters<typeof mx.setAccountData>[1]
),
]);
}
/**
* remove a id from the index of profile ids, used when deleting a profile to
* remove the id from the list of profiles, so it doesn't show up in the list of profiles to manage anymore.
* The actual profile data is also removed when deleting a profile, but this function is only responsible
* for removing the id from the index.
* @param mx the matrix client
* @param id the id to drop from the index
*/
async function dropIdFromIndex(mx: MatrixClient, id: string) {
const profileListIndex = mx.getAccountData(
`${ACCOUNT_DATA_PREFIX}.index` as Parameters<typeof mx.getAccountData>[0]
);
const profileIds = profileListIndex?.getContent()?.profileIds || [];
const newProfileIds = profileIds.filter((profileId: string) => profileId !== id);
await mx.setAccountData(
`${ACCOUNT_DATA_PREFIX}.index` as Parameters<typeof mx.setAccountData>[0],
{ profileIds: newProfileIds } as Parameters<typeof mx.setAccountData>[1]
);
}
async function getRoomsUsingProfile(mx: MatrixClient, profileId: string): Promise<string[]> {
const accountData = mx.getAccountData(
`${ACCOUNT_DATA_PREFIX}.roomassociation` as Parameters<typeof mx.getAccountData>[0]
);
const content: PerMessageProfileRoomAssociationWrapper | undefined = accountData?.getContent();
const associations = getAssociationsMap(content);
const roomsUsingProfile: string[] = [];
Array.from(associations.entries()).forEach(([roomId, assoc]) => {
if (assoc?.profileId === profileId) roomsUsingProfile.push(roomId);
});
return roomsUsingProfile;
}
/**
* sets the per message profile to be used for messages in a room. This is done by setting account data with a list of room associations, which is then checked when sending a message to apply the profile to the message if the room matches an association. The associations can also have an optional expiration time, after which they will be ignored and removed.
* @param mx matrix client
* @param roomId the room id your querying for
* @param profileId the profile id you are querying for
* @param validUntil the timestamp until the pmp association is valid
* @param reset if true, the association for the room will be removed, if false and profileId is undefined, the association will be set to undefined but not removed, meaning it will still be visible in the list of associations but won't have any effect. This is useful for resetting the association without losing the information of which profile was associated before.
* @returns promose that resolves when the association has been set
*/
export async function setCurrentlyUsedPerMessageProfileIdForRoom(
mx: MatrixClient,
roomId: string,
profileId: string | undefined,
validUntil?: number,
reset?: boolean
) {
const accountData = mx.getAccountData(
`${ACCOUNT_DATA_PREFIX}.roomassociation` as Parameters<typeof mx.getAccountData>[0]
);
const content: PerMessageProfileRoomAssociationWrapper | undefined = accountData?.getContent();
const associations = getAssociationsMap(content);
if (reset) {
associations.delete(roomId);
mx.setAccountData(
`${ACCOUNT_DATA_PREFIX}.roomassociation` as Parameters<typeof mx.setAccountData>[0],
{ associations: associationsMapToObject(associations) } as Parameters<
typeof mx.setAccountData
>[1]
);
return;
}
if (!profileId) {
throw new Error("profile Id is empty, yet it isn't a reset");
}
associations.set(roomId, { profileId, validUntil });
mx.setAccountData(
`${ACCOUNT_DATA_PREFIX}.roomassociation` as Parameters<typeof mx.setAccountData>[0],
{ associations: associationsMapToObject(associations) } as Parameters<
typeof mx.setAccountData
>[1]
);
}
/**
*
* @param mx the matrix client
* @param profileId the profile id which the prefix should be attached to
* @param proxy the prefix to use as index
* @param proxyRegExp the regex we can use to match the prefix
* @param reset wheather to delete the prefix
*/
export async function associateProxyWithProfile(
mx: MatrixClient,
profileId: string | undefined,
proxy: string,
proxyRegExp: RegExp,
reset: boolean
) {
const associations = getProxyAssociationMap(
mx
.getAccountData(
`${ACCOUNT_DATA_PREFIX}.proxyassociation` as Parameters<typeof mx.getAccountData>[0]
)
?.getContent()
);
if (reset) associations.delete(proxy);
if (!profileId) throw new Error('profileId might not be undefined');
if (profileId)
associations.set(proxy, {
profileId,
regexString: proxyRegExp.toString(),
} satisfies PerMessageProfileProxyAssociation);
mx.setAccountData(
`${ACCOUNT_DATA_PREFIX}.proxyassociation` as Parameters<typeof mx.setAccountData>[0],
{ associations: proxyAssociationsMapToObject(associations) } as Parameters<
typeof mx.setAccountData
>[1]
);
}
/**
* get a profile based on a proxy
* @param mx the matrix client
* @param proxy the proxy to look for
* @returns the profile, if any, associated with the prefix
*/
export async function getProfileAssociatedWithProxy(
mx: MatrixClient,
proxy: string
): Promise<PerMessageProfile | undefined> {
const profileId = getAssociationsMap(
mx
.getAccountData(
`${ACCOUNT_DATA_PREFIX}.proxyassociation` as Parameters<typeof mx.getAccountData>[0]
)
?.getContent()
).get(proxy)?.profileId;
if (!profileId) return undefined;
return getPerMessageProfileById(mx, profileId);
}
/**
*
*
* @export
* @param {MatrixClient} mx the matrix client
* @return {*} {Promise<PerMessageProfileProxyAssociation[]>}
*/
export async function getAllPerMessageProfileProxies(
mx: MatrixClient
): Promise<PerMessageProfileProxyAssociation[]> {
const cont: PerMessageProfileProxyAssociationWrapper | undefined = mx
.getAccountData(
`${ACCOUNT_DATA_PREFIX}.proxyassociation` as Parameters<typeof mx.getAccountData>[0]
)
?.getContent();
if (!cont) return [];
const pmap = getProxyAssociationMap(cont);
const parr = new Array<PerMessageProfileProxyAssociation>();
pmap.values().forEach((v) => parr.push(v));
return parr;
}
export async function dropProxyAssociationForPMP(mx: MatrixClient, proxy: string) {
const associations = getProxyAssociationMap(
mx
.getAccountData(
`${ACCOUNT_DATA_PREFIX}.proxyassociation` as Parameters<typeof mx.getAccountData>[0]
)
?.getContent()
);
if (!associations) return;
associations.delete(proxy);
mx.setAccountData(
`${ACCOUNT_DATA_PREFIX}.proxyassociation` as Parameters<typeof mx.setAccountData>[0],
{ associations: proxyAssociationsMapToObject(associations) } as Parameters<
typeof mx.setAccountData
>[1]
);
}
/**
*
* drops all room associations for a profile, used when deleting a profile to make sure there are no dangling associations left that point to a non existing profile, which could cause issues when trying to apply the profile to a message in a room that still has an association for the deleted profile.
*
* @param {MatrixClient} mx the matrix client
* @param {string} id the id of the profile to drop associations for
*/
async function dropPerMessageProfileRoomAssociations(mx: MatrixClient, id: string) {
const accountData = mx.getAccountData(
`${ACCOUNT_DATA_PREFIX}.roomassociation` as Parameters<typeof mx.getAccountData>[0]
);
const content: PerMessageProfileRoomAssociationWrapper | undefined = accountData?.getContent();
if (!content) return;
const associations = getAssociationsMap(content);
const roomsUsingProfile = await getRoomsUsingProfile(mx, id);
if (roomsUsingProfile.length === 0) return;
roomsUsingProfile.forEach((roomId) => {
associations.delete(roomId);
});
await mx.setAccountData(
`${ACCOUNT_DATA_PREFIX}.roomassociation` as Parameters<typeof mx.setAccountData>[0],
{ associations: associationsMapToObject(associations) } as Parameters<
typeof mx.setAccountData
>[1]
);
}
/**
* deletes a per message profile by its id
* @param mx the matrix client
* @param id the id of the profile to delete
*/
export async function deletePerMessageProfile(mx: MatrixClient, id: string) {
await dropPerMessageProfileRoomAssociations(mx, id);
await mx.setAccountData(
`${ACCOUNT_DATA_PREFIX}.${id}` as Parameters<typeof mx.setAccountData>[0],
{}
);
await dropIdFromIndex(mx, id);
}
/**
* move a profile from one id to another, used when renaming a profile to change the id.
* This is done by creating a new profile with the new id and the same data as the old profile, and then deleting the old profile.
* @param mx the matrix client
* @param oldId the id the profile is currently saved under
* @param newId the id it will be moved to
*/
export async function renamePerMessageProfile(mx: MatrixClient, oldId: string, newId: string) {
const profile = await getPerMessageProfileById(mx, oldId);
if (!profile) {
throw new Error('Profile not found');
}
const newProfile = { ...profile, id: newId };
await addOrUpdatePerMessageProfile(mx, newProfile);
await deletePerMessageProfile(mx, oldId);
}
export async function getListOfRoomsUsingProfile(
mx: MatrixClient,
profileId: string
): Promise<string[]> {
return getRoomsUsingProfile(mx, profileId);
}
/**
* gets the per message profile to be used for messages in a room
* @param mx matrix client
* @param roomId the room id you are querying for
* @returns the profile to be used
*/
export async function getCurrentlyUsedPerMessageProfileForRoom(
mx: MatrixClient,
roomId: string
): Promise<PerMessageProfile | undefined> {
const accountData = mx.getAccountData(
`${ACCOUNT_DATA_PREFIX}.roomassociation` as Parameters<typeof mx.getAccountData>[0]
);
const content: PerMessageProfileRoomAssociationWrapper | undefined = accountData?.getContent();
const associations = getAssociationsMap(content);
const profileId = associations.get(roomId)?.profileId;
const pmp = profileId ? await getPerMessageProfileById(mx, profileId) : undefined;
return profileId ? pmp : undefined;
}