Skip to content

Commit

Permalink
chore: upgrade to prettier@v3 (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
book000 authored Aug 15, 2023
1 parent 397d13c commit b5d7dba
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 66 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
"fix:prettier": "prettier --write src"
},
"devDependencies": {
"@book000/node-utils": "1.3.57",
"@book000/node-utils": "1.4.0",
"@fastify/basic-auth": "5.0.0",
"@fastify/cors": "8.3.0",
"@types/cheerio": "0.22.31",
"@types/node": "20.2.6",
"@types/node": "20.5.0",
"@types/web-push": "3.3.2",
"@typescript-eslint/eslint-plugin": "6.4.0",
"@typescript-eslint/parser": "6.4.0",
Expand All @@ -44,12 +44,12 @@
"eslint-plugin-promise": "6.1.1",
"eslint-plugin-unicorn": "48.0.1",
"fastify": "4.21.0",
"prettier": "2.8.8",
"prettier": "3.0.2",
"ts-node": "10.9.1",
"ts-node-dev": "2.0.0",
"typescript": "5.1.6",
"typescript-json-schema": "0.59.0",
"web-push": "3.6.4",
"yarn-run-all": "3.1.1"
}
}
}
30 changes: 15 additions & 15 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function getDirectionText(direction: SyslogCall['direction']): string {

function getStatusText(
status: SyslogCall['status'],
direction: SyslogCall['direction']
direction: SyslogCall['direction'],
): string {
switch (status) {
case 'connected': {
Expand All @@ -61,13 +61,13 @@ function getCallerName(callerResult: PhoneDetailResult) {

function getIDestinations(
config: Configuration,
detail: CallDetail
detail: CallDetail,
): IDestination[] | null {
const destination = config.destinations.filter((d) =>
Object.entries(d.condition).every(
// @ts-ignore
([key, value]) => new RegExp(value).test(detail[key])
)
([key, value]) => new RegExp(value).test(detail[key]),
),
)
if (destination) {
return destination
Expand All @@ -79,8 +79,8 @@ function getSelfName(config: Configuration, detail: CallDetail): string {
const self = config.selfs.find((d) =>
Object.entries(d.condition).every(
// @ts-ignore
([key, value]) => new RegExp(value).test(detail[key])
)
([key, value]) => new RegExp(value).test(detail[key]),
),
)
if (self) {
return self.name
Expand All @@ -94,7 +94,7 @@ function getNotGoogleSearchMessage(
callerNumber: string,
callerName: string,
source: string,
selfName: string
selfName: string,
): string {
return [
`☎ **【${connectedText}${directionText} \`${callerName}\` (\`${callerNumber}\`)**`,
Expand All @@ -111,10 +111,10 @@ function getGoogleSearchMessage(
callerName: string,
source: string,
selfName: string,
googleResult: GoogleSearchResult
googleResult: GoogleSearchResult,
) {
const googleResults = googleResult.items.map(
(item, index) => `#${index + 1} \`${item.title}\` ${item.url}`
(item, index) => `#${index + 1} \`${item.title}\` ${item.url}`,
)
return [
`☎ **【${connectedText}${directionText} \`${callerName}\` (\`${callerNumber}\`)**`,
Expand All @@ -134,22 +134,22 @@ async function checker(config: Configuration) {
const nvr510 = new NVR510(
config.router.ip,
config.router.username,
config.router.password
config.router.password,
)

const calls = await nvr510.getCallsFromSyslog()
const filteredCalls = calls.filter(
(call) => !Checked.isChecked(call.date, call.time)
(call) => !Checked.isChecked(call.date, call.time),
)
logger.info(
`📞 calls: ${calls.length}, filteredCalls: ${filteredCalls.length}`
`📞 calls: ${calls.length}, filteredCalls: ${filteredCalls.length}`,
)
for (const call of filteredCalls.reverse()) {
const directionText = getDirectionText(call.direction)
const connectedText = getStatusText(call.status, call.direction)

logger.info(
`📞 ${directionText} ${call.fromNumber} -> ${call.toNumber} (${connectedText})`
`📞 ${directionText} ${call.fromNumber} -> ${call.toNumber} (${connectedText})`,
)

// 着信だったら、toNumber、発信だったら、fromNumber がこっち側の番号
Expand Down Expand Up @@ -184,15 +184,15 @@ async function checker(config: Configuration) {
callerName,
source,
selfName,
callerResult
callerResult,
)
: getNotGoogleSearchMessage(
connectedText,
directionText,
callerNumber,
callerName,
source,
selfName
selfName,
)

if (!isFirst && destinations.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/public/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
Expand Down
2 changes: 1 addition & 1 deletion src/public/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ async function subscribe(destinationName) {
worker.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: validPublicKey,
})
}),
)

const subscriptionJSON = currentLocalSubscription.toJSON()
Expand Down
4 changes: 3 additions & 1 deletion src/public/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ button#unsubscribe {
background-color: rgba(0, 0, 0, 0.5);
z-index: 1;

transition: opacity 1s, visibility 1s;
transition:
opacity 1s,
visibility 1s;
opacity: 0;
visibility: hidden;
}
Expand Down
10 changes: 5 additions & 5 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,13 @@ const isConfig = (config: any): config is Configuration => {
}

export const isDestinationDiscordWebhook = (
destination: any
destination: any,
): destination is DestinationDiscordWebhook => {
return destination.type === 'discord-webhook' && !!destination.webhook_url
}

export const isDestinationDiscordBot = (
destination: any
destination: any,
): destination is DestinationDiscordBot => {
return (
destination.type === 'discord-bot' &&
Expand All @@ -161,19 +161,19 @@ export const isDestinationDiscordBot = (
}

export const isDestinationSlack = (
destination: any
destination: any,
): destination is DestinationSlack => {
return destination.type === 'slack' && !!destination.webhook_url
}

export const isDestinationLINENotify = (
destination: any
destination: any,
): destination is DestinationLINENotify => {
return destination.type === 'line-notify' && !!destination.token
}

export const isDestinationWebPush = (
destination: any
destination: any,
): destination is DestinationWebPush => {
return destination.type === 'web-push'
}
Expand Down
10 changes: 5 additions & 5 deletions src/utils/destination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { WebPush } from './web-push'
class BaseDestination {
public async send(message: string): Promise<void> {
throw new Error(
`Not implemented: ${this.constructor.name}.send() ${message}`
`Not implemented: ${this.constructor.name}.send() ${message}`,
)
}
}
Expand All @@ -33,7 +33,7 @@ class DiscordWebhookDestination extends BaseDestination {
class DiscordBotDestination extends BaseDestination {
constructor(
private readonly token: string,
private readonly channelId: string
private readonly channelId: string,
) {
super()
}
Expand All @@ -47,7 +47,7 @@ class DiscordBotDestination extends BaseDestination {
Authorization: `Bot ${this.token}`,
},
validateStatus: () => true,
}
},
)
if (response.status !== 204 && response.status !== 200) {
throw new Error(`Discord webhook failed (${response.status})`)
Expand All @@ -66,7 +66,7 @@ class SlackDestination extends BaseDestination {
{ text: message },
{
validateStatus: () => true,
}
},
)
if (response.status !== 200) {
throw new Error(`Slack webhook failed (${response.status})`)
Expand All @@ -89,7 +89,7 @@ class LINENotifyDestination extends BaseDestination {
headers: {
Authorization: `Bearer ${this.token}`,
},
}
},
)
if (response.status !== 200) {
throw new Error(`LINE Notify failed (${response.status})`)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/nvr510.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class NVR510 {
username: this.username,
password: this.password,
},
}
},
)
if (response.status !== 200) {
throw new Error(`Failed to get syslog: ${response.status}`)
Expand Down
6 changes: 3 additions & 3 deletions src/utils/search-number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class GoogleSearch extends BaseSearchNumber {

export async function searchNumber(
config: Configuration,
number: string
number: string,
): Promise<PhoneDetailResult> {
const logger = Logger.configure('GoogleSearch::searchNumber')
const searchers = [
Expand All @@ -183,13 +183,13 @@ export async function searchNumber(
}

export function isPhoneDetail(
result: PhoneDetailResult
result: PhoneDetailResult,
): result is PhoneDetail {
return result !== null && 'name' in result
}

export function isGoogleSearchResult(
result: PhoneDetailResult
result: PhoneDetailResult,
): result is GoogleSearchResult {
return result !== null && 'count' in result
}
22 changes: 11 additions & 11 deletions src/utils/web-push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export class WebPush {
vapid: vapidKeys,
},
null,
2
)
2,
),
)
}

Expand Down Expand Up @@ -66,7 +66,7 @@ export class WebPush {
const index = subscriptions.findIndex(
(s) =>
s.destinationName === subscription.destinationName &&
s.endpoint === subscription.endpoint
s.endpoint === subscription.endpoint,
)
if (index !== -1) {
subscriptions.splice(index, 1)
Expand All @@ -76,11 +76,11 @@ export class WebPush {
}

public async removeSubscription(
subscription: Subscription
subscription: Subscription,
): Promise<boolean> {
const subscriptions = this.getSubscriptions()
const index = subscriptions.findIndex(
(s) => s.endpoint === subscription.endpoint
(s) => s.endpoint === subscription.endpoint,
)
if (index === -1) {
return false
Expand All @@ -107,7 +107,7 @@ export class WebPush {

public async sendNotification(
subscription: Subscription,
payload: string
payload: string,
): Promise<number> {
const logger = Logger.configure('WebPush.sendNotification')
const response = await webpush
Expand All @@ -129,12 +129,12 @@ export class WebPush {
public async sendNotifications(
destinationName: string,
title: string,
body: string
body: string,
): Promise<void> {
const logger = Logger.configure('WebPush.sendNotifications')
const subscriptions = this.getSubscriptions()
const destinationSubscriptions = subscriptions.filter(
(s) => s.destinationName === destinationName
(s) => s.destinationName === destinationName,
)
if (destinationSubscriptions.length === 0) {
return
Expand All @@ -154,7 +154,7 @@ export class WebPush {
})

logger.info(
`Sending notification to ${destinationSubscriptions.length} subscriptions...`
`Sending notification to ${destinationSubscriptions.length} subscriptions...`,
)
const promises = destinationSubscriptions.map((subscription) => {
return this.sendNotification(subscription, payload)
Expand All @@ -164,13 +164,13 @@ export class WebPush {
logger.info(
`Successfully sent notification to ${
results.filter((r) => r === 201).length
} subscriptions!`
} subscriptions!`,
)
if (results.some((r) => r !== 201)) {
logger.warn(
`Failed to send notification to ${
results.filter((r) => r !== 201).length
} subscriptions.`
} subscriptions.`,
)
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/web/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class ApiRouter extends BaseRouter {
.addHook('onRequest', fastify.basicAuth)
done()
},
{ prefix: '/api' }
{ prefix: '/api' },
)
}

Expand Down Expand Up @@ -60,7 +60,7 @@ export class ApiRouter extends BaseRouter {
}
}
}>,
reply: FastifyReply
reply: FastifyReply,
) {
const subscription = request.body

Expand All @@ -70,7 +70,7 @@ export class ApiRouter extends BaseRouter {
JSON.stringify({
title: '購読完了',
body: `購読が完了しました。${subscription.destinationName} の通知をお届けします。`,
})
}),
)

await this.webPush.addSubscription(subscription)
Expand Down Expand Up @@ -105,7 +105,7 @@ export class ApiRouter extends BaseRouter {
}
}
}>,
reply: FastifyReply
reply: FastifyReply,
) {
const subscription = request.body
const result = await this.webPush.removeSubscription(subscription)
Expand Down
Loading

0 comments on commit b5d7dba

Please sign in to comment.