Skip to content

Commit 6df9dc5

Browse files
committed
Create empty profiles record on first sign of actor_id, closes #4
1 parent 5b647ba commit 6df9dc5

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

datasette_profiles/__init__.py

+27
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,33 @@ async def inner():
3737
return inner
3838

3939

40+
_track_event_seen_actor_ids = set()
41+
42+
43+
@hookimpl
44+
def permission_allowed(datasette, actor):
45+
actor_id = actor.get("id") if actor else None
46+
if not actor_id:
47+
return
48+
if actor_id in _track_event_seen_actor_ids:
49+
return
50+
51+
async def inner():
52+
db = datasette.get_internal_database()
53+
# Insert into profiles if it doesn't exist
54+
await db.execute_write(
55+
"""
56+
insert into profiles (id)
57+
values (:id)
58+
on conflict (id) do nothing
59+
""",
60+
{"id": str(actor_id)},
61+
)
62+
_track_event_seen_actor_ids.add(actor_id)
63+
64+
return inner
65+
66+
4067
# Regular expression to extract base64 data from Data URL
4168
DATA_URL_RE = re.compile(r"data:image/(jpeg|png|gif|webp);base64,(.*)")
4269

tests/test_profiles.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,21 @@
33

44

55
@pytest.mark.asyncio
6-
async def test_plugin_is_installed():
6+
async def test_profiles_table_populated_on_visit():
77
datasette = Datasette(memory=True)
8-
response = await datasette.client.get("/-/plugins.json")
9-
assert response.status_code == 200
10-
installed_plugins = {p["name"] for p in response.json()}
11-
assert "datasette-profiles" in installed_plugins
8+
await datasette.invoke_startup()
9+
internal_db = datasette.get_internal_database()
10+
for actor_id in ("user1", "user2"):
11+
assert not (
12+
await internal_db.execute(
13+
"select count(*) from profiles where id = ?", (actor_id,)
14+
)
15+
).single_value()
16+
await datasette.client.get(
17+
"/", cookies={"ds_actor": datasette.client.actor_cookie({"id": actor_id})}
18+
)
19+
assert (
20+
await internal_db.execute(
21+
"select count(*) from profiles where id = ?", (actor_id,)
22+
)
23+
).single_value() == 1

0 commit comments

Comments
 (0)