Пример #1
0
def update_chat_order(controller: Controller, update: Dict[str, Any]) -> None:
    current_chat_id = controller.model.current_chat_id
    chat_id = update["chat_id"]
    order = update["order"]

    if controller.model.chats.update_chat(chat_id, order=order):
        controller.refresh_current_chat(current_chat_id)
Пример #2
0
def update_chat_notification_settings(controller: Controller,
                                      update: Dict[str, Any]) -> None:
    chat_id = update["chat_id"]
    notification_settings = update["notification_settings"]
    if controller.model.chats.update_chat(
            chat_id, notification_settings=notification_settings):
        controller.render()
Пример #3
0
def update_chat_notification_settings(controller: Controller, update):
    log.info("Proccessing update_chat_notification_settings")
    chat_id = update["chat_id"]
    notification_settings = update["notification_settings"]
    if controller.model.chats.update_chat(
            chat_id, notification_settings=notification_settings):
        controller.render()
Пример #4
0
def run(tg: Tdlib, stdscr: window) -> None:

    # handle ctrl+c, to avoid interrupting tg when subprocess is called
    def interrupt_signal_handler(sig: int, frame: FrameType) -> None:
        # TODO: draw on status pane: to quite press <q>
        log.info("Interrupt signal is handled and ignored on purpose.")

    signal.signal(signal.SIGINT, interrupt_signal_handler)

    model = Model(tg)
    status_view = StatusView(stdscr)
    msg_view = MsgView(stdscr, model)
    chat_view = ChatView(stdscr, model)
    view = View(stdscr, chat_view, msg_view, status_view)
    controller = Controller(model, view, tg)

    # hanlde resize of terminal correctly
    signal.signal(signal.SIGWINCH, controller.resize_handler)

    for msg_type, handler in update_handlers.handlers.items():
        tg.add_update_handler(msg_type, partial(handler, controller))

    thread = threading.Thread(target=controller.run)
    thread.daemon = True
    thread.start()

    controller.draw()
Пример #5
0
def update_message_content_opened(
    controller: Controller, update: Dict[str, Any]
) -> None:
    chat_id = update["chat_id"]
    message_id = update["message_id"]
    controller.model.msgs.update_msg_content_opened(chat_id, message_id)
    controller.render_msgs()
Пример #6
0
def update_chat_title(controller: Controller, update: Dict[str, Any]) -> None:
    chat_id = update["chat_id"]
    title = update["title"]

    current_chat_id = controller.model.current_chat_id
    if controller.model.chats.update_chat(chat_id, title=title):
        controller.refresh_current_chat(current_chat_id)
Пример #7
0
def update_chat_title(controller: Controller, update: Dict[str, Any]):
    log.info("Proccessing updateChatTitle")
    chat_id = update["chat_id"]
    title = update["title"]

    current_chat_id = controller.model.current_chat_id
    if controller.model.chats.update_chat(chat_id, title=title):
        controller._refresh_current_chat(current_chat_id)
Пример #8
0
def update_user_chat_action(controller: Controller, update: Dict[str,
                                                                 Any]) -> None:
    chat_id = update["chat_id"]
    if update["action"]["@type"] == "chatActionCancel":
        controller.model.users.actions.pop(chat_id, None)
    else:
        controller.model.users.actions[chat_id] = update
    controller.render()
Пример #9
0
def update_chat_order(controller: Controller, update: Dict[str, Any]):
    log.info("Proccessing updateChatOrder")
    current_chat_id = controller.model.current_chat_id
    chat_id = update["chat_id"]
    order = update["order"]

    if controller.model.chats.update_chat(chat_id, order=order):
        controller._refresh_current_chat(current_chat_id)
Пример #10
0
def update_message_edited(controller: Controller, update: Dict[str, Any]):
    chat_id = update["chat_id"]
    message_id = update["message_id"]
    edit_date = update["edit_date"]
    controller.model.msgs.update_msg(chat_id, message_id, edit_date=edit_date)

    current_chat_id = controller.model.current_chat_id
    if current_chat_id == chat_id:
        controller.render_msgs()
Пример #11
0
def update_delete_messages(controller: Controller, update: Dict[str,
                                                                Any]) -> None:
    if not update["is_permanent"]:
        log.debug("Ignoring deletiong becuase not permanent: %s", update)
        return
    chat_id = update["chat_id"]
    msg_ids = update["message_ids"]
    controller.model.msgs.remove_messages(chat_id, msg_ids)
    controller.render_msgs()
Пример #12
0
def update_message_send_succeeded(controller: Controller, update):
    chat_id = update["message"]["chat_id"]
    msg_id = update["old_message_id"]
    controller.model.msgs.add_message(chat_id, update["message"])
    controller.model.msgs.remove_message(chat_id, msg_id)

    current_chat_id = controller.model.current_chat_id
    if current_chat_id == chat_id:
        controller.render_msgs()
Пример #13
0
def update_chat_read_outbox(controller: Controller, update: Dict[str,
                                                                 Any]) -> None:
    chat_id = update["chat_id"]
    last_read_outbox_message_id = update["last_read_outbox_message_id"]

    current_chat_id = controller.model.current_chat_id
    if controller.model.chats.update_chat(
            chat_id, last_read_outbox_message_id=last_read_outbox_message_id):
        controller.refresh_current_chat(current_chat_id)
Пример #14
0
def update_chat_is_marked_as_unread(controller: Controller,
                                    update: Dict[str, Any]) -> None:
    chat_id = update["chat_id"]
    is_marked_as_unread = update["is_marked_as_unread"]

    current_chat_id = controller.model.current_chat_id
    if controller.model.chats.update_chat(
            chat_id, is_marked_as_unread=is_marked_as_unread):
        controller.refresh_current_chat(current_chat_id)
Пример #15
0
def update_chat_is_marked_as_unread(controller: Controller, update: Dict[str,
                                                                         Any]):
    log.info("Proccessing updateChatIsMarkedAsUnread")
    chat_id = update["chat_id"]
    is_marked_as_unread = update["is_marked_as_unread"]

    current_chat_id = controller.model.current_chat_id
    if controller.model.chats.update_chat(
            chat_id, is_marked_as_unread=is_marked_as_unread):
        controller._refresh_current_chat(current_chat_id)
Пример #16
0
def update_chat_position(controller: Controller, update: Dict[str,
                                                              Any]) -> None:
    current_chat_id = controller.model.current_chat_id
    chat_id = update["chat_id"]
    info = {}
    info["order"] = update["position"]["order"]
    if "is_pinned" in update:
        info["is_pinned"] = update["is_pinned"]
    if controller.model.chats.update_chat(chat_id, **info):
        controller.refresh_current_chat(current_chat_id)
Пример #17
0
def update_chat_draft_message(controller: Controller,
                              update: Dict[str, Any]) -> None:
    chat_id = update["chat_id"]
    # FIXME: ignoring draft message itself for now because UI can't show it
    # draft_message = update["draft_message"]
    order = update["order"]

    current_chat_id = controller.model.current_chat_id
    if controller.model.chats.update_chat(chat_id, order=order):
        controller.refresh_current_chat(current_chat_id)
Пример #18
0
def update_message_send_succeeded(controller: Controller,
                                  update: Dict[str, Any]) -> None:
    chat_id = update["message"]["chat_id"]
    msg_id = update["old_message_id"]
    controller.model.msgs.add_message(chat_id, update["message"])
    controller.model.msgs.remove_messages(chat_id, [msg_id])

    current_chat_id = controller.model.current_chat_id
    if current_chat_id == chat_id:
        controller.render_msgs()
Пример #19
0
def update_message_content(controller: Controller, update: Dict[str, Any]):
    chat_id = update["chat_id"]
    message_id = update["message_id"]
    controller.model.msgs.update_msg(chat_id,
                                     message_id,
                                     content=update["new_content"])

    current_chat_id = controller.model.current_chat_id
    if current_chat_id == chat_id:
        controller.render_msgs()
Пример #20
0
def update_chat_is_pinned(controller: Controller, update: Dict[str, Any]):
    log.info("Proccessing updateChatIsPinned")
    chat_id = update["chat_id"]
    is_pinned = update["is_pinned"]
    order = update["order"]

    current_chat_id = controller.model.current_chat_id
    if controller.model.chats.update_chat(chat_id,
                                          is_pinned=is_pinned,
                                          order=order):
        controller._refresh_current_chat(current_chat_id)
Пример #21
0
def update_chat_read_outbox(controller: Controller, update: Dict[str, Any]):
    log.info("Proccessing updateChatReadOutbox")
    chat_id = update["chat_id"]
    last_read_outbox_message_id = update["last_read_outbox_message_id"]

    current_chat_id = controller.model.current_chat_id
    if controller.model.chats.update_chat(
            chat_id,
            last_read_outbox_message_id=last_read_outbox_message_id,
    ):
        controller._refresh_current_chat(current_chat_id)
Пример #22
0
def update_chat_is_pinned(controller: Controller, update: Dict[str,
                                                               Any]) -> None:
    chat_id = update["chat_id"]
    is_pinned = update["is_pinned"]
    order = update["order"]

    current_chat_id = controller.model.current_chat_id
    if controller.model.chats.update_chat(chat_id,
                                          is_pinned=is_pinned,
                                          order=order):
        controller.refresh_current_chat(current_chat_id)
Пример #23
0
def update_connection_state(controller: Controller, update: Dict[str, Any]):
    log.info("state:: %s", update)
    state = update["state"]["@type"]
    states = {
        "connectionStateWaitingForNetwork": "Waiting for network...",
        "connectionStateConnectingToProxy": "Connecting to proxy...",
        "connectionStateConnecting": "Connecting...",
        "connectionStateUpdating": "Updating...",
        "connectionStateReady": "Ready",
    }
    msg = states.get(state, "Unknown state")
    controller.present_info(msg)
Пример #24
0
def update_connection_state(controller: Controller, update: Dict[str,
                                                                 Any]) -> None:
    state = update["state"]["@type"]
    states = {
        "connectionStateWaitingForNetwork": "Waiting for network...",
        "connectionStateConnectingToProxy": "Connecting to proxy...",
        "connectionStateConnecting": "Connecting...",
        "connectionStateUpdating": "Updating...",
        # state exists, but when it's "Ready" we want to show "Chats"
        # "connectionStateReady": "Ready",
    }
    controller.model.chats.title = states.get(state, "Chats")
    controller.render_chats()
Пример #25
0
def update_chat_last_message(controller: Controller,
                             update: Dict[str, Any]) -> None:
    chat_id = update["chat_id"]
    last_message = update.get("last_message")
    if not last_message:
        # according to documentation it can be null
        log.warning("last_message is null: %s", update)
        return
    order = update["order"]

    current_chat_id = controller.model.current_chat_id
    if controller.model.chats.update_chat(chat_id,
                                          last_message=last_message,
                                          order=order):
        controller.refresh_current_chat(current_chat_id)
Пример #26
0
def update_file(controller: Controller, update: Dict[str, Any]) -> None:
    file_id = update["file"]["id"]
    local = update["file"]["local"]
    chat_id, msg_id = controller.model.downloads.get(file_id, (None, None))
    if chat_id is None or msg_id is None:
        log.warning("Can't find information about file with file_id=%s",
                    file_id)
        return
    msg = controller.model.msgs.msgs[chat_id].get(msg_id)
    if not msg:
        return
    proxy = MsgProxy(msg)
    proxy.local = local
    controller.render_msgs()
    if proxy.is_downloaded:
        controller.model.downloads.pop(file_id)
Пример #27
0
def update_chat_last_message(controller: Controller,
                             update: Dict[str, Any]) -> None:
    chat_id = update["chat_id"]
    last_message = update.get("last_message")
    if not last_message:
        # according to documentation it can be null
        log.warning("last_message is null: %s", update)
        return

    info = {}
    info["last_message"] = last_message
    if len(update["positions"]) > 0:
        info["order"] = update["positions"][0]["order"]

    current_chat_id = controller.model.current_chat_id
    if controller.model.chats.update_chat(chat_id, **info):
        controller.refresh_current_chat(current_chat_id)
Пример #28
0
def update_file(controller: Controller, update):
    log.info("update_file: %s", update)
    file_id = update["file"]["id"]
    local = update["file"]["local"]
    chat_id, msg_id = controller.model.downloads.get(file_id, (None, None))
    if chat_id is None:
        log.warning("Can't find information about file with file_id=%s",
                    file_id)
        return
    msgs = controller.model.msgs.msgs[chat_id]
    for msg in msgs:
        if msg["id"] == msg_id:
            proxy = MsgProxy(msg)
            proxy.local = local
            controller.render_msgs()
            if proxy.is_downloaded:
                controller.model.downloads.pop(file_id)
            break
Пример #29
0
def update_new_message(controller: Controller, update: Dict[str, Any]) -> None:
    msg = MsgProxy(update["message"])
    controller.model.msgs.add_message(msg.chat_id, msg.msg)
    current_chat_id = controller.model.current_chat_id
    if current_chat_id == msg.chat_id:
        controller.render_msgs()
    if msg.file_id and msg.size and msg.size <= max_download_size:
        controller.download(msg.file_id, msg.chat_id, msg["id"])

    controller.notify_for_message(msg.chat_id, msg)
Пример #30
0
def update_supergroup(controller: Controller, update: Dict[str, Any]) -> None:
    supergroup = update["supergroup"]
    controller.model.users.supergroups[supergroup["id"]] = supergroup
    controller.render_msgs()