Skip to content

Commit

Permalink
use dict instead of list
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanjma committed Jul 17, 2024
1 parent 540cba6 commit 1346e56
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions happiness-backend/api/util/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from api.models.models import Happiness, User

# temporary table for recently created entries
# (use lists for vales since 1 happiness entry can be sent to multiple webhooks)
db_discord_map = defaultdict(list)
# (use dict for vales since 1 happiness entry can be sent to multiple webhooks)
db_discord_map = defaultdict(dict)

def process_webhooks(user: User, happiness: Happiness, on_edit=False):
if current_app.config["TESTING"]: return
Expand Down Expand Up @@ -44,9 +44,8 @@ def send_webhook(user: User, happiness: Happiness, url: str, on_edit: bool):
}
res = requests.post(url + '?wait=True', json=payload)
print(f"webhook post sent: {res.status_code}, {res.reason}")
db_discord_map[happiness.id].append(res.json()["id"])
db_discord_map[happiness.id][url] = res.json()["id"]
# for (recent) entry edits: look up discord msg id and send updated entry
elif happiness.id in db_discord_map.keys():
for msg_id in db_discord_map[happiness.id]:
res = requests.patch(f'{url}/messages/{msg_id}', json=payload)
print(f"webhook edit sent: {res.status_code}, {res.reason}")
res = requests.patch(f'{url}/messages/{db_discord_map[happiness.id][url]}', json=payload)
print(f"webhook edit sent: {res.status_code}, {res.reason}")

0 comments on commit 1346e56

Please sign in to comment.