fix(bot): strip trailing `/` in server url

This commit is contained in:
izzy lyseggen 2021-07-23 15:00:15 +01:00
Родитель 2bacbdfd54
Коммит 388626d965
1 изменённых файлов: 36 добавлений и 30 удалений

Просмотреть файл

@ -66,29 +66,27 @@ class DiscordBot(object):
} }
def add_author(self, msg, user_info, server_url): def add_author(self, msg, user_info, server_url):
filename = None
avatar = user_info["avatar"] avatar = user_info["avatar"]
if avatar: if avatar and not avatar.startswith("http"):
type = avatar[5:].split(";")[0] type = avatar[5:].split(";")[0]
filename = f"avatar.{type.split('/')[1]}" filename = f"avatar.{type.split('/')[1]}"
msg["files"].append( msg["files"].append(
( (
"file", "file",
( (
"avatar.jpeg", filename,
(base64.b64decode(avatar.split(",")[1])), (base64.b64decode(avatar.split(",")[1])),
type, type,
), ),
) )
) )
avatar = f"attachment://{filename}"
msg["embeds"][0]["author"].update( msg["embeds"][0]["author"].update(
{ {
"name": user_info["name"], "name": user_info["name"],
"url": f"{server_url}/profile/{user_info['id']}", "url": f"{server_url}/profile/{user_info['id']}",
"icon_url": f"attachment://{filename}" "icon_url": avatar or self.SPECKLE_LOGO,
if filename
else self.SPECKLE_LOGO,
} }
) )
@ -102,12 +100,12 @@ class DiscordBot(object):
def on_stream_update( def on_stream_update(
self, server_info, user_info, stream_info, webhook_info, event_info self, server_info, user_info, stream_info, webhook_info, event_info
): ):
server_url = server_info["canonicalUrl"].rstrip("/")
msg = self.get_message_template() msg = self.get_message_template()
msg["embeds"][0].update( msg["embeds"][0].update(
{ {
"title": f"Stream Updated: [{stream_info['name']}]", "title": f"Stream Updated: [{stream_info['name']}]",
"url": f"{server_info['canonicalUrl']}/streams/{stream_info['id']}", "url": f"{server_url}/streams/{stream_info['id']}",
"description": f"{user_info['name']} updated stream `{stream_info['id']}`", "description": f"{user_info['name']} updated stream `{stream_info['id']}`",
"color": COLOURS["speckle blue"], "color": COLOURS["speckle blue"],
"fields": [ "fields": [
@ -122,28 +120,31 @@ class DiscordBot(object):
"inline": True, "inline": True,
}, },
], ],
"image": {"url": f"{server_url}/preview/{stream_info['id']}"},
} }
) )
self.add_author(msg, user_info, server_info["canonicalUrl"]) self.add_author(msg, user_info, server_url)
self.send_to_discord(msg) self.send_to_discord(msg)
def on_stream_permissions_add( # def on_stream_permissions_add(
self, server_info, user_info, stream_info, webhook_info, event_info # self, server_info, user_info, stream_info, webhook_info, event_info
): # ):
msg = self.get_message_template() # msg = self.get_message_template()
def on_stream_permissions_remove( # def on_stream_permissions_remove(
self, server_info, user_info, stream_info, webhook_info, event_info # self, server_info, user_info, stream_info, webhook_info, event_info
): # ):
msg = self.get_message_template() # msg = self.get_message_template()
def on_commit_create( def on_commit_create(
self, server_info, user_info, stream_info, webhook_info, event_info self, server_info, user_info, stream_info, webhook_info, event_info
): ):
server_url = server_info["canonicalUrl"].rstrip("/")
commit_id = event_info["id"] commit_id = event_info["id"]
commit_info = event_info["commit"] commit_info = event_info["commit"]
client = SpeckleClient(host=server_info["canonicalUrl"], use_ssl=False) client = SpeckleClient(host=server_url, use_ssl=False)
client.authenticate(os.environ.get("SPECKLE_TOKEN")) client.authenticate(os.environ.get("SPECKLE_TOKEN"))
commit_obj = client.object.get(stream_info["id"], commit_info["objectId"]) commit_obj = client.object.get(stream_info["id"], commit_info["objectId"])
obj_count = getattr(commit_obj, "totalChildrenCount", "_unknown_") obj_count = getattr(commit_obj, "totalChildrenCount", "_unknown_")
@ -152,7 +153,7 @@ class DiscordBot(object):
msg["embeds"][0].update( msg["embeds"][0].update(
{ {
"title": f"Commit Created on {stream_info['name']}/{commit_info['branchName']}", "title": f"Commit Created on {stream_info['name']}/{commit_info['branchName']}",
"url": f"{server_info['canonicalUrl']}/streams/{stream_info['id']}/commits/{commit_id}", "url": f"{server_url}/streams/{stream_info['id']}/commits/{commit_id}",
"description": f"{user_info['name']} created a new commit `{commit_id}`", "description": f"{user_info['name']} created a new commit `{commit_id}`",
"color": COLOURS["futures"], "color": COLOURS["futures"],
"fields": [ "fields": [
@ -170,23 +171,25 @@ class DiscordBot(object):
"image": { "image": {
"url": None "url": None
if commit_info["branchName"] == "globals" if commit_info["branchName"] == "globals"
else f"{server_info['canonicalUrl']}/preview/{stream_info['id']}/commits/{commit_id}" else f"{server_url}/preview/{stream_info['id']}/commits/{commit_id}"
}, },
} }
) )
self.add_author(msg, user_info, server_info["canonicalUrl"]) self.add_author(msg, user_info, server_url)
self.send_to_discord(msg) self.send_to_discord(msg)
def on_commit_update( def on_commit_update(
self, server_info, user_info, stream_info, webhook_info, event_info self, server_info, user_info, stream_info, webhook_info, event_info
): ):
server_url = server_info["canonicalUrl"].rstrip("/")
commit_info = event_info["old"] commit_info = event_info["old"]
msg = self.get_message_template() msg = self.get_message_template()
msg["embeds"][0].update( msg["embeds"][0].update(
{ {
"title": f"Commit Updated on [{stream_info['name']}]/{commit_info['branchName']}", "title": f"Commit Updated on [{stream_info['name']}]/{commit_info['branchName']}",
"url": f"{server_info['canonicalUrl']}/streams/{stream_info['id']}/commits/{commit_info['id']}", "url": f"{server_url}/streams/{stream_info['id']}/commits/{commit_info['id']}",
"description": f"Commit `{commit_info['id']}` on branch `{commit_info['branchName']}` updated by {commit_info['authorName']}", "description": f"Commit `{commit_info['id']}` on branch `{commit_info['branchName']}` updated by {commit_info['authorName']}",
"color": COLOURS["spark"], "color": COLOURS["spark"],
"fields": [ "fields": [
@ -204,22 +207,23 @@ class DiscordBot(object):
"image": { "image": {
"url": None "url": None
if commit_info["branchName"] == "globals" if commit_info["branchName"] == "globals"
else f"{server_info['canonicalUrl']}/preview/{stream_info['id']}/commits/{commit_info['id']}" else f"{server_url}/preview/{stream_info['id']}/commits/{commit_info['id']}"
}, },
} }
) )
self.add_author(msg, user_info, server_info["canonicalUrl"]) self.add_author(msg, user_info, server_url)
self.send_to_discord(msg) self.send_to_discord(msg)
def on_branch_create( def on_branch_create(
self, server_info, user_info, stream_info, webhook_info, event_info self, server_info, user_info, stream_info, webhook_info, event_info
): ):
server_url = server_info["canonicalUrl"].rstrip("/")
branch = event_info["branch"] branch = event_info["branch"]
msg = self.get_message_template() msg = self.get_message_template()
msg["embeds"][0].update( msg["embeds"][0].update(
{ {
"title": f"Branch Created: [{stream_info['name']}]/{branch['name']}", "title": f"Branch Created: [{stream_info['name']}]/{branch['name']}",
"url": f"{server_info['canonicalUrl']}/streams/{stream_info['id']}/branches/{quote(branch['name'])}", "url": f"{server_url}/streams/{stream_info['id']}/branches/{quote(branch['name'])}",
"description": f"{user_info['name']} created branch `{branch['id']}` on stream _{stream_info['name']}_ (`{stream_info['id']}`)", "description": f"{user_info['name']} created branch `{branch['id']}` on stream _{stream_info['name']}_ (`{stream_info['id']}`)",
"color": COLOURS["twist"], "color": COLOURS["twist"],
"fields": [ "fields": [
@ -235,18 +239,19 @@ class DiscordBot(object):
], ],
} }
) )
self.add_author(msg, user_info, server_info["canonicalUrl"]) self.add_author(msg, user_info, server_url)
self.send_to_discord(msg) self.send_to_discord(msg)
def on_branch_update( def on_branch_update(
self, server_info, user_info, stream_info, webhook_info, event_info self, server_info, user_info, stream_info, webhook_info, event_info
): ):
server_url = server_info["canonicalUrl"].rstrip("/")
branch = event_info["new"] branch = event_info["new"]
msg = self.get_message_template() msg = self.get_message_template()
msg["embeds"][0].update( msg["embeds"][0].update(
{ {
"title": f"Branch Updated: [{stream_info['name']}]/{branch['name']}", "title": f"Branch Updated: [{stream_info['name']}]/{branch['name']}",
"url": f"{server_info['canonicalUrl']}/streams/{stream_info['id']}/branches/{quote(branch['name'])}", "url": f"{server_url}/streams/{stream_info['id']}/branches/{quote(branch['name'])}",
"description": f"{user_info['name']} updated branch `{branch['id']}` on stream _{stream_info['name']}_ (`{stream_info['id']}`)", "description": f"{user_info['name']} updated branch `{branch['id']}` on stream _{stream_info['name']}_ (`{stream_info['id']}`)",
"color": COLOURS["mantis"], "color": COLOURS["mantis"],
"fields": [ "fields": [
@ -263,21 +268,22 @@ class DiscordBot(object):
], ],
} }
) )
self.add_author(msg, user_info, server_info["canonicalUrl"]) self.add_author(msg, user_info, server_url)
self.send_to_discord(msg) self.send_to_discord(msg)
def on_branch_delete( def on_branch_delete(
self, server_info, user_info, stream_info, webhook_info, event_info self, server_info, user_info, stream_info, webhook_info, event_info
): ):
server_url = server_info["canonicalUrl"].rstrip("/")
branch = event_info["branch"] branch = event_info["branch"]
msg = self.get_message_template() msg = self.get_message_template()
msg["embeds"][0].update( msg["embeds"][0].update(
{ {
"title": f"Branch Deleted: [{stream_info['name']}]/{branch['name']}", "title": f"Branch Deleted: [{stream_info['name']}]/{branch['name']}",
"url": f"{server_info['canonicalUrl']}/streams/{stream_info['id']}/branches", "url": f"{server_url}/streams/{stream_info['id']}/branches",
"description": f"{user_info['name']} deleted branch `{branch['id']}` on stream _{stream_info['name']}_ (`{stream_info['id']}`)", "description": f"{user_info['name']} deleted branch `{branch['id']}` on stream _{stream_info['name']}_ (`{stream_info['id']}`)",
"color": COLOURS["red"], "color": COLOURS["red"],
} }
) )
self.add_author(msg, user_info, server_info["canonicalUrl"]) self.add_author(msg, user_info, server_url)
self.send_to_discord(msg) self.send_to_discord(msg)