aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/models.py
blob: 8c378aa0b5723f4149ceef2a429d403592081982 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import uuid
from datetime import datetime, timezone
from typing import Optional

from flask_login import UserMixin
from sqlalchemy import DateTime, String, Text, UniqueConstraint, Uuid
from sqlalchemy.orm import Mapped, mapped_column
from werkzeug.security import check_password_hash, generate_password_hash

from . import Base


class User(UserMixin, Base):
    __tablename__ = "users"

    id: Mapped[int] = mapped_column(primary_key=True)
    username: Mapped[str] = mapped_column(
        String(64),
        index=True,
        unique=True,
    )
    password_hash: Mapped[Optional[str]] = mapped_column(String(256))

    def __repr__(self):
        return f"<User {self.username}>"

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(
            self.password_hash,
            password,
        )


class ReceivedWebmention(Base):
    __tablename__ = "received_webmentions"

    uuid: Mapped[uuid.UUID] = mapped_column(
        Uuid(as_uuid=True),
        primary_key=True,
    )

    source: Mapped[str] = mapped_column(
        Text(),
        nullable=False,
    )
    target: Mapped[str] = mapped_column(
        Text(),
        nullable=False,
    )

    status: Mapped[str] = mapped_column(
        Text(),
        nullable=False,
        default="received",
    )
    failure_reason: Mapped[str | None] = mapped_column(
        Text(),
        nullable=True,
        default=None,
    )

    updated_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        default=lambda: datetime.now(timezone.utc),
        onupdate=lambda: datetime.now(timezone.utc),
        nullable=False,
    )

    __table_args__ = (
        UniqueConstraint(
            "source",
            "target",
            name="uq_webmention_source_target",
        ),
    )

    @property
    def verified(self) -> bool:
        return self.status == "verified"

    @property
    def created_at(self) -> datetime:
        return datetime.fromtimestamp(
            self.uuid.time / 1000,
            tz=timezone.utc,
        )