Skip to content

Commit c65e8b0

Browse files
committed
Add payreq trackers
1 parent 630426d commit c65e8b0

File tree

3 files changed

+44
-29
lines changed

3 files changed

+44
-29
lines changed

src/payload-protocol/parsers.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import version from "./version";
21
import ReactMarkdown from "react-markdown";
32
import C13nImage from "../components/content-pages/markdown/c13n-image";
43
import c13nLink from "../components/content-pages/markdown/c13n-link";
54
import c13nLinkRef from "../components/content-pages/markdown/c13n-link-ref";
65
import c13nText from "../components/content-pages/markdown/c13n-text";
76
import c13nInlineCode from "../components/content-pages/markdown/c13n-inline-code";
87

9-
import { checkPayreq } from "../utils/payreq/payreq-tracker";
8+
import { checkPayreq, registerPaidPayreq } from "../utils/payreq/payreq-tracker";
9+
import { appendToChatHistory } from "../utils/discussion-utils";
1010

1111
import { NotificationManager } from "react-notifications";
1212

@@ -18,7 +18,7 @@ import messageClient from "../services/messageServices";
1818
import React from "react";
1919

2020
const cryptoUtils = require("../utils/crypto-utils");
21-
var lightningPayReq = require('bolt11');
21+
const lightningPayReq = require('bolt11');
2222

2323
/**
2424
* The markdown renderers.
@@ -162,7 +162,7 @@ const c13nPpToDom = (props, payloadObj, myMessage) => {
162162
myMessage ? ' receive' : ' pay'
163163
} <b>{cryptoUtils.msatToCurrentCryptoUnit(props, invoiceObj?.millisatoshis)} {props.selectedCryptoUnit}</b></h3>
164164
{
165-
myMessage ? checkPayreq(payloadObj.c)
165+
myMessage ? checkMyPayreq(payloadObj.c)
166166
?
167167
<h2 style={{ color: 'green' }}><b>Paid <CheckOutlined /></b></h2>
168168
:
@@ -191,6 +191,9 @@ const c13nPpToDom = (props, payloadObj, myMessage) => {
191191
}
192192
if(res) {
193193
console.log(res);
194+
props.selectedDiscussion.lastMsgId = res.sentMessage.id;
195+
props.selectedDiscussion.lastReadMsgId = res.sentMessage.id;
196+
appendToChatHistory(props, res.sentMessage);
194197
}
195198
}
196199
);
@@ -276,24 +279,30 @@ const c13nPpToDom = (props, payloadObj, myMessage) => {
276279
</div>
277280
);
278281
case "payreq_pay":
282+
registerPaidPayreq(payloadObj.c);
279283
return(
280284
<div
281285
style={{
282-
fontSize: '20px',
286+
fontSize: '16px',
283287
border: '2px solid gray',
284288
borderRadius: '5px',
285289
padding: '15px'
286290
}}
287291
>
288292
Paid
289-
<b>
293+
<b
294+
style={{
295+
paddingLeft: "5px",
296+
fontSize: "14px"
297+
}}
298+
>
290299
{`${payloadObj?.c?.substring(
291300
0,
292301
5
293302
)}...${payloadObj?.c?.substring(
294303
payloadObj?.c?.length - 5,
295304
payloadObj?.c?.length
296-
)}: `}
305+
)}`}
297306
</b>
298307
</div>
299308
);

src/utils/payreq/issue-payreq.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/utils/payreq/payreq-tracker.js

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
1-
let payreqs = {};
1+
import paymentClient from "../../services/paymentServices";
22

3-
const issuePayreq = (id) => {
4-
if(!(id in payreqs)) {
5-
payreqs[id] = false;
6-
}
3+
let paidPayreqs = {};
4+
let myPaidPayreqs = {};
5+
6+
const registerPaidPayreq = (payreq) => {
7+
paidPayreqs[payreq] = true;
78
};
89

9-
const satisfyPayreq = (id) => {
10-
payreqs[id] = true;
10+
const checkPayreq = (payreq) => {
11+
return paidPayreqs[payreq] == true;
1112
};
1213

13-
const checkPayreq = (id) => {
14-
if(id in payreqs) {
15-
return payreqs[id];
16-
} else {
17-
return false;
14+
const checkMyPayreq = async (payreq) => {
15+
if(myPaidPayreqs[payreq] == true) {
16+
return true;
1817
}
18+
paymentClient().lookupInvoice(
19+
{
20+
payReq: payreq
21+
},
22+
(err, res) => {
23+
if(err) {
24+
console.log(err);
25+
}
26+
if(res) {
27+
console.log(res);
28+
}
29+
}
30+
);
31+
1932
};
2033

2134
export {
22-
issuePayreq,
23-
satisfyPayreq,
24-
checkPayreq
35+
registerPaidPayreq,
36+
checkPayreq,
37+
checkMyPayreq
2538
};

0 commit comments

Comments
 (0)