move mm_comm to new file

This commit is contained in:
mcbloch 2022-10-07 13:29:18 +02:00
parent 3aec9ffe3a
commit f5ac069f20
3 changed files with 60 additions and 56 deletions

View file

@ -0,0 +1,53 @@
import mattermostdriver.exceptions
from mattermost_client import ChannelApi, MMApi
def send_message(file_id, file_info, message):
channel_id = file_info["originating_mm_post_channel_id"]
post_id = file_info["originating_mm_post_id"]
# TODO Comment below line, this is for testing purposes
# channel_id = MMApi().get_channel_id("bestuur-dev")
channel = ChannelApi(
channel_id=channel_id,
user=mattermost.users["flynn"],
)
prefix = ""
# This is bestuur-INTERN where you can only post when you prefix you message with a '!'
if file_info["originating_mm_post_channel_id"] == "hrx6pgfswjbttcj8nim3jrwe7w":
prefix = "! "
try:
channel.create_threaded_post(
post_id,
f"{prefix}{message}",
)
except mattermostdriver.exceptions.InvalidOrMissingParameters as e:
# This will occur when we try to react to a file in a channel that is not the same as the originating channel.
unique_post_url = f"https://mattermost.zeus.gent/zeus/pl/{post_id}"
channel.create_post(
f"{unique_post_url}\n\n{message}",
)
def report_newly_found_file(file_id, file_info):
message = f"I found a new CodiMD file in this post! Making work of putting it on gitlab :)\n - Requested location in the [drive](https://git.zeus.gent/bestuur/drive): {file_info['metadata']['sync-to']}"
send_message(file_id, file_info, message)
def report_newly_found_but_invalid_file(file_id, file_info):
message = """Hi there! :wave:
I'm your friendly neighbourhood document sync bot.
I could synchronize this CodiMD file automatically to our Gitlab DRIVE for safekeeping, but the necessary metadata block is not present.
You can easily add the correct info and I will do the rest of the work for you!
Just add the following lines to your file, the location in your file is not important but at the top would be my recommendation.
```
:::spoiler Gitlab sync
- sync-to: <a valid path on the DRIVE, for ex.: verslagen/21-22/2022-05-13.md>
:::
```"""
send_message(file_id, file_info, message)

View file

@ -5,18 +5,17 @@ import time
from pprint import pprint
import hug
import mattermostdriver.exceptions
import requests
import db
import dir_utils
import mattermost
from mattermost import ChannelApi, MMApi
import mattermost_client
import mattermost_communication
from utils import id_to_url, url_to_id
def find_codimd_files_on_mattermost():
mattermost = MMApi()
mattermost = mattermost_client.MMApi()
channels = [
"hrx6pgfswjbttcj8nim3jrwe7w", # bestuur-INTERN
"uda7ax9poprduq8ob56e1fqk4e", # bestuur
@ -70,56 +69,6 @@ def download_files():
db.set_local_file_path(file_id, local_file_path)
def send_message(file_id, file_info, message):
channel_id = file_info["originating_mm_post_channel_id"]
post_id = file_info["originating_mm_post_id"]
# TODO Comment below line, this is for testing purposes
# channel_id = MMApi().get_channel_id("bestuur-dev")
channel = ChannelApi(
channel_id=channel_id,
user=mattermost.users["flynn"],
)
prefix = ""
# This is bestuur-INTERN where you can only post when you prefix you message with a '!'
if file_info["originating_mm_post_channel_id"] == "hrx6pgfswjbttcj8nim3jrwe7w":
prefix = "! "
try:
channel.create_threaded_post(
post_id,
f"{prefix}{message}",
)
except mattermostdriver.exceptions.InvalidOrMissingParameters as e:
# This will occur when we try to react to a file in a channel that is not the same as the originating channel.
unique_post_url = f"https://mattermost.zeus.gent/zeus/pl/{post_id}"
channel.create_post(
f"{unique_post_url}\n\n{message}",
)
def report_newly_found_file(file_id, file_info):
message = f"I found a new CodiMD file in this post! Making work of putting it on gitlab :)\n - Requested location in the [drive](https://git.zeus.gent/bestuur/drive): {file_info['metadata']['sync-to']}"
send_message(file_id, file_info, message)
def report_newly_found_but_invalid_file(file_id, file_info):
message = """Hi there! :wave:
I'm your friendly neighbourhood document sync bot.
I could synchronize this CodiMD file automatically to our Gitlab DRIVE for safekeeping, but the necessary metadata block is not present.
You can easily add the correct info and I will do the rest of the work for you!
Just add the following lines to your file, the location in your file is not important but at the top would be my recommendation.
```
:::spoiler Gitlab sync
- sync-to: <a valid path on the DRIVE, for ex.: verslagen/21-22/2022-05-13.md>
:::
```"""
send_message(file_id, file_info, message)
def validate_downloaded_files():
path = "data"
dir_list = os.listdir(path)
@ -130,11 +79,13 @@ def validate_downloaded_files():
if metadata is not None:
is_new_file, new_file_info = db.mark_file_valid(file_id, metadata)
if is_new_file:
report_newly_found_file(file_id, new_file_info)
mattermost_communication.report_newly_found_file(file_id, new_file_info)
else:
changed, new_file_info = db.mark_file_invalid(file_id)
if changed:
report_newly_found_but_invalid_file(file_id, new_file_info)
mattermost_communication.report_newly_found_but_invalid_file(
file_id, new_file_info
)
return db._load_db()