2022-12-04 01:16:47 +00:00
/ *
* Vencord , a modification for Discord ' s desktop app
* Copyright ( c ) 2022 Vendicated and contributors
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < https : //www.gnu.org/licenses/>.
* /
import * as DataStore from "@api/DataStore" ;
import ErrorBoundary from "@components/ErrorBoundary" ;
import { Devs } from "@utils/constants" ;
2023-05-05 23:36:00 +00:00
import { useForceUpdater } from "@utils/react" ;
2022-12-04 01:16:47 +00:00
import definePlugin from "@utils/types" ;
2023-03-08 03:11:59 +00:00
import { findByPropsLazy , findStoreLazy } from "@webpack" ;
2022-12-04 01:16:47 +00:00
import { Tooltip } from "webpack/common" ;
2023-06-13 00:36:25 +00:00
const enum ActivitiesTypes {
2022-12-04 01:16:47 +00:00
Game ,
Embedded
}
interface IgnoredActivity {
id : string ;
type : ActivitiesTypes ;
}
const RegisteredGamesClasses = findByPropsLazy ( "overlayToggleIconOff" , "overlayToggleIconOn" ) ;
2023-01-08 01:15:22 +00:00
const TryItOutClasses = findByPropsLazy ( "tryItOutBadge" , "tryItOutBadgeIcon" ) ;
2022-12-04 01:16:47 +00:00
const BaseShapeRoundClasses = findByPropsLazy ( "baseShapeRound" , "baseShapeRoundLeft" , "baseShapeRoundRight" ) ;
2023-03-08 03:11:59 +00:00
const RunningGameStore = findStoreLazy ( "RunningGameStore" ) ;
2022-12-04 01:16:47 +00:00
function ToggleIconOff() {
return (
< svg
className = { RegisteredGamesClasses . overlayToggleIconOff }
height = "24"
width = "24"
2023-06-25 09:57:18 +00:00
viewBox = "0 2.2 32 26"
2022-12-04 01:16:47 +00:00
aria - hidden = { true }
role = "img"
>
< g
fill = "none"
fillRule = "evenodd"
>
< path
className = { RegisteredGamesClasses . fill }
fill = "currentColor"
d = "M 16 8 C 7.664063 8 1.25 15.34375 1.25 15.34375 L 0.65625 16 L 1.25 16.65625 C 1.25 16.65625 7.097656 23.324219 14.875 23.9375 C 15.246094 23.984375 15.617188 24 16 24 C 16.382813 24 16.753906 23.984375 17.125 23.9375 C 24.902344 23.324219 30.75 16.65625 30.75 16.65625 L 31.34375 16 L 30.75 15.34375 C 30.75 15.34375 24.335938 8 16 8 Z M 16 10 C 18.203125 10 20.234375 10.601563 22 11.40625 C 22.636719 12.460938 23 13.675781 23 15 C 23 18.613281 20.289063 21.582031 16.78125 21.96875 C 16.761719 21.972656 16.738281 21.964844 16.71875 21.96875 C 16.480469 21.980469 16.242188 22 16 22 C 15.734375 22 15.476563 21.984375 15.21875 21.96875 C 11.710938 21.582031 9 18.613281 9 15 C 9 13.695313 9.351563 12.480469 9.96875 11.4375 L 9.9375 11.4375 C 11.71875 10.617188 13.773438 10 16 10 Z M 16 12 C 14.34375 12 13 13.34375 13 15 C 13 16.65625 14.34375 18 16 18 C 17.65625 18 19 16.65625 19 15 C 19 13.34375 17.65625 12 16 12 Z M 7.25 12.9375 C 7.09375 13.609375 7 14.285156 7 15 C 7 16.753906 7.5 18.394531 8.375 19.78125 C 5.855469 18.324219 4.105469 16.585938 3.53125 16 C 4.011719 15.507813 5.351563 14.203125 7.25 12.9375 Z M 24.75 12.9375 C 26.648438 14.203125 27.988281 15.507813 28.46875 16 C 27.894531 16.585938 26.144531 18.324219 23.625 19.78125 C 24.5 18.394531 25 16.753906 25 15 C 25 14.285156 24.90625 13.601563 24.75 12.9375 Z"
/ >
< rect
className = { RegisteredGamesClasses . fill }
x = "3"
y = "26"
width = "26"
height = "2"
transform = "rotate(-45 2 20)"
/ >
< / g >
< / svg >
) ;
}
2023-03-08 03:11:59 +00:00
function ToggleIconOn ( { forceWhite } : { forceWhite? : boolean ; } ) {
2022-12-04 01:16:47 +00:00
return (
< svg
className = { RegisteredGamesClasses . overlayToggleIconOn }
height = "24"
width = "24"
2023-06-25 09:57:18 +00:00
viewBox = "0 2.2 32 26"
2022-12-04 01:16:47 +00:00
>
< path
2023-03-08 03:11:59 +00:00
className = { forceWhite ? "" : RegisteredGamesClasses . fill }
fill = { forceWhite ? "var(--white-500)" : "" }
2022-12-04 01:16:47 +00:00
d = "M 16 8 C 7.664063 8 1.25 15.34375 1.25 15.34375 L 0.65625 16 L 1.25 16.65625 C 1.25 16.65625 7.097656 23.324219 14.875 23.9375 C 15.246094 23.984375 15.617188 24 16 24 C 16.382813 24 16.753906 23.984375 17.125 23.9375 C 24.902344 23.324219 30.75 16.65625 30.75 16.65625 L 31.34375 16 L 30.75 15.34375 C 30.75 15.34375 24.335938 8 16 8 Z M 16 10 C 18.203125 10 20.234375 10.601563 22 11.40625 C 22.636719 12.460938 23 13.675781 23 15 C 23 18.613281 20.289063 21.582031 16.78125 21.96875 C 16.761719 21.972656 16.738281 21.964844 16.71875 21.96875 C 16.480469 21.980469 16.242188 22 16 22 C 15.734375 22 15.476563 21.984375 15.21875 21.96875 C 11.710938 21.582031 9 18.613281 9 15 C 9 13.695313 9.351563 12.480469 9.96875 11.4375 L 9.9375 11.4375 C 11.71875 10.617188 13.773438 10 16 10 Z M 16 12 C 14.34375 12 13 13.34375 13 15 C 13 16.65625 14.34375 18 16 18 C 17.65625 18 19 16.65625 19 15 C 19 13.34375 17.65625 12 16 12 Z M 7.25 12.9375 C 7.09375 13.609375 7 14.285156 7 15 C 7 16.753906 7.5 18.394531 8.375 19.78125 C 5.855469 18.324219 4.105469 16.585938 3.53125 16 C 4.011719 15.507813 5.351563 14.203125 7.25 12.9375 Z M 24.75 12.9375 C 26.648438 14.203125 27.988281 15.507813 28.46875 16 C 27.894531 16.585938 26.144531 18.324219 23.625 19.78125 C 24.5 18.394531 25 16.753906 25 15 C 25 14.285156 24.90625 13.601563 24.75 12.9375 Z"
/ >
< / svg >
) ;
}
2023-04-10 21:59:48 +00:00
function ToggleActivityComponent ( { activity , forceWhite , forceLeftMargin } : { activity : IgnoredActivity ; forceWhite? : boolean ; forceLeftMargin? : boolean ; } ) {
2022-12-04 01:16:47 +00:00
const forceUpdate = useForceUpdater ( ) ;
return (
< Tooltip text = "Toggle activity" >
{ ( { onMouseLeave , onMouseEnter } ) = > (
< div
onMouseLeave = { onMouseLeave }
onMouseEnter = { onMouseEnter }
className = { RegisteredGamesClasses . overlayToggleIcon }
role = "button"
aria - label = "Toggle activity"
tabIndex = { 0 }
2023-04-10 21:59:48 +00:00
style = { forceLeftMargin ? { marginLeft : "2px" } : undefined }
2022-12-04 01:16:47 +00:00
onClick = { e = > handleActivityToggle ( e , activity , forceUpdate ) }
>
{
ignoredActivitiesCache . has ( activity . id )
? < ToggleIconOff / >
2023-03-08 03:11:59 +00:00
: < ToggleIconOn forceWhite = { forceWhite } / >
2022-12-04 01:16:47 +00:00
}
< / div >
) }
< / Tooltip >
) ;
}
function ToggleActivityComponentWithBackground ( { activity } : { activity : IgnoredActivity ; } ) {
return (
< div
2023-01-08 01:15:22 +00:00
className = { ` ${ TryItOutClasses . tryItOutBadge } ${ BaseShapeRoundClasses . baseShapeRound } ` }
2023-06-25 09:57:18 +00:00
style = { { padding : "0px 2px" , height : 28 } }
2022-12-04 01:16:47 +00:00
>
2023-03-08 03:11:59 +00:00
< ToggleActivityComponent activity = { activity } forceWhite = { true } / >
2022-12-04 01:16:47 +00:00
< / div >
) ;
}
function handleActivityToggle ( e : React.MouseEvent < HTMLDivElement , MouseEvent > , activity : IgnoredActivity , forceUpdateComponent : ( ) = > void ) {
e . stopPropagation ( ) ;
if ( ignoredActivitiesCache . has ( activity . id ) ) ignoredActivitiesCache . delete ( activity . id ) ;
else ignoredActivitiesCache . set ( activity . id , activity ) ;
forceUpdateComponent ( ) ;
saveCacheToDatastore ( ) ;
}
async function saveCacheToDatastore() {
await DataStore . set ( "IgnoreActivities_ignoredActivities" , ignoredActivitiesCache ) ;
}
let ignoredActivitiesCache = new Map < IgnoredActivity [ " id " ] , IgnoredActivity > ( ) ;
export default definePlugin ( {
name : "IgnoreActivities" ,
authors : [ Devs . Nuckyz ] ,
description : "Ignore certain activities (like games and actual activities) from showing up on your status. You can configure which ones are ignored from the Registered Games and Activities tabs." ,
2023-03-08 03:11:59 +00:00
patches : [
{
find : ".Messages.SETTINGS_GAMES_TOGGLE_OVERLAY" ,
replacement : {
2023-03-31 04:11:15 +00:00
match : /!(\i)(\)return null;var \i=(\i)\.overlay.+?children:)(\[.{0,70}overlayStatusText.+?\])(?=}\)}\(\))/ ,
replace : ( _ , platformCheck , restWithoutPlatformCheck , props , children ) = > "false"
2023-03-08 03:11:59 +00:00
+ ` ${ restWithoutPlatformCheck } `
+ ` ( ${ platformCheck } ? ${ children } :[]) `
+ ` .concat(Vencord.Plugins.plugins.IgnoreActivities.renderToggleGameActivityButton( ${ props } )) `
}
} ,
{
find : ".overlayBadge" ,
2023-06-25 09:57:18 +00:00
replacement : [
{
match : /(?<=\(\)\.badgeContainer,children:).{0,50}?name:(\i)\.name.+?null/ ,
replace : ( m , props ) = > ` [ ${ m } , $ self.renderToggleActivityButton( ${ props } )] `
} ,
{
match : /(?<=\(\)\.badgeContainer,children:).{0,50}?name:(\i\.application)\.name.+?null/ ,
replace : ( m , props ) = > ` ${ m } , $ self.renderToggleActivityButton( ${ props } ) `
}
]
2023-03-08 03:11:59 +00:00
} ,
{
find : '.displayName="LocalActivityStore"' ,
replacement : {
match : /LISTENING.+?\)\);(?<=(\i)\.push.+?)/ ,
replace : ( m , activities ) = > ` ${ m } ${ activities } = ${ activities } .filter( $ self.isActivityNotIgnored); `
}
2022-12-04 01:16:47 +00:00
}
2023-03-08 03:11:59 +00:00
] ,
2022-12-04 01:16:47 +00:00
async start() {
const ignoredActivitiesData = await DataStore . get < string [ ] | Map < IgnoredActivity [ " id " ] , IgnoredActivity > > ( "IgnoreActivities_ignoredActivities" ) ? ? new Map < IgnoredActivity [ " id " ] , IgnoredActivity > ( ) ;
/** Migrate old data */
if ( Array . isArray ( ignoredActivitiesData ) ) {
for ( const id of ignoredActivitiesData ) {
ignoredActivitiesCache . set ( id , { id , type : ActivitiesTypes . Game } ) ;
}
await saveCacheToDatastore ( ) ;
} else ignoredActivitiesCache = ignoredActivitiesData ;
if ( ignoredActivitiesCache . size !== 0 ) {
const gamesSeen : { id? : string ; exePath : string ; } [ ] = RunningGameStore . getGamesSeen ( ) ;
for ( const ignoredActivity of ignoredActivitiesCache . values ( ) ) {
if ( ignoredActivity . type !== ActivitiesTypes . Game ) continue ;
if ( ! gamesSeen . some ( game = > game . id === ignoredActivity . id || game . exePath === ignoredActivity . id ) ) {
/** Custom added game which no longer exists */
ignoredActivitiesCache . delete ( ignoredActivity . id ) ;
}
}
await saveCacheToDatastore ( ) ;
}
} ,
2023-01-12 01:50:31 +00:00
renderToggleGameActivityButton ( props : { id? : string ; exePath : string ; } ) {
2022-12-04 01:16:47 +00:00
return (
< ErrorBoundary noop >
2023-04-10 21:59:48 +00:00
< ToggleActivityComponent activity = { { id : props.id ? ? props . exePath , type : ActivitiesTypes . Game } } forceLeftMargin = { true } / >
2022-12-04 01:16:47 +00:00
< / ErrorBoundary >
) ;
} ,
renderToggleActivityButton ( props : { id : string ; } ) {
return (
< ErrorBoundary noop >
< ToggleActivityComponentWithBackground activity = { { id : props.id , type : ActivitiesTypes . Embedded } } / >
< / ErrorBoundary >
) ;
} ,
2023-01-21 13:37:36 +00:00
isActivityNotIgnored ( props : { type : number ; application_id? : string ; name? : string ; } ) {
2022-12-04 01:16:47 +00:00
if ( props . type === 0 ) {
if ( props . application_id !== undefined ) return ! ignoredActivitiesCache . has ( props . application_id ) ;
else {
const exePath = RunningGameStore . getRunningGames ( ) . find ( game = > game . name === props . name ) ? . exePath ;
if ( exePath ) return ! ignoredActivitiesCache . has ( exePath ) ;
}
}
return true ;
2023-03-08 03:11:59 +00:00
}
2022-12-04 01:16:47 +00:00
} ) ;