Files
meshview/meshview/models.py
T
Mike BennerandClaude Opus 5.5 ae7ba15263 Include the node's public key in the contact QR code (#185)
* Include the node's public key in the contact QR code #184

The contact QR code and import URL never set user.public_key. Current
Meshtastic apps refuse to import a keyless contact. On 2.7.x firmware,
an older app's add_contact also overwrites the key the radio already
holds, so direct messages to that node fail.

Read the key from the node's latest stored NODEINFO_APP packet. Share
the contact only when a 32-byte key is known. Otherwise the node page
hides the QR buttons and says why. The QR API response now also
returns public_key.

Also drop the stray blank line that fails ruff format --check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Do not share a contact when a node has reported more than one key #184

A forged NODEINFO uplinked over MQTT could otherwise become the newest
packet, and the shared contact would then replace the key a scanning
radio already trusts. The QR endpoint now reads every stored NODEINFO
from the node and refuses to share when their 32-byte keys disagree.
It reports that in public_key_conflict, and the node page says why.

Add an index on packet (from_node_id, portnum, import_time_us DESC) so
the lookup reads only the node's NODEINFO packets. Order with NULLS
LAST so PostgreSQL does not treat old rows without import_time_us as
newest. Carry is_licensed and is_unmessagable into the contact.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Skip NODEINFO rows with no payload when building the contact #184

packet.payload is nullable, and MeshPacket.FromString(None) raises
TypeError, so one such row made the QR endpoint answer 500.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 17:08:54 -07:00

125 lines
5.0 KiB
Python

from datetime import date
from sqlalchemy import BigInteger, Date, ForeignKey, Index, desc
from sqlalchemy.ext.asyncio import AsyncAttrs
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
class Base(AsyncAttrs, DeclarativeBase):
pass
# Node
class Node(Base):
__tablename__ = "node"
id: Mapped[str] = mapped_column(primary_key=True)
node_id: Mapped[int] = mapped_column(BigInteger, nullable=True, unique=True)
long_name: Mapped[str] = mapped_column(nullable=True)
short_name: Mapped[str] = mapped_column(nullable=True)
hw_model: Mapped[str] = mapped_column(nullable=True)
firmware: Mapped[str] = mapped_column(nullable=True)
role: Mapped[str] = mapped_column(nullable=True)
last_lat: Mapped[int] = mapped_column(BigInteger, nullable=True)
last_long: Mapped[int] = mapped_column(BigInteger, nullable=True)
channel: Mapped[str] = mapped_column(nullable=True)
is_mqtt_gateway: Mapped[bool] = mapped_column(nullable=True)
first_seen_us: Mapped[int] = mapped_column(BigInteger, nullable=True)
last_seen_us: Mapped[int] = mapped_column(BigInteger, nullable=True)
__table_args__ = (
Index("idx_node_node_id", "node_id"),
Index("idx_node_first_seen_us", "first_seen_us"),
Index("idx_node_last_seen_us", "last_seen_us"),
)
def to_dict(self):
return {column.name: getattr(self, column.name) for column in self.__table__.columns}
class Packet(Base):
__tablename__ = "packet"
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
portnum: Mapped[int] = mapped_column(nullable=True)
from_node_id: Mapped[int] = mapped_column(BigInteger, nullable=True)
from_node: Mapped["Node"] = relationship(
primaryjoin="Packet.from_node_id == foreign(Node.node_id)", lazy="joined"
)
to_node_id: Mapped[int] = mapped_column(BigInteger, nullable=True)
to_node: Mapped["Node"] = relationship(
primaryjoin="Packet.to_node_id == foreign(Node.node_id)",
lazy="joined",
overlaps="from_node",
)
payload: Mapped[bytes] = mapped_column(nullable=True)
import_time_us: Mapped[int] = mapped_column(BigInteger, nullable=True)
channel: Mapped[str] = mapped_column(nullable=True)
__table_args__ = (
Index("idx_packet_from_node_id", "from_node_id"),
Index("idx_packet_to_node_id", "to_node_id"),
Index("idx_packet_import_time_us", desc("import_time_us")),
Index("idx_packet_from_node_time_us", "from_node_id", desc("import_time_us")),
Index(
"idx_packet_from_node_portnum_time_us",
"from_node_id",
"portnum",
desc("import_time_us"),
),
)
class PacketSeen(Base):
__tablename__ = "packet_seen"
packet_id = mapped_column(ForeignKey("packet.id"), primary_key=True)
node_id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
node: Mapped["Node"] = relationship(
lazy="joined",
primaryjoin="PacketSeen.node_id == foreign(Node.node_id)",
overlaps="from_node,to_node",
)
rx_time: Mapped[int] = mapped_column(BigInteger, primary_key=True)
hop_limit: Mapped[int] = mapped_column(nullable=True)
hop_start: Mapped[int] = mapped_column(nullable=True)
channel: Mapped[str] = mapped_column(nullable=True)
rx_snr: Mapped[float] = mapped_column(nullable=True)
rx_rssi: Mapped[int] = mapped_column(nullable=True)
topic: Mapped[str] = mapped_column(nullable=True)
import_time_us: Mapped[int] = mapped_column(BigInteger, nullable=True)
__table_args__ = (
Index("idx_packet_seen_node_id", "node_id"),
# Index for /top endpoint performance - JOIN on packet_id
Index("idx_packet_seen_packet_id", "packet_id"),
Index("idx_packet_seen_import_time_us", "import_time_us"),
)
class Traceroute(Base):
__tablename__ = "traceroute"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
packet_id = mapped_column(ForeignKey("packet.id"))
packet: Mapped["Packet"] = relationship(
primaryjoin="Traceroute.packet_id == foreign(Packet.id)", lazy="joined"
)
gateway_node_id: Mapped[int] = mapped_column(BigInteger, nullable=True)
done: Mapped[bool] = mapped_column(nullable=True)
route: Mapped[bytes] = mapped_column(nullable=True)
route_return: Mapped[bytes] = mapped_column(nullable=True)
import_time_us: Mapped[int] = mapped_column(BigInteger, nullable=True)
__table_args__ = (
Index("idx_traceroute_packet_id", "packet_id"),
Index("idx_traceroute_import_time_us", "import_time_us"),
)
class DailySnapshot(Base):
__tablename__ = "daily_snapshot"
snapshot_date: Mapped[date] = mapped_column(Date, primary_key=True)
node_count: Mapped[int] = mapped_column(BigInteger, nullable=False)
packet_count: Mapped[int] = mapped_column(BigInteger, nullable=False)
gateway_count: Mapped[int] = mapped_column(BigInteger, nullable=False)
captured_at_us: Mapped[int] = mapped_column(BigInteger, nullable=False)