APIRouteCollection

protocol APIRouteCollection : APICollection

Guards

Post-commit Side Effects

  • dispatchEffect(on:fileID:line:_:) Extension method, asynchronous

    Runs work that can’t be rolled back (Redis notifications, socket pushes, cache refreshes, appointment bookkeeping) for a change that has already been committed. A failure is logged, not thrown: throwing would give the client an error for a request that succeeded, and a retry would then duplicate the content.

    Claude got cute with the fileID and line stuff. Until we see how this pattern goes, I’ll allow it. It’s better than the long form descriptions of everything it was doing.

    Declaration

    Swift

    func dispatchEffect(on req: Request, fileID: String = #fileID, line: Int = #line, _ work: () async throws -> Void) async
  • dispatchEffects(on:fileID:line:_:) Extension method, asynchronous

    Runs several independent effects in order, each with its own failure handling (see dispatchEffect): one failing doesn’t skip the rest.

    Declaration

    Swift

    func dispatchEffects(on req: Request, fileID: String = #fileID, line: Int = #line, _ effects: (() async throws -> Void)...) async

Notification Management

  • addNotifications(users:type:info:creatorID:on:) Extension method, asynchronous

    Declaration

    Swift

    func addNotifications(users: [UUID], type: NotificationType, info: String, creatorID: UUID? = nil, on req: Request)
    	async throws
  • Declaration

    Swift

    func bookkeepUserAddedToChat(
    	req: Request,
    	msgID: UUID,
    	chatType: FezType,
    	users: [UUID],
    	group: inout ThrowingTaskGroup<Void, Error>
    ) -> [UUID]
  • Declaration

    Swift

    func bookkeepNewChatMessage(
    	req: Request,
    	msgID: UUID,
    	chatType: FezType,
    	users: [UUID],
    	group: inout ThrowingTaskGroup<Void, Error>,
    	creatorID: UUID? = nil
    ) -> [UUID]
  • Declaration

    Swift

    func subtractNotifications(users: [UUID], type: NotificationType, subtractCount: Int = 1, on req: Request)
    	async throws
  • deleteFezNotifications(userIDs:fez:on:) Extension method, asynchronous

    Declaration

    Swift

    func deleteFezNotifications(userIDs: [UUID], fez: FriendlyFez, on req: Request) async throws
  • markNotificationViewed(user:type:on:) Extension method, asynchronous

    Declaration

    Swift

    func markNotificationViewed(user: UserCacheData, type: NotificationType, on req: Request) async throws
  • markNotificationViewed(for:type:on:) Extension method, asynchronous

    Declaration

    Swift

    func markNotificationViewed(for users: [UserCacheData], type: NotificationType, on req: Request) async throws
  • storeNextFollowedEvent(userID:on:) Extension method, asynchronous

    Declaration

    Swift

    func storeNextFollowedEvent(userID: UUID, on req: Request) async throws -> (Date, UUID)?
  • storeNextJoinedAppointment(userID:on:) Extension method, asynchronous

    Declaration

    Swift

    @discardableResult
    func storeNextJoinedAppointment(userID: UUID, on req: Request) async throws -> (Date, UUID)?
  • processImages(_:usage:maxImages:on:) Extension method, asynchronous

    Takes an an array of ImageUploadData as input. Some of the input elements may be new image Data that needs procssing; some of the input elements may refer to already-processed images in our image store. Once all the ImageUploadData elements are processed, returns a [String] containing the filenames where al the images are stored. The use case here is for editing existing content with image attachments in a way that prevents re-uploading of photos that are already on the server.

    • req: The incoming Request, on which this processing must run.

    Declaration

    Swift

    func processImages(_ images: [ImageUploadData], usage: ImageUsage, maxImages: Int = 4, on req: Request) async throws -> [String]

    Parameters

    images

    The images in ImageUploadData format.

    usage

    The type of model using the image content.

    maxImages

    Maximum number of images allowed. Defaults to 4 for backward compatibility.

    Return Value

    The generated names of the stored files.

  • processImagesTracked(_:usage:maxImages:on:) Extension method, asynchronous

    Like processImages, but also reports which of the returned filenames were newly written by this call, as opposed to being existing filenames that were passed through. Callers that go on to write the filenames to the database use this to know which files to delete if that write fails. If processing fails partway through, files already written by this call are deleted before the error is rethrown.

    Declaration

    Swift

    func processImagesTracked(_ images: [ImageUploadData], usage: ImageUsage, maxImages: Int = 4, on req: Request) async throws
    	-> (filenames: [String], created: [String])
  • withProcessedImages(_:usage:maxImages:on:_:) Extension method, asynchronous

    Processes the images, then runs body with the resulting filenames. body should contain the database transaction that stores the filenames and nothing that must not be undone–if it throws, the image files this call newly wrote are deleted so a failed request leaves nothing on disk. Run side effects that can’t be rolled back (notifications, sockets) after this returns.

    Declaration

    Swift

    func withProcessedImages<T>(_ images: [ImageUploadData], usage: ImageUsage, maxImages: Int = 4, on req: Request,
    		_ body: ([String]) async throws -> T) async throws -> T
  • deleteImageFiles(_:on:) Extension method

    Deletes the full-size and thumbnail files for images that were written but never referenced by committed data. Failures are logged and otherwise ignored; an orphaned file is no worse than before this call.

    Declaration

    Swift

    func deleteImageFiles(_ filenames: [String], on req: Request)
  • processImage(data:usage:on:) Extension method, asynchronous

    Takes an optional image in Data form as input, produces full and thumbnail JPEG vrsions, places both the thumbnail and full image in their respective directories, and returns the generated name of the file on success, an empty string otherwise.

    • req: The incoming Request, on which this processing must run.

    Declaration

    Swift

    func processImage(data: Data?, usage: ImageUsage, on req: Request) async throws -> String?

    Parameters

    data

    The uploaded image in Data format.

    usage

    The type of model using the image content.

    Return Value

    The generated name of the stored file, or nil.

  • regenerateThumbnail(for:on:) Extension method, asynchronous

    Declaration

    Swift

    func regenerateThumbnail(for imageSource: URL, on req: Request) async throws
  • archiveImage(_:on:) Extension method

    Archives an image that is no longer needed other than for accountability tracking, by removing the full-sized image and moving the thumbnail into the archive/ subdirectory of the provided base image directory.

    This is a synchronous operation, until such time as we can use SwiftNIO 2’s asynchronous file I/O.

    Declaration

    Swift

    func archiveImage(_ image: String, on req: Request)

    Parameters

    image

    The filename of the image.

    imageDir

    The base image directory path for the image’s context.