@@ -535,6 +535,7 @@ class UserInput(graphene.InputObjectType):
535535
536536# MARK: - Friendship
537537
538+
538539class Friendship (SQLAlchemyObjectType ):
539540 class Meta :
540541 model = FriendshipModel
@@ -636,11 +637,14 @@ class Query(graphene.ObjectType):
636637 )
637638 get_user_friends = graphene .List (
638639 User , user_id = graphene .Int (required = True ), description = "Get all friends for a user."
640+ User , user_id = graphene .Int (required = True ), description = "Get all friends for a user."
639641 )
640642 get_capacity_reminder_by_id = graphene .Field (
641643 CapacityReminder , id = graphene .Int (required = True ), description = "Get a specific capacity reminder by its ID."
644+ CapacityReminder , id = graphene .Int (required = True ), description = "Get a specific capacity reminder by its ID."
642645 )
643646 get_all_capacity_reminders = graphene .List (CapacityReminder , description = "Get all capacity reminders." )
647+ get_all_capacity_reminders = graphene .List (CapacityReminder , description = "Get all capacity reminders." )
644648
645649 def resolve_get_all_gyms (self , info ):
646650 query = Gym .get_query (info )
@@ -723,13 +727,23 @@ def resolve_get_user_friends(self, info, user_id):
723727 .filter ((FriendshipModel .user_id == user_id ) & (FriendshipModel .is_accepted == True ))
724728 .all ()
725729 )
730+ direct_friendships = (
731+ Friendship .get_query (info )
732+ .filter ((FriendshipModel .user_id == user_id ) & (FriendshipModel .is_accepted == True ))
733+ .all ()
734+ )
726735
727736 # Reverse friendships where user is the recipient
728737 reverse_friendships = (
729738 Friendship .get_query (info )
730739 .filter ((FriendshipModel .friend_id == user_id ) & (FriendshipModel .is_accepted == True ))
731740 .all ()
732741 )
742+ reverse_friendships = (
743+ Friendship .get_query (info )
744+ .filter ((FriendshipModel .friend_id == user_id ) & (FriendshipModel .is_accepted == True ))
745+ .all ()
746+ )
733747
734748 friend_ids = set ()
735749 for friendship in direct_friendships :
@@ -741,6 +755,7 @@ def resolve_get_user_friends(self, info, user_id):
741755 # Query for all friends at once
742756 return User .get_query (info ).filter (UserModel .id .in_ (friend_ids )).all ()
743757
758+
744759 @jwt_required ()
745760 def resolve_get_capacity_reminder_by_id (self , info , id ):
746761 reminder = CapacityReminder .get_query (info ).filter (CapacityReminderModel .id == id ).first ()
@@ -750,6 +765,7 @@ def resolve_get_capacity_reminder_by_id(self, info, id):
750765
751766 return reminder
752767
768+
753769 @jwt_required ()
754770 def resolve_get_all_capacity_reminders (self , info ):
755771 query = CapacityReminder .get_query (info )
@@ -945,6 +961,7 @@ def mutate(self, info, name):
945961 db_session .commit ()
946962 return giveaway
947963
964+
948965class AddFriend (graphene .Mutation ):
949966 class Arguments :
950967 user_net_id = graphene .String (required = True , description = "The Net ID of the user." )
@@ -968,6 +985,7 @@ def mutate(self, info, user_net_id, friend_net_id):
968985 db_session .commit ()
969986 return user
970987
988+
971989class RemoveFriend (graphene .Mutation ):
972990 class Arguments :
973991 user_net_id = graphene .String (required = True , description = "The Net ID of the user." )
@@ -991,6 +1009,7 @@ def mutate(self, info, user_net_id, friend_net_id):
9911009 db_session .commit ()
9921010 return user
9931011
1012+
9941013class SetWorkoutGoals (graphene .Mutation ):
9951014 class Arguments :
9961015 user_id = graphene .Int (required = True , description = "The ID of the user." )
@@ -1217,6 +1236,7 @@ def mutate(self, info, reminder_id, new_gyms, days_of_week, new_capacity_thresho
12171236 try :
12181237 response = messaging .unsubscribe_from_topic (reminder .fcm_token , topic )
12191238 logging .info ("Unsubscribe %s from %s" , reminder .fcm_token [:12 ], topic )
1239+ logging .info ("Unsubscribe %s from %s" , reminder .fcm_token [:12 ], topic )
12201240 for error in response .errors :
12211241 logging .warning (
12221242 "Error unsubscribing %s from %s -> reason: %s" , reminder .fcm_token [:12 ], topic , error .reason
@@ -1233,6 +1253,7 @@ def mutate(self, info, reminder_id, new_gyms, days_of_week, new_capacity_thresho
12331253 try :
12341254 response = messaging .subscribe_to_topic (reminder .fcm_token , topic )
12351255 logging .info ("Resubscribing %s to %s" , reminder .fcm_token [:12 ], topic )
1256+ logging .info ("Resubscribing %s to %s" , reminder .fcm_token [:12 ], topic )
12361257 if response .success_count == 0 :
12371258 raise Exception (response .errors [0 ].reason )
12381259 except Exception as error :
@@ -1267,8 +1288,10 @@ def mutate(self, info, reminder_id):
12671288 try :
12681289 response = messaging .unsubscribe_from_topic (reminder .fcm_token , topic )
12691290 logging .info ("Unsubscribe %s from %s" , reminder .fcm_token [:12 ], topic )
1291+ logging .info ("Unsubscribe %s from %s" , reminder .fcm_token [:12 ], topic )
12701292 if response .success_count == 0 :
12711293 raise Exception (response .errors [0 ].reason )
1294+ raise Exception (response .errors [0 ].reason )
12721295 except Exception as error :
12731296 raise GraphQLError (f"Error unsubscribing from topic { topic } : { error } " )
12741297
@@ -1278,6 +1301,7 @@ def mutate(self, info, reminder_id):
12781301 return reminder
12791302
12801303
1304+
12811305class AddFriend (graphene .Mutation ):
12821306 class Arguments :
12831307 user_id = graphene .Int (required = True )
@@ -1305,6 +1329,14 @@ def mutate(self, info, user_id, friend_id):
13051329 )
13061330 .first ()
13071331 )
1332+ existing = (
1333+ Friendship .get_query (info )
1334+ .filter (
1335+ ((FriendshipModel .user_id == user_id ) & (FriendshipModel .friend_id == friend_id ))
1336+ | ((FriendshipModel .user_id == friend_id ) & (FriendshipModel .friend_id == user_id ))
1337+ )
1338+ .first ()
1339+ )
13081340
13091341 if existing :
13101342 raise GraphQLError ("Friendship already exists." )
@@ -1317,6 +1349,7 @@ def mutate(self, info, user_id, friend_id):
13171349 return friendship
13181350
13191351
1352+
13201353class AcceptFriendRequest (graphene .Mutation ):
13211354 class Arguments :
13221355 friendship_id = graphene .Int (required = True )
@@ -1341,6 +1374,7 @@ def mutate(self, info, friendship_id):
13411374
13421375 return friendship
13431376
1377+
13441378class RemoveFriend (graphene .Mutation ):
13451379 class Arguments :
13461380 user_id = graphene .Int (required = True )
@@ -1359,6 +1393,14 @@ def mutate(self, info, user_id, friend_id):
13591393 )
13601394 .first ()
13611395 )
1396+ friendship = (
1397+ Friendship .get_query (info )
1398+ .filter (
1399+ ((FriendshipModel .user_id == user_id ) & (FriendshipModel .friend_id == friend_id ))
1400+ | ((FriendshipModel .user_id == friend_id ) & (FriendshipModel .friend_id == user_id ))
1401+ )
1402+ .first ()
1403+ )
13621404
13631405 if not friendship :
13641406 raise GraphQLError ("Friendship not found." )
@@ -1370,6 +1412,7 @@ def mutate(self, info, user_id, friend_id):
13701412 return RemoveFriend (success = True )
13711413
13721414
1415+
13731416class GetPendingFriendRequests (graphene .Mutation ):
13741417 class Arguments :
13751418 user_id = graphene .Int (required = True )
@@ -1389,6 +1432,11 @@ def mutate(self, info, user_id):
13891432 .filter ((FriendshipModel .friend_id == user_id ) & (FriendshipModel .is_accepted == False ))
13901433 .all ()
13911434 )
1435+ pending = (
1436+ Friendship .get_query (info )
1437+ .filter ((FriendshipModel .friend_id == user_id ) & (FriendshipModel .is_accepted == False ))
1438+ .all ()
1439+ )
13921440
13931441 return GetPendingFriendRequests (pending_requests = pending )
13941442
@@ -1405,6 +1453,7 @@ class Mutation(graphene.ObjectType):
14051453 refresh_access_token = RefreshAccessToken .Field (description = "Refreshes the access token." )
14061454 create_report = CreateReport .Field (description = "Creates a new report." )
14071455 delete_report = DeleteReport .Field (description = "Deletes a report by ID." )
1456+ delete_report = DeleteReport .Field (description = "Deletes a report by ID." )
14081457 delete_user = DeleteUserById .Field (description = "Deletes a user by ID." )
14091458 add_friend = AddFriend .Field (description = "Adds a friend to a user." )
14101459 remove_friend = RemoveFriend .Field (description = "Removes a friend from a user." )
@@ -1417,6 +1466,8 @@ class Mutation(graphene.ObjectType):
14171466 get_pending_friend_requests = GetPendingFriendRequests .Field (
14181467 description = "Get all pending friend requests for a user."
14191468 )
1469+ description = "Get all pending friend requests for a user."
1470+ )
14201471
14211472
14221473schema = graphene .Schema (query = Query , mutation = Mutation )
0 commit comments