-
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.
Signed-off-by: Sumner Evans <[email protected]>
- Loading branch information
1 parent
9c9f98a
commit da20228
Showing
9 changed files
with
147 additions
and
48 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,57 @@ | ||
from __future__ import annotations | ||
|
||
from typing import cast | ||
|
||
from asyncpg import Record | ||
from attr import dataclass | ||
|
||
from linkedin_messaging import URN | ||
from mautrix.types import RoomID, UserID | ||
|
||
from .model_base import Model | ||
|
||
|
||
@dataclass | ||
class Cookie(Model): | ||
mxid: UserID | ||
name: string | ||
value: string | ||
|
||
_table_name = "cookie" | ||
_field_list = [ | ||
"mxid", | ||
"name", | ||
"value", | ||
] | ||
|
||
@classmethod | ||
def _from_row(cls, row: Record | None) -> Cookie | None: | ||
if row is None: | ||
return None | ||
return cls(**row) | ||
|
||
@classmethod | ||
async def get_for_mxid(cls, mxid: id.UserID) -> list[Cookie]: | ||
query = Cookie.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 self.db.execute("DELETE FROM cookie WHERE mxid=$1", self.mxid) | ||
|
||
@classmethod | ||
async def bulk_upsert(cls, mxid: id.UserID, cookies: dict[str, str]): | ||
for name, value in cookies.items(): | ||
cookie = cls(mxid, name, value) | ||
await cookie.upsert() | ||
|
||
async def upsert(self): | ||
query = """ | ||
INSERT INTO cookie (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,39 @@ | ||
import logging | ||
import pickle | ||
|
||
from mautrix.util.async_db import Connection | ||
|
||
from . import upgrade_table | ||
|
||
|
||
@upgrade_table.register(description="Add a cookie table for storing all of the cookies") | ||
async def upgrade_v9(conn: Connection): | ||
await conn.execute( | ||
""" | ||
CREATE TABLE cookie ( | ||
mxid TEXT, | ||
name TEXT, | ||
value TEXT, | ||
PRIMARY KEY (mxid, name) | ||
) | ||
""" | ||
) | ||
|
||
for row in await conn.fetch('SELECT mxid, jsessionid, li_at FROM "user"'): | ||
mxid = row["mxid"] | ||
jsessionid = row["jsessionid"] | ||
li_at = row["li_at"] | ||
|
||
if jsessionid: | ||
await conn.execute( | ||
"INSERT INTO cookie (mxid, name, value) VALUES ($1, 'JSESSIONID', $2)", | ||
mxid, | ||
jsessionid, | ||
) | ||
if li_at: | ||
await conn.execute( | ||
"INSERT INTO cookie (mxid, name, value) VALUES ($1, 'li_at', $2)", | ||
mxid, | ||
li_at, | ||
) |
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
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