Skip to content

Commit

Permalink
Added block user function
Browse files Browse the repository at this point in the history
  • Loading branch information
f-r00t committed May 31, 2024
1 parent 8646d6f commit 45cb27b
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 8 deletions.
70 changes: 70 additions & 0 deletions src/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ async function createTables(DB) {
)`
);

tx.executeSql(
`CREATE TABLE IF NOT EXISTS blocklist (
address TEXT,
name TEXT,
UNIQUE (address)
)`
);

/* Add new columns */
if (dbVersion === 0) {
tx.executeSql(
Expand Down Expand Up @@ -2147,3 +2155,65 @@ export async function loadTransactionDetailsFromDatabase() {

return undefined;
}

async function removeGroupMessagesFromUser(address) {

const [data] = await database.executeSql(
`DELETE FROM privateboards_messages_db WHERE address = "${address}"`
)

Globals.updateGroups();

}

export async function blockUser(address, nickname) {

const [data] = await database.executeSql(
`REPLACE INTO blocklist
(address, name)
VALUES
(?, ?)`,
[address, nickname]
)

console.log('Added to block list', address)
removeGroupMessagesFromUser(address);

}

export async function getBlockList() {

console.log('Getting blocklist..');

const [data] = await database.executeSql(
`SELECT * FROM blocklist`
);

let blockList = [];
if (data && data.rows && data.rows.length) {

for (let i = 0; i < data.rows.length; i++) {

const item = data.rows.item(i);
timestamp = item.timestamp;
blockList.push(item.address);

}

}

return blockList;

}

export async function unBlockUsers (address) {

const [data] = await database.executeSql(
`DELETE FROM
blocklist
WHERE
address = ${address}`);


console.log('Removed from block list', address)
}
7 changes: 4 additions & 3 deletions src/Globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import NetInfo from "@react-native-community/netinfo";

import { deleteUserPinCode } from '@haskkor/react-native-pincode';

import { getLastSync, setHaveWallet, openDB, deleteDB, getKnownTransactions, getUnreadMessages, getGroupMessages, saveGroupToDatabase, removeMessages, loadPayeeDataFromDatabase, savePayeeToDatabase, removePayeeFromDatabase,
import { getBlockList, getLastSync, setHaveWallet, openDB, deleteDB, getKnownTransactions, getUnreadMessages, getGroupMessages, saveGroupToDatabase, removeMessages, loadPayeeDataFromDatabase, savePayeeToDatabase, removePayeeFromDatabase,
loadTransactionDetailsFromDatabase, saveTransactionDetailsToDatabase, removeGroupFromDatabase, getMessages, getLatestMessages, getBoardsMessages, getBoardSubscriptions, loadGroupsDataFromDatabase } from './Database';
import Config from './Config';
import { Logger } from './Logger';
Expand Down Expand Up @@ -139,6 +139,8 @@ class globals {

this.navigation = undefined;

this.blockList = [];

}

async reset() {
Expand Down Expand Up @@ -451,7 +453,6 @@ export async function initGlobals() {

Globals.lastMessageTimestamp = lastSync.lastSyncGroup;

console.log('lastSync', lastSync);

Globals.blockList = await getBlockList();

}
32 changes: 30 additions & 2 deletions src/Groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {intToRGB, hashCode, get_avatar, sendGroupsMessage, createGroup, getBoard

import {toastPopUp} from './Utilities';

import { getGroupsMessage, loadGroupsDataFromDatabase, subscribeToGroup, markGroupConversationAsRead, getGroupMessages, getReplies, saveGroupMessage, removeGroupMessage} from './Database';
import { getGroupsMessage, loadGroupsDataFromDatabase, subscribeToGroup, markGroupConversationAsRead, getGroupMessages, getReplies, saveGroupMessage, removeGroupMessage, blockUser} from './Database';

import './i18n.js';
import { withTranslation } from 'react-i18next';
Expand Down Expand Up @@ -962,6 +962,7 @@ export class GroupChatScreenNoTranslation extends React.Component {
borderTopRightRadius: 20,
borderTopLeftRadius: 20,
padding: 25,
paddingBottom: 100,
alignItems: "center",
shadowColor: "#000",
shadowOffset: {
Expand Down Expand Up @@ -1071,7 +1072,7 @@ export class GroupChatScreenNoTranslation extends React.Component {
<View style={{
flex: 1,
backgroundColor: this.props.screenProps.theme.backgroundColour,
alignItems: 'center',
alignItems: 'center'
}}>

<View style={{
Expand Down Expand Up @@ -1387,6 +1388,33 @@ export class GroupChatScreenNoTranslation extends React.Component {
}}>
Reply</Text>
</TouchableOpacity>
<TouchableOpacity
style={{
backgroundColor: 'rgba(255,255,255,0.02)',
borderWidth: 1,
borderColor: this.props.screenProps.theme.borderColour,
borderRadius: 15,
padding: 10,
flexDirection: 'row',
alignContent: 'center',
justifyContent: 'space-between',
marginLeft: 10
}}
onPress={() => {
blockUser(this.state.activePost.address, this.state.activePost.nickname);
this.setMessageModalVisible(false);
}}
>
<CustomIcon name='user-remove' size={18} style={{marginRight: 4, color: 'rgba(255,255,255,0.8)'}} />
<Text style={{
color: this.props.screenProps.theme.primaryColour,
textAlign: 'left',
fontSize: 14,
fontFamily: 'Montserrat-Bold',
textAlign: 'center'
}}>
Block</Text>
</TouchableOpacity>
</View>


Expand Down
6 changes: 3 additions & 3 deletions src/HuginUtilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,6 @@ export async function cacheSync(first=true, page=1) {
let last_read_timestamp = 0;

latest_board_message_timestamp = Globals.lastMessageTimestamp;
console.log('CHECKMEPLS', Globals.lastMessageTimestamp)
Globals.logger.addLogMessage(`Syncing group messages from ${new Date(latest_board_message_timestamp).toISOString().replace('T', ' ').replace(/\.\d+Z$/, '')} to ${new Date(Date.now()).toISOString().replace('T', ' ').replace(/\.\d+Z$/, '')}.. 💌`);
let cacheURL = Globals.preferences.cache ? Globals.preferences.cache : Config.defaultCache;
console.log(`${cacheURL}/api/v1/posts-encrypted-group?from=${parseInt(latest_board_message_timestamp/1000)}&to=${parseInt(Date.now()/1000)}&size=50&page=` + page);
Expand All @@ -534,8 +533,6 @@ export async function cacheSync(first=true, page=1) {
setLastSyncGroup(last_read_timestamp);
Globals.lastSyncEvent = Date.now();

console.log('Finished presync');

if (await groupMessageExists(items[item].tx_timestamp)) continue;
// if (Globals.knownTXs.indexOf(items[item].tx_hash) != -1) continue;

Expand Down Expand Up @@ -886,6 +883,9 @@ 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 from_myself = (from == Globals.wallet.getPrimaryAddress() ? true : false);

const received = (from_myself ? 'sent' : 'received');
Expand Down

0 comments on commit 45cb27b

Please sign in to comment.