Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reactions prerelease adjustments #114

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ cli:
plugins:
sources:
- id: trunk
ref: v1.6.5
ref: v1.6.6
uri: https://github.com/trunk-io/plugins
# Many linters and tools depend on runtimes - configure them here. (https://docs.trunk.io/runtimes)
runtimes:
enabled:
- node@18.12.1
- node@18.20.5
- [email protected]
# This is the section where you manage your linters. (https://docs.trunk.io/check/configuration)
lint:
Expand All @@ -21,14 +21,14 @@ lint:
- [email protected]
- [email protected]
- [email protected]
- [email protected].317
- [email protected].334
- git-diff-check
- [email protected]
- [email protected]
- [email protected]
- [email protected].1
- [email protected].0
- trufflehog@3.84.1
- [email protected].2
- [email protected].3
- trufflehog@3.86.1
- [email protected]
actions:
disabled:
Expand Down
73 changes: 71 additions & 2 deletions db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

logger = get_logger(name="db_utils")


# Initialize SQLite database
def initialize_database():
with sqlite3.connect("meshtastic.sqlite") as conn:
Expand Down Expand Up @@ -37,6 +38,7 @@ def initialize_database():

conn.commit()


def store_plugin_data(plugin_name, meshtastic_id, data):
with sqlite3.connect("meshtastic.sqlite") as conn:
cursor = conn.cursor()
Expand All @@ -46,6 +48,7 @@ def store_plugin_data(plugin_name, meshtastic_id, data):
)
conn.commit()


def delete_plugin_data(plugin_name, meshtastic_id):
with sqlite3.connect("meshtastic.sqlite") as conn:
cursor = conn.cursor()
Expand All @@ -55,6 +58,7 @@ def delete_plugin_data(plugin_name, meshtastic_id):
)
conn.commit()


# Get the data for a given plugin and Meshtastic ID
def get_plugin_data_for_node(plugin_name, meshtastic_id):
with sqlite3.connect("meshtastic.sqlite") as conn:
Expand All @@ -69,6 +73,7 @@ def get_plugin_data_for_node(plugin_name, meshtastic_id):
result = cursor.fetchone()
return json.loads(result[0] if result else "[]")


# Get the data for a given plugin
def get_plugin_data(plugin_name):
with sqlite3.connect("meshtastic.sqlite") as conn:
Expand All @@ -79,6 +84,7 @@ def get_plugin_data(plugin_name):
)
return cursor.fetchall()


# Get the longname for a given Meshtastic ID
def get_longname(meshtastic_id):
with sqlite3.connect("meshtastic.sqlite") as conn:
Expand All @@ -89,6 +95,7 @@ def get_longname(meshtastic_id):
result = cursor.fetchone()
return result[0] if result else None


def save_longname(meshtastic_id, longname):
with sqlite3.connect("meshtastic.sqlite") as conn:
cursor = conn.cursor()
Expand All @@ -98,6 +105,7 @@ def save_longname(meshtastic_id, longname):
)
conn.commit()


def update_longnames(nodes):
if nodes:
for node in nodes.values():
Expand All @@ -107,6 +115,7 @@ def update_longnames(nodes):
longname = user.get("longName", "N/A")
save_longname(meshtastic_id, longname)


def get_shortname(meshtastic_id):
with sqlite3.connect("meshtastic.sqlite") as conn:
cursor = conn.cursor()
Expand All @@ -116,6 +125,7 @@ def get_shortname(meshtastic_id):
result = cursor.fetchone()
return result[0] if result else None


def save_shortname(meshtastic_id, shortname):
with sqlite3.connect("meshtastic.sqlite") as conn:
cursor = conn.cursor()
Expand All @@ -125,6 +135,7 @@ def save_shortname(meshtastic_id, shortname):
)
conn.commit()


def update_shortnames(nodes):
if nodes:
for node in nodes.values():
Expand All @@ -134,7 +145,14 @@ def update_shortnames(nodes):
shortname = user.get("shortName", "N/A")
save_shortname(meshtastic_id, shortname)

def store_message_map(meshtastic_id, matrix_event_id, matrix_room_id, meshtastic_text, meshtastic_meshnet=None):

def store_message_map(
meshtastic_id,
matrix_event_id,
matrix_room_id,
meshtastic_text,
meshtastic_meshnet=None,
):
"""
Stores a message map in the database.

Expand All @@ -152,10 +170,17 @@ def store_message_map(meshtastic_id, matrix_event_id, matrix_room_id, meshtastic
)
cursor.execute(
"INSERT OR REPLACE INTO message_map (meshtastic_id, matrix_event_id, matrix_room_id, meshtastic_text, meshtastic_meshnet) VALUES (?, ?, ?, ?, ?)",
(meshtastic_id, matrix_event_id, matrix_room_id, meshtastic_text, meshtastic_meshnet),
(
meshtastic_id,
matrix_event_id,
matrix_room_id,
meshtastic_text,
meshtastic_meshnet,
),
)
conn.commit()


def get_message_map_by_meshtastic_id(meshtastic_id):
with sqlite3.connect("meshtastic.sqlite") as conn:
cursor = conn.cursor()
Expand All @@ -172,6 +197,7 @@ def get_message_map_by_meshtastic_id(meshtastic_id):
return result[0], result[1], result[2], result[3]
return None


def get_message_map_by_matrix_event_id(matrix_event_id):
with sqlite3.connect("meshtastic.sqlite") as conn:
cursor = conn.cursor()
Expand All @@ -187,3 +213,46 @@ def get_message_map_by_matrix_event_id(matrix_event_id):
# result = (meshtastic_id, matrix_room_id, meshtastic_text, meshtastic_meshnet)
return result[0], result[1], result[2], result[3]
return None


def wipe_message_map():
"""
Wipes all entries from the message_map table.
Useful when db.msg_map.wipe_on_restart is True, ensuring no stale data remains.
"""
with sqlite3.connect("meshtastic.sqlite") as conn:
cursor = conn.cursor()
cursor.execute("DELETE FROM message_map")
conn.commit()
logger.info("message_map table wiped successfully.")


def prune_message_map(msgs_to_keep):
"""
Prune the message_map table to keep only the most recent msgs_to_keep entries
in order to prevent database bloat.
We use the matrix_event_id's insertion order as a heuristic.
Note: matrix_event_id is a string, so we rely on the rowid or similar approach.

Approach:
- Count total rows.
- If total > msgs_to_keep, delete oldest entries based on rowid.
"""
with sqlite3.connect("meshtastic.sqlite") as conn:
cursor = conn.cursor()
# Count total entries
cursor.execute("SELECT COUNT(*) FROM message_map")
total = cursor.fetchone()[0]

if total > msgs_to_keep:
# Delete oldest entries by rowid since matrix_event_id is primary key but not necessarily numeric.
# rowid is auto-incremented and reflects insertion order.
to_delete = total - msgs_to_keep
cursor.execute(
"DELETE FROM message_map WHERE rowid IN (SELECT rowid FROM message_map ORDER BY rowid ASC LIMIT ?)",
(to_delete,),
)
conn.commit()
logger.info(
f"Pruned {to_delete} old message_map entries, keeping last {msgs_to_keep}."
)
2 changes: 1 addition & 1 deletion log_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def get_logger(name):
"backup_count", 1
) # Default to 1 backup
file_handler = RotatingFileHandler(
log_file, maxBytes=max_bytes, backupCount=backup_count, encoding='utf-8'
log_file, maxBytes=max_bytes, backupCount=backup_count, encoding="utf-8"
)

file_handler.setFormatter(
Expand Down
25 changes: 23 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@
import sys
from typing import List

from nio import RoomMessageNotice, RoomMessageText, ReactionEvent, RoomMessageEmote
from nio import ReactionEvent, RoomMessageEmote, RoomMessageNotice, RoomMessageText

# Import meshtastic_utils as a module to set event_loop
import meshtastic_utils
from config import relay_config
from db_utils import initialize_database, update_longnames, update_shortnames
from db_utils import (
initialize_database,
update_longnames,
update_shortnames,
wipe_message_map,
)
from log_utils import get_logger
from matrix_utils import connect_matrix, join_matrix_room
from matrix_utils import logger as matrix_logger
Expand All @@ -36,13 +41,24 @@
async def main():
"""
Main asynchronous function to set up and run the relay.
Includes logic for wiping the message_map if configured in db.msg_map.wipe_on_restart.
Also updates longnames and shortnames periodically as before.
"""
# Set the event loop in meshtastic_utils
meshtastic_utils.event_loop = asyncio.get_event_loop()

# Initialize the SQLite database
initialize_database()

# Check db config for wipe_on_restart
db_config = relay_config.get("db", {})
msg_map_config = db_config.get("msg_map", {})
wipe_on_restart = msg_map_config.get("wipe_on_restart", False)

if wipe_on_restart:
logger.debug("wipe_on_restart enabled. Wiping message_map now (startup).")
wipe_message_map()

# Load plugins early
load_plugins()

Expand Down Expand Up @@ -131,6 +147,11 @@ async def shutdown():
except Exception as e:
meshtastic_logger.warning(f"Error closing Meshtastic client: {e}")

# Attempt to wipe message_map on shutdown if enabled
if wipe_on_restart:
logger.debug("wipe_on_restart enabled. Wiping message_map now (shutdown).")
wipe_message_map()

# Cancel the reconnect task if it exists
if meshtastic_utils.reconnect_task:
meshtastic_utils.reconnect_task.cancel()
Expand Down
Loading