diff --git a/app/bot.py b/app/bot.py
index fe6032e..ce3b7c9 100644
--- a/app/bot.py
+++ b/app/bot.py
@@ -106,6 +106,16 @@ def parse_cancellation(raw: str) -> int:
return transaction_id
+def parse_friend_removal(raw: str) -> int:
+ payload = decode_payload(raw)
+ if payload.get("type") != "friend_remove" or payload.get("v") != 1:
+ raise ValueError("Неизвестный формат данных")
+ friend_user_id = payload.get("friend_user_id")
+ if not isinstance(friend_user_id, int) or friend_user_id <= 0:
+ raise ValueError("Некорректный ID друга")
+ return friend_user_id
+
+
class TelegramBotService:
def __init__(self, settings: Settings, database: Database):
self.settings = settings
@@ -187,6 +197,11 @@ class TelegramBotService:
if not self.database.cancel_transaction(user.id, transaction_id):
raise ValueError("Запись не найдена или уже отменена")
response = "Запись отменена и больше не участвует в расчётах."
+ elif action == "friend_remove":
+ friend_user_id = parse_friend_removal(raw)
+ if not self.database.remove_friendship(user.id, friend_user_id):
+ raise ValueError("Пользователь не найден в списке друзей")
+ response = "Пользователь удалён из друзей."
else:
raise ValueError("Неизвестный формат данных")
except ValueError as exc:
diff --git a/app/db.py b/app/db.py
index feadea1..89dbba5 100644
--- a/app/db.py
+++ b/app/db.py
@@ -298,6 +298,18 @@ class Database:
).fetchall()
return [int(row["friend_id"]) for row in rows]
+ def remove_friendship(self, first_user_id: int, second_user_id: int) -> bool:
+ if first_user_id == second_user_id:
+ return False
+ low, high = sorted((first_user_id, second_user_id))
+ with closing(self.connect()) as connection:
+ cursor = connection.execute(
+ "DELETE FROM friendships WHERE user_id_low = ? AND user_id_high = ?",
+ (low, high),
+ )
+ connection.commit()
+ return cursor.rowcount > 0
+
def set_meta(self, key: str, value: str) -> None:
with closing(self.connect()) as connection:
connection.execute(
diff --git a/static/app.js b/static/app.js
index 1fd0c82..f6b2d5d 100644
--- a/static/app.js
+++ b/static/app.js
@@ -449,13 +449,36 @@
? ``
: `${initial}`;
const percent = Number(item.saved_percent);
+ const canRemove = state.board === "friends" && item.user_id !== userId;
return `