generated from K-FOSS/TS-Core-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(iCloud): Send notifications to user!
- Loading branch information
Showing
4 changed files
with
116 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,64 @@ | ||
// src/Library/iCloud.ts | ||
import iCloud from 'icloudjs'; | ||
import { CONFIG } from './Config'; | ||
import got from 'got'; | ||
import { logger, LogMode } from './Logger'; | ||
import readline from 'readline/promises'; | ||
|
||
|
||
export const iCloudAPI = new iCloud({ | ||
...CONFIG.iCloud, | ||
trustDevice: true, | ||
saveCredentials: true, | ||
}); | ||
|
||
export async function initiCloud(): Promise<void> { | ||
logger.log(LogMode.INFO, `Logging into iCloud`); | ||
|
||
await iCloudAPI.authenticate(); | ||
|
||
logger.log(LogMode.DEBUG, `iCloud Status`, iCloudAPI.status); | ||
|
||
if (iCloudAPI.status === 'MfaRequested') { | ||
logger.log(LogMode.WARN, `iCloud needs multifactor`); | ||
|
||
const consoleInterface = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
|
||
const mfaCode = await consoleInterface.question('Please provide MFA code\n'); | ||
logger.log(LogMode.DEBUG, `Receieved code ${mfaCode}`); | ||
|
||
logger.log(LogMode.INFO, `Providing MFA to iCloud`); | ||
await iCloudAPI.provideMfaCode(mfaCode); | ||
} | ||
|
||
await iCloudAPI.awaitReady; | ||
} | ||
|
||
export async function sendAlert(device: string, { | ||
subject = 'Find My iPhone Alert', | ||
text = 'Hello World!' | ||
}): Promise<any> { | ||
if (iCloudAPI.accountInfo) { | ||
const { findme: { url: prefixUrl } } = iCloudAPI.accountInfo.webservices | ||
|
||
logger.log(LogMode.INFO, `Attempting to send alert`, prefixUrl) | ||
|
||
const api = got.extend({ | ||
headers: iCloudAPI.authStore.getHeaders(), | ||
}) | ||
|
||
return api.post(`${prefixUrl}/fmipservice/client/web/sendMessage`, { | ||
json: { | ||
device, | ||
subject, | ||
sounds: true, | ||
userText: true, | ||
text | ||
} | ||
}) | ||
} else { | ||
throw new Error('iCloud Not Connected') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,36 @@ | ||
// src/Modules/User/index.ts | ||
import { iCloudAPI, sendAlert } from "../../Library/iCloud"; | ||
import { logger, LogMode } from "../../Library/Logger"; | ||
|
||
export class User { | ||
public name: string; | ||
|
||
public deviceID: string; | ||
|
||
|
||
public async findUserNotifyDevice(deviceName: string): Promise<void> { | ||
const findMyAPI = iCloudAPI.getService('findme'); | ||
|
||
await findMyAPI.refresh() | ||
|
||
for (const [deviceID, device] of findMyAPI.devices) { | ||
logger.log(LogMode.DEBUG, `Looping over user Devices`, deviceID, device); | ||
|
||
if (device.deviceInfo.name === deviceName) { | ||
logger.log(LogMode.DEBUG, `Found device ${deviceName} with the ID of ${deviceID}`); | ||
|
||
this.deviceID = deviceID; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Sends notification to user via her Apple Watch Ultra | ||
*/ | ||
public async notifyUser() { | ||
|
||
public async notifyUser(options: { | ||
subject?: string, | ||
text?: string | ||
}) { | ||
await sendAlert(this.deviceID, options) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters