File size: 5,394 Bytes
f45efbd |
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
"""
MIT License
Copyright (c) 2022 Aʙɪsʜɴᴏɪ
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import threading
import typing
from sqlalchemy import BigInteger, Boolean, Column, String, distinct, func
from Database.sql import BASE, SESSION
class GroupLogs(BASE):
__tablename__ = "log_channels"
chat_id = Column(String(14), primary_key=True)
log_channel = Column(String(14), nullable=False)
def __init__(self, chat_id, log_channel):
self.chat_id = str(chat_id)
self.log_channel = str(log_channel)
class LogChannelSettings(BASE):
__tablename__ = "log_channel_setting"
chat_id = Column(BigInteger, primary_key=True)
log_joins = Column(Boolean, default=True)
log_leave = Column(Boolean, default=True)
log_warn = Column(Boolean, default=True)
log_action = Column(Boolean, default=True)
# log_media = Column(Boolean)
log_report = Column(Boolean, default=True)
def __init__(
self,
chat_id: int,
log_join: bool,
log_leave: bool,
log_warn: bool,
log_action: bool,
log_report: bool,
):
self.chat_id = chat_id
self.log_warn = log_warn
self.log_joins = log_join
self.log_leave = log_leave
self.log_report = log_report
self.log_action = log_action
def toggle_warn(self) -> bool:
self.log_warn = not self.log_warn
SESSION.commit()
return self.log_warn
def toggle_joins(self) -> bool:
self.log_joins = not self.log_joins
SESSION.commit()
return self.log_joins
def toggle_leave(self) -> bool:
self.log_leave = not self.log_leave
SESSION.commit()
return self.log_leave
def toggle_report(self) -> bool:
self.log_report = not self.log_report
SESSION.commit()
return self.log_report
def toggle_action(self) -> bool:
self.log_action = not self.log_action
SESSION.commit()
return self.log_action
GroupLogs.__table__.create(checkfirst=True)
LogChannelSettings.__table__.create(checkfirst=True)
LOGS_INSERTION_LOCK = threading.RLock()
LOG_SETTING_LOCK = threading.RLock()
CHANNELS = {}
def get_chat_setting(chat_id: int) -> typing.Optional[LogChannelSettings]:
with LOG_SETTING_LOCK:
return SESSION.query(LogChannelSettings).get(chat_id)
def set_chat_setting(setting: LogChannelSettings):
with LOGS_INSERTION_LOCK:
res: LogChannelSettings = SESSION.query(LogChannelSettings).get(setting.chat_id)
if res:
res.log_warn = setting.log_warn
res.log_action = setting.log_action
res.log_report = setting.log_report
res.log_joins = setting.log_joins
res.log_leave = setting.log_leave
else:
SESSION.add(setting)
SESSION.commit()
def set_chat_log_channel(chat_id, log_channel):
with LOGS_INSERTION_LOCK:
res = SESSION.query(GroupLogs).get(str(chat_id))
if res:
res.log_channel = log_channel
else:
res = GroupLogs(chat_id, log_channel)
SESSION.add(res)
CHANNELS[str(chat_id)] = log_channel
SESSION.commit()
def get_chat_log_channel(chat_id):
return CHANNELS.get(str(chat_id))
def stop_chat_logging(chat_id):
with LOGS_INSERTION_LOCK:
res = SESSION.query(GroupLogs).get(str(chat_id))
if res:
if str(chat_id) in CHANNELS:
del CHANNELS[str(chat_id)]
log_channel = res.log_channel
SESSION.delete(res)
SESSION.commit()
return log_channel
def num_logchannels():
try:
return SESSION.query(func.count(distinct(GroupLogs.chat_id))).scalar()
finally:
SESSION.close()
def migrate_chat(old_chat_id, new_chat_id):
with LOGS_INSERTION_LOCK:
chat = SESSION.query(GroupLogs).get(str(old_chat_id))
if chat:
chat.chat_id = str(new_chat_id)
SESSION.add(chat)
if str(old_chat_id) in CHANNELS:
CHANNELS[str(new_chat_id)] = CHANNELS.get(str(old_chat_id))
SESSION.commit()
def __load_log_channels():
global CHANNELS
try:
all_chats = SESSION.query(GroupLogs).all()
CHANNELS = {chat.chat_id: chat.log_channel for chat in all_chats}
finally:
SESSION.close()
__load_log_channels()
|