Skip to content

Commit

Permalink
Make phonecalls only succeed callee has favorited caller. (#277)
Browse files Browse the repository at this point in the history
Due to vapor/vapor#3155, the server-mediated path won't return the correct error message when attempting to call someone who hasn't favorited you (although the call does fail correctly and doesn't ring the callee's phone). Works correctly in the direct-call path.

Also, freaked out when I saw 5 "Sending incoming phonecall to callee." messages in a row. Turns out it's because I had 5 devices running logged in as that user. Still, this logging is better.

Co-authored-by: Chall Fry <chall@challfry.com>
  • Loading branch information
challfry and challf committed Mar 3, 2024
1 parent f4584c2 commit f4c8811
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Sources/swiftarr/Controllers/PhonecallController.swift
Expand Up @@ -142,6 +142,10 @@ struct PhonecallController: APIRouteCollection {
if callee.getMutes().contains(caller.userID) || callee.getBlocks().contains(caller.userID) {
throw Abort(.badRequest, reason: "Cannot call this user.")
}
guard let _ = try await UserFavorite.query(on: req.db).filter(\.$user.$id == callee.userID)
.filter(\.$favorite.$id == caller.userID).first() else {
throw Abort(.badRequest, reason: "Cannot call a user who has not made you a favorite user.")
}

let calleeNotificationSockets = req.webSocketStore.getSockets(calleeID)
guard !calleeNotificationSockets.isEmpty else {
Expand Down Expand Up @@ -205,6 +209,10 @@ struct PhonecallController: APIRouteCollection {
if callee.getMutes().contains(caller.userID) || callee.getBlocks().contains(caller.userID) {
throw Abort(.badRequest, reason: "Cannot call this user.")
}
guard let _ = try await UserFavorite.query(on: req.db).filter(\.$user.$id == callee.userID)
.filter(\.$favorite.$id == caller.userID).first() else {
throw Abort(.badRequest, reason: "Cannot call a user who has not made you a favorite user.")
}

// Make sure we can notify the callee
let callerNotificationSockets = req.webSocketStore.getSockets(calleeID)
Expand Down Expand Up @@ -250,8 +258,8 @@ struct PhonecallController: APIRouteCollection {
// Send incoming call notification to all of the callee's devices
let msgStruct = SocketNotificationData(callID: callID, caller: caller.makeHeader(), callerAddr: nil)
if let jsonData = try? encoder.encode(msgStruct), let jsonDataStr = String(data: jsonData, encoding: .utf8) {
req.logger.log(level: .notice, "Sending incoming phonecall to callee's \(calleeNotificationSockets.count) devices.")
calleeNotificationSockets.forEach { userSocket in
req.logger.log(level: .notice, "Sending incoming phonecall to callee.")
userSocket.socket.send(jsonDataStr)
}
}
Expand Down

0 comments on commit f4c8811

Please sign in to comment.