Skip to content

Commit

Permalink
Added rudimentary spam filter
Browse files Browse the repository at this point in the history
  • Loading branch information
f-r00t committed Jun 1, 2024
1 parent 45cb27b commit 9d6cbce
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
53 changes: 50 additions & 3 deletions src/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ export async function openDB() {

await createTables(database);
} catch (err) {
Globals.logger.addLogMessage('Failed to open DB: ' + err);
Globals.logger.addLogMessage('Failed to open DB: ' + JSON.stringify(err));
}
}

Expand Down Expand Up @@ -2177,6 +2177,8 @@ export async function blockUser(address, nickname) {
)

console.log('Added to block list', address)
Globals.blockList.push(address);
Globals.logger.addLogMessage('Blocked users: ' + Globals.blockList.join(' ,'));
removeGroupMessagesFromUser(address);

}
Expand All @@ -2195,7 +2197,7 @@ export async function getBlockList() {
for (let i = 0; i < data.rows.length; i++) {

const item = data.rows.item(i);
timestamp = item.timestamp;

blockList.push(item.address);

}
Expand All @@ -2212,8 +2214,53 @@ export async function unBlockUsers (address) {
`DELETE FROM
blocklist
WHERE
address = ${address}`);
address = "${address}"`);


console.log('Removed from block list', address)
}


export async function isSpam (address, message, timestamp) {

const length = message.length;
const an_hour_ago = Date.now() - (60*60*1000);

if (length < 10) {
const [data] = await database.executeSql(
`
SELECT hash
FROM privateboards_messages_db D
WHERE message = "${message}"
AND address = "${address}"
AND timestamp > ${an_hour_ago}
`);

if (data && data.rows && data.rows.length) {
return true;
} else {
return false;
}
};

const partLength = Math.ceil(length / 3);

const part1 = message.slice(0, partLength);
const part2 = message.slice(partLength, partLength * 2);
const part3 = message.slice(partLength * 2);

const [data] = await database.executeSql(
`
SELECT hash
FROM privateboards_messages_db D
WHERE message LIKE "%${part1}%" OR message LIKE "%${part2}%" OR message LIKE "%${part3}%"
AND timestamp > ${an_hour_ago}
`);

if (data && data.rows && data.rows.length) {
return true;
} else {
return false;
}

}
9 changes: 8 additions & 1 deletion src/Globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import NetInfo from "@react-native-community/netinfo";
import { deleteUserPinCode } from '@haskkor/react-native-pincode';

import { getBlockList, getLastSync, setHaveWallet, openDB, deleteDB, getKnownTransactions, getUnreadMessages, getGroupMessages, saveGroupToDatabase, removeMessages, loadPayeeDataFromDatabase, savePayeeToDatabase, removePayeeFromDatabase,
loadTransactionDetailsFromDatabase, saveTransactionDetailsToDatabase, removeGroupFromDatabase, getMessages, getLatestMessages, getBoardsMessages, getBoardSubscriptions, loadGroupsDataFromDatabase } from './Database';
loadTransactionDetailsFromDatabase, saveTransactionDetailsToDatabase, removeGroupFromDatabase, getMessages, getLatestMessages, getBoardsMessages, getBoardSubscriptions, loadGroupsDataFromDatabase, unBlockUsers } from './Database';
import Config from './Config';
import { Logger } from './Logger';
import { getCoinPriceFromAPI } from './Currency';
Expand Down Expand Up @@ -453,6 +453,13 @@ export async function initGlobals() {

Globals.lastMessageTimestamp = lastSync.lastSyncGroup;

await unBlockUsers('SEKReWcYe3X1bVh2Cv3YfY6nosjm1ZDhAG5y6jwMbPofEUUwi5Kgt6dM8z1tJjuHe6AASFqViHxBxM92x1oJ71WgRT4aL4XGbC8');
await unBlockUsers('SEKReTbT3Kr1YAJdGT7HudX9nG7iq99vpbxgWKhHgg3G4daZpnp5zxfeMpQpigJKAf8Fu4RYybyQpSz2rGj4TPDGePE4oUkP4Sp');
await unBlockUsers('SEKReXtKuFcEfMuvf9SY4nFYVz6KifongWRt3Kuvo5TfU1yDh5DGFYoCNVAVhE6oUZ83GVhpQg8XJg14w8Tzb5XdRJH5iz4VVdE');
await unBlockUsers('SEKReUabHDKVWqE7mtYjQeY2mMQYq49U8TQYzEQ2KidDd7fwtmWMhorMhHoXChpikL7c8rYi2AAyTS4SVxWfvkK18ku4zVd8zFv');

Globals.blockList = await getBlockList();

Globals.logger.addLogMessage('Blocked users: ' + Globals.blockList.join(' ,'));

}
7 changes: 5 additions & 2 deletions src/HuginUtilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import * as NaclSealed from 'tweetnacl-sealed-box';

import Identicon from 'identicon.js';

import {setLastSyncGroup, setLastSyncDM, emptyKnownTXs, updateGroupMessage, updateMessage, savePreferencesToDatabase, getGroupName, saveGroupMessage, groupMessageExists, getGroupKey, getLatestGroupMessage, getHistory, getLatestMessages, saveToDatabase, loadPayeeDataFromDatabase, saveMessage, saveBoardsMessage, savePayeeToDatabase, messageExists, getLatestMessage, saveKnownTransaction } from './Database';
import {setLastSyncGroup, setLastSyncDM, emptyKnownTXs, updateGroupMessage, updateMessage, savePreferencesToDatabase, getGroupName, saveGroupMessage, groupMessageExists, getGroupKey, getLatestGroupMessage, getHistory, getLatestMessages, saveToDatabase, loadPayeeDataFromDatabase, saveMessage, saveBoardsMessage, savePayeeToDatabase, messageExists, getLatestMessage, saveKnownTransaction, isSpam } from './Database';

/**
* Save wallet in background
Expand Down Expand Up @@ -883,8 +883,11 @@ async function getGroupMessage(tx) {
Globals.logger.addLogMessage('[Message sync] New message found: ' + payload_json.m.slice(0,10));

const from = payload_json.k;
if (Globals.blockList.some(a => a === from)) return false;

const spam = await isSpam(from, payload_json.m, tx.t);
if (spam) return false;

if (Globals.blockList.some(a => a === from)) return false;

const from_myself = (from == Globals.wallet.getPrimaryAddress() ? true : false);

Expand Down

0 comments on commit 9d6cbce

Please sign in to comment.