File size: 6,708 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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
"""
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
from sqlalchemy import (
BigInteger,
Column,
ForeignKey,
String,
UnicodeText,
UniqueConstraint,
func,
)
from Database.sql import BASE, SESSION
from Mikobot import dispatcher
class Users(BASE):
__tablename__ = "users"
user_id = Column(BigInteger, primary_key=True)
username = Column(UnicodeText)
def __init__(self, user_id, username=None):
self.user_id = user_id
self.username = username
def __repr__(self):
return "<ᴜsᴇʀ {} ({})>".format(self.username, self.user_id)
class Chats(BASE):
__tablename__ = "chats"
chat_id = Column(String(14), primary_key=True)
chat_name = Column(UnicodeText, nullable=False)
def __init__(self, chat_id, chat_name):
self.chat_id = str(chat_id)
self.chat_name = chat_name
def __repr__(self):
return "<ᴄʜᴀᴛ {} ({})>".format(self.chat_name, self.chat_id)
class ChatMembers(BASE):
__tablename__ = "chat_members"
priv_chat_id = Column(BigInteger, primary_key=True)
# NOTE: Use dual primary key instead of private primary key?
chat = Column(
String(14),
ForeignKey("chats.chat_id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=False,
)
user = Column(
BigInteger,
ForeignKey("users.user_id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=False,
)
__table_args__ = (UniqueConstraint("chat", "user", name="_chat_members_uc"),)
def __init__(self, chat, user):
self.chat = chat
self.user = user
def __repr__(self):
return "<ᴄʜᴀᴛ ᴜsᴇʀ {} ({}) ɪɴ ᴄʜᴀᴛ {} ({})>".format(
self.user.username,
self.user.user_id,
self.chat.chat_name,
self.chat.chat_id,
)
Users.__table__.create(checkfirst=True)
Chats.__table__.create(checkfirst=True)
ChatMembers.__table__.create(checkfirst=True)
INSERTION_LOCK = threading.RLock()
def ensure_bot_in_db():
with INSERTION_LOCK:
bot = Users(dispatcher.bot.id, dispatcher.bot.username)
SESSION.merge(bot)
SESSION.commit()
def update_user(user_id, username, chat_id=None, chat_name=None):
with INSERTION_LOCK:
user = SESSION.query(Users).get(user_id)
if not user:
user = Users(user_id, username)
SESSION.add(user)
SESSION.flush()
else:
user.username = username
if not chat_id or not chat_name:
SESSION.commit()
return
chat = SESSION.query(Chats).get(str(chat_id))
if not chat:
chat = Chats(str(chat_id), chat_name)
SESSION.add(chat)
SESSION.flush()
else:
chat.chat_name = chat_name
member = (
SESSION.query(ChatMembers)
.filter(ChatMembers.chat == chat.chat_id, ChatMembers.user == user.user_id)
.first()
)
if not member:
chat_member = ChatMembers(chat.chat_id, user.user_id)
SESSION.add(chat_member)
SESSION.commit()
def get_userid_by_name(username):
try:
return (
SESSION.query(Users)
.filter(func.lower(Users.username) == username.lower())
.all()
)
finally:
SESSION.close()
def get_name_by_userid(user_id):
try:
return SESSION.query(Users).get(Users.user_id == int(user_id)).first()
finally:
SESSION.close()
def get_chat_members(chat_id):
try:
return SESSION.query(ChatMembers).filter(ChatMembers.chat == str(chat_id)).all()
finally:
SESSION.close()
def get_all_chats():
try:
return SESSION.query(Chats).all()
finally:
SESSION.close()
def get_all_users():
try:
return SESSION.query(Users).all()
finally:
SESSION.close()
def get_user_num_chats(user_id):
try:
return (
SESSION.query(ChatMembers).filter(ChatMembers.user == int(user_id)).count()
)
finally:
SESSION.close()
def get_user_com_chats(user_id):
try:
chat_members = (
SESSION.query(ChatMembers).filter(ChatMembers.user == int(user_id)).all()
)
return [i.chat for i in chat_members]
finally:
SESSION.close()
def num_chats():
try:
return SESSION.query(Chats).count()
finally:
SESSION.close()
def num_users():
try:
return SESSION.query(Users).count()
finally:
SESSION.close()
def migrate_chat(old_chat_id, new_chat_id):
with INSERTION_LOCK:
chat = SESSION.query(Chats).get(str(old_chat_id))
if chat:
chat.chat_id = str(new_chat_id)
SESSION.commit()
chat_members = (
SESSION.query(ChatMembers)
.filter(ChatMembers.chat == str(old_chat_id))
.all()
)
for member in chat_members:
member.chat = str(new_chat_id)
SESSION.commit()
ensure_bot_in_db()
def del_user(user_id):
with INSERTION_LOCK:
curr = SESSION.query(Users).get(user_id)
if curr:
SESSION.delete(curr)
SESSION.commit()
return True
ChatMembers.query.filter(ChatMembers.user == user_id).delete()
SESSION.commit()
SESSION.close()
return False
def rem_chat(chat_id):
with INSERTION_LOCK:
chat = SESSION.query(Chats).get(str(chat_id))
if chat:
SESSION.delete(chat)
SESSION.commit()
else:
SESSION.close()
|