Tag functionality added
This commit is contained in:
@@ -2,6 +2,7 @@ import sqlite3
|
||||
import os
|
||||
from hashlib import sha256
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
DB_PATH = Path(os.getenv('LINKLOG_DATABASE_PATH', BASE_DIR / 'data' / 'linklog.db'))
|
||||
@@ -13,6 +14,8 @@ AVATARS_DIR.mkdir(parents=True, exist_ok=True)
|
||||
def hash_password(password: str) -> str:
|
||||
return sha256(password.encode('utf-8')).hexdigest()
|
||||
|
||||
DEFAULT_TAGS = ('#Internet', '#Cybersecurity', '#Fediverse', '#Food', '#Photography', '#Music', '#AI')
|
||||
|
||||
MIGRATIONS = [
|
||||
(1, '''
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
@@ -71,6 +74,26 @@ CREATE TABLE IF NOT EXISTS user_plugin_config (
|
||||
UNIQUE(user_id, plugin_name),
|
||||
FOREIGN KEY(user_id) REFERENCES users(id)
|
||||
);
|
||||
'''),
|
||||
(2, '''
|
||||
CREATE TABLE IF NOT EXISTS tags (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS link_tags (
|
||||
link_id TEXT NOT NULL,
|
||||
tag_id TEXT NOT NULL,
|
||||
PRIMARY KEY (link_id, tag_id),
|
||||
FOREIGN KEY(link_id) REFERENCES links(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(tag_id) REFERENCES tags(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_link_tags_tag_id ON link_tags(tag_id);
|
||||
'''),
|
||||
(3, '''
|
||||
UPDATE tags SET name = '#' || name WHERE name NOT LIKE '#%';
|
||||
'''),
|
||||
]
|
||||
|
||||
@@ -96,6 +119,14 @@ def apply_migrations(conn: sqlite3.Connection) -> None:
|
||||
conn.commit()
|
||||
|
||||
|
||||
def seed_default_tags(conn: sqlite3.Connection) -> None:
|
||||
for tag in DEFAULT_TAGS:
|
||||
conn.execute(
|
||||
'INSERT OR IGNORE INTO tags (id, name) VALUES (?, ?)',
|
||||
(str(uuid4()), tag),
|
||||
)
|
||||
|
||||
|
||||
def init_db() -> None:
|
||||
with get_connection() as conn:
|
||||
apply_migrations(conn)
|
||||
@@ -130,4 +161,5 @@ def init_db() -> None:
|
||||
''',
|
||||
('plugin-2', 'mastodon', '1.0.0', '{"enabled": true, "instance": "mastodon.social"}')
|
||||
)
|
||||
seed_default_tags(conn)
|
||||
conn.commit()
|
||||
|
||||
Reference in New Issue
Block a user