Localized release notes

This commit is contained in:
yash-signal 2025-01-02 12:56:56 -06:00 committed by GitHub
parent ebc01d0ce5
commit 55705a08d4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 79 additions and 15 deletions

View file

@ -106,20 +106,51 @@ export class ReleaseNotesFetcher {
}
const { uuid, ctaId, link } = note;
const result = await window.textsecure.server.getReleaseNote({
uuid,
});
strictAssert(
result.uuid === uuid,
'UUID of localized release note should match requested UUID'
);
const globalLocale = new Intl.Locale(window.SignalContext.getI18nLocale());
const localesToTry = [
globalLocale.toString(),
globalLocale.language.toString(),
'en',
].map(locale => locale.toLocaleLowerCase().replace('-', '_'));
return {
...result,
uuid,
ctaId,
link,
};
for (const localeToTry of localesToTry) {
try {
// eslint-disable-next-line no-await-in-loop
const hash = await window.textsecure.server.getReleaseNoteHash({
uuid,
locale: localeToTry,
});
if (hash === undefined) {
continue;
}
// eslint-disable-next-line no-await-in-loop
const result = await window.textsecure.server.getReleaseNote({
uuid,
locale: localeToTry,
});
strictAssert(
result.uuid === uuid,
'UUID of localized release note should match requested UUID'
);
return {
...result,
uuid,
ctaId,
link,
};
} catch {
// If either request fails, try the next locale
continue;
}
}
throw new Error(
`Could not fetch release note with any locale for UUID ${uuid}`
);
}
private async processReleaseNotes(

View file

@ -1216,6 +1216,7 @@ export type GetBackupInfoResponseType = z.infer<
export type GetReleaseNoteOptionsType = Readonly<{
uuid: string;
locale: string;
}>;
export const releaseNoteSchema = z.object({
@ -1408,6 +1409,9 @@ export type WebAPIType = {
getReleaseNote: (
options: GetReleaseNoteOptionsType
) => Promise<ReleaseNoteResponseType>;
getReleaseNoteHash: (
options: GetReleaseNoteOptionsType
) => Promise<string | undefined>;
getReleaseNotesManifest: () => Promise<ReleaseNotesManifestResponseType>;
getReleaseNotesManifestHash: () => Promise<string | undefined>;
getSticker: (packId: string, stickerId: number) => Promise<Uint8Array>;
@ -1880,6 +1884,7 @@ export function initialize({
getProfileUnauth,
getProvisioningResource,
getReleaseNote,
getReleaseNoteHash,
getReleaseNotesManifest,
getReleaseNotesManifestHash,
getTransferArchive,
@ -2174,15 +2179,43 @@ export function initialize({
languages: Record<string, Array<string>>;
};
}
async function getReleaseNoteHash({
uuid,
locale,
}: {
uuid: string;
locale: string;
}): Promise<string | undefined> {
const { response } = await _ajax({
call: 'releaseNotes',
host: resourcesUrl,
httpType: 'HEAD',
urlParameters: `/${uuid}/${locale}.json`,
responseType: 'byteswithdetails',
});
const etag = response.headers.get('etag');
if (etag == null) {
return undefined;
}
return etag;
}
async function getReleaseNote({
uuid,
}: GetReleaseNoteOptionsType): Promise<ReleaseNoteResponseType> {
locale,
}: {
uuid: string;
locale: string;
}): Promise<ReleaseNoteResponseType> {
const rawRes = await _ajax({
call: 'releaseNotes',
host: resourcesUrl,
httpType: 'GET',
responseType: 'json',
urlParameters: `/${uuid}/en.json`,
urlParameters: `/${uuid}/${locale}.json`,
});
return parseUnknown(releaseNoteSchema, rawRes);
}