mirror of
https://github.com/paul-nameless/tg
synced 2024-11-22 03:43:19 +00:00
First version of adding users to view
This commit is contained in:
parent
b19ada8091
commit
ac72c5bcb0
4 changed files with 82 additions and 10 deletions
|
@ -130,6 +130,7 @@ class Controller:
|
|||
self.view.draw_status()
|
||||
|
||||
def refresh_msgs(self):
|
||||
self.view.msgs.users = self.model.users
|
||||
msgs = self.model.get_current_msgs(limit=self.view.msgs.h)
|
||||
self.view.draw_msgs(self.model.get_current_msg(), msgs)
|
||||
|
||||
|
|
8
main.py
8
main.py
|
@ -13,7 +13,7 @@ from view import View
|
|||
|
||||
logging.basicConfig(
|
||||
level=logging.DEBUG,
|
||||
format='%(asctime)s %(message)s',
|
||||
format='%(asctime)s %(levelname)s %(message)s',
|
||||
handlers=[
|
||||
logging.handlers.RotatingFileHandler(
|
||||
'./tg.log',
|
||||
|
@ -56,6 +56,12 @@ def main():
|
|||
)
|
||||
tg.login()
|
||||
|
||||
# model = Model(tg)
|
||||
# print(model.get_me())
|
||||
# print(model.get_user(246785877))
|
||||
# print(model.chats.get_chat(77769955))
|
||||
# return
|
||||
|
||||
wrapper(partial(run, tg))
|
||||
|
||||
|
||||
|
|
30
model.py
30
model.py
|
@ -12,6 +12,12 @@ class Model:
|
|||
self.users = UserModel(tg)
|
||||
self.current_chat = 0
|
||||
|
||||
def get_me(self):
|
||||
return self.users.get_me()
|
||||
|
||||
def get_user(self, user_id):
|
||||
return self.users.get_user(user_id)
|
||||
|
||||
def get_current_chat_id(self):
|
||||
return self.chats.chat_ids[self.current_chat]
|
||||
|
||||
|
@ -205,3 +211,27 @@ class UserModel:
|
|||
|
||||
def __init__(self, tg):
|
||||
self.tg = tg
|
||||
self.me = None
|
||||
self.users = {}
|
||||
|
||||
def get_me(self):
|
||||
if self.me:
|
||||
return self.me
|
||||
result = self.tg.get_me()
|
||||
result.wait()
|
||||
if result.error:
|
||||
logger.error(f'get chat ids error: {result.error_info}')
|
||||
return {}
|
||||
self.me = result.update
|
||||
return self.me
|
||||
|
||||
def get_user(self, user_id):
|
||||
if user_id in self.users:
|
||||
return self.users[user_id]
|
||||
result = self.tg.call_method('getUser', {'user_id': user_id})
|
||||
result.wait()
|
||||
if result.error:
|
||||
logger.error(f'get chat ids error: {result.error_info}')
|
||||
return {}
|
||||
self.users[user_id] = result.update
|
||||
return result.update
|
||||
|
|
53
view.py
53
view.py
|
@ -31,6 +31,7 @@ class View:
|
|||
curses.init_pair(7, -1, curses.COLOR_BLACK)
|
||||
curses.init_pair(8, curses.COLOR_CYAN, curses.COLOR_BLACK)
|
||||
curses.init_pair(9, curses.COLOR_BLUE, curses.COLOR_BLACK)
|
||||
curses.init_pair(10, curses.COLOR_MAGENTA, curses.COLOR_BLACK)
|
||||
|
||||
self.stdscr = stdscr
|
||||
self.chats = ChatView(stdscr)
|
||||
|
@ -146,28 +147,51 @@ class ChatView:
|
|||
self.win.clear()
|
||||
# self.win.vline(0, self.w-1, curses.ACS_VLINE, self.h)
|
||||
for i, chat in enumerate(chats):
|
||||
msg = f' {get_date(chat)} {chat["title"]} [{chat["unread_count"]}]: {get_last_msg(chat)}'
|
||||
# msg = f' {get_date(chat)} {chat["title"]} [{chat["unread_count"]}]: {get_last_msg(chat)}'
|
||||
date, title, unread, last_msg = get_date(
|
||||
chat), chat["title"], chat["unread_count"], get_last_msg(chat)
|
||||
# msg = emoji_pattern.sub(r'', msg)[:self.w-1]
|
||||
msg = emoji_pattern.sub(r'', msg)[:self.w-2] + ' '
|
||||
# last_msg = emoji_pattern.sub(r'', msg)[:self.w-2] + ' '
|
||||
last_msg = emoji_pattern.sub(r'', last_msg)
|
||||
# msg = msg[:self.w-1]
|
||||
if len(msg) < self.w:
|
||||
msg += ' ' * (self.w - len(msg) - 1)
|
||||
# if len(msg) < self.w:
|
||||
# msg += ' ' * (self.w - len(msg) - 1)
|
||||
|
||||
if i == current:
|
||||
colors = [7, 8, 9, 7]
|
||||
colors = [7, 8, 9, 10]
|
||||
else:
|
||||
colors = [1, 2, 3, 1]
|
||||
colors = [1, 2, 3, 4]
|
||||
|
||||
offset = 0
|
||||
j = 0
|
||||
for color, e in zip(colors, msg.split(' ', maxsplit=3)):
|
||||
# for color, e in zip(colors, msg.split(' ', maxsplit=3)):
|
||||
for color, e in zip(colors, ['', date, title]):
|
||||
attr = curses.color_pair(color)
|
||||
if offset > self.w:
|
||||
break
|
||||
j += 1
|
||||
if j < 4:
|
||||
e = e + ' '
|
||||
self.win.addstr(i, offset, e, attr)
|
||||
self.win.addstr(i, offset, e[:self.w-offset-1], attr)
|
||||
offset += len(e)
|
||||
|
||||
if offset >= self.w:
|
||||
continue
|
||||
|
||||
attr = curses.color_pair(colors[0])
|
||||
msg = last_msg[:self.w-offset-1]
|
||||
|
||||
# msg = msg[:self.w-1]
|
||||
if len(msg) < self.w:
|
||||
msg += ' ' * (self.w - offset - len(msg) - 1)
|
||||
|
||||
self.win.addstr(i, offset, msg, attr)
|
||||
|
||||
if unread:
|
||||
attr = curses.color_pair(colors[-1])
|
||||
unread = ' ' + str(unread) + ' '
|
||||
self.win.addstr(i, self.w - len(unread) - 1, unread, attr)
|
||||
|
||||
# if i == current:
|
||||
# # attr = curses.A_REVERSE | curses.color_pair(1)
|
||||
# attr = curses.A_REVERSE
|
||||
|
@ -265,14 +289,25 @@ class MsgView:
|
|||
|
||||
self.win.refresh()
|
||||
|
||||
def _get_user_by_id(self, user_id):
|
||||
if user_id == 0:
|
||||
return ''
|
||||
user = self.users.get_user(user_id)
|
||||
if user.get('username'):
|
||||
return '@' + user['username']
|
||||
if user["first_name"] and user["last_name"]:
|
||||
return f'{user["first_name"]} {user["last_name"]}'
|
||||
return f'{user["first_name"]}'
|
||||
|
||||
def _parse_msg(self, msg):
|
||||
dt = datetime.fromtimestamp(
|
||||
msg['date']).strftime("%H:%M:%S")
|
||||
_type = msg['@type']
|
||||
if _type == 'message':
|
||||
user = self._get_user_by_id(msg['sender_user_id'])
|
||||
return " {} {}: {}".format(
|
||||
dt,
|
||||
msg['sender_user_id'],
|
||||
user,
|
||||
parse_content(msg['content'])
|
||||
)
|
||||
logger.debug('Unknown message type: %s', msg)
|
||||
|
|
Loading…
Reference in a new issue