This commit is contained in:
2021-01-08 11:30:52 +08:00
parent b2ec5700fc
commit 9ab4fb72e4
3 changed files with 134 additions and 3 deletions

View File

@@ -31,6 +31,7 @@ from textc import textc
from re import search, I
from rsschecker import RSSCheckerThread
from rsslist import getInlineKeyBoardForRSSList, InlineKeyBoardForRSSList
from usercheck import checkUserPermissionsInChat, UserPermissionsInChatCheckResult
def getMediaInfo(m: dict, config: RSSConfig = RSSConfig()) -> str:
@@ -498,6 +499,7 @@ class messageHandle(Thread):
else:
di['text'] = '正在获取信息中……'
elif self._botCommand == '/rsslist':
self._needCheckUser = False
self._botCommandPara = self._getCommandlinePara()
targetChatId = self._chatId
for i in self._botCommandPara:
@@ -513,6 +515,7 @@ class messageHandle(Thread):
di['text'] = '获取列表失败。'
else:
di['text'] = '正在确认操作者权限……'
self._needCheckUser = True
re = self._main._request('sendMessage', 'post', json=di)
if self._botCommand == '/rss' and self._uri is not None and re is not None and 'ok' in re and re['ok']:
re = re['result']
@@ -545,6 +548,27 @@ class messageHandle(Thread):
self._main._rssMetaList.addRSSMeta(rssMetaInfo(
re['message_id'], chatId, media, p.itemList, self._hash))
self._main._request('editMessageText', 'post', json=di)
if self._botCommand == '/rsslist' and self._needCheckUser and re is not None and 'ok' in re and re['ok']:
messageInfo = re['result']
messageChatId = messageInfo['chat']['id']
messageId = messageInfo['message_id']
checkResult = checkUserPermissionsInChat(
self._main, targetChatId, self._fromUserId)
di = {'chat_id': messageChatId, 'message_id': messageId}
if checkResult == UserPermissionsInChatCheckResult.OK:
rssList = self._main._db.getRSSListByChatId(targetChatId)
di['text'] = '列表如下:'
di['reply_markup'] = getInlineKeyBoardForRSSList(
targetChatId, rssList)
elif checkResult == UserPermissionsInChatCheckResult.GetChatInfoError:
di['text'] = '获取群/频道信息失败。'
elif checkResult == UserPermissionsInChatCheckResult.PrivateChat:
di['text'] = '该chat ID为私聊。'
elif checkResult == UserPermissionsInChatCheckResult.GetChatAdministratorsError:
di['text'] = '获取群/频道管理员列表失败。'
elif checkResult == UserPermissionsInChatCheckResult.NoPermissions:
di['text'] = '您没有权限进行操作。'
self._main._request('editMessageText', 'post', json=di)
class callbackQueryHandle(Thread):
@@ -757,6 +781,60 @@ class callbackQueryHandle(Thread):
self._main._request("editMessageText", "post", json=di)
self.answer()
return
elif self._loc == 1:
chatId = int(self._inputList[1])
self._inlineKeyBoardForRSSListCommand = InlineKeyBoardForRSSList(
int(self._inputList[2]))
if 'message' not in self._data:
self.answer('找不到信息。')
return
if self._inlineKeyBoardForRSSListCommand == InlineKeyBoardForRSSList.FirstPage:
di = {'chat_id': self._data['message']['chat']['id'],
'message_id': self._data['message']['message_id']}
di['text'] = '列表如下:'
rssList = self._main._db.getRSSListByChatId(chatId)
di['reply_markup'] = getInlineKeyBoardForRSSList(
chatId, rssList)
self._main._request("editMessageText", "post", json=di)
self.answer()
return
elif self._inlineKeyBoardForRSSListCommand == InlineKeyBoardForRSSList.LastPage:
di = {'chat_id': self._data['message']['chat']['id'],
'message_id': self._data['message']['message_id']}
di['text'] = '列表如下:'
rssList = self._main._db.getRSSListByChatId(chatId)
di['reply_markup'] = getInlineKeyBoardForRSSList(
chatId, rssList, lastPage=True)
self._main._request("editMessageText", "post", json=di)
self.answer()
return
elif self._inlineKeyBoardForRSSListCommand == InlineKeyBoardForRSSList.PrevPage:
di = {'chat_id': self._data['message']['chat']['id'],
'message_id': self._data['message']['message_id']}
pageNum = int(self._inputList[3])
di['text'] = '列表如下:'
rssList = self._main._db.getRSSListByChatId(chatId)
di['reply_markup'] = getInlineKeyBoardForRSSList(
chatId, rssList, pageNum-1)
self._main._request("editMessageText", "post", json=di)
self.answer()
return
elif self._inlineKeyBoardForRSSListCommand == InlineKeyBoardForRSSList.NextPage:
di = {'chat_id': self._data['message']['chat']['id'],
'message_id': self._data['message']['message_id']}
pageNum = int(self._inputList[3])
di['text'] = '列表如下:'
rssList = self._main._db.getRSSListByChatId(chatId)
di['reply_markup'] = getInlineKeyBoardForRSSList(
chatId, rssList, pageNum+1)
self._main._request("editMessageText", "post", json=di)
self.answer()
return
elif self._inlineKeyBoardForRSSListCommand == InlineKeyBoardForRSSList.Close:
di = {'chat_id': self._data['message']['chat']['id'],
'message_id': self._data['message']['message_id']}
self._main._request("deleteMessage", "post", json=di)
return
else:
self.answer('未知的按钮。')
return

View File

@@ -29,12 +29,14 @@ class InlineKeyBoardForRSSList(Enum):
Content = 5
def getInlineKeyBoardForRSSList(chatId: int, RSSEntries: List[RSSEntry], page=1) -> dict:
def getInlineKeyBoardForRSSList(chatId: int, RSSEntries: List[RSSEntry], page: int = 1, lastPage: bool = False) -> dict:
d = []
i = -1
lineLimit = 7
l = len(RSSEntries)
pn = ceil(l / lineLimit)
if lastPage:
page = pn
if l != 0:
page = max(min(pn, page), 1)
s = max(lineLimit * (page - 1), 0)
@@ -43,8 +45,8 @@ def getInlineKeyBoardForRSSList(chatId: int, RSSEntries: List[RSSEntry], page=1)
rss = RSSEntries[s]
d.append([])
i = i + 1
d[i].append({'text': rss.title[0:20],
'callback_data': f'1,{chatId},{InlineKeyBoardForRSSList.Content.value},{s}'})
d[i].append(
{'text': rss.title, 'callback_data': f'1,{chatId},{InlineKeyBoardForRSSList.Content.value},{s}'})
s = s + 1
if pn != 1:
d.append([])

51
usercheck.py Normal file
View File

@@ -0,0 +1,51 @@
# (C) 2021 lifegpc
# This file is part of rssbot.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from enum import Enum, unique
@unique
class UserPermissionsInChatCheckResult(Enum):
OK = 0
GetChatInfoError = 1
PrivateChat = 2
GetChatAdministratorsError = 3
NoPermissions = 4
def checkUserPermissionsInChat(m, chatId: int, userId: int) -> UserPermissionsInChatCheckResult:
from rssbot import main
_main: main = m
re = _main._request('getChat', 'post', {'chat_id': chatId})
if re is None or 'ok' not in re or not re['ok']:
return UserPermissionsInChatCheckResult.GetChatInfoError
chatInfo = re['result']
if chatInfo['type'] == 'private':
return UserPermissionsInChatCheckResult.PrivateChat
re = _main._request('getChatAdministrators', 'post', {'chat_id': chatId})
if re is None or 'ok' not in re or not re['ok']:
return UserPermissionsInChatCheckResult.GetChatAdministratorsError
chatAdministrators = re['result']
for chatMember in chatAdministrators:
if chatMember['user']['id'] != userId:
continue
if chatMember['status'] not in ['creator', 'administrator']:
continue
if chatInfo['type'] == 'channel' and chatMember['status'] == 'administrator' and ('can_post_messages' not in chatMember or not chatMember['can_post_messages']):
continue
if chatInfo['type'] == 'channel' and chatMember['status'] == 'administrator' and ('can_edit_messages' not in chatMember or not chatMember['can_edit_messages']):
continue
return UserPermissionsInChatCheckResult.OK
return UserPermissionsInChatCheckResult.NoPermissions