@ -626,7 +626,7 @@ const startServer = async () => {
const listener = message => {
const listener = message => {
const { event , payload , queued _at } = message ;
const { event , payload , queued _at } = message ;
const transmit = ( ) => {
const transmit = ( payload ) => {
const now = new Date ( ) . getTime ( ) ;
const now = new Date ( ) . getTime ( ) ;
const delta = now - queued _at ;
const delta = now - queued _at ;
const encodedPayload = typeof payload === 'object' ? JSON . stringify ( payload ) : payload ;
const encodedPayload = typeof payload === 'object' ? JSON . stringify ( payload ) : payload ;
@ -638,22 +638,21 @@ const startServer = async () => {
// Only messages that may require filtering are statuses, since notifications
// Only messages that may require filtering are statuses, since notifications
// are already personalized and deletes do not matter
// are already personalized and deletes do not matter
if ( ! needsFiltering || event !== 'update' ) {
if ( ! needsFiltering || event !== 'update' ) {
transmit ( ) ;
transmit ( payload ) ;
return ;
return ;
}
}
const unpackedPayload = payload ;
const targetAccountIds = [ payload . account . id ] . concat ( payload . mentions . map ( item => item . id ) ) ;
const targetAccountIds = [ unpackedPayload . account . id ] . concat ( unpackedPayload . mentions . map ( item => item . id ) ) ;
const accountDomain = payload . account . acct . split ( '@' ) [ 1 ] ;
const accountDomain = unpackedPayload . account . acct . split ( '@' ) [ 1 ] ;
if ( Array . isArray ( req . chosenLanguages ) && un packedP ayload. language !== null && req . chosenLanguages . indexOf ( un packedP ayload. language ) === - 1 ) {
if ( Array . isArray ( req . chosenLanguages ) && payload. language !== null && req . chosenLanguages . indexOf ( payload. language ) === - 1 ) {
log . silly ( req . requestId , ` Message ${ un packedP ayload. id } filtered by language ( ${ un packedP ayload. language } ) ` ) ;
log . silly ( req . requestId , ` Message ${ payload. id } filtered by language ( ${ payload. language } ) ` ) ;
return ;
return ;
}
}
// When the account is not logged in, it is not necessary to confirm the block or mute
// When the account is not logged in, it is not necessary to confirm the block or mute
if ( ! req . accountId ) {
if ( ! req . accountId ) {
transmit ( ) ;
transmit ( payload ) ;
return ;
return ;
}
}
@ -672,14 +671,14 @@ const startServer = async () => {
SELECT 1
SELECT 1
FROM mutes
FROM mutes
WHERE account _id = $1
WHERE account _id = $1
AND target _account _id IN ( $ { placeholders ( targetAccountIds , 2 ) } ) ` , [req.accountId, un packedP ayload.account.id].concat(targetAccountIds)),
AND target _account _id IN ( $ { placeholders ( targetAccountIds , 2 ) } ) ` , [req.accountId, payload.account.id].concat(targetAccountIds)),
] ;
] ;
if ( accountDomain ) {
if ( accountDomain ) {
queries . push ( client . query ( 'SELECT 1 FROM account_domain_blocks WHERE account_id = $1 AND domain = $2' , [ req . accountId , accountDomain ] ) ) ;
queries . push ( client . query ( 'SELECT 1 FROM account_domain_blocks WHERE account_id = $1 AND domain = $2' , [ req . accountId , accountDomain ] ) ) ;
}
}
if ( ! un packedP ayload. filtered && ! req . cachedFilters ) {
if ( ! payload. filtered && ! req . cachedFilters ) {
queries . push ( client . query ( 'SELECT filter.id AS id, filter.phrase AS title, filter.context AS context, filter.expires_at AS expires_at, filter.action AS filter_action, keyword.keyword AS keyword, keyword.whole_word AS whole_word FROM custom_filter_keywords keyword JOIN custom_filters filter ON keyword.custom_filter_id = filter.id WHERE filter.account_id = $1 AND (filter.expires_at IS NULL OR filter.expires_at > NOW())' , [ req . accountId ] ) ) ;
queries . push ( client . query ( 'SELECT filter.id AS id, filter.phrase AS title, filter.context AS context, filter.expires_at AS expires_at, filter.action AS filter_action, keyword.keyword AS keyword, keyword.whole_word AS whole_word FROM custom_filter_keywords keyword JOIN custom_filters filter ON keyword.custom_filter_id = filter.id WHERE filter.account_id = $1 AND (filter.expires_at IS NULL OR filter.expires_at > NOW())' , [ req . accountId ] ) ) ;
}
}
@ -690,7 +689,7 @@ const startServer = async () => {
return ;
return ;
}
}
if ( ! un packedP ayload. filtered && ! req . cachedFilters ) {
if ( ! payload. filtered && ! req . cachedFilters ) {
const filterRows = values [ accountDomain ? 2 : 1 ] . rows ;
const filterRows = values [ accountDomain ? 2 : 1 ] . rows ;
req . cachedFilters = filterRows . reduce ( ( cache , row ) => {
req . cachedFilters = filterRows . reduce ( ( cache , row ) => {
@ -733,27 +732,30 @@ const startServer = async () => {
}
}
// Check filters
// Check filters
if ( req . cachedFilters && ! unpackedPayload . filtered ) {
if ( req . cachedFilters && ! payload . filtered ) {
const status = unpackedPayload ;
const mutatedPayload = { ... payload } ;
const status = payload ;
const searchContent = ( [ status . spoiler _text || '' , status . content ] . concat ( ( status . poll && status . poll . options ) ? status . poll . options . map ( option => option . title ) : [ ] ) ) . concat ( status . media _attachments . map ( att => att . description ) ) . join ( '\n\n' ) . replace ( /<br\s*\/?>/g , '\n' ) . replace ( /<\/p><p>/g , '\n\n' ) ;
const searchContent = ( [ status . spoiler _text || '' , status . content ] . concat ( ( status . poll && status . poll . options ) ? status . poll . options . map ( option => option . title ) : [ ] ) ) . concat ( status . media _attachments . map ( att => att . description ) ) . join ( '\n\n' ) . replace ( /<br\s*\/?>/g , '\n' ) . replace ( /<\/p><p>/g , '\n\n' ) ;
const searchIndex = JSDOM . fragment ( searchContent ) . textContent ;
const searchIndex = JSDOM . fragment ( searchContent ) . textContent ;
const now = new Date ( ) ;
const now = new Date ( ) ;
p ayload. filtered = [ ] ;
mutatedP ayload. filtered = [ ] ;
Object . values ( req . cachedFilters ) . forEach ( ( cachedFilter ) => {
Object . values ( req . cachedFilters ) . forEach ( ( cachedFilter ) => {
if ( ( cachedFilter . expires _at === null || cachedFilter . expires _at > now ) ) {
if ( ( cachedFilter . expires _at === null || cachedFilter . expires _at > now ) ) {
const keyword _matches = searchIndex . match ( cachedFilter . regexp ) ;
const keyword _matches = searchIndex . match ( cachedFilter . regexp ) ;
if ( keyword _matches ) {
if ( keyword _matches ) {
p ayload. filtered . push ( {
mutatedP ayload. filtered . push ( {
filter : cachedFilter . repr ,
filter : cachedFilter . repr ,
keyword _matches ,
keyword _matches ,
} ) ;
} ) ;
}
}
}
}
} ) ;
} ) ;
}
transmit ( ) ;
transmit ( mutatedPayload ) ;
} else {
transmit ( payload ) ;
}
} ) . catch ( err => {
} ) . catch ( err => {
log . error ( err ) ;
log . error ( err ) ;
done ( ) ;
done ( ) ;