Skip to content

Commit

Permalink
Update core to use mattermostautodriver instead of mattermostdriver (#…
Browse files Browse the repository at this point in the history
…337)

* create_ephemeral_post -> create_post_ephemeral

* create_reaction -> save_reaction

* create_direct_message_channel -> create_direct_channel

* get_channels_for_user -> get_channels_for_team_for_user

* get_thread -> get_post_thread

* get_reactions_of_post -> get_reactions

* webhooks.call_webhook -> call_webhook

* mattermostdriver -> mattermostautodriver
  • Loading branch information
unode authored Aug 26, 2022
1 parent f6d8b98 commit 2da4b30
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 24 deletions.
47 changes: 33 additions & 14 deletions mmpy_bot/driver.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import queue
import warnings
from pathlib import Path
from typing import Dict, List, Optional, Sequence, Union

import mattermostdriver
import mattermostautodriver
from aiohttp.client import ClientSession

from mmpy_bot.threadpool import ThreadPool
from mmpy_bot.webhook_server import WebHookServer
from mmpy_bot.wrappers import Message, WebHookEvent


class Driver(mattermostdriver.Driver):
class Driver(mattermostautodriver.Driver):
user_id: str = ""
username: str = ""

def __init__(self, *args, num_threads=10, **kwargs):
"""Wrapper around the mattermostdriver Driver with some convenience functions
and attributes.
"""Wrapper around the mattermostautodriver Driver with some convenience
functions and attributes.
Arguments:
- num_threads: int, number of threads to use for the default worker threadpool.
Expand Down Expand Up @@ -67,7 +68,7 @@ def create_post(
)

if ephemeral_user_id:
return self.posts.create_ephemeral_post(
return self.posts.create_post_ephemeral(
{
"user_id": ephemeral_user_id,
"post": post,
Expand All @@ -77,9 +78,15 @@ def create_post(
return self.posts.create_post(post)

def get_thread(self, post_id: str):
"""Wrapper around driver.posts.get_thread, which for some reason returns
warnings.warn(
"get_thread is deprecated. Use get_post_thread instead", DeprecationWarning
)
return self.get_post_thread(post_id)

def get_post_thread(self, post_id: str):
"""Wrapper around driver.posts.get_post_thread, which for some reason returns
duplicate and wrongly ordered entries in the ordered list."""
thread_info = self.posts.get_thread(post_id)
thread_info = self.posts.get_post_thread(post_id)

id_stamps = []
for id, post in thread_info["posts"].items():
Expand All @@ -96,7 +103,7 @@ def get_user_info(self, user_id: str):

def react_to(self, message: Message, emoji_name: str):
"""Adds an emoji reaction to the given message."""
return self.reactions.create_reaction(
return self.reactions.save_reaction(
{
"user_id": self.user_id,
"post_id": message.id,
Expand Down Expand Up @@ -158,9 +165,9 @@ def direct_message(
):
# Private/direct messages are sent to a special channel that
# includes the bot and the recipient
direct_id = self.channels.create_direct_message_channel(
[self.user_id, receiver_id]
)["id"]
direct_id = self.channels.create_direct_channel([self.user_id, receiver_id])[
"id"
]

return self.create_post(
channel_id=direct_id,
Expand Down Expand Up @@ -192,10 +199,22 @@ def upload_files(
) -> List[str]:
"""Given a list of file paths and the channel id, uploads the corresponding
files and returns a list their internal file IDs."""
file_dict = {}
file_list = []
for path in file_paths:
path = Path(path)
file_dict[path.name] = Path(path).read_bytes()
# Note: 'files' should be a name of an expected attribute in the body
# but seems to be ignored when simply uploading files to mattermost
file_list.append(
(
"files",
(
path.name,
Path(path).read_bytes(),
),
)
)

result = self.files.upload_file(channel_id, file_dict)
result = self.files.upload_file(
files=file_list, data={"channel_id": channel_id}
)
return list(info["id"] for info in result["file_infos"])
6 changes: 3 additions & 3 deletions mmpy_bot/plugins/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pathlib import Path

import click
import mattermostdriver
import mattermostautodriver

from mmpy_bot.function import listen_to
from mmpy_bot.plugins.base import Plugin
Expand Down Expand Up @@ -65,7 +65,7 @@ async def hello_ephemeral(self, message: Message):
permissions."""
try:
self.driver.reply_to(message, "hello sender!", ephemeral=True)
except mattermostdriver.exceptions.NotEnoughPermissions:
except mattermostautodriver.exceptions.NotEnoughPermissions:
self.driver.reply_to(
message, "I do not have permission to create ephemeral posts!"
)
Expand All @@ -85,7 +85,7 @@ async def hello_file(self, message: Message):
@listen_to("^!hello_webhook$", re.IGNORECASE, category="webhook")
async def hello_webhook(self, message: Message):
"""A webhook that says hello."""
self.driver.webhooks.call_webhook(
self.driver.client.call_webhook(
"eauegoqk4ibxigfybqrsfmt48r",
options={
"username": "webhook_test", # Requires the right webhook permissions
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
aiohttp>=3.7.4.post0
click>=7.0
mattermostdriver>=7.3.2
mattermostautodriver>=1.2.0
schedule>=0.6.0
Sphinx>=1.3.3
6 changes: 4 additions & 2 deletions tests/integration_tests/test_direct_message_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def bot_and_user_direct_channel(channel):
# which is implemented by mattermost as a channel
driver.create_post(OFF_TOPIC_ID, trigger)

user_channels = driver.channels.get_channels_for_user(driver.user_id, TEAM_ID)
user_channels = driver.channels.get_channels_for_team_for_user(
driver.user_id, TEAM_ID
)
channels = list(filter(bot_and_user_direct_channel, user_channels))

# We need to wait for the reply to be processed by mattermost
Expand All @@ -51,7 +53,7 @@ def bot_and_user_direct_channel(channel):
for _ in range(retries):
if len(channels) != 1:
time.sleep(RESPONSE_TIMEOUT)
user_channels = driver.channels.get_channels_for_user(
user_channels = driver.channels.get_channels_for_team_for_user(
driver.user_id, TEAM_ID
)
channels = list(filter(bot_and_user_direct_channel, user_channels))
Expand Down
6 changes: 3 additions & 3 deletions tests/integration_tests/test_example_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ def test_admin(self, driver):
# Since this is not a direct message, we expect no reply at all
post_id = driver.create_post(OFF_TOPIC_ID, "@main_bot admin")["id"]
time.sleep(RESPONSE_TIMEOUT)
thread_info = driver.get_thread(post_id)
thread_info = driver.get_post_thread(post_id)
assert len(thread_info["order"]) == 1

# For the direct message, we expect to have insufficient permissions, since
# our name isn't admin
private_channel = driver.channels.create_direct_message_channel(
private_channel = driver.channels.create_direct_channel(
[driver.user_id, MAIN_BOT_ID]
)["id"]
post = driver.create_post(private_channel, "admin")
Expand Down Expand Up @@ -93,7 +93,7 @@ def test_hello_ephemeral(self, driver):
def test_react(self, driver):
post_id = driver.create_post(OFF_TOPIC_ID, "@main_bot hello_react")["id"]
time.sleep(RESPONSE_TIMEOUT)
reactions = driver.reactions.get_reactions_of_post(post_id)
reactions = driver.reactions.get_reactions(post_id)
assert len(reactions) == 1
assert reactions[0]["emoji_name"] == "+1"

Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def expect_reply(driver: Driver, post: Dict, wait=RESPONSE_TIMEOUT, retries=1):
reply = None
for _ in range(retries + 1):
time.sleep(wait)
thread_info = driver.get_thread(post["id"])
thread_info = driver.get_post_thread(post["id"])
print(thread_info)
reply_id = thread_info["order"][-1]
if reply_id != post["id"]:
Expand Down

0 comments on commit 2da4b30

Please sign in to comment.