add route guard for pages that require database connection

This commit is contained in:
liamcottle
2025-02-14 00:34:15 +13:00
parent f0a96b04ee
commit 2a7ed6203f
3 changed files with 22 additions and 20 deletions

View File

@@ -4,23 +4,22 @@ import {getRxStorageDexie} from 'rxdb/plugins/storage-dexie';
import GlobalState from "./GlobalState.js"; import GlobalState from "./GlobalState.js";
import Utils from "./Utils.js"; import Utils from "./Utils.js";
var database = null;
async function initDatabase(publicKeyHex) { async function initDatabase(publicKeyHex) {
// close any exsiting database connection // close any exsiting database connection
if(database){ if(GlobalState.database){
await database.close(); await GlobalState.database.close();
} }
// create a database with a unique name per identity // create a database with a unique name per identity
database = await createRxDatabase({ GlobalState.database = await createRxDatabase({
name: `meshcore_companion_db_${publicKeyHex}`, name: `meshcore_companion_db_${publicKeyHex}`,
storage: getRxStorageDexie(), storage: getRxStorageDexie(),
allowSlowCount: true, allowSlowCount: true,
}); });
// add database schemas // add database schemas
await database.addCollections({ await GlobalState.database.addCollections({
messages: { messages: {
schema: { schema: {
version: 0, version: 0,
@@ -125,7 +124,7 @@ class Message {
// insert a message into the database // insert a message into the database
static async insert(data) { static async insert(data) {
return await database.messages.insert({ return await GlobalState.database.messages.insert({
id: v4(), id: v4(),
status: data.status, status: data.status,
to: Utils.bytesToHex(data.to), to: Utils.bytesToHex(data.to),
@@ -142,7 +141,7 @@ class Message {
} }
static async getMessageById(id) { static async getMessageById(id) {
return await database.messages.findOne({ return await GlobalState.database.messages.findOne({
selector: { selector: {
id: { id: {
$eq: id, $eq: id,
@@ -152,7 +151,7 @@ class Message {
} }
static async deleteMessageById(id) { static async deleteMessageById(id) {
return await database.messages.findOne({ return await GlobalState.database.messages.findOne({
selector: { selector: {
id: { id: {
$eq: id, $eq: id,
@@ -166,7 +165,7 @@ class Message {
// find one latest message by ack code // find one latest message by ack code
// this will prevent updating older messages that might have the same ack code // this will prevent updating older messages that might have the same ack code
const message = database.messages.findOne({ const message = GlobalState.database.messages.findOne({
selector: { selector: {
expected_ack_crc: { expected_ack_crc: {
$eq: ackCode, $eq: ackCode,
@@ -213,12 +212,12 @@ class Message {
// get all messages // get all messages
static getAllMessages() { static getAllMessages() {
return database.messages.find(); return GlobalState.database.messages.find();
} }
// get direct messages for the provided public key // get direct messages for the provided public key
static getContactMessages(publicKey) { static getContactMessages(publicKey) {
return database.messages.find({ return GlobalState.database.messages.find({
selector: { selector: {
$or: [ $or: [
// messages from us to other contact // messages from us to other contact
@@ -251,7 +250,7 @@ class Message {
// get unread direct messages count for the provided public key // get unread direct messages count for the provided public key
static getContactMessagesUnreadCount(publicKey, messagesLastReadTimestamp) { static getContactMessagesUnreadCount(publicKey, messagesLastReadTimestamp) {
return database.messages.count({ return GlobalState.database.messages.count({
selector: { selector: {
timestamp: { timestamp: {
$gt: messagesLastReadTimestamp, $gt: messagesLastReadTimestamp,
@@ -277,7 +276,7 @@ class ContactMessagesReadState {
// update the read state of messages for the provided public key // update the read state of messages for the provided public key
static async touch(publicKey) { static async touch(publicKey) {
return await database.contact_messages_read_state.upsert({ return await GlobalState.database.contact_messages_read_state.upsert({
id: Utils.bytesToHex(publicKey), id: Utils.bytesToHex(publicKey),
timestamp: Date.now(), timestamp: Date.now(),
}); });
@@ -285,7 +284,7 @@ class ContactMessagesReadState {
// get the read state of messages for the provided public key // get the read state of messages for the provided public key
static get(publicKey) { static get(publicKey) {
return database.contact_messages_read_state.findOne({ return GlobalState.database.contact_messages_read_state.findOne({
selector: { selector: {
id: { id: {
$eq: Utils.bytesToHex(publicKey), $eq: Utils.bytesToHex(publicKey),
@@ -300,7 +299,7 @@ class ChannelMessage {
// insert a channel message into the database // insert a channel message into the database
static async insert(data) { static async insert(data) {
return await database.channel_messages.insert({ return await GlobalState.database.channel_messages.insert({
id: v4(), id: v4(),
channel_idx: data.channel_idx, channel_idx: data.channel_idx,
from: data.from != null ? Utils.bytesToHex(data.from) : null, from: data.from != null ? Utils.bytesToHex(data.from) : null,
@@ -314,7 +313,7 @@ class ChannelMessage {
// get channel messages for the provided channel idx // get channel messages for the provided channel idx
static getChannelMessages(channelIdx) { static getChannelMessages(channelIdx) {
return database.channel_messages.find({ return GlobalState.database.channel_messages.find({
selector: { selector: {
channel_idx: { channel_idx: {
$eq: channelIdx, $eq: channelIdx,

View File

@@ -3,6 +3,7 @@ import {reactive} from "vue";
// global state // global state
const globalState = reactive({ const globalState = reactive({
connection: null, connection: null,
database: null,
selfInfo: null, selfInfo: null,
contacts: [], contacts: [],
channels: [ channels: [

View File

@@ -6,9 +6,9 @@ import "./style.css";
import App from './components/App.vue'; import App from './components/App.vue';
import GlobalState from "./js/GlobalState.js"; import GlobalState from "./js/GlobalState.js";
// helper function that force redirects to the main page if there is no device connection // helper function that force redirects to the main page if there is no database connection
function handleRouteThatRequiresDeviceConnection() { function handleRouteThatRequiresDatabase() {
if(!GlobalState.connection){ if(!GlobalState.database){
return { return {
name: 'main', name: 'main',
}; };
@@ -31,18 +31,20 @@ const routes = [
path: '/contacts/:publicKey/messages', path: '/contacts/:publicKey/messages',
props: true, props: true,
component: () => import("./components/pages/ContactMessagesPage.vue"), component: () => import("./components/pages/ContactMessagesPage.vue"),
beforeEnter: handleRouteThatRequiresDatabase,
}, },
{ {
name: "channel.messages", name: "channel.messages",
path: '/channels/:channelIdx/messages', path: '/channels/:channelIdx/messages',
props: true, props: true,
component: () => import("./components/pages/ChannelMessagesPage.vue"), component: () => import("./components/pages/ChannelMessagesPage.vue"),
beforeEnter: handleRouteThatRequiresDatabase,
}, },
{ {
name: "settings.radio", name: "settings.radio",
path: '/settings/radio', path: '/settings/radio',
component: () => import("./components/pages/RadioSettingsPage.vue"), component: () => import("./components/pages/RadioSettingsPage.vue"),
beforeEnter: handleRouteThatRequiresDeviceConnection, beforeEnter: handleRouteThatRequiresDatabase,
}, },
]; ];