Add Discord collectibles to profiles, messages, and settings.
Trigger cinny-mobile / dispatch (push) Successful in 1s
Trigger cinny-mobile / dispatch (push) Successful in 1s
Integrate profile effects, nameplates, and avatar decorations with live catalog browsing, Matrix profile storage, and rendering across user heroes, DMs, messages, and the sidebar avatar.
This commit is contained in:
@@ -13,6 +13,28 @@ export const PROFILE_KEY_BANNER_URL_UNSTABLE = 'chat.commet.profile_banner';
|
||||
/** MSC4427 stable profile field for banner */
|
||||
export const PROFILE_KEY_BANNER_URL_STABLE = 'm.banner_url';
|
||||
|
||||
/** Paarrot profile effect (animated profile background) */
|
||||
export const PROFILE_KEY_PROFILE_EFFECT = 'im.paarrot.profile_effect';
|
||||
|
||||
/** Paarrot nameplate (username bar background) */
|
||||
export const PROFILE_KEY_NAMEPLATE = 'im.paarrot.nameplate';
|
||||
|
||||
/** Paarrot avatar decoration (avatar frame) */
|
||||
export const PROFILE_KEY_AVATAR_DECORATION = 'im.paarrot.avatar_decoration';
|
||||
|
||||
export type StoredCollectible = {
|
||||
sku_id: string;
|
||||
name: string;
|
||||
assets: Record<string, string>;
|
||||
effect?: unknown;
|
||||
};
|
||||
|
||||
export type CollectibleProfileFields = {
|
||||
profile_effect?: StoredCollectible;
|
||||
nameplate?: StoredCollectible;
|
||||
avatar_decoration?: StoredCollectible;
|
||||
};
|
||||
|
||||
export type ColorPreference = {
|
||||
on_dark?: string;
|
||||
on_light?: string;
|
||||
@@ -66,6 +88,89 @@ export function extractBannerUrlFromProfile(profile: Record<string, unknown>): s
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function parseStoredCollectible(value: unknown): StoredCollectible | undefined {
|
||||
if (!value || typeof value !== 'object') return undefined;
|
||||
const obj = value as Record<string, unknown>;
|
||||
const sku_id = typeof obj.sku_id === 'string' ? obj.sku_id : undefined;
|
||||
const name = typeof obj.name === 'string' ? obj.name : undefined;
|
||||
const assets = obj.assets;
|
||||
if (!sku_id || !name || !assets || typeof assets !== 'object') return undefined;
|
||||
|
||||
const parsedAssets: Record<string, string> = {};
|
||||
for (const [key, mxc] of Object.entries(assets as Record<string, unknown>)) {
|
||||
if (typeof mxc === 'string' && mxc.startsWith('mxc://')) {
|
||||
parsedAssets[key] = mxc;
|
||||
}
|
||||
}
|
||||
if (Object.keys(parsedAssets).length === 0) return undefined;
|
||||
|
||||
return {
|
||||
sku_id,
|
||||
name,
|
||||
assets: parsedAssets,
|
||||
effect: obj.effect,
|
||||
};
|
||||
}
|
||||
|
||||
export function extractCollectiblesFromProfile(profile: Record<string, unknown>): CollectibleProfileFields {
|
||||
return {
|
||||
profile_effect: parseStoredCollectible(profile[PROFILE_KEY_PROFILE_EFFECT]),
|
||||
nameplate: parseStoredCollectible(profile[PROFILE_KEY_NAMEPLATE]),
|
||||
avatar_decoration: parseStoredCollectible(profile[PROFILE_KEY_AVATAR_DECORATION]),
|
||||
};
|
||||
}
|
||||
|
||||
async function loadCollectiblesFromProfile(mx: MatrixClient, userId: string): Promise<CollectibleProfileFields> {
|
||||
if (await mx.doesServerSupportExtendedProfiles()) {
|
||||
try {
|
||||
const profile = await mx.getExtendedProfile(userId);
|
||||
return extractCollectiblesFromProfile(profile);
|
||||
} catch {
|
||||
// fall through
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const profile = (await mx.getProfileInfo(userId)) as Record<string, unknown>;
|
||||
return extractCollectiblesFromProfile(profile);
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
export async function loadUserCollectibles(mx: MatrixClient, userId: string): Promise<CollectibleProfileFields> {
|
||||
return loadCollectiblesFromProfile(mx, userId);
|
||||
}
|
||||
|
||||
export async function saveCollectible(
|
||||
mx: MatrixClient,
|
||||
key: string,
|
||||
collectible: StoredCollectible | undefined
|
||||
): Promise<void> {
|
||||
if (!(await mx.doesServerSupportExtendedProfiles())) {
|
||||
throw new Error('Server does not support extended profile fields (MSC4133)');
|
||||
}
|
||||
|
||||
if (!collectible) {
|
||||
await mx.deleteExtendedProfileProperty(key);
|
||||
return;
|
||||
}
|
||||
|
||||
await mx.setExtendedProfileProperty(key, collectible);
|
||||
}
|
||||
|
||||
export async function saveProfileEffect(mx: MatrixClient, collectible: StoredCollectible | undefined): Promise<void> {
|
||||
await saveCollectible(mx, PROFILE_KEY_PROFILE_EFFECT, collectible);
|
||||
}
|
||||
|
||||
export async function saveNameplate(mx: MatrixClient, collectible: StoredCollectible | undefined): Promise<void> {
|
||||
await saveCollectible(mx, PROFILE_KEY_NAMEPLATE, collectible);
|
||||
}
|
||||
|
||||
export async function saveAvatarDecoration(mx: MatrixClient, collectible: StoredCollectible | undefined): Promise<void> {
|
||||
await saveCollectible(mx, PROFILE_KEY_AVATAR_DECORATION, collectible);
|
||||
}
|
||||
|
||||
export async function getBannerUrlProfileKey(mx: MatrixClient): Promise<string> {
|
||||
if (await mx.isVersionSupported('v1.16')) {
|
||||
return PROFILE_KEY_BANNER_URL_STABLE;
|
||||
|
||||
Reference in New Issue
Block a user