2021-11-13 11:10:14 +01:00
/* eslint-disable key-spacing */
2022-02-27 03:07:39 +01:00
import { emojiRegex } from './emoji-regex.js' ;
import { fetchMeta } from './fetch-meta.js' ;
import { Emojis } from '@/models/index.js' ;
import { toPunyNullable } from './convert-host.js' ;
2022-03-26 07:34:00 +01:00
import { IsNull } from 'typeorm' ;
2019-03-17 16:03:57 +01:00
2020-02-19 14:06:54 +01:00
const legacies : Record < string , string > = {
2020-01-29 20:37:25 +01:00
'like' : '👍' ,
2023-01-12 23:14:45 +01:00
'love' : '❤️' , // ここに記述する場合は異体字セレクタを入れない <- not that good because modern browsers just display it as the red heart so just convert it to it to not end up with two seperate reactions of "the same emoji" for the user
2020-01-29 20:37:25 +01:00
'laugh' : '😆' ,
'hmm' : '🤔' ,
'surprise' : '😮' ,
'congrats' : '🎉' ,
'angry' : '💢' ,
'confused' : '😥' ,
'rip' : '😇' ,
'pudding' : '🍮' ,
2020-02-19 14:06:54 +01:00
'star' : '⭐' ,
2019-03-17 16:03:57 +01:00
} ;
export async function getFallbackReaction ( ) : Promise < string > {
2019-03-17 17:03:35 +01:00
const meta = await fetchMeta ( ) ;
2023-01-05 05:06:48 +01:00
return meta . defaultReaction ;
2019-03-17 16:03:57 +01:00
}
2020-02-18 22:36:50 +01:00
export function convertLegacyReactions ( reactions : Record < string , number > ) {
const _reactions = { } as Record < string , number > ;
for ( const reaction of Object . keys ( reactions ) ) {
2020-04-15 17:45:43 +02:00
if ( reactions [ reaction ] <= 0 ) continue ;
2020-02-19 14:06:54 +01:00
if ( Object . keys ( legacies ) . includes ( reaction ) ) {
if ( _reactions [ legacies [ reaction ] ] ) {
_reactions [ legacies [ reaction ] ] += reactions [ reaction ] ;
2020-02-18 22:36:50 +01:00
} else {
2020-02-19 14:06:54 +01:00
_reactions [ legacies [ reaction ] ] = reactions [ reaction ] ;
2020-02-18 22:36:50 +01:00
}
} else {
if ( _reactions [ reaction ] ) {
_reactions [ reaction ] += reactions [ reaction ] ;
} else {
_reactions [ reaction ] = reactions [ reaction ] ;
}
}
}
2020-04-13 17:42:59 +02:00
const _reactions2 = { } as Record < string , number > ;
for ( const reaction of Object . keys ( _reactions ) ) {
_reactions2 [ decodeReaction ( reaction ) . reaction ] = _reactions [ reaction ] ;
}
return _reactions2 ;
2020-02-18 22:36:50 +01:00
}
2020-04-13 17:42:59 +02:00
export async function toDbReaction ( reaction? : string | null , reacterHost? : string | null ) : Promise < string > {
2019-03-17 16:03:57 +01:00
if ( reaction == null ) return await getFallbackReaction ( ) ;
2020-04-13 17:42:59 +02:00
reacterHost = toPunyNullable ( reacterHost ) ;
2023-01-12 23:14:45 +01:00
// Convert string-type reactions to pictograms
2020-02-19 14:06:54 +01:00
if ( Object . keys ( legacies ) . includes ( reaction ) ) return legacies [ reaction ] ;
2019-03-17 16:03:57 +01:00
2023-01-12 23:14:45 +01:00
// Unicode Pictograms
/ * c o n s t m a t c h = e m o j i R e g e x . e x e c ( r e a c t i o n ) ;
2019-03-17 16:03:57 +01:00
if ( match ) {
2023-01-12 23:14:45 +01:00
// One pictogram with ligatures
2019-03-17 16:03:57 +01:00
const unicode = match [ 0 ] ;
2023-01-12 23:14:45 +01:00
// variant character selector removal
2020-01-29 20:37:25 +01:00
return unicode . match ( '\u200d' ) ? unicode : unicode.replace ( /\ufe0f/g , '' ) ;
2023-01-12 23:14:45 +01:00
} * /
2019-03-17 16:03:57 +01:00
2020-04-15 17:47:17 +02:00
const custom = reaction . match ( /^:([\w+-]+)(?:@\.)?:$/ ) ;
2019-03-17 16:03:57 +01:00
if ( custom ) {
2020-04-13 17:42:59 +02:00
const name = custom [ 1 ] ;
2022-03-26 07:34:00 +01:00
const emoji = await Emojis . findOneBy ( {
host : reacterHost ? ? IsNull ( ) ,
2020-04-13 17:42:59 +02:00
name ,
2019-03-17 16:03:57 +01:00
} ) ;
2020-05-10 10:25:16 +02:00
if ( emoji ) return reacterHost ? ` : ${ name } @ ${ reacterHost } : ` : ` : ${ name } : ` ;
2019-03-17 16:03:57 +01:00
}
return await getFallbackReaction ( ) ;
}
2020-01-29 20:37:25 +01:00
2020-04-13 17:42:59 +02:00
type DecodedReaction = {
/ * *
* リ ア ク シ ョ ン 名 ( Unicode Emoji or ':name@hostname' or ':name@.' )
* /
reaction : string ;
/ * *
* name ( カ ス タ ム 絵 文 字 の 場 合 name , Emojiクエリに使う )
* /
name? : string ;
/ * *
* host ( カ ス タ ム 絵 文 字 の 場 合 host , Emojiクエリに使う )
* /
host? : string | null ;
} ;
export function decodeReaction ( str : string ) : DecodedReaction {
const custom = str . match ( /^:([\w+-]+)(?:@([\w.-]+))?:$/ ) ;
if ( custom ) {
const name = custom [ 1 ] ;
const host = custom [ 2 ] || null ;
return {
reaction : ` : ${ name } @ ${ host || '.' } : ` , // ローカル分は@以降を省略するのではなく.にする
name ,
2021-12-09 15:58:30 +01:00
host ,
2020-04-13 17:42:59 +02:00
} ;
}
return {
reaction : str ,
name : undefined ,
2021-12-09 15:58:30 +01:00
host : undefined ,
2020-04-13 17:42:59 +02:00
} ;
}
2020-01-29 20:37:25 +01:00
export function convertLegacyReaction ( reaction : string ) : string {
2020-04-13 17:42:59 +02:00
reaction = decodeReaction ( reaction ) . reaction ;
2020-02-19 14:06:54 +01:00
if ( Object . keys ( legacies ) . includes ( reaction ) ) return legacies [ reaction ] ;
2020-01-29 20:37:25 +01:00
return reaction ;
}