modified to use regex matching for messages
This commit is contained in:
parent
c754c14272
commit
d526168e25
2 changed files with 47 additions and 40 deletions
79
app.js
79
app.js
|
@ -14,6 +14,9 @@ if (config.debug) {
|
||||||
const baseUrl = 'https://oauth.reddit.com';
|
const baseUrl = 'https://oauth.reddit.com';
|
||||||
const rateUrl = 'https://api.lbry.io/lbc/exchange_rate';
|
const rateUrl = 'https://api.lbry.io/lbc/exchange_rate';
|
||||||
const tokenUrlFormat = 'https://%s:%s@www.reddit.com/api/v1/access_token';
|
const tokenUrlFormat = 'https://%s:%s@www.reddit.com/api/v1/access_token';
|
||||||
|
const tipRegex = /(\$\d+|\d+( usd| lbc))/i;
|
||||||
|
const gildRegex = new RegExp('gild (u|\/u)\/lbryian|(u|\/u)\/lbryian gild', 'ig');
|
||||||
|
|
||||||
// Other globals
|
// Other globals
|
||||||
const commentKind = 't1';
|
const commentKind = 't1';
|
||||||
const privateMessageKind = 't4';
|
const privateMessageKind = 't4';
|
||||||
|
@ -640,54 +643,48 @@ const doGild = function(message, callback) {
|
||||||
], callback);
|
], callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
const doSendTip = function(body, message, callback) {
|
const doSendTip = (body, message, callback) => {
|
||||||
/**
|
/**
|
||||||
* accepted formats:
|
* accepted matched strings:
|
||||||
* 1 usd u/lbryian OR u/lbryian 1 usd
|
* "1 usd" or "1 lbc" or "$1"
|
||||||
* 1 lbc u/lbryian OR u/lbryian 1 lbc
|
|
||||||
* $1 u/lbryian OR u/lbryian $1
|
|
||||||
*/
|
*/
|
||||||
const parts = body.split(' ', 3);
|
// Use regex matching
|
||||||
const parentId = message.data.parent_id ? message.data.parent_id.trim() : null;
|
|
||||||
if ((!parentId || parentId.length === 0) || (parts.length === 0) || (parts.length !== 3 && (parts.length === 2 && parts[0].substring(0,1) !== '$'))) {
|
|
||||||
// ignore the comment
|
|
||||||
return callback(null, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parts[0] && parts[0].substring(0, 1) === '/') {
|
|
||||||
parts[0] = parts[0].substring(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
let amountUsd = 0;
|
let amountUsd = 0;
|
||||||
let amountLbc = 0;
|
let amountLbc = 0;
|
||||||
const nameFirst = parts[0] === config.redditName;
|
let matchedString = '';
|
||||||
if (parts.length === 2) {
|
const match = String(message.data.body).match(tipRegex);
|
||||||
// get the amount
|
if (match.length > 0) {
|
||||||
amountUsd = parseFloat(parts[nameFirst ? 1 : 0].substring(1));
|
matchedString = match[0];
|
||||||
if (isNaN(amountUsd) || amountUsd <= 0) {
|
if (matchedString.indexOf(' ') > -1) {
|
||||||
return sendPMUsingTemplate('onsendtip.invalidamount', { how_to_use_url: config.howToUseUrl }, 'Invalid amount for send tip', message.data.author, () => {
|
const parts = matchedString.split(' ', 2);
|
||||||
markMessageRead(message.data.name, callback);
|
const amount = parseFloat(parts[0]);
|
||||||
});
|
const unit = parts[1].toLowerCase();
|
||||||
}
|
if (isNaN(amount) || amount <= 0 || ['usd', 'lbc'].indexOf(unit) === -1) {
|
||||||
} else if (parts.length === 3) {
|
// invalid amount or unit
|
||||||
const amount = parseFloat(parts[nameFirst ? 1 : 0]);
|
return sendPMUsingTemplate('onsendtip.invalidamount', { how_to_use_url: config.howToUseUrl }, 'Invalid amount for send tip', message.data.author, () => {
|
||||||
const unit = parts[nameFirst ? 2 : 1].toLowerCase();
|
markMessageRead(message.data.name, callback);
|
||||||
if (isNaN(amount) || amount <= 0 || ['usd', 'lbc'].indexOf(unit) === -1) {
|
});
|
||||||
// invalid amount or unit
|
}
|
||||||
return callback(null, null);
|
|
||||||
}
|
if (unit === 'lbc') {
|
||||||
|
amountLbc = amount;
|
||||||
if (unit === 'lbc') {
|
} else {
|
||||||
amountLbc = amount;
|
amountUsd = amount;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
amountUsd = amount;
|
amountUsd = parseFloat(matchedString.substring(1));
|
||||||
|
if (isNaN(amountUsd) || amountUsd <= 0) {
|
||||||
|
return sendPMUsingTemplate('onsendtip.invalidamount', { how_to_use_url: config.howToUseUrl }, 'Invalid amount for send tip', message.data.author, () => {
|
||||||
|
markMessageRead(message.data.name, callback);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (amountLbc > 0 || amountUsd > 0) {
|
if (amountLbc > 0 || amountUsd > 0) {
|
||||||
const parsedAmount = (parts.length === 2) ? parts[nameFirst ? 1 : 0] : [parts[nameFirst ? 1 : 0], parts[nameFirst ? 2 : 1]].join(' ');
|
const parsedAmount = matchedString;
|
||||||
// get the author of the parent message
|
// get the author of the parent message
|
||||||
async.waterfall([
|
return async.waterfall([
|
||||||
(cb) => {
|
(cb) => {
|
||||||
getMessageAuthor(message.data.parent_id, globalAccessToken, cb);
|
getMessageAuthor(message.data.parent_id, globalAccessToken, cb);
|
||||||
},
|
},
|
||||||
|
@ -733,6 +730,9 @@ const doSendTip = function(body, message, callback) {
|
||||||
}
|
}
|
||||||
], callback);
|
], callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// not a valid or recognised message, simply mark the message as read
|
||||||
|
return markMessageRead(message.data.name, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
const doSendBalance = (message, callback) => {
|
const doSendBalance = (message, callback) => {
|
||||||
|
@ -940,8 +940,7 @@ const processMessage = function(message, callback) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (message.kind === commentKind) {
|
if (message.kind === commentKind) {
|
||||||
const bodyParts = body.split(' ', 2);
|
if (body.match(gildRegex).length > 0) {
|
||||||
if (bodyParts.length === 2 && ('gild' === bodyParts[0].toLowerCase() || 'gild' === bodyParts[1].toLowerCase())) {
|
|
||||||
doGild(message, callback);
|
doGild(message, callback);
|
||||||
} else {
|
} else {
|
||||||
doSendTip(body, message, callback);
|
doSendTip(body, message, callback);
|
||||||
|
|
|
@ -44,6 +44,14 @@ CREATE TABLE Tips
|
||||||
FOREIGN KEY `FK_TipRecipient` (`RecipientId`) REFERENCES `Users` (`Id`)
|
FOREIGN KEY `FK_TipRecipient` (`RecipientId`) REFERENCES `Users` (`Id`)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4;
|
||||||
|
|
||||||
|
CREATE TABLE PendingMessageQueue
|
||||||
|
(
|
||||||
|
`Id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||||
|
`ReplyToMessageId` BIGINT UNSIGNED NOT NULL,
|
||||||
|
`MessageText` TEXT,
|
||||||
|
PRIMARY KEY `PK_PendingMessageId` (`Id`)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4;
|
||||||
|
|
||||||
CREATE TABLE Deposits
|
CREATE TABLE Deposits
|
||||||
(
|
(
|
||||||
`Id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
`Id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||||
|
|
Loading…
Reference in a new issue