Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

notifications #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"lint": "eslint src --ext ts,tsx,js,jsx --cache"
},
"dependencies": {
"@textshq/platform-sdk": "https://github.com/TextsHQ/platform-sdk",
"@textshq/platform-sdk": "https://github.com/TextsHQ/platform-sdk#main",
"form-data": "^4.0.0",
"tough-cookie": "^4.0.0",
"ws": "^8.3.0"
Expand Down
8 changes: 6 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { LoginCreds, LoginResult, Message, MessageContent, OnServerEventCallback, Paginated, PaginationArg, PlatformAPI, Thread } from '@textshq/platform-sdk'
import { ActivityType } from '@textshq/platform-sdk'
import { ActivityType, LoginCreds, LoginResult, Message, MessageContent, OnServerEventCallback, Paginated, PaginationArg, PlatformAPI, Thread } from '@textshq/platform-sdk'
import { CookieJar } from 'tough-cookie'

import type { RedditUser } from './lib/types'
Expand Down Expand Up @@ -114,4 +113,9 @@ export default class Reddit implements PlatformAPI {
addReaction = this.api.addReaction

removeReaction = this.api.removeReaction

registerForPushNotifications = async (type: 'web', token: string) => {
const parsed: PushSubscriptionJSON = JSON.parse(token)
await this.api.registerPush(parsed.endpoint, parsed.keys.p256dh, parsed.keys.auth)
}
}
5 changes: 5 additions & 0 deletions src/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ const info: PlatformInfo = {
default: false,
},
},
notifications: {
web: {
vapidKey: 'BJ2nJR9HeBwCWe4s7bKfKgWZkx2Q8Q59yBdSVLxWIhPaWuzHDUxQ2YJnhLvbAVujWBZYuQv60V6a6oipSw09FT0',
},
},
attachments: {
noSupportForFiles: true,
noSupportForAudio: true,
Expand Down
27 changes: 26 additions & 1 deletion src/lib/reddit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { randomUUID as uuid } from 'crypto'
import FormData from 'form-data'
import { promises as fs } from 'fs'
import { setTimeout as sleep } from 'timers/promises'
import type { Message, MessageContent, OnServerEventCallback, Thread, User } from '@textshq/platform-sdk'
import { Message, MessageContent, OnServerEventCallback, texts, Thread, User } from '@textshq/platform-sdk'
import type { CookieJar } from 'tough-cookie'

import { MOBILE_USERAGENT, OAUTH_CLIENT_ID_B64, RedditURLs, WEB_USERAGENT } from './constants'
Expand Down Expand Up @@ -560,6 +560,31 @@ class RedditAPI {
await this.wsClient.sendTyping(threadID)
}

registerPush = async (endpoint: string, p256dh: string, auth: string) => {
const headers = this.getRedditHeaders()
const payload = {
id: '197650c1946c',
variables: {
pushToken: JSON.stringify({
endpoint,
expirationTime: null,
keys: { p256dh, auth },
}),
timezoneName: 'Europe/London',
timestamp: new Date().toJSON(),
language: 'en_us',
},
}
const res = await this.http.post(RedditURLs.API_GRAPHQL, {
body: JSON.stringify(payload),
headers,
})
if (!res?.data?.registerWebPushToken?.ok) {
texts.log(res)
throw Error('could not register push')
}
}

addReaction = async (threadID: string, messageID: string, reactionKey: string) => {
const headers = this.getRedditHeaders()
const [, id] = threadID.split('channel_')
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ __metadata:
resolution: "@textshq/platform-reddit@workspace:."
dependencies:
"@textshq/eslint-config": "https://github.com/TextsHQ/eslint-config"
"@textshq/platform-sdk": "https://github.com/TextsHQ/platform-sdk"
"@textshq/platform-sdk": "https://github.com/TextsHQ/platform-sdk#main"
"@types/eslint": ^7.28.0
"@types/tough-cookie": ^4.0.1
"@types/ws": ^8.2.0
Expand All @@ -257,12 +257,12 @@ __metadata:
languageName: unknown
linkType: soft

"@textshq/platform-sdk@https://github.com/TextsHQ/platform-sdk":
"@textshq/platform-sdk@https://github.com/TextsHQ/platform-sdk#main":
version: 0.4.4
resolution: "@textshq/platform-sdk@https://github.com/TextsHQ/platform-sdk.git#commit=4f4febf00f0ee5f6940cf2bf7b6684d0d069032e"
resolution: "@textshq/platform-sdk@https://github.com/TextsHQ/platform-sdk.git#commit=872b1e896be308bb0120924dabdefa9fd451a861"
dependencies:
form-data: ^4.0.0
checksum: e096ee4c0e6db89b3c89acdbbc70d8b8e113c2ac7f20d7605ae5c49ad7c6ce84926fc5302bbfff42bb8bdf8e573dfcb2728a52959b1bf35939ad8b959236a5ce
checksum: 8fac33a19bd63bec00c1c999f2a343ebcc410629e8f82c4548d820aa2bba6c03081d5285c2f5420c6cadd36958b5dc6103ced7891cb34a5e4fa4914f768e8c12
languageName: node
linkType: hard

Expand Down