Skip to content

Commit fe36ab8

Browse files
authored
Reapply "feat: Credit Username on Approval (#162)" (#176)
1 parent 277119a commit fe36ab8

File tree

5 files changed

+53
-29
lines changed

5 files changed

+53
-29
lines changed

.github/CHANGELOG.md

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
### Added
11-
12-
- Add blacklist messages feature as anti-spam
13-
14-
### Changed
15-
16-
- Bump python-telegram-bot to version 22.2
17-
- Bump cryptography to version 45.0.5
18-
- Bump pytz to version 2025.2
19-
- Bump APScheduler to version 3.11.0
20-
21-
### Fixed
22-
23-
- Fix **spam_comment_msg** handler to avoid raising an exception when the message does not contain text
24-
- Ensure message in report has a text
25-
26-
### Changed
10+
- Show credit username during approval process
2711

28-
- Bump python-telegram-bot to version 21.6
29-
- Bump cryptography to version 43.0.1
30-
- Bump PyYAML to version 6.0.2
31-
- Bump pytz to version 2024.2
12+
...
3213

3314
## [3.1.0] - 2024-02-18
3415

src/spotted/config/db/post_db_init.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CREATE TABLE IF NOT EXISTS pending_post
55
u_message_id BIGINT NOT NULL,
66
g_message_id BIGINT NOT NULL,
77
admin_group_id BIGINT NOT NULL,
8+
credit_username VARCHAR(255) DEFAULT NULL,
89
message_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
910
PRIMARY KEY (admin_group_id, g_message_id)
1011
);

src/spotted/data/pending_post.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class PendingPost:
1717
u_message_id: id of the original message of the post
1818
g_message_id: id of the post in the group
1919
admin_group_id: id of the admin group
20+
credit_username: username of the user that sent the post if it's a credit post
2021
date: when the post was sent
2122
"""
2223

@@ -25,15 +26,19 @@ class PendingPost:
2526
g_message_id: int
2627
admin_group_id: int
2728
date: datetime
29+
credit_username: str | None = None
2830

2931
@classmethod
30-
def create(cls, user_message: Message, g_message_id: int, admin_group_id: int) -> "PendingPost":
32+
def create(
33+
cls, user_message: Message, g_message_id: int, admin_group_id: int, credit_username: str | None = None
34+
) -> "PendingPost":
3135
"""Creates a new post and inserts it in the table of pending posts
3236
3337
Args:
3438
user_message: message sent by the user that contains the post
3539
g_message_id: id of the post in the group
3640
admin_group_id: id of the admin group
41+
credit_username: username of the user that sent the post if it's a credit post
3742
3843
Returns:
3944
instance of the class
@@ -47,6 +52,7 @@ def create(cls, user_message: Message, g_message_id: int, admin_group_id: int) -
4752
u_message_id=u_message_id,
4853
g_message_id=g_message_id,
4954
admin_group_id=admin_group_id,
55+
credit_username=credit_username,
5056
date=date,
5157
).save_post()
5258

@@ -76,6 +82,7 @@ def from_group(cls, g_message_id: int, admin_group_id: int) -> "PendingPost | No
7682
u_message_id=pending_post["u_message_id"],
7783
admin_group_id=pending_post["admin_group_id"],
7884
g_message_id=pending_post["g_message_id"],
85+
credit_username=pending_post["credit_username"],
7986
date=pending_post["message_date"],
8087
)
8188

@@ -101,6 +108,7 @@ def from_user(cls, user_id: int) -> "PendingPost | None":
101108
u_message_id=pending_post["u_message_id"],
102109
admin_group_id=pending_post["admin_group_id"],
103110
g_message_id=pending_post["g_message_id"],
111+
credit_username=pending_post["credit_username"],
104112
date=pending_post["message_date"],
105113
)
106114

@@ -138,10 +146,15 @@ def get_all(admin_group_id: int, before: datetime | None = None) -> list["Pendin
138146

139147
def save_post(self) -> "PendingPost":
140148
"""Saves the pending_post in the database"""
149+
columns = ("user_id", "u_message_id", "g_message_id", "admin_group_id", "message_date")
150+
values = (self.user_id, self.u_message_id, self.g_message_id, self.admin_group_id, self.date)
151+
if self.credit_username is not None:
152+
columns += ("credit_username",)
153+
values += (self.credit_username,)
141154
DbManager.insert_into(
142155
table_name="pending_post",
143-
columns=("user_id", "u_message_id", "g_message_id", "admin_group_id", "message_date"),
144-
values=(self.user_id, self.u_message_id, self.g_message_id, self.admin_group_id, self.date),
156+
columns=columns,
157+
values=values,
145158
)
146159
return self
147160

@@ -160,6 +173,14 @@ def get_votes(self, vote: bool) -> int:
160173
where_args=(self.g_message_id, self.admin_group_id, vote),
161174
)
162175

176+
def get_credit_username(self) -> str | None:
177+
"""Gets the username of the user that credited the post
178+
179+
Returns:
180+
username of the user that credited the post, or None if the post is not credited
181+
"""
182+
return self.credit_username
183+
163184
def get_list_admin_votes(self, vote: "bool | None" = None) -> "list[int] | list[tuple[int, bool]]":
164185
"""Gets the list of admins that approved or rejected the post
165186
@@ -257,5 +278,6 @@ def __repr__(self) -> str:
257278
f"u_message_id: {self.u_message_id}\n"
258279
f"admin_group_id: {self.admin_group_id}\n"
259280
f"g_message_id: {self.g_message_id}\n"
281+
f"credit_username: {self.credit_username}\n"
260282
f"date : {self.date} ]"
261283
)

src/spotted/utils/info_util.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,12 @@ async def send_post_to_admins(self) -> bool:
334334
admin_group_id = Config.post_get("admin_group_id")
335335
poll = message.poll # if the message is a poll, get its reference
336336

337+
user = User(self.user_id)
338+
credit_username: str | None = None
339+
if user.is_credited:
340+
chat = await self.bot.get_chat(self.chat_id)
341+
if chat.username:
342+
credit_username = chat.username
337343
try:
338344
if poll: # makes sure the poll is anonym
339345
g_message = await self.__bot.send_poll(
@@ -343,14 +349,14 @@ async def send_post_to_admins(self) -> bool:
343349
type=poll.type,
344350
allows_multiple_answers=poll.allows_multiple_answers,
345351
correct_option_id=poll.correct_option_id,
346-
reply_markup=get_approve_kb(),
352+
reply_markup=get_approve_kb(credited_username=credit_username),
347353
)
348354
elif message.text and message.entities: # maintains the previews, if present
349355
show_preview = self.user_data.get("show_preview", True)
350356
g_message = await self.__bot.send_message(
351357
chat_id=admin_group_id,
352358
text=message.text,
353-
reply_markup=get_approve_kb(),
359+
reply_markup=get_approve_kb(credited_username=credit_username),
354360
entities=message.entities,
355361
link_preview_options=LinkPreviewOptions(not show_preview),
356362
)
@@ -359,13 +365,18 @@ async def send_post_to_admins(self) -> bool:
359365
chat_id=admin_group_id,
360366
from_chat_id=message.chat_id,
361367
message_id=message.message_id,
362-
reply_markup=get_approve_kb(),
368+
reply_markup=get_approve_kb(credited_username=credit_username),
363369
)
364370
except BadRequest as ex:
365371
logger.error("Sending the post on send_post_to: %s", ex)
366372
return False
367373

368-
PendingPost.create(user_message=message, admin_group_id=admin_group_id, g_message_id=g_message.message_id)
374+
PendingPost.create(
375+
user_message=message,
376+
admin_group_id=admin_group_id,
377+
g_message_id=g_message.message_id,
378+
credit_username=credit_username,
379+
)
369380

370381
return True
371382

src/spotted/utils/keyboard_util.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ def get_settings_kb() -> InlineKeyboardMarkup:
5757
)
5858

5959

60-
def get_approve_kb(pending_post: PendingPost = None, approve: int = -1, reject: int = -1) -> InlineKeyboardMarkup:
60+
def get_approve_kb(
61+
pending_post: PendingPost = None, approve: int = -1, reject: int = -1, credited_username: str | None = None
62+
) -> InlineKeyboardMarkup:
6163
"""Generates the InlineKeyboard for the pending post.
6264
If the pending post is None, the keyboard will be generated with 0 votes.
6365
Otherwise, the keyboard will be generated with the correct number of votes.
@@ -68,6 +70,7 @@ def get_approve_kb(pending_post: PendingPost = None, approve: int = -1, reject:
6870
pending_post: existing pending post to which the keyboard is attached
6971
approve: number of approve votes known in advance
7072
reject: number of reject votes known in advance
73+
credited_username: username of the user who credited the post if it was credited
7174
7275
Returns:
7376
new inline keyboard
@@ -78,8 +81,14 @@ def get_approve_kb(pending_post: PendingPost = None, approve: int = -1, reject:
7881
else:
7982
n_approve = pending_post.get_votes(vote=True) if approve < 0 else approve
8083
n_reject = pending_post.get_votes(vote=False) if reject < 0 else reject
84+
credited_username = pending_post.get_credit_username()
8185
return InlineKeyboardMarkup(
8286
[
87+
(
88+
[]
89+
if credited_username is None
90+
else [InlineKeyboardButton(f"👤 {credited_username}", callback_data="none")]
91+
),
8392
[
8493
InlineKeyboardButton(f"🟢 {n_approve}", callback_data="approve_yes"),
8594
InlineKeyboardButton(f"🔴 {n_reject}", callback_data="approve_no"),

0 commit comments

Comments
 (0)