Skip to content

Commit

Permalink
fix(sw): プッシュ通知でカスタム絵文字リアクションを表示できるように (#10122)
Browse files Browse the repository at this point in the history
* fix(sw): プッシュ通知でカスタム絵文字リアクションを表示できるように
Fix #10117
/emoji/*.webpにbadgeモードを

* ?

* ✌️

* fix
  • Loading branch information
tamaina authored Feb 28, 2023
1 parent c63447e commit ffe3386
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
20 changes: 14 additions & 6 deletions packages/backend/src/server/ServerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class ServerService {
fastify.register(this.nodeinfoServerService.createServer);
fastify.register(this.wellKnownServerService.createServer);

fastify.get<{ Params: { path: string }; Querystring: { static?: any; }; }>('/emoji/:path(.*)', async (request, reply) => {
fastify.get<{ Params: { path: string }; Querystring: { static?: any; badge?: any; }; }>('/emoji/:path(.*)', async (request, reply) => {
const path = request.params.path;

reply.header('Cache-Control', 'public, max-age=86400');
Expand Down Expand Up @@ -105,11 +105,19 @@ export class ServerService {
}
}

const url = new URL(`${this.config.mediaProxy}/emoji.webp`);
// || emoji.originalUrl してるのは後方互換性のため(publicUrlはstringなので??はだめ)
url.searchParams.set('url', emoji.publicUrl || emoji.originalUrl);
url.searchParams.set('emoji', '1');
if ('static' in request.query) url.searchParams.set('static', '1');
let url: URL;
if ('badge' in request.query) {
url = new URL(`${this.config.mediaProxy}/emoji.png`);
// || emoji.originalUrl してるのは後方互換性のため(publicUrlはstringなので??はだめ)
url.searchParams.set('url', emoji.publicUrl || emoji.originalUrl);
url.searchParams.set('badge', '1');
} else {
url = new URL(`${this.config.mediaProxy}/emoji.webp`);
// || emoji.originalUrl してるのは後方互換性のため(publicUrlはstringなので??はだめ)
url.searchParams.set('url', emoji.publicUrl || emoji.originalUrl);
url.searchParams.set('emoji', '1');
if ('static' in request.query) url.searchParams.set('static', '1');
}

return await reply.redirect(
301,
Expand Down
26 changes: 6 additions & 20 deletions packages/sw/src/scripts/create-notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,26 +138,11 @@ async function composeNotification(data: pushNotificationDataMap[keyof pushNotif

if (reaction.startsWith(':')) {
// カスタム絵文字の場合
const customEmoji = data.body.note.emojis.find(x => x.name === reaction.substr(1, reaction.length - 2));
if (customEmoji) {
if (reaction.includes('@')) {
reaction = `:${reaction.substr(1, reaction.indexOf('@') - 1)}:`;
}

const u = new URL(customEmoji.url);
if (u.href.startsWith(`${origin}/proxy/`)) {
// もう既にproxyっぽそうだったらsearchParams付けるだけ
u.searchParams.set('badge', '1');
badge = u.href;
} else {
// 拡張子がないとキャッシュしてくれないCDNがあるので
const dummy = `${encodeURIComponent(`${u.host}${u.pathname}`)}.png`;
badge = `${origin}/proxy/${dummy}?${url.query({
url: u.href,
badge: '1',
})}`;
}
}
const name = reaction.substring(1, reaction.length - 1);
badge = `${origin}/emoji/${name}.webp?${url.query({
badge: '1',
})}`;
reaction = name.split('@')[0];
} else {
// Unicode絵文字の場合
badge = `/twemoji-badge/${char2fileName(reaction)}.png`;
Expand All @@ -171,6 +156,7 @@ async function composeNotification(data: pushNotificationDataMap[keyof pushNotif
return [`${reaction} ${getUserName(data.body.user)}`, {
body: data.body.note.text ?? '',
icon: data.body.user.avatarUrl,
tag,
badge,
data,
actions: [
Expand Down

0 comments on commit ffe3386

Please sign in to comment.