-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserService.ts
82 lines (71 loc) · 2.96 KB
/
UserService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import ClientWebSocket, { Filter, MessageType, ClientMessage } from "./ClientWebSocket";
//ClientWebSocket
class UserService {
clientWebSocket : ClientWebSocket;
//msgHandler : (msg: any , kind: number) => any;
//constructor(clientWebSocket : ClientWebSocket, msgHandler : (msg: any , kind: number) => any) {
constructor(clientWebSocket : ClientWebSocket) {
this.clientWebSocket = clientWebSocket;
//this.msgHandler = msgHandler;
}
/*
[
"REQ","cn",
{"limit":2,"authors":"npub1sg6plzptd64u62a878hep2kev88swjh3tw00gjsfl8f237lmu63q0uf63m", "kinds":[0]}
]
*/
getUserDetails(author : string) {
console.log("[getUserDetails] :: author :: " + author)
let clientMsg = this.clientWebSocket.metaMessages.find((msg)=>{
msg.fullEvent.pubkey===author && msg.fullEvent.kind===0
})
if(clientMsg){
return clientMsg;
} else {
let filter = new Filter();
filter.limit = 2;
filter.kinds = [0];
filter.authors = [author];
//this.clientWebSocket.sendMessage(MessageType.REQ, author, undefined, [filter]);
this.clientWebSocket.sendReq(author, [filter]);
clientMsg = this.clientWebSocket.metaMessages.find((msg)=>{
msg.fullEvent.pubkey===author && msg.fullEvent.kind===0
})
}
return clientMsg;
/*
let promise = new Promise((resolve, reject) => {
let clientMsg = this.clientWebSocket.metaMessages.find((msg)=>{
msg.fullEvent.pubkey===author && msg.fullEvent.kind===0
})
if(clientMsg) {
resolve(clientMsg);
} else {
reject("details not found");
}
});
promise.then(value => {
return value;
}
).catch( rej => {});
*/
}
/*
["REQ","cn",{"ids":["78384accb8c39eaa05641803336813f698d11692a87b30c07d1ebfc6f6bf7aa1"]}]
*/
getMsgFromEventId(eventId : string, remoteUrl? : string, msgHandler? : (msg: any , kind: number) => any) {
console.log("[UserService] :: [getMsgFromEventId] ::");
console.log("[UserService] :: [getMsgFromEventId] :: remoteUrl :: " + remoteUrl);
console.log("[UserService] :: [getMsgFromEventId] :: msgHandler " + msgHandler);
if(remoteUrl && msgHandler) {
console.log("[UserService] :: [getMsgFromEventId] :: creating new socket to url :: " + remoteUrl);
this.clientWebSocket.addRemoteUrl(remoteUrl);
this.clientWebSocket.openConnections(msgHandler);
}
let filter = new Filter()
filter.ids = [eventId];
console.log("[UserService] :: [getMsgFromEventId] :: sending request to :: " + remoteUrl);
this.clientWebSocket.sendReq(ClientWebSocket.eventReqId, [filter]);
}
}
export default UserService