-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include more headers with logins (#79)
* Include more headers with logins * Fix lint
- Loading branch information
1 parent
169f1ab
commit 6b65cf5
Showing
8 changed files
with
239 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from __future__ import annotations | ||
|
||
from asyncpg import Record | ||
from attr import dataclass | ||
|
||
from mautrix.types import UserID | ||
|
||
from .model_base import Model | ||
|
||
|
||
@dataclass | ||
class HttpHeader(Model): | ||
mxid: UserID | ||
name: str | ||
value: str | ||
|
||
_table_name = "http_header" | ||
_field_list = [ | ||
"mxid", | ||
"name", | ||
"value", | ||
] | ||
|
||
@classmethod | ||
def _from_row(cls, row: Record | None) -> HttpHeader | None: | ||
if row is None: | ||
return None | ||
return cls(**row) | ||
|
||
@classmethod | ||
async def get_for_mxid(cls, mxid: id.UserID) -> list[HttpHeader]: | ||
query = HttpHeader.select_constructor("mxid=$1") | ||
rows = await cls.db.fetch(query, mxid) | ||
return [cls._from_row(row) for row in rows if row] | ||
|
||
@classmethod | ||
async def delete_all_for_mxid(cls, mxid: id.UserID): | ||
await cls.db.execute("DELETE FROM http_header WHERE mxid=$1", mxid) | ||
|
||
@classmethod | ||
async def bulk_upsert(cls, mxid: id.UserID, http_headers: dict[str, str]): | ||
for name, value in http_headers.items(): | ||
http_header = cls(mxid, name, value) | ||
await http_header.upsert() | ||
|
||
async def upsert(self): | ||
query = """ | ||
INSERT INTO http_header (mxid, name, value) | ||
VALUES ($1, $2, $3) | ||
ON CONFLICT (mxid, name) | ||
DO UPDATE | ||
SET value=excluded.value | ||
""" | ||
await self.db.execute(query, self.mxid, self.name, self.value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from mautrix.util.async_db import Connection | ||
|
||
from . import upgrade_table | ||
|
||
|
||
@upgrade_table.register(description="Add a header table for storing all of the headers") | ||
async def upgrade_v10(conn: Connection): | ||
await conn.execute( | ||
""" | ||
CREATE TABLE http_header ( | ||
mxid TEXT, | ||
name TEXT, | ||
value TEXT, | ||
PRIMARY KEY (mxid, name) | ||
) | ||
""" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.