@2en/clawly-plugins 1.1.1 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/notification.ts +13 -24
  2. package/package.json +1 -1
package/notification.ts CHANGED
@@ -19,24 +19,6 @@ const TOKEN_DIR = path.join(os.homedir(), '.openclaw', 'clawly')
19
19
  const TOKEN_FILE = path.join(TOKEN_DIR, 'expo-push-token.json')
20
20
  const EXPO_PUSH_URL = 'https://exp.host/--/api/v2/push/send'
21
21
 
22
- let pushToken: string | null = null
23
-
24
- function loadPersistedToken(api: PluginApi): void {
25
- try {
26
- if (fs.existsSync(TOKEN_FILE)) {
27
- const data = JSON.parse(fs.readFileSync(TOKEN_FILE, 'utf-8'))
28
- if (typeof data.token === 'string' && data.token) {
29
- pushToken = data.token
30
- api.logger.info(`notification: loaded persisted push token`)
31
- }
32
- }
33
- } catch (err) {
34
- api.logger.warn(
35
- `notification: failed to load persisted token: ${err instanceof Error ? err.message : String(err)}`,
36
- )
37
- }
38
- }
39
-
40
22
  function persistToken(token: string, api: PluginApi): void {
41
23
  try {
42
24
  fs.mkdirSync(TOKEN_DIR, {recursive: true})
@@ -49,14 +31,23 @@ function persistToken(token: string, api: PluginApi): void {
49
31
  }
50
32
 
51
33
  export function getPushToken(): string | null {
52
- return pushToken
34
+ try {
35
+ if (fs.existsSync(TOKEN_FILE)) {
36
+ const data = JSON.parse(fs.readFileSync(TOKEN_FILE, 'utf-8'))
37
+ if (typeof data.token === 'string' && data.token) {
38
+ return data.token
39
+ }
40
+ }
41
+ } catch {}
42
+ return null
53
43
  }
54
44
 
55
45
  export async function sendPushNotification(
56
46
  opts: {body: string; title?: string; data?: Record<string, unknown>},
57
47
  api: PluginApi,
58
48
  ): Promise<boolean> {
59
- if (!pushToken) {
49
+ const token = getPushToken()
50
+ if (!token) {
60
51
  api.logger.warn('notification: no push token registered, skipping notification')
61
52
  return false
62
53
  }
@@ -66,13 +57,14 @@ export async function sendPushNotification(
66
57
  method: 'POST',
67
58
  headers: {'Content-Type': 'application/json'},
68
59
  body: JSON.stringify({
69
- to: pushToken,
60
+ to: token,
70
61
  sound: 'default',
71
62
  title: opts.title ?? 'Clawly',
72
63
  body: opts.body,
73
64
  data: opts.data,
74
65
  }),
75
66
  })
67
+ const json = await res.json()
76
68
 
77
69
  if (!res.ok) {
78
70
  api.logger.error(`notification: push failed — ${res.status} ${res.statusText}`)
@@ -90,8 +82,6 @@ export async function sendPushNotification(
90
82
  }
91
83
 
92
84
  export function registerNotification(api: PluginApi) {
93
- loadPersistedToken(api)
94
-
95
85
  api.registerGatewayMethod('clawly.notification.setToken', async ({params, respond}) => {
96
86
  const token = typeof params.token === 'string' ? params.token.trim() : ''
97
87
 
@@ -100,7 +90,6 @@ export function registerNotification(api: PluginApi) {
100
90
  return
101
91
  }
102
92
 
103
- pushToken = token
104
93
  persistToken(token, api)
105
94
  api.logger.info('notification: push token registered')
106
95
  respond(true, {registered: true})
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@2en/clawly-plugins",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "repository": {