1
- import { addExtension , decode , decodeMultiple } from "cbor-x" ;
2
- import { CID } from "multiformats" ;
3
- import { CarReader } from "@ipld/car" ;
4
1
import { realtime , realtimeStatuses } from '$lib/stores' ;
2
+ import { PUBLIC_TOKIMEKI_STREAM_API } from '$env/static/public' ;
3
+
4
+ const COLLECTIONS = [ 'app.bsky.feed.post' , 'app.bsky.feed.repost' , 'app.bsky.feed.like' , 'app.bsky.graph.follow' ] ;
5
5
6
6
export class RealtimeClient {
7
7
private host : string ;
@@ -13,49 +13,33 @@ export class RealtimeClient {
13
13
14
14
connect ( ) {
15
15
if ( ! this . socket ) {
16
- this . socket = new WebSocket ( 'wss://' + this . host + '/xrpc/com.atproto.sync.subscribeRepos' ) ;
16
+ this . socket = new WebSocket ( ` ${ PUBLIC_TOKIMEKI_STREAM_API } /subscribe? ${ COLLECTIONS . map ( item => `wantedCollections= ${ item } ` ) . join ( '&' ) } ` ) ;
17
17
}
18
18
19
- addExtension ( {
20
- Class : CID ,
21
- tag : 42 ,
22
- encode : ( ) => {
23
- throw new Error ( 'Cannot encode cids' ) ;
24
- } ,
25
- decode : ( bytes ) => {
26
- if ( bytes [ 0 ] !== 0 ) {
27
- throw new Error ( 'Invalid cid' ) ;
28
- }
29
- return CID . decode ( bytes . subarray ( 1 ) ) ; // ignore leading 0x00
30
- } ,
31
- } ) ;
32
-
33
19
this . socket . onmessage = async function ( event ) {
34
- const messageBuf = await event . data . arrayBuffer ( ) ;
35
- const [ header , body ] = decodeMultiple ( new Uint8Array ( messageBuf ) ) ;
36
-
37
- if ( header . op !== 1 ) {
20
+ if ( ! event . data ) {
38
21
return ;
39
22
}
40
23
41
- try {
42
- const car = await CarReader . fromBytes ( body . blocks ) ;
43
-
44
- for ( const op of body . ops ) {
45
- if ( ! op . cid ) continue ;
46
- const block = await car . get ( op . cid ) ;
47
- const record = decode ( block . bytes ) ;
48
- realtime . set ( {
49
- isConnected : true ,
50
- data : {
51
- record : record ,
52
- op : op ,
53
- body : body ,
54
- }
55
- } )
56
- }
57
- } catch ( e ) {
58
- // do nothing.
24
+ const dataWrapper = JSON . parse ( event . data ) ;
25
+ const data = dataWrapper . commit ;
26
+
27
+ if ( data ?. type === 'c' ) {
28
+ const record = data . record ;
29
+
30
+ realtime . set ( {
31
+ isConnected : true ,
32
+ data : {
33
+ record : record ,
34
+ op : {
35
+ path : data . rkey ,
36
+ collection : data . collection ,
37
+ } ,
38
+ body : {
39
+ repo : dataWrapper . did ,
40
+ } ,
41
+ }
42
+ } )
59
43
}
60
44
} ;
61
45
@@ -72,6 +56,10 @@ export class RealtimeClient {
72
56
return [ ...r , this . host ] ;
73
57
} )
74
58
}
59
+
60
+ this . socket . onerror = async ( event ) => {
61
+ console . log ( event )
62
+ }
75
63
}
76
64
77
65
disconnect ( ) {
@@ -129,7 +117,7 @@ async function getRecord(_agent, uri, repost = undefined, retryCount = 0) {
129
117
export async function getPostRealtime ( realtime , actors , _agent ) {
130
118
const path = realtime . data . op . path ;
131
119
const repo = realtime . data . body . repo ;
132
- const uri = 'at://' + repo + '/' + path ;
120
+ const uri = 'at://' + repo + '/' + realtime . data . op . collection + '/' + path ;
133
121
const isStream : boolean = actors . some ( actor => actor === repo ) ;
134
122
135
123
if ( realtime . data . record . $type === 'app.bsky.feed.post' && typeof realtime . data . record . text === 'string' ) {
0 commit comments