Skip to content
Open
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
17 changes: 0 additions & 17 deletions .env.example

This file was deleted.

9 changes: 9 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

if [ -f .venv/bin/activate ]; then
source .venv/bin/activate
fi

if [ -f .env ]; then
dotenv .env
fi
6 changes: 6 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[flake8]
max-line-length = 88
exclude = .venv, .git, __pycache__, build, dist
Comment thread
gamesguru marked this conversation as resolved.
extend-ignore = E203

[flake8:local-plugins]
# Flake8 doesn't support per-directory line length natively in the config file
# without plugins, but we can use the --max-line-length flag in the Makefile
# or just keep this here for general use.
30 changes: 12 additions & 18 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@ on:
jobs:
integration:
runs-on: ubuntu-latest
services:
conduwuit:
image: ghcr.io/continuwuity/continuwuity:latest
ports:
- 8008:8008
env:
CONDUWUIT_SERVER_NAME: localhost
CONDUWUIT_ALLOW_REGISTRATION: "true"
CONDUWUIT_DATABASE_PATH: /tmp/conduwuit
CONDUWUIT_DATABASE_BACKEND: sqlite
CONDUWUIT_ADDRESS: "0.0.0.0"
CONDUWUIT_PORT: 8008
CONDUWUIT_REGISTRATION_TOKEN: "ci_test_token_123"

steps:
- uses: actions/checkout@v4

Expand All @@ -37,13 +23,22 @@ jobs:
run: |
make init
make deps
Comment thread
gamesguru marked this conversation as resolved.
. .venv/bin/activate
pip install .
echo "$PWD/.venv/bin" >> $GITHUB_PATH

- name: Start Conduwuit Configured Homeserver
run: |
docker run -d --name conduwuit \
-p 8008:8008 \
-v $PWD/docker/conduwuit.toml:/etc/conduwuit.toml:ro \
-e CONDUWUIT_CONFIG=/etc/conduwuit.toml \
ghcr.io/continuwuity/continuwuity:latest
- name: Wait for Conduwuit to be ready
run: |
echo "Waiting for Conduwuit to be ready..."
for i in {1..30}; do
if curl -s http://localhost:8008/_matrix/client/versions > /dev/null; then
if curl -fsS http://localhost:8008/_matrix/client/versions > /dev/null 2>&1; then
echo "Conduwuit Ready!"
exit 0
fi
Expand All @@ -53,7 +48,6 @@ jobs:
exit 1

- name: Run CI Integration Tests
env:
CONDUWUIT_CONTAINER_ID: ${{ job.services.conduwuit.id }}
run: |
Comment thread
gamesguru marked this conversation as resolved.
python docker/test_integration.py
export PYTHONPATH="$PWD/src:${PYTHONPATH}"
PYTHONPATH=src python docker/test_integration.py
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,5 @@ __marimo__/

# Streamlit
.streamlit/secrets.toml
config.json
_version.py
11 changes: 11 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"overrides": [
{
"files": ["*.json", ".prettierrc"],
"options": {
"tabWidth": 4,
"useTabs": false
}
}
]
}
79 changes: 40 additions & 39 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,91 +11,92 @@ PYTHON=$(VENV)/bin/python
PIP=$(VENV)/bin/pip

.PHONY: init
init:
init: ##H Initialize .venv virtual dev env
python3 -m venv $(VENV)
-direnv allow

.PHONY: deps
deps: ##H Install standard and dev dependencies
deps: ##H Install standard and dev dependencies
$(VENV)/bin/pip install -r requirements.txt -r requirements-dev.txt

# Default install location
INSTALL_DIR ?= /opt/matrix-premid

.PHONY: install
install: ##H Install dependencies, env, binary, and systemd service to /opt (requires sudo)
@echo "Installing globally to $(INSTALL_DIR)..."
sudo mkdir -p $(INSTALL_DIR)
sudo cp matrix_premid.py requirements.txt $(INSTALL_DIR)/
if [ -f .env ]; then \
sudo cp .env $(INSTALL_DIR)/.env; \
sudo chown $$(id -un):$$(id -gn) $(INSTALL_DIR)/.env; \
sudo chmod 600 $(INSTALL_DIR)/.env; \
fi
sudo python3 -m venv $(INSTALL_DIR)/.venv
sudo $(INSTALL_DIR)/.venv/bin/pip install -r $(INSTALL_DIR)/requirements.txt
sudo ln -sf $(INSTALL_DIR)/matrix_premid.py /usr/local/bin/matrix_premid
sudo chmod +x $(INSTALL_DIR)/matrix_premid.py
sudo cp etc/matrix-premid.service /etc/systemd/system/matrix-premid.service
sudo systemctl daemon-reload
sudo systemctl enable matrix-premid.service
@echo "Installed to $(INSTALL_DIR) and service created."

install: ##H Install locally and setup systemd user service
sudo mkdir -p /opt/matrix-premid
sudo python3 -m venv /opt/matrix-premid/venv
sudo /opt/matrix-premid/venv/bin/pip install .
sudo ln -sf /opt/matrix-premid/venv/bin/matrix-premid /usr/local/bin/matrix-premid
@echo "Setting up systemd service..."
if [ -n "$$SUDO_USER" ]; then sudo -u "$$SUDO_USER" /usr/local/bin/matrix-premid install-service; else /usr/local/bin/matrix-premid install-service; fi

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Unit tests and local running
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.PHONY: test
test: deps ##H Run unit tests with coverage
PYTHONPATH=. $(VENV)/bin/python -m pytest --cov=matrix_premid --cov-report=term-missing tests/
test: ##H Run unit tests with coverage
PYTHONPATH=src $(VENV)/bin/python -m pytest --cov=matrix_premid --cov-report=term-missing tests/

.PHONY: run
run: deps ##H Run the application locally
$(PYTHON) matrix_premid.py
run: ##H Run the application locally
PYTHONPATH=src $(PYTHON) -m matrix_premid --debug

.PHONY: start
start: ##H Start the background systemd service
systemctl --user start matrix-premid.service

.PHONY: restart
restart: ##H Restart the background systemd service
sudo systemctl restart matrix-premid.service
systemctl --user restart matrix-premid.service

.PHONY: log
log: ##H Watch journalctl logs of installed/running service
sudo journalctl -fu matrix-premid
journalctl --user -fu matrix-premid

.PHONY: stop
stop: ##H Stop the background systemd service
sudo systemctl stop matrix-premid.service
systemctl --user stop matrix-premid.service


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Linting, formatting
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


LINT_LOCS_PY = $$(git ls-files '*.py')
LINT_LOCS_PY = $$(git ls-files 'src/**/*.py' 'tests/*.py')

.PHONY: format
format: ##H Format the code using Black
$(VENV)/bin/black $(LINT_LOCS_PY)
$(VENV)/bin/isort $(LINT_LOCS_PY)
$(VENV)/bin/black src/ tests/
$(VENV)/bin/isort src/ tests/
-prettier -w .
-pre-commit run --all-files


.PHONY: lint
lint: ##H Lint the code using Flake8
flake8 $(LINT_LOCS_PY)
pylint $(LINT_LOCS_PY)
ruff check $(LINT_LOCS_PY)
flake8 src/
flake8 --max-line-length=100 tests/
pylint src/ tests/
ruff check src/ tests/

.PHONY: build
build: ##H Build the package (requires hatch)
$(VENV)/bin/pip install hatch
$(VENV)/bin/hatch build

.PHONY: publish
publish: build ##H Upload the package to PyPI using twine
$(VENV)/bin/pip install twine
$(VENV)/bin/twine upload dist/*

.PHONY: clean
clean: ##H Clean the virtual environment and caches
rm -rf $(VENV)
#rm -rf $(VENV)
find . -type f -name '*.pyc' -delete
find . -type d -name '__pycache__' -exec rm -rf {} +
rm -rf .mypy_cache

.PHONY: _help
_help: ##H Show this help, list available targets
@grep -hE '^[a-zA-Z0-9_\/-]+:.*?##H .*$$' $(MAKEFILE_LIST) \
| awk 'BEGIN {FS = ":.*?##H "}; {printf "$(STYLE_CYAN)%-15s$(STYLE_RESET) %s\n", $$1, $$2}'
@grep -hE '^[a-zA-Z0-9_\/-]+:[[:space:]]*##H .*$$' $(MAKEFILE_LIST) \
| awk 'BEGIN {FS = ":[[:space:]]*##H "}; {printf "$(STYLE_CYAN)%-15s$(STYLE_RESET) %s\n", $$1, $$2}'
75 changes: 57 additions & 18 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,59 +23,98 @@ If you want to run this constantly in the background as a Linux service, indepen
git clone https://github.com/user/matrix-premid
cd matrix-premid

2. Configure your credentials locally (or edit later):
2. Install the script, systemd user service, and dependencies globally to ``/opt``:

.. code-block:: bash

cp .env.example .env
nano .env
make install

This creates the directory ``/opt/matrix-premid``, sets up an isolated Python virtual environment exclusively for the service, symlinks the script to ``/usr/local/bin/matrix-premid``, and automatically initializes a systemd user service and config template for your user account.

*(Note: If you populate the ``.env`` file locally before installing, the installer will automatically copy and use it for the background service.)*
3. Edit your configuration file at ``~/.config/matrix-premid/config.json`` to add your homeserver and username.

3. Install the script, systemd service, and dependencies globally to ``/opt``:
4. Store your Matrix access token in the system keyring:

.. code-block:: bash

make install
python -m keyring set matrix-premid @username:domain.com

User Installation
-----------------

Alternatively, you can install the package to your user site-packages:

This creates the directory ``/opt/matrix-premid``, copies the script and ``.env`` there, sets up an isolated Python virtual environment exclusively for the service, and symlinks the script to ``/usr/local/bin/matrix_premid``. The systemd service is placed in ``/etc/systemd/system/``.
.. code-block:: bash

pip install --user .

This will install the ``matrix-premid`` command to your ``~/.local/bin`` (make sure ``~/.local/bin`` is on your ``PATH``).

4. (Optional) Edit credentials after installation:
Basic Usage
-----------

1. **Install dependencies**: ``pip install .`` (or use installation methods above).
2. **Setup configuration**: Create your configuration file at ``~/.config/matrix-premid/config.json``. You can use ``matrix-premid install-service`` to generate a default template, or copy ``config.template.json`` from this repository to that location.
3. **Store credentials**: For security, store your Matrix access token in your system keyring rather than putting it in plain text inside the config file:

.. code-block:: bash

sudo nano /opt/matrix-premid/.env
sudo systemctl restart matrix-premid.service
python -m keyring set matrix-premid @username:domain.com

*(Alternatively, for non-interactive setups like CI, you can set the ``access_token`` directly in the ``accounts`` section of ``config.json``).*

5. Start and enable the background service:
4. **Run the script**: ``matrix-premid``

Comment on lines +56 to 67

Copilot AI Mar 29, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The README now instructs users to place a .env file for configuration, but the new CLI no longer loads dotenv/env-based configuration; it requires ~/.config/matrix-premid/config.json and uses keyring for missing access tokens. Update the “Basic Usage” steps to describe creating/editing config.json (or using the provided template) and storing tokens in keyring (or placing access_token in config for non-interactive setups like CI).

Copilot uses AI. Check for mistakes.
Command-line Options
--------------------

* ``--unset`` or ``--clear``: Manually set Matrix presence to ``offline``, clear the status message/account data, and exit.
* ``--debug``: Enable verbose debug logging.
* ``--help``: Show all available options.

Shell Completion
----------------

This script supports bash/zsh completion via ``argcomplete``. To enable it:

1. Install ``argcomplete`` (included in requirements).
2. Register the script:

.. code-block:: bash

sudo systemctl daemon-reload
sudo systemctl enable --now matrix-premid.service
eval "$(register-python-argcomplete matrix-premid)"

(Add this to your ``.bashrc`` or ``.zshrc`` for persistence).

Development / Local Running
---------------------------

If you want to run the script locally from the folder (for testing or development) without installing it system-wide:

1. Clone the repository and configure your environment:
1. Clone the repository and configure your configuration:

.. code-block:: bash

git clone https://github.com/user/matrix-premid
cd matrix-premid
cp .env.example .env
mkdir -p ~/.config/matrix-premid
cp config.template.json ~/.config/matrix-premid/config.json

Edit the ``~/.config/matrix-premid/config.json`` file to set your Matrix username and homeserver.

2. Store your Matrix access token in the system keyring:

.. code-block:: bash

Edit the ``.env`` file and fill in your Matrix credentials. Make sure to export them to your shell (e.g., using ``direnv allow`` or sourcing the file) because the script reads directly from ``os.environ``.
python -m keyring set matrix-premid @your_username:homeserver.com

2. Install development dependencies:
3. Install development dependencies:

.. code-block:: bash

make deps

3. Run the script directly:
4. Run the script directly:

.. code-block:: bash

Expand Down
Loading
Loading