core/orm/reaction.py

50 lines
1.8 KiB
Python
Raw Normal View History

from datetime import datetime
from sqlalchemy import Column, String, ForeignKey, DateTime
2022-08-11 05:53:14 +00:00
from base.orm import Base, local_session
import enum
from sqlalchemy import Enum
2022-08-11 09:09:57 +00:00
from services.stat.viewed import ViewedStorage
class ReactionKind(enum.Enum):
AGREE = 1 # +1
2022-07-25 09:34:57 +00:00
DISAGREE = 2 # -1
PROOF = 3 # +1
2022-07-25 09:34:57 +00:00
DISPROOF = 4 # -1
ASK = 5 # +0
PROPOSE = 6 # +0
2022-07-25 09:34:57 +00:00
QUOTE = 7 # +0
COMMENT = 8 # +0
ACCEPT = 9 # +1
REJECT = 0 # -1
2022-07-25 09:34:57 +00:00
LIKE = 11 # +1
DISLIKE = 12 # -1
# TYPE = <reaction index> # rating change guess
class Reaction(Base):
__tablename__ = 'reaction'
body: str = Column(String, nullable=True, comment="Reaction Body")
createdAt = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
createdBy: str = Column(ForeignKey("user.slug"), nullable=False, comment="Sender")
updatedAt = Column(DateTime, nullable=True, comment="Updated at")
updatedBy = Column(ForeignKey("user.slug"), nullable=True, comment="Last Editor")
deletedAt = Column(DateTime, nullable=True, comment="Deleted at")
deletedBy = Column(ForeignKey("user.slug"), nullable=True, comment="Deleted by")
shout = Column(ForeignKey("shout.slug"), nullable=False)
replyTo: int = Column(ForeignKey("reaction.id"), nullable=True, comment="Reply to reaction ID")
range: str = Column(String, nullable=True, comment="Range in format <start index>:<end>")
kind: int = Column(Enum(ReactionKind), nullable=False, comment="Reaction kind")
oid: str = Column(String, nullable=True, comment="Old ID")
@property
2022-07-21 16:13:21 +00:00
async def stat(self):
reacted = 0
try:
with local_session() as session:
reacted = session.query(Reaction).filter(Reaction.replyTo == self.id).count()
except Exception as e:
print(e)
return {
"viewed": await ViewedStorage.get_reaction(self.slug),
"reacted": reacted
}