Skip to content

Commit

Permalink
feat: support timeline v2
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Dec 18, 2023
1 parent 85e7424 commit 69c5b67
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 99 deletions.
34 changes: 22 additions & 12 deletions api/v1/timeline_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions api/v1/timeline_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,20 @@ class Episode(_message.Message):
def __init__(self, id: _Optional[int] = ..., type: _Optional[int] = ..., name: _Optional[str] = ..., name_cn: _Optional[str] = ..., sort: _Optional[float] = ...) -> None: ...

class SubjectCollectRequest(_message.Message):
__slots__ = ["user_id", "subject", "collection", "comment", "rate"]
__slots__ = ["user_id", "subject", "collection", "comment", "rate", "collection_id"]
USER_ID_FIELD_NUMBER: _ClassVar[int]
SUBJECT_FIELD_NUMBER: _ClassVar[int]
COLLECTION_FIELD_NUMBER: _ClassVar[int]
COMMENT_FIELD_NUMBER: _ClassVar[int]
RATE_FIELD_NUMBER: _ClassVar[int]
COLLECTION_ID_FIELD_NUMBER: _ClassVar[int]
user_id: int
subject: Subject
collection: int
comment: str
rate: int
def __init__(self, user_id: _Optional[int] = ..., subject: _Optional[_Union[Subject, _Mapping]] = ..., collection: _Optional[int] = ..., comment: _Optional[str] = ..., rate: _Optional[int] = ...) -> None: ...
collection_id: int
def __init__(self, user_id: _Optional[int] = ..., subject: _Optional[_Union[Subject, _Mapping]] = ..., collection: _Optional[int] = ..., comment: _Optional[str] = ..., rate: _Optional[int] = ..., collection_id: _Optional[int] = ...) -> None: ...

class EpisodeCollectRequest(_message.Message):
__slots__ = ["user_id", "last", "subject"]
Expand Down
5 changes: 3 additions & 2 deletions chii/db/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,8 +893,8 @@ def __init__(
type: int,
related: str,
memo: str,
img: str,
batch: int,
img: str = "",
source: Optional[int] = None,
replies: int = 0,
id: Optional[int] = None,
Expand All @@ -914,7 +914,8 @@ def __init__(
"tml_related", CHAR(255), nullable=False, server_default=text("'0'"), default=0
)
memo: str = Column("tml_memo", MEDIUMTEXT, nullable=False)
img: str = Column("tml_img", MEDIUMTEXT, nullable=False)
#: deprecated
img: str = Column("tml_img", MEDIUMTEXT, nullable=False, default="")
batch = Column("tml_batch", TINYINT(3), nullable=False, index=True)
source = Column(
"tml_source",
Expand Down
1 change: 1 addition & 0 deletions chii/timeline/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class SubjectMemo(BaseModel):
collect_comment: str
collect_rate: int
subject_id: str
collect_id: int = 0


class SubjectImage(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion proto
Submodule proto updated 1 files
+6 −5 api/v1/timeline.proto
94 changes: 12 additions & 82 deletions rpc/timeline_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import html
import time
from typing import Any, Optional
from typing import Optional

import phpserialize as php
import pydantic
Expand All @@ -26,15 +26,12 @@
from chii.timeline import (
SUBJECT_TYPE_MAP,
ProgressMemo,
SubjectImage,
SubjectMemo,
TimelineCat,
)

BatchMeme: pydantic.TypeAdapter[dict[int, Any]] = pydantic.TypeAdapter(dict[int, Any])

BatchSubjectImage: pydantic.TypeAdapter[dict[int, SubjectImage]] = pydantic.TypeAdapter(
dict[int, SubjectImage]
BatchMeme: pydantic.TypeAdapter[dict[int, SubjectMemo]] = pydantic.TypeAdapter(
dict[int, SubjectMemo]
)


Expand All @@ -50,6 +47,9 @@ def Hello(self, request: HelloRequest, context) -> HelloResponse:
def SubjectCollect(
self, request: SubjectCollectRequest, context: RpcContext
) -> SubjectCollectResponse:
"""
https://github.com/bangumi/dev-docs/blob/master/Timeline.md#cat_sbj_collect-条目收藏
"""
tlType = SUBJECT_TYPE_MAP[request.subject.type][request.collection]
if config.debug:
print(request)
Expand All @@ -62,7 +62,7 @@ def SubjectCollect(
)
)

if tl and tl.dateline >= int(time.time() - 15 * 60):
if tl and tl.dateline >= int(time.time() - 10 * 60):
logger.info("find previous timeline, merging")
if tl.cat == TimelineCat.Subject and tl.type == tlType:
self.merge_previous_timeline(session, tl, request)
Expand Down Expand Up @@ -101,27 +101,19 @@ def merge_previous_timeline(
session.add(tl)
return

memo = {int(m.subject_id): m.model_dump()}
memo = {int(m.subject_id): m}

memo[req.subject.id] = SubjectMemo(
subject_id=str(req.subject.id),
collect_comment=escaped,
collect_rate=req.rate,
).model_dump()

if tl.batch:
img = BatchSubjectImage.validate_python(phpseralize.loads(tl.img.encode()))
else:
i = SubjectImage.model_validate(phpseralize.loads(tl.img.encode()))
img = {int(i.subject_id): i}

img[req.subject.id] = SubjectImage(
subject_id=str(req.subject.id), images=req.subject.image
collect_id=req.collection_id,
)

tl.batch = 1
tl.memo = php.serialize(memo)
tl.img = php.serialize({key: value.model_dump() for key, value in img.items()})
tl.memo = php.serialize(
{key: value.model_dump() for key, value in memo.items()}
)

session.add(tl)

Expand All @@ -135,15 +127,12 @@ def create_subject_collection_timeline(
collect_rate=req.rate,
)

img = SubjectImage(subject_id=str(req.subject.id), images=req.subject.image)

session.add(
ChiiTimeline(
cat=TimelineCat.Subject,
type=type,
uid=req.user_id,
memo=php.serialize(memo.model_dump()),
img=php.serialize(img.model_dump()),
batch=0,
related=str(req.subject.id),
)
Expand All @@ -154,54 +143,7 @@ def EpisodeCollect(
self, req: EpisodeCollectRequest, context
) -> EpisodeCollectResponse:
"""
cat 4 type 2 "看过 ep2 ${subject name}"
tml_id tml_uid tml_cat tml_type tml_related
32073511 287622 4 2 363612
tml_memo
a:5:{
s:5:"ep_id";s:7:"1075441";
s:7:"ep_name";s:0:"";
s:7:"ep_sort";s:1:"2";
s:10:"subject_id";s:6:"363612";
s:12:"subject_name";s:6:"沙盒";
}
tml_img
a:2:{s:10:"subject_id";s:6:"363612";s:6:"images";s:22:"82/15/363612_On6wg.jpg";}
tml_batch tml_source tml_replies tml_dateline
0 0 0 1672520183
cat 4 type 2 "完成了 ${subject name} {ep} of {ep_total} 话"
memo: a:7:{
s:9:"eps_total";s:2:"12";
s:10:"eps_update";i:12;
s:10:"vols_total";s:2:"??";
s:11:"vols_update";N;
s:10:"subject_id";s:6:"353657";
s:12:"subject_name";s:21:"勇者、辞めます";
s:15:"subject_type_id";s:1:"2";
}
"""

tlType = 2
Expand All @@ -214,11 +156,6 @@ def EpisodeCollect(
ep_sort=req.last.sort,
)

img = SubjectImage(
subject_id=str(req.subject.id),
images=req.subject.image,
)

if config.debug:
print(req)

Expand Down Expand Up @@ -246,7 +183,6 @@ def EpisodeCollect(
ChiiTimeline(
uid=req.user_id,
memo=php.serialize(memo.model_dump()),
img=php.serialize(img.model_dump()),
cat=TimelineCat.Progress,
type=tlType,
source=5,
Expand Down Expand Up @@ -274,11 +210,6 @@ def SubjectProgress(

tlType = 0

img = SubjectImage(
subject_id=str(req.subject.id),
images=req.subject.image,
)

if config.debug:
print(req)

Expand Down Expand Up @@ -307,7 +238,6 @@ def SubjectProgress(
ChiiTimeline(
uid=req.user_id,
memo=php.serialize(memo.model_dump()),
img=php.serialize(img.model_dump()),
cat=TimelineCat.Progress,
type=tlType,
batch=0,
Expand Down

0 comments on commit 69c5b67

Please sign in to comment.