Skip to content

Commit 1d1624c

Browse files
committed
feat(elems): change elems base class (#40)
1 parent d4276e4 commit 1d1624c

File tree

4 files changed

+93
-29
lines changed

4 files changed

+93
-29
lines changed

lagrange/client/message/decoder.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ async def parse_msg_new(
6464
size=ptt.size,
6565
id=ptt.file_id,
6666
md5=ptt.md5,
67-
text=f"[audio:{ptt.name}]",
6867
time=ptt.time,
6968
file_key=ptt.group_file_key if ptt.group_file_key else ptt.friend_file_key,
7069
qmsg=None,
@@ -113,7 +112,7 @@ async def parse_msg_new(
113112
mf = raw.market_face
114113
msg_chain.append(
115114
elems.MarketFace(
116-
text=mf.name,
115+
name=mf.name,
117116
face_id=mf.face_id,
118117
tab_id=mf.tab_id,
119118
width=mf.width,
@@ -129,7 +128,7 @@ async def parse_msg_new(
129128
size=img.size,
130129
id=img.fileid,
131130
md5=img.md5,
132-
text=img.args.display_name,
131+
display_name=img.args.display_name,
133132
width=img.width,
134133
height=img.height,
135134
url="https://gchat.qpic.cn" + img.original_url,
@@ -145,7 +144,7 @@ async def parse_msg_new(
145144
size=img.file_len,
146145
id=int(img.download_path.split("-")[1]),
147146
md5=img.file_md5,
148-
text=img.args.display_name,
147+
display_name=img.args.display_name,
149148
width=img.width,
150149
height=img.height,
151150
url="https://gchat.qpic.cn" + img.origin_path,
@@ -158,7 +157,7 @@ async def parse_msg_new(
158157
if common.service_type == 2:
159158
msg_chain.append(
160159
elems.Poke(
161-
text=f"[poke:{common.pb_elem[1]}]",
160+
# text=f"[poke:{common.pb_elem[1]}]",
162161
id=common.pb_elem[1],
163162
f7=common.pb_elem[7],
164163
f8=common.pb_elem[8],
@@ -181,7 +180,7 @@ async def parse_msg_new(
181180
size=index.info.size,
182181
id=0,
183182
md5=bytes.fromhex(index.info.hash),
184-
text=extra.biz_info.pic.summary if extra.biz_info.pic.summary else "[图片]",
183+
display_name=extra.biz_info.pic.summary if extra.biz_info.pic.summary else "[图片]",
185184
width=index.info.width,
186185
height=index.info.height,
187186
url=url,
@@ -213,10 +212,10 @@ async def parse_msg_new(
213212
content = zlib.decompress(jr[1:])
214213
else:
215214
content = jr[1:]
216-
msg_chain.append(elems.Service(id=sid, raw=content, text=f"[service:{sid}]"))
215+
msg_chain.append(elems.Service(id=sid, raw=content))
217216
ignore_next = True
218217
elif raw.open_data:
219-
msg_chain.append(elems.Raw(text=f"[raw:{len(raw.open_data.data)}]", data=raw.open_data.data))
218+
msg_chain.append(elems.Raw(data=raw.open_data.data))
220219
elif raw.src_msg: # msg source info
221220
src = raw.src_msg
222221
msg_text = ""
@@ -230,7 +229,6 @@ async def parse_msg_new(
230229
# src[10]: optional[grp_id]
231230
msg_chain.append(
232231
elems.Quote(
233-
text=f"[quote:{msg_text}]",
234232
seq=src.seq,
235233
uin=src.uin,
236234
timestamp=src.timestamp,
@@ -246,7 +244,7 @@ async def parse_msg_new(
246244
content = zlib.decompress(service[1:])
247245
else:
248246
content = service[1:]
249-
msg_chain.append(elems.Json(text=f"[json:{len(content)}]", raw=content))
247+
msg_chain.append(elems.Json(raw=content))
250248
ignore_next = True
251249
# elif 53 in raw: # q emoji
252250
# qe = raw[53]
@@ -275,7 +273,6 @@ async def parse_msg_new(
275273
elems.Video(
276274
id=0,
277275
time=video.length,
278-
text="[视频]",
279276
name=video.name,
280277
size=video.size,
281278
file_key=video.id,

lagrange/client/message/elems.py

+75-16
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ def type(self) -> str:
1717
return self.__class__.__name__.lower()
1818

1919

20+
@dataclass
21+
class CompatibleText(BaseElem):
22+
"""仅用于兼容性变更,不应作为判断条件"""
23+
@property
24+
def text(self) -> str:
25+
return self.display
26+
27+
@text.setter
28+
def text(self, text: str):
29+
"""ignore"""
30+
2031
@dataclass
2132
class MediaInfo:
2233
name: str
@@ -37,7 +48,7 @@ def display(self) -> str:
3748

3849

3950
@dataclass
40-
class Quote(Text):
51+
class Quote(CompatibleText):
4152
seq: int
4253
uin: int
4354
timestamp: int
@@ -47,34 +58,51 @@ class Quote(Text):
4758
@classmethod
4859
def build(cls, ev: GroupMessage) -> "Quote":
4960
return cls(
50-
text=f"[quote:{ev.msg}]",
5161
seq=ev.seq,
5262
uid=ev.uid,
5363
uin=ev.uin,
5464
timestamp=ev.time,
5565
msg=ev.msg,
5666
)
5767

68+
@property
69+
def display(self) -> str:
70+
return f"[quote:{self.msg}]"
71+
5872

5973
@dataclass
60-
class Json(Text):
74+
class Json(CompatibleText):
6175
raw: bytes
6276

6377
def to_dict(self) -> dict:
6478
return json.loads(self.raw)
6579

80+
@property
81+
def display(self) -> str:
82+
return f"[json:{len(self.raw)}]"
83+
6684

6785
@dataclass
6886
class Service(Json):
6987
id: int
7088

89+
@property
90+
def display(self) -> str:
91+
return f"[service:{self.id}]"
92+
7193

7294
@dataclass
73-
class AtAll(Text): ...
95+
class AtAll(BaseElem):
96+
text: str
97+
98+
@property
99+
def display(self) -> str:
100+
return self.text
74101

75102

76103
@dataclass
77-
class At(Text):
104+
class At(BaseElem):
105+
text: str
78106
uin: int
79107
uid: str
80108

@@ -84,37 +112,54 @@ def build(cls, ev: GroupMessage) -> "At":
84112

85113

86114
@dataclass
87-
class Image(Text, MediaInfo):
115+
class Image(CompatibleText, MediaInfo):
88116
width: int
89117
height: int
90118
is_emoji: bool
119+
display_name: str
120+
121+
@property
122+
def display(self) -> str:
123+
return self.display_name
91124

92125

93126
@dataclass
94-
class Video(Text, MediaInfo):
127+
class Video(CompatibleText, MediaInfo):
95128
width: int
96129
height: int
97130
time: int
98131
file_key: str = field(repr=True)
99132

133+
@property
134+
def display(self) -> str:
135+
return f"[video:{self.width}x{self.height},{self.time}s]"
136+
100137

101138
@dataclass
102-
class Audio(Text, MediaInfo):
139+
class Audio(CompatibleText, MediaInfo):
103140
time: int
104141
file_key: str = field(repr=True)
105142

143+
@property
144+
def display(self) -> str:
145+
return f"[audio:{self.time}]"
146+
106147

107148
@dataclass
108-
class Raw(Text):
149+
class Raw(CompatibleText):
109150
data: bytes
110151

152+
@property
153+
def display(self) -> str:
154+
return f"[raw:{len(self.data)}]"
155+
111156

112157
@dataclass
113-
class Emoji(BaseElem):
158+
class Emoji(CompatibleText):
114159
id: int
115160

116161
@property
117-
def text(self) -> str:
162+
def display(self) -> str:
118163
return f"[emoji:{self.id}]"
119164

120165

@@ -127,14 +172,19 @@ class Reaction(Emoji):
127172

128173

129174
@dataclass
130-
class Poke(Text):
175+
class Poke(CompatibleText):
131176
id: int
132177
f7: int = 0
133178
f8: int = 0
134179

180+
@property
181+
def display(self) -> str:
182+
return f"[poke:{self.id}]"
183+
135184

136185
@dataclass
137-
class MarketFace(Text):
186+
class MarketFace(CompatibleText):
187+
name: str
138188
face_id: bytes
139189
tab_id: int
140190
width: int
@@ -145,9 +195,13 @@ def url(self) -> str:
145195
pic_id = self.face_id.hex()
146196
return f"https://i.gtimg.cn/club/item/parcel/item/{pic_id[:2]}/{pic_id}/{self.width}x{self.height}.png"
147197

198+
@property
199+
def display(self) -> str:
200+
return f"[marketface:{self.name}]"
201+
148202

149203
@dataclass
150-
class File(Text):
204+
class File(CompatibleText):
151205
file_size: int
152206
file_name: str
153207
file_md5: bytes
@@ -156,12 +210,15 @@ class File(Text):
156210
file_uuid: Optional[str] # only in private
157211
file_hash: Optional[str]
158212

213+
@property
214+
def display(self) -> str:
215+
return f"[file:{self.file_name}]"
216+
159217
@classmethod
160218
def _paste_build(cls, file_size: int, file_name: str,
161219
file_md5: bytes, file_id: Optional[str] = None,
162220
file_uuid: Optional[str] = None, file_hash: Optional[str] = None) -> "File":
163221
return cls(
164-
text=f"[file:{file_name}]",
165222
file_size=file_size,
166223
file_name=file_name,
167224
file_md5=file_md5,
@@ -181,12 +238,14 @@ def pri_paste_build(cls, file_size: int, file_name: str, file_md5: bytes, file_u
181238

182239

183240
@dataclass
184-
class GreyTips(Text):
241+
class GreyTips(BaseElem):
185242
"""
186243
奇怪的整活元素
187244
建议搭配Text使用
188245
冷却3分钟左右?
189246
"""
247+
text: str
248+
190249
@property
191250
def display(self) -> str:
192251
return f"<GreyTips: {self.text}>"

lagrange/client/message/encoder.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def build_message(msg_chain: list[Element], compatible=True) -> RichText:
116116
size=msg.size,
117117
args=ImageReserveArgs(
118118
is_emoji=msg.is_emoji,
119-
display_name=msg.text
119+
display_name=msg.display_name
120120
or ("[动画表情]" if msg.is_emoji else "[图片]"),
121121
),
122122
)
@@ -166,7 +166,7 @@ def build_message(msg_chain: list[Element], compatible=True) -> RichText:
166166
msg_pb.append(
167167
Elems(
168168
market_face=PBMarketFace(
169-
name=msg.text,
169+
name=msg.name,
170170
item_type=6,
171171
face_info=1,
172172
face_id=msg.face_id,

lagrange/client/message/types.py

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
Audio,
1515
Poke,
1616
MarketFace,
17+
GreyTips,
18+
Video,
19+
Service,
20+
File
1721
)
1822

1923
# T = TypeVar(
@@ -42,4 +46,8 @@
4246
"Audio",
4347
"Poke",
4448
"MarketFace",
49+
"GreyTips",
50+
"Video",
51+
"Service",
52+
"File"
4553
]

0 commit comments

Comments
 (0)