142 lines
5.2 KiB
Python
142 lines
5.2 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import logging
|
||
|
|
|
||
|
|
from yuxi.channel.extensions.mattermost.client import MattermostClient
|
||
|
|
from yuxi.channel.extensions.mattermost.errors import MattermostError
|
||
|
|
from yuxi.channel.extensions.mattermost.types import MattermostChannel, MattermostUser
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
|
||
|
|
class MattermostDirectoryAdapter:
|
||
|
|
def __init__(self, client: MattermostClient, account_id: str = ""):
|
||
|
|
self.client = client
|
||
|
|
self.account_id = account_id
|
||
|
|
|
||
|
|
async def list_channels(self, team_id: str | None = None) -> list[dict]:
|
||
|
|
channels: list[dict] = []
|
||
|
|
try:
|
||
|
|
if team_id:
|
||
|
|
raw = await self.client.fetch_team_channels(team_id)
|
||
|
|
for item in raw:
|
||
|
|
ch = MattermostChannel.from_dict(item)
|
||
|
|
channels.append(
|
||
|
|
{
|
||
|
|
"id": ch.id,
|
||
|
|
"name": ch.name,
|
||
|
|
"display_name": ch.display_name,
|
||
|
|
"type": ch.type,
|
||
|
|
"team_id": ch.team_id,
|
||
|
|
}
|
||
|
|
)
|
||
|
|
else:
|
||
|
|
me = await self.client.fetch_me()
|
||
|
|
teams = await self.client.fetch_user_teams(me["id"])
|
||
|
|
for team_data in teams:
|
||
|
|
tid = team_data["id"]
|
||
|
|
raw = await self.client.fetch_team_channels(tid)
|
||
|
|
for item in raw:
|
||
|
|
ch = MattermostChannel.from_dict(item)
|
||
|
|
channels.append(
|
||
|
|
{
|
||
|
|
"id": ch.id,
|
||
|
|
"name": ch.name,
|
||
|
|
"display_name": ch.display_name,
|
||
|
|
"type": ch.type,
|
||
|
|
"team_id": ch.team_id,
|
||
|
|
}
|
||
|
|
)
|
||
|
|
except MattermostError as e:
|
||
|
|
logger.error("Failed to list channels: %s", e)
|
||
|
|
return channels
|
||
|
|
|
||
|
|
async def list_users(self, team_id: str | None = None) -> list[dict]:
|
||
|
|
users: list[dict] = []
|
||
|
|
try:
|
||
|
|
raw = await self.client.fetch_users(page=0, per_page=200, in_team=team_id or "")
|
||
|
|
for item in raw:
|
||
|
|
user = MattermostUser.from_dict(item)
|
||
|
|
users.append(
|
||
|
|
{
|
||
|
|
"id": user.id,
|
||
|
|
"username": user.username,
|
||
|
|
"nickname": user.nickname,
|
||
|
|
"first_name": user.first_name,
|
||
|
|
"last_name": user.last_name,
|
||
|
|
"email": user.email,
|
||
|
|
"is_bot": user.is_bot,
|
||
|
|
}
|
||
|
|
)
|
||
|
|
except MattermostError as e:
|
||
|
|
logger.error("Failed to list users: %s", e)
|
||
|
|
return users
|
||
|
|
|
||
|
|
async def get_user_info(self, user_id: str) -> dict | None:
|
||
|
|
try:
|
||
|
|
result = await self.client.fetch_user(user_id)
|
||
|
|
user = MattermostUser.from_dict(result)
|
||
|
|
return {
|
||
|
|
"id": user.id,
|
||
|
|
"username": user.username,
|
||
|
|
"nickname": user.nickname,
|
||
|
|
"first_name": user.first_name,
|
||
|
|
"last_name": user.last_name,
|
||
|
|
"email": user.email,
|
||
|
|
"is_bot": user.is_bot,
|
||
|
|
}
|
||
|
|
except MattermostError:
|
||
|
|
return None
|
||
|
|
|
||
|
|
async def get_channel_info(self, channel_id: str) -> dict | None:
|
||
|
|
try:
|
||
|
|
result = await self.client.fetch_channel(channel_id)
|
||
|
|
ch = MattermostChannel.from_dict(result)
|
||
|
|
return {
|
||
|
|
"id": ch.id,
|
||
|
|
"name": ch.name,
|
||
|
|
"display_name": ch.display_name,
|
||
|
|
"type": ch.type,
|
||
|
|
"team_id": ch.team_id,
|
||
|
|
}
|
||
|
|
except MattermostError:
|
||
|
|
return None
|
||
|
|
|
||
|
|
async def search_channels(self, query: str) -> list[dict]:
|
||
|
|
try:
|
||
|
|
me = await self.client.fetch_me()
|
||
|
|
teams = await self.client.fetch_user_teams(me["id"])
|
||
|
|
results: list[dict] = []
|
||
|
|
for team_data in teams:
|
||
|
|
tid = team_data["id"]
|
||
|
|
try:
|
||
|
|
api_results = await self.client.search_channels(tid, query)
|
||
|
|
for item in api_results:
|
||
|
|
ch = MattermostChannel.from_dict(item)
|
||
|
|
results.append({
|
||
|
|
"id": ch.id,
|
||
|
|
"name": ch.name,
|
||
|
|
"display_name": ch.display_name,
|
||
|
|
"type": ch.type,
|
||
|
|
"team_id": ch.team_id,
|
||
|
|
})
|
||
|
|
except MattermostError:
|
||
|
|
continue
|
||
|
|
return results
|
||
|
|
except MattermostError:
|
||
|
|
pass
|
||
|
|
|
||
|
|
all_channels = await self.list_channels()
|
||
|
|
q = query.lower()
|
||
|
|
return [ch for ch in all_channels if q in ch.get("name", "").lower() or q in ch.get("display_name", "").lower()]
|
||
|
|
|
||
|
|
async def search_posts(
|
||
|
|
self,
|
||
|
|
team_id: str,
|
||
|
|
terms: str,
|
||
|
|
*,
|
||
|
|
page: int = 0,
|
||
|
|
per_page: int = 60,
|
||
|
|
) -> dict:
|
||
|
|
return await self.client.search_posts(team_id, terms, page=page, per_page=per_page)
|