-
Notifications
You must be signed in to change notification settings - Fork 1
/
base.py
65 lines (56 loc) · 2.15 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy.pool import NullPool
from functools import wraps
Base = declarative_base()
# Create an engine that stores data in the local directory's
# bot.sqlite file.
engine = create_engine('sqlite:///bot.sqlite', poolclass=NullPool)
_SessionFactory = sessionmaker(bind=engine)
Session = scoped_session(_SessionFactory)
# @MWT(timeout=60*60)
# TODO call clean up before switching on
def get_admin_ids(bot, chat_id):
"""
Returns a list of admin IDs for a given chat. Results are cached for 1 hour.
Private chats and groups with all_members_are_administrator flag are handled as empty admin list
"""
chat = bot.getChat(chat_id)
if chat.type == "private" or chat.all_members_are_administrators:
return []
return [admin.user.id for admin in bot.get_chat_administrators(chat_id)]
def transactional(func):
def call(*args, **kwargs):
session = Session()
try:
result = func(*args, **kwargs)
session.commit()
except:
session.rollback()
raise
finally:
session.close()
return result
return call
# TODO give creator rights
def only_admin(user_allowed=False):
def decorate(func):
@wraps(func)
def call(*args, **kwargs):
if len(args) is 3:
bot = args[1]
update = args[2]
channel_telegram_id = update.message.chat.id
user_telegram_id = update.message.from_user.id
if not user_allowed:
admins = get_admin_ids(bot, channel_telegram_id)
if not admins:
# in case of private chats evey user is admin and has all rights
admins = [user_telegram_id]
if user_telegram_id not in admins:
update.message.reply_text("You are not authorised to perform this operation")
return
return func(*args, **kwargs)
return call
return decorate