Skip to content

Commit

Permalink
Delete notification markers when we are deleting notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
mczachurski committed Oct 5, 2024
1 parent fcfda0d commit 8f96156
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Sources/VernissageServer/Errors/StatusError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ enum StatusError: String, Error {
case cannotReblogMentionedStatus
case cannotReblogComments
case cannotAddCommentWithoutCommentedStatus
case cannotDeleteStatus
}

extension StatusError: LocalizedTerminateError {
var status: HTTPResponseStatus {
switch self {
case .cannotReblogMentionedStatus, .cannotReblogComments:
return .forbidden
case .cannotDeleteStatus:
return .internalServerError
default:
return .badRequest
}
Expand All @@ -35,6 +38,7 @@ extension StatusError: LocalizedTerminateError {
case .cannotReblogMentionedStatus: return "Cannot reblog status with mentioned visibility."
case .cannotReblogComments: return "Cannot reblog comments."
case .cannotAddCommentWithoutCommentedStatus: return "Cannot add comment without commented status."
case .cannotDeleteStatus: return "Error occurred while deleting status."
}
}

Expand Down
12 changes: 11 additions & 1 deletion Sources/VernissageServer/Services/StatusesService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -948,8 +948,18 @@ final class StatusesService: StatusesServiceType {
.field(\.$id)
.all()

var errorOccurred = false
for status in statuses {
try await self.delete(id: status.requireID(), on: context.application.db)
do {
try await self.delete(id: status.requireID(), on: context.application.db)
} catch {
errorOccurred = true
context.logger.error("Failed to delete status: '\(status.stringId() ?? "<unkown>")': \(error).")
}
}

if errorOccurred {
throw StatusError.cannotDeleteStatus
}
}

Expand Down
14 changes: 11 additions & 3 deletions Sources/VernissageServer/Services/UsersService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ final class UsersService: UsersServiceType {
func delete(localUser userId: Int64, on context: QueueContext) async throws {
let statusesService = context.application.services.statusesService

// We have to delete all user's statuses from local database.
// We have to try to delete all user's statuses from local database.
try await statusesService.delete(owner: userId, on: context)

// We have to delete all user's follows.
Expand All @@ -481,7 +481,7 @@ final class UsersService: UsersServiceType {
.filter(\.$source.$id == userId)
}
.all()
let sourceIds = follows.map({ $0.$source.id })
let sourceIds = follows.map { $0.$source.id }

// We have to delete all statuses featured by the user.
let featuredStatuses = try await FeaturedStatus.query(on: context.application.db)
Expand All @@ -493,7 +493,7 @@ final class UsersService: UsersServiceType {
.filter(\.$user.$id == userId)
.all()

// We have to delete all user's notifications.
// We have to delete all user's notifications and notifications to other users.
let notifications = try await Notification.query(on: context.application.db)
.group(.or) { group in
group
Expand All @@ -502,6 +502,13 @@ final class UsersService: UsersServiceType {
}
.all()

// We have to delete notification markers which points to notification to delete.
// Maybe in the future we can figure out something more clever.
let notificationIds = try notifications.map { try $0.requireID() }
let notificationMarkers = try await NotificationMarker.query(on: context.application.db)
.filter(\.$notification.$id ~~ notificationIds)
.all()

// We have to delete all user's reports.
let reports = try await Report.query(on: context.application.db)
.group(.or) { group in
Expand Down Expand Up @@ -554,6 +561,7 @@ final class UsersService: UsersServiceType {
try await userAliases.delete(on: transaction)
try await follows.delete(on: transaction)
try await notificationMarker.delete(on: transaction)
try await notificationMarkers.delete(on: transaction)
try await notifications.delete(on: transaction)
try await reports.delete(on: transaction)
try await trendingUser.delete(on: transaction)
Expand Down

0 comments on commit 8f96156

Please sign in to comment.