2022-07-21 11:58:50 +00:00
|
|
|
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
|
2022-07-21 11:58:50 +00:00
|
|
|
import enum
|
|
|
|
from sqlalchemy import Enum
|
2022-08-11 09:09:57 +00:00
|
|
|
from services.stat.viewed import ViewedStorage
|
2022-07-21 11:58:50 +00:00
|
|
|
|
|
|
|
class ReactionKind(enum.Enum):
|
|
|
|
AGREE = 1 # +1
|
2022-07-25 09:34:57 +00:00
|
|
|
DISAGREE = 2 # -1
|
2022-07-21 11:58:50 +00:00
|
|
|
PROOF = 3 # +1
|
2022-07-25 09:34:57 +00:00
|
|
|
DISPROOF = 4 # -1
|
2022-08-13 09:48:07 +00:00
|
|
|
ASK = 5 # +0 bookmark
|
2022-07-21 11:58:50 +00:00
|
|
|
PROPOSE = 6 # +0
|
2022-08-13 09:48:07 +00:00
|
|
|
QUOTE = 7 # +0 bookmark
|
2022-07-21 11:58:50 +00:00
|
|
|
COMMENT = 8 # +0
|
|
|
|
ACCEPT = 9 # +1
|
|
|
|
REJECT = 0 # -1
|
2022-07-25 09:34:57 +00:00
|
|
|
LIKE = 11 # +1
|
2022-07-21 11:58:50 +00:00
|
|
|
DISLIKE = 12 # -1
|
2022-08-13 09:48:07 +00:00
|
|
|
# TYPE = <reaction index> # rating diff
|
2022-07-21 11:58:50 +00:00
|
|
|
|
2022-08-13 09:48:07 +00:00
|
|
|
def kind_to_rate(kind) -> int:
|
|
|
|
if kind in [
|
|
|
|
ReactionKind.AGREE,
|
|
|
|
ReactionKind.LIKE,
|
|
|
|
ReactionKind.PROOF,
|
|
|
|
ReactionKind.ACCEPT
|
|
|
|
]: return 1
|
|
|
|
elif kind in [
|
|
|
|
ReactionKind.DISAGREE,
|
|
|
|
ReactionKind.DISLIKE,
|
|
|
|
ReactionKind.DISPROOF,
|
|
|
|
ReactionKind.REJECT
|
|
|
|
]: return -1
|
|
|
|
else: return 0
|
|
|
|
|
|
|
|
def get_bookmarked(reactions):
|
|
|
|
c = 0
|
|
|
|
for r in reactions:
|
|
|
|
c += 1 if r.kind in [ ReactionKind.QUOTE, ReactionKind.ASK] else 0
|
|
|
|
return c
|
|
|
|
|
|
|
|
def get_rating(reactions):
|
|
|
|
rating = 0
|
|
|
|
for r in reactions:
|
|
|
|
rating += kind_to_rate(r.kind)
|
|
|
|
return rating
|
2022-07-21 11:58:50 +00:00
|
|
|
|
|
|
|
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):
|
2022-08-13 09:48:07 +00:00
|
|
|
reacted = []
|
2022-07-21 11:58:50 +00:00
|
|
|
try:
|
|
|
|
with local_session() as session:
|
2022-08-13 09:48:07 +00:00
|
|
|
reacted = session.query(Reaction).filter(Reaction.replyTo == self.id).all()
|
2022-07-21 11:58:50 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
return {
|
2022-08-13 09:48:07 +00:00
|
|
|
"viewed": await ViewedStorage.get_reaction(self.id),
|
|
|
|
"reacted": reacted.count(),
|
|
|
|
"rating": get_rating(reacted),
|
|
|
|
"bookmarked": get_bookmarked(reacted)
|
2022-07-21 11:58:50 +00:00
|
|
|
}
|