aboutsummaryrefslogtreecommitdiffstats
path: root/slack/slack_user.py
blob: 871c1ef58f229a627ada226b67efb7f20b7d7767 (plain) (blame)
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
from __future__ import annotations

from typing import TYPE_CHECKING

import weechat

from slack.shared import shared
from slack.util import with_color

if TYPE_CHECKING:
    from slack_api.slack_bots_info import SlackBotInfo
    from slack_api.slack_users_info import SlackUserInfo

    from slack.slack_workspace import SlackApi, SlackWorkspace


def nick_color(nick: str) -> str:
    return weechat.info_get("nick_color_name", nick)


# TODO: Probably need to do some mapping here based on the existing users, in case some has been changed to avoid duplicate names
def _name_from_user_info(workspace: SlackWorkspace, info: SlackUserInfo) -> str:
    display_name = info["profile"].get("display_name")
    if display_name and not workspace.config.use_real_names.value:
        return display_name

    return info["profile"].get("display_name") or info.get("real_name") or info["name"]


def name_from_user_info_without_spaces(
    workspace: SlackWorkspace, info: SlackUserInfo
) -> str:
    return _name_from_user_info(workspace, info).replace(" ", "")


def format_bot_nick(nick: str, colorize: bool = False) -> str:
    nick = nick.replace(" ", "")

    if colorize:
        nick = with_color(nick_color(nick), nick)

    return nick + shared.config.look.bot_user_suffix.value


class SlackUser:
    def __init__(self, workspace: SlackWorkspace, id: str, info: SlackUserInfo):
        self.workspace = workspace
        self.id = id
        self._info = info

    @classmethod
    async def create(cls, workspace: SlackWorkspace, id: str):
        info_response = await workspace.api.fetch_user_info(id)
        return cls(workspace, id, info_response["user"])

    @property
    def _api(self) -> SlackApi:
        return self.workspace.api

    def nick(self, colorize: bool = False) -> str:
        nick = self._name_without_spaces()

        if colorize:
            nick = with_color(self._nick_color(), nick)

        if self._info["profile"]["team"] != self.workspace.id:
            nick += shared.config.look.external_user_suffix.value

        return nick

    def _name_without_spaces(self) -> str:
        return name_from_user_info_without_spaces(self.workspace, self._info)

    def _nick_color(self) -> str:
        if self.id == self.workspace.my_user.id:
            return weechat.config_string(
                weechat.config_get("weechat.color.chat_nick_self")
            )

        return nick_color(self._name_without_spaces())


class SlackBot:
    def __init__(self, workspace: SlackWorkspace, id: str, info: SlackBotInfo):
        self.workspace = workspace
        self.id = id
        self._info = info

    @classmethod
    async def create(cls, workspace: SlackWorkspace, id: str):
        info_response = await workspace.api.fetch_bot_info(id)
        return cls(workspace, id, info_response["bot"])

    @property
    def _api(self) -> SlackApi:
        return self.workspace.api

    def nick(self, colorize: bool = False) -> str:
        return format_bot_nick(self._info["name"], colorize)