Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Credit Username on Approval #162

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Show credit username during approval process

...

## [3.1.0] - 2024-02-18
Expand Down
1 change: 1 addition & 0 deletions src/spotted/config/db/post_db_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CREATE TABLE IF NOT EXISTS pending_post
u_message_id BIGINT NOT NULL,
g_message_id BIGINT NOT NULL,
admin_group_id BIGINT NOT NULL,
credit_username VARCHAR(255) DEFAULT NULL,
LightDestory marked this conversation as resolved.
Show resolved Hide resolved
message_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (admin_group_id, g_message_id)
);
Expand Down
28 changes: 25 additions & 3 deletions src/spotted/data/pending_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class PendingPost:
u_message_id: id of the original message of the post
g_message_id: id of the post in the group
admin_group_id: id of the admin group
credit_username: username of the user that sent the post if it's a credit post
date: when the post was sent
"""

Expand All @@ -25,15 +26,19 @@ class PendingPost:
g_message_id: int
admin_group_id: int
date: datetime
credit_username: str | None = None

@classmethod
def create(cls, user_message: Message, g_message_id: int, admin_group_id: int) -> "PendingPost":
def create(
cls, user_message: Message, g_message_id: int, admin_group_id: int, credit_username: str | None = None
) -> "PendingPost":
"""Creates a new post and inserts it in the table of pending posts

Args:
user_message: message sent by the user that contains the post
g_message_id: id of the post in the group
admin_group_id: id of the admin group
credit_username: username of the user that sent the post if it's a credit post

Returns:
instance of the class
Expand All @@ -47,6 +52,7 @@ def create(cls, user_message: Message, g_message_id: int, admin_group_id: int) -
u_message_id=u_message_id,
g_message_id=g_message_id,
admin_group_id=admin_group_id,
credit_username=credit_username,
date=date,
).save_post()

Expand Down Expand Up @@ -76,6 +82,7 @@ def from_group(cls, g_message_id: int, admin_group_id: int) -> "PendingPost | No
u_message_id=pending_post["u_message_id"],
admin_group_id=pending_post["admin_group_id"],
g_message_id=pending_post["g_message_id"],
credit_username=pending_post["credit_username"],
date=pending_post["message_date"],
)

Expand All @@ -101,6 +108,7 @@ def from_user(cls, user_id: int) -> "PendingPost | None":
u_message_id=pending_post["u_message_id"],
admin_group_id=pending_post["admin_group_id"],
g_message_id=pending_post["g_message_id"],
credit_username=pending_post["credit_username"],
date=pending_post["message_date"],
)

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

def save_post(self) -> "PendingPost":
"""Saves the pending_post in the database"""
columns = ("user_id", "u_message_id", "g_message_id", "admin_group_id", "message_date")
values = (self.user_id, self.u_message_id, self.g_message_id, self.admin_group_id, self.date)
if self.credit_username is not None:
columns += ("credit_username",)
values += (self.credit_username,)
DbManager.insert_into(
table_name="pending_post",
columns=("user_id", "u_message_id", "g_message_id", "admin_group_id", "message_date"),
values=(self.user_id, self.u_message_id, self.g_message_id, self.admin_group_id, self.date),
columns=columns,
values=values,
)
return self

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

def get_credit_username(self) -> str | None:
"""Gets the username of the user that credited the post

Returns:
username of the user that credited the post, or None if the post is not credited
"""
return self.credit_username

def get_list_admin_votes(self, vote: "bool | None" = None) -> "list[int] | list[tuple[int, bool]]":
"""Gets the list of admins that approved or rejected the post

Expand Down Expand Up @@ -257,5 +278,6 @@ def __repr__(self) -> str:
f"u_message_id: {self.u_message_id}\n"
f"admin_group_id: {self.admin_group_id}\n"
f"g_message_id: {self.g_message_id}\n"
f"credit_username: {self.credit_username}\n"
f"date : {self.date} ]"
)
19 changes: 15 additions & 4 deletions src/spotted/utils/info_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,12 @@ async def send_post_to_admins(self) -> bool:
admin_group_id = Config.post_get("admin_group_id")
poll = message.poll # if the message is a poll, get its reference

user = User(self.user_id)
credit_username: str | None = None
if user.is_credited:
chat = await self.bot.get_chat(self.chat_id)
if chat.username:
credit_username = chat.username
try:
if poll: # makes sure the poll is anonym
g_message = await self.__bot.send_poll(
Expand All @@ -343,14 +349,14 @@ async def send_post_to_admins(self) -> bool:
type=poll.type,
allows_multiple_answers=poll.allows_multiple_answers,
correct_option_id=poll.correct_option_id,
reply_markup=get_approve_kb(),
reply_markup=get_approve_kb(credited_username=credit_username),
)
elif message.text and message.entities: # maintains the previews, if present
show_preview = self.user_data.get("show_preview", True)
g_message = await self.__bot.send_message(
chat_id=admin_group_id,
text=message.text,
reply_markup=get_approve_kb(),
reply_markup=get_approve_kb(credited_username=credit_username),
entities=message.entities,
link_preview_options=LinkPreviewOptions(not show_preview),
)
Expand All @@ -359,13 +365,18 @@ async def send_post_to_admins(self) -> bool:
chat_id=admin_group_id,
from_chat_id=message.chat_id,
message_id=message.message_id,
reply_markup=get_approve_kb(),
reply_markup=get_approve_kb(credited_username=credit_username),
)
except BadRequest as ex:
logger.error("Sending the post on send_post_to: %s", ex)
return False

PendingPost.create(user_message=message, admin_group_id=admin_group_id, g_message_id=g_message.message_id)
PendingPost.create(
user_message=message,
admin_group_id=admin_group_id,
g_message_id=g_message.message_id,
credit_username=credit_username,
)

return True

Expand Down
11 changes: 10 additions & 1 deletion src/spotted/utils/keyboard_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ def get_settings_kb() -> InlineKeyboardMarkup:
)


def get_approve_kb(pending_post: PendingPost = None, approve: int = -1, reject: int = -1) -> InlineKeyboardMarkup:
def get_approve_kb(
pending_post: PendingPost = None, approve: int = -1, reject: int = -1, credited_username: str | None = None
) -> InlineKeyboardMarkup:
"""Generates the InlineKeyboard for the pending post.
If the pending post is None, the keyboard will be generated with 0 votes.
Otherwise, the keyboard will be generated with the correct number of votes.
Expand All @@ -68,6 +70,7 @@ def get_approve_kb(pending_post: PendingPost = None, approve: int = -1, reject:
pending_post: existing pending post to which the keyboard is attached
approve: number of approve votes known in advance
reject: number of reject votes known in advance
credited_username: username of the user who credited the post if it was credited

Returns:
new inline keyboard
Expand All @@ -78,8 +81,14 @@ def get_approve_kb(pending_post: PendingPost = None, approve: int = -1, reject:
else:
n_approve = pending_post.get_votes(vote=True) if approve < 0 else approve
n_reject = pending_post.get_votes(vote=False) if reject < 0 else reject
credited_username = pending_post.get_credit_username()
return InlineKeyboardMarkup(
[
(
[]
if credited_username is None
else [InlineKeyboardButton(f"👤 {credited_username}", callback_data="none")]
),
[
InlineKeyboardButton(f"🟢 {n_approve}", callback_data="approve_yes"),
InlineKeyboardButton(f"🔴 {n_reject}", callback_data="approve_no"),
Expand Down