Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ type Capacity {

type CapacityReminder {
id: ID!
fcmToken: String!
gyms: [CapacityReminderGym]!
capacityThreshold: Int!
daysOfWeek: [DayOfWeekEnum]!
Expand Down Expand Up @@ -227,7 +226,7 @@ type Mutation {
createReport(createdAt: DateTime!, description: String!, gymId: Int!, issue: String!): CreateReport
deleteUser(userId: Int!): User
createCapacityReminder(capacityPercent: Int!, daysOfWeek: [String]!, fcmToken: String!, gyms: [String]!): CapacityReminder
editCapacityReminder(capacityPercent: Int!, daysOfWeek: [String]!, gyms: [String]!, reminderId: Int!): CapacityReminder
editCapacityReminder(daysOfWeek: [String]!, newCapacityThreshold: Int!, newGyms: [String]!, reminderId: Int!): CapacityReminder
deleteCapacityReminder(reminderId: Int!): CapacityReminder
addFriend(friendId: Int!, userId: Int!): Friendship
acceptFriendRequest(friendshipId: Int!): Friendship
Expand Down Expand Up @@ -274,6 +273,8 @@ type Query {
getUserStreak(id: Int!): JSONString
getHourlyAverageCapacitiesByFacilityId(facilityId: Int): [HourlyAverageCapacity]
getUserFriends(userId: Int!): [User]
getCapacityReminderById(id: Int!): CapacityReminder
getAllCapacityReminders: [CapacityReminder]
}

type RefreshAccessToken {
Expand Down
37 changes: 33 additions & 4 deletions src/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
import json
import os
from firebase_admin import messaging
import logging


def resolve_enum_value(entry):
"""Return the raw value for Enum objects while leaving plain strings untouched."""
return getattr(entry, "value", entry)

# MARK: - Gym

Expand Down Expand Up @@ -913,14 +919,25 @@ def mutate(self, info, reminder_id, new_gyms, days_of_week, new_capacity_thresho

# Unsubscribe from old reminders
topics = [
f"{gym}_{day}_{reminder.capacity_threshold}" for gym in reminder.gyms for day in reminder.days_of_week
f"{resolve_enum_value(gym)}_{resolve_enum_value(day)}_{reminder.capacity_threshold}"
for gym in reminder.gyms
for day in reminder.days_of_week
]

for topic in topics:
try:
response = messaging.unsubscribe_from_topic(reminder.fcm_token, topic)
logging.info(
"Unsubscribe %s from %s",
reminder.fcm_token[:12],
topic,
)
for error in response.errors:
logging.warning(
"Error unsubscribing %s from %s -> reason: %s", reminder.fcm_token[:12], topic, error.reason
)
if response.success_count == 0:
raise Exception(response.errors[0].reason)
raise Exception(response.errors[0].reason)
except Exception as error:
raise GraphQLError(f"Error subscribing to topic: {error}")

Expand All @@ -930,8 +947,13 @@ def mutate(self, info, reminder_id, new_gyms, days_of_week, new_capacity_thresho
for topic in topics:
try:
response = messaging.subscribe_to_topic(reminder.fcm_token, topic)
logging.info(
"Resubscribing %s to %s",
reminder.fcm_token[:12],
topic,
)
if response.success_count == 0:
raise Exception(response.errors[0].reason)
raise Exception(response.errors[0].reason)
except Exception as error:
raise GraphQLError(f"Error subscribing to topic: {error}")

Expand All @@ -955,12 +977,19 @@ def mutate(self, info, reminder_id):
raise GraphQLError("CapacityReminder not found.")

topics = [
f"{gym}_{day}_{reminder.capacity_threshold}" for gym in reminder.gyms for day in reminder.days_of_week
f"{resolve_enum_value(gym)}_{resolve_enum_value(day)}_{reminder.capacity_threshold}"
for gym in reminder.gyms
for day in reminder.days_of_week
]

for topic in topics:
try:
response = messaging.unsubscribe_from_topic(reminder.fcm_token, topic)
logging.info(
"Unsubscribe %s from %s",
reminder.fcm_token[:12],
topic,
)
if response.success_count == 0:
raise Exception(response.errors[0].reason)
except Exception as error:
Expand Down
Loading