@@ -13,7 +13,6 @@ import type {
1313 SyncPeerMetadata ,
1414 SyncPeerPlatform ,
1515} from "../../../../desktop/src/shared/types" ;
16- import { normalizeNotificationPreferences , type NotificationPreferences } from "../../../../desktop/src/shared/types/sync" ;
1716import type { Logger } from "../../../../desktop/src/main/services/logging/logger" ;
1817import { mapPlatform } from "./syncProtocol" ;
1918import { resolveTailscaleCliPath } from "./resolveTailscaleCliPath" ;
@@ -53,7 +52,6 @@ type ClusterStateRow = {
5352
5453const DEVICE_ID_FILE = "sync-device-id" ;
5554export const DEFAULT_SYNC_CLUSTER_ID = "default" ;
56- const WORKSPACE_ACTIVITY_ID = "workspace" ;
5755const TAILSCALE_STATUS_CACHE_MS = 30_000 ;
5856
5957let tailscaleStatusCache :
@@ -477,174 +475,6 @@ export function createDeviceRegistryService(args: DeviceRegistryServiceArgs) {
477475 } ) ;
478476 } ;
479477
480- type ApnsTokenKind = "alert" | "activity-start" | "activity-update" ;
481-
482- const apnsMetaKey = ( kind : ApnsTokenKind ) : string => {
483- if ( kind === "alert" ) return "apnsAlertToken" ;
484- if ( kind === "activity-start" ) return "apnsActivityStartToken" ;
485- return "apnsActivityUpdateTokens" ;
486- } ;
487-
488- const setApnsToken = (
489- deviceId : string ,
490- token : string ,
491- kind : ApnsTokenKind ,
492- env : "sandbox" | "production" ,
493- extras : { bundleId ?: string ; activityId ?: string } = { } ,
494- ) : SyncDeviceRecord | null => {
495- const device = getDevice ( deviceId ) ;
496- if ( ! device ) return null ;
497- const nextMetadata : Record < string , unknown > = {
498- ...device . metadata ,
499- apnsEnv : env ,
500- apnsTokenUpdatedAt : nowIso ( ) ,
501- } ;
502- if ( extras . bundleId ) nextMetadata . apnsBundleId = extras . bundleId ;
503- if ( kind === "activity-update" ) {
504- const existing = ( device . metadata . apnsActivityUpdateTokens as Record < string , string > | undefined ) ?? { } ;
505- const activityId = extras . activityId ?. trim ( ) || WORKSPACE_ACTIVITY_ID ;
506- nextMetadata . apnsActivityUpdateTokens = { ...existing , [ activityId ] : token } ;
507- } else {
508- nextMetadata [ apnsMetaKey ( kind ) ] = token ;
509- }
510- return upsertDeviceRecord ( {
511- deviceId : device . deviceId ,
512- siteId : device . siteId ,
513- name : device . name ,
514- platform : device . platform ,
515- deviceType : device . deviceType ,
516- lastSeenAt : device . lastSeenAt ,
517- lastHost : device . lastHost ,
518- lastPort : device . lastPort ,
519- tailscaleIp : device . tailscaleIp ,
520- ipAddresses : device . ipAddresses ,
521- metadata : nextMetadata ,
522- } ) ;
523- } ;
524-
525- const getApnsTokenForDevice = (
526- deviceId : string ,
527- kind : ApnsTokenKind ,
528- activityId ?: string ,
529- ) : string | null => {
530- const device = getDevice ( deviceId ) ;
531- if ( ! device ) return null ;
532- if ( kind === "activity-update" ) {
533- const map = ( device . metadata . apnsActivityUpdateTokens as Record < string , string > | undefined ) ?? { } ;
534- return map [ activityId ?. trim ( ) || WORKSPACE_ACTIVITY_ID ] ?? null ;
535- }
536- const raw = device . metadata [ apnsMetaKey ( kind ) ] ;
537- return typeof raw === "string" && raw . trim ( ) . length > 0 ? raw : null ;
538- } ;
539-
540- const setNotificationPreferences = (
541- deviceId : string ,
542- prefs : NotificationPreferences ,
543- ) : SyncDeviceRecord | null => {
544- const device = getDevice ( deviceId ) ;
545- if ( ! device ) return null ;
546- const normalizedPrefs = normalizeNotificationPreferences ( prefs ) ;
547- return upsertDeviceRecord ( {
548- deviceId : device . deviceId ,
549- siteId : device . siteId ,
550- name : device . name ,
551- platform : device . platform ,
552- deviceType : device . deviceType ,
553- lastSeenAt : device . lastSeenAt ,
554- lastHost : device . lastHost ,
555- lastPort : device . lastPort ,
556- tailscaleIp : device . tailscaleIp ,
557- ipAddresses : device . ipAddresses ,
558- metadata : {
559- ...device . metadata ,
560- notificationPreferences : normalizedPrefs ,
561- notificationPreferencesUpdatedAt : nowIso ( ) ,
562- } ,
563- } ) ;
564- } ;
565-
566- const getNotificationPreferences = ( deviceId : string ) : NotificationPreferences | null => {
567- const prefs = getDevice ( deviceId ) ?. metadata . notificationPreferences ;
568- if ( ! prefs || typeof prefs !== "object" || Array . isArray ( prefs ) ) return null ;
569- return normalizeNotificationPreferences ( prefs ) ;
570- } ;
571-
572- const invalidateApnsToken = ( deviceToken : string ) : void => {
573- const token = deviceToken . trim ( ) ;
574- if ( ! token ) return ;
575- const device = findDeviceByApnsToken ( token ) ;
576- if ( ! device ) return ;
577- const nextMetadata = { ...device . metadata } ;
578- if ( nextMetadata . apnsAlertToken === token ) {
579- delete nextMetadata . apnsAlertToken ;
580- }
581- if ( nextMetadata . apnsActivityStartToken === token ) {
582- delete nextMetadata . apnsActivityStartToken ;
583- }
584- const updates = nextMetadata . apnsActivityUpdateTokens ;
585- if ( updates && typeof updates === "object" && ! Array . isArray ( updates ) ) {
586- const nextUpdates = { ...( updates as Record < string , string > ) } ;
587- for ( const [ activityId , value ] of Object . entries ( nextUpdates ) ) {
588- if ( value === token ) delete nextUpdates [ activityId ] ;
589- }
590- if ( Object . keys ( nextUpdates ) . length > 0 ) {
591- nextMetadata . apnsActivityUpdateTokens = nextUpdates ;
592- } else {
593- delete nextMetadata . apnsActivityUpdateTokens ;
594- }
595- }
596- upsertDeviceRecord ( {
597- deviceId : device . deviceId ,
598- siteId : device . siteId ,
599- name : device . name ,
600- platform : device . platform ,
601- deviceType : device . deviceType ,
602- lastSeenAt : device . lastSeenAt ,
603- lastHost : device . lastHost ,
604- lastPort : device . lastPort ,
605- tailscaleIp : device . tailscaleIp ,
606- ipAddresses : device . ipAddresses ,
607- metadata : nextMetadata ,
608- } ) ;
609- } ;
610-
611- const invalidateApnsTokensForDevice = ( deviceId : string ) : void => {
612- const device = getDevice ( deviceId ) ;
613- if ( ! device ) return ;
614- const nextMetadata = { ...device . metadata } ;
615- delete nextMetadata . apnsAlertToken ;
616- delete nextMetadata . apnsActivityStartToken ;
617- delete nextMetadata . apnsActivityUpdateTokens ;
618- upsertDeviceRecord ( {
619- deviceId : device . deviceId ,
620- siteId : device . siteId ,
621- name : device . name ,
622- platform : device . platform ,
623- deviceType : device . deviceType ,
624- lastSeenAt : device . lastSeenAt ,
625- lastHost : device . lastHost ,
626- lastPort : device . lastPort ,
627- tailscaleIp : device . tailscaleIp ,
628- ipAddresses : device . ipAddresses ,
629- metadata : nextMetadata ,
630- } ) ;
631- } ;
632-
633- const findDeviceByApnsToken = ( token : string ) : SyncDeviceRecord | null => {
634- for ( const device of listDevices ( ) ) {
635- const alert = device . metadata . apnsAlertToken ;
636- const activity = device . metadata . apnsActivityStartToken ;
637- if ( alert === token || activity === token ) return device ;
638- const updates = device . metadata . apnsActivityUpdateTokens ;
639- if ( updates && typeof updates === "object" ) {
640- for ( const value of Object . values ( updates as Record < string , unknown > ) ) {
641- if ( value === token ) return device ;
642- }
643- }
644- }
645- return null ;
646- } ;
647-
648478 const applyBrainStatus = ( payload : SyncBrainStatusPayload ) : void => {
649479 upsertPeerMetadata ( payload . brain , { lastSeenAt : nowIso ( ) } ) ;
650480 for ( const peer of payload . connectedPeers ) {
@@ -698,13 +528,6 @@ export function createDeviceRegistryService(args: DeviceRegistryServiceArgs) {
698528 applyBrainStatus,
699529 clearClusterRegistryForViewerJoin,
700530 forgetDevice,
701- setApnsToken,
702- getApnsTokenForDevice,
703- setNotificationPreferences,
704- getNotificationPreferences,
705- invalidateApnsToken,
706- invalidateApnsTokensForDevice,
707- findDeviceByApnsToken,
708531 } ;
709532}
710533
0 commit comments