Skip to content

Commit

Permalink
Fixes #235.
Browse files Browse the repository at this point in the history
PostSearchData has lost its 'totalPosts', 'start' and 'limit' members and now has a Paginator member, which does pretty much the same thing.

This breaks current apps, and is on the API changelist.
  • Loading branch information
challf committed Jun 26, 2024
1 parent 02da8e5 commit c62c521
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 42 deletions.
16 changes: 3 additions & 13 deletions Sources/swiftarr/Controllers/ForumController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -851,19 +851,9 @@ struct ForumController: APIRouteCollection {
try await markNotificationViewed(user: cacheUser, type: .forumMention(0), on: req)
}
}
let postData = try await buildPostData(
postFilteredPosts,
userID: cacheUser.userID,
on: req,
mutewords: cacheUser.mutewords
)
return try await PostSearchData(
queryString: req.url.query ?? "",
totalPosts: totalPostsFound,
start: start,
limit: limit,
posts: postData
)
let postData = try await buildPostData(postFilteredPosts, userID: cacheUser.userID, on: req, mutewords: cacheUser.mutewords)
return try await PostSearchData(queryString: req.url.query ?? "", posts: postData,
paginator: Paginator(total: totalPostsFound, start: start, limit: limit))
}

// MARK: POST and DELETE actions
Expand Down
12 changes: 4 additions & 8 deletions Sources/swiftarr/Controllers/Structs/ControllerStructs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ extension FezContentData: RCFValidatable {
///
///
public struct FezListData: Content {
/// Pagination into the results set..
/// Pagination into the results set.
var paginator: Paginator
///The fezzes in the result set.
var fezzes: [FezData]
Expand Down Expand Up @@ -1315,15 +1315,11 @@ extension PostData {
public struct PostSearchData: Content {
/// The search query used to create these results.
var queryString: String
/// The total number of posts in the result set. The actual # of results returned may be fewer than this, even if we return 'complete' results. This is due to additional filtering that
/// is done after the database query. See notes on `ContentFilterable.filterForMention(of:)`
var totalPosts: Int
/// The index into totalPosts of the first post in the `posts` array. 0 is the index of the first result. This number is usually a multiple of `limit` and indicates the page of results.
var start: Int
/// The number of posts the server attempted to gather. posts.count may be less than this number if posts were filtered out by post-query filtering, or if start + limit > totalPosts.
var limit: Int
/// The posts in the forum.
var posts: [PostData]
/// Pagination into the results set. Because `/forum/post/search` filters the result set after the db query, `posts.count` could be less than `paginator.limit`
/// even if we're not at the end of the results. To get the next 'page' of results, be sure to add `limit` to `start`; don't add`posts.count`.
var paginator: Paginator
}

/// Used to return a `ForumPost`'s data with full user `LikeType` info.
Expand Down
2 changes: 1 addition & 1 deletion Sources/swiftarr/Resources/Views/Forums/forum.html
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ <h5 class="modal-title" id="helpModalTitle">Help</h5>
<div class="container-fluid collapse swiftarr-searchbar" id="searchBar">
<form action="/forum/search">
<input type="hidden" name="searchType" value="posts"/>
#if(forum.forumID != nil):<input type="hidden" name="forumID" value="#(forum.forumID)" />#endif
#if(forum.forumID != nil):<input type="hidden" name="forum" value="#(forum.forumID)" />#endif
<div class="row justify-content-between">
<div class="col flex-grow-1 pe-0">
<input class="form-control" type="search" name="search" value="" placeholder="Search This Forum" aria-label="Search" autocapitalize="off" required>
Expand Down
2 changes: 1 addition & 1 deletion Sources/swiftarr/Resources/Views/Forums/forums.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ <h5 class="modal-title" id="helpModalTitle">Help</h5>
#export("searchform"):
<div class="container-fluid collapse swiftarr-searchbar" id="searchBar">
<form action="/forum/search">
<input type="hidden" name="categoryID" value="#(forums.categoryID)" />
<input type="hidden" name="category" value="#(forums.categoryID)" />
<div class="btn-group w-100 mb-1" role="group" aria-label="Toggle for searching Forums or Posts">
<input type="radio" class="btn-check" name="searchType" value="forums" id="forumsButton" autocomplete="off" checked>
<label class="btn btn-outline-success flex-grow-0 col-6 ms-0" for="forumsButton">Forums</label>
Expand Down
33 changes: 14 additions & 19 deletions Sources/swiftarr/Site/SiteForumController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ struct SearchFormData: Content {
var search: String?
var creator: String?
var creatorid: String?
var categoryID: UUID?
var forumID: UUID?
var category: UUID?
var forum: UUID?
}

// Used for Forum Search, Favorite Forums, Recent Forums and Forums You Created pages.
Expand Down Expand Up @@ -199,27 +199,27 @@ struct PostSearchPageContext: Encodable {
switch searchType {
case .userMentions:
title = "Posts Mentioning You"
filterDescription = "\(posts.totalPosts) Posts Mentioning You"
filterDescription = "\(posts.paginator.total) Posts Mentioning You"
paginatorClosure = { pageIndex in
"/forumpost/mentions?start=\(pageIndex * posts.limit)&limit=\(posts.limit)"
"/forumpost/mentions?start=\(pageIndex * posts.paginator.limit)&limit=\(posts.paginator.limit)"
}
case .owned:
title = "Your Forum Posts"
filterDescription = "Your \(posts.totalPosts) Posts"
filterDescription = "Your \(posts.paginator.total) Posts"
paginatorClosure = { pageIndex in
"/forumpost/owned?start=\(pageIndex * posts.limit)&limit=\(posts.limit)"
"/forumpost/owned?start=\(pageIndex * posts.paginator.limit)&limit=\(posts.paginator.limit)"
}
case .favorite:
title = "Favorite Posts"
filterDescription = "\(posts.totalPosts) Favorite Posts"
filterDescription = "\(posts.paginator.total) Favorite Posts"
paginatorClosure = { pageIndex in
"/forumpost/favorite?start=\(pageIndex * posts.limit)&limit=\(posts.limit)"
"/forumpost/favorite?start=\(pageIndex * posts.paginator.limit)&limit=\(posts.paginator.limit)"
}
case .textSearch:
title = "Forum Post Search"
filterDescription = "\(posts.totalPosts) Posts with \"\(formData?.search ?? "search text")\""
filterDescription = "\(posts.paginator.total) Posts with \"\(formData?.search ?? "search text")\""
paginatorClosure = { pageIndex in
"/forum/search?search=\(formData?.search ?? "")&searchType=posts&start=\(pageIndex * posts.limit)&limit=\(posts.limit)"
"/forum/search?search=\(formData?.search ?? "")&searchType=posts&start=\(pageIndex * posts.paginator.limit)&limit=\(posts.paginator.limit)"
}
case .direct:
let searchParams = try req.query.decode(ForumPostSearchQueryOptions.self)
Expand Down Expand Up @@ -251,12 +251,7 @@ struct PostSearchPageContext: Encodable {
self.postSearch = posts
self.searchType = searchType
self.formData = formData
paginator = .init(
start: posts.start,
total: Int(posts.totalPosts),
limit: posts.limit,
urlForPage: paginatorClosure
)
paginator = .init(posts.paginator, urlForPage: paginatorClosure)
}
}

Expand Down Expand Up @@ -882,7 +877,7 @@ struct SiteForumController: SiteControllerUtils {
filterDesc.append(contentsOf: " with \"\(searchStr)\"")
}
var ctx = try ForumsSearchPageContext(req, forums: responseData, searchType: .textSearch, filterDesc: filterDesc, formData: formData)
if let categoryID = formData.categoryID {
if let categoryID = formData.category {
let catResponse = try await apiQuery(req, endpoint: "/forum/categories", query: [URLQueryItem(name: "cat", value: categoryID.uuidString)])
let catInfo = try catResponse.content.decode([CategoryData].self)
ctx.categoryData = catInfo.first
Expand All @@ -894,11 +889,11 @@ struct SiteForumController: SiteControllerUtils {
let response = try await apiQuery(req, endpoint: "/forum/post/search", passThroughQuery: true)
let responseData = try response.content.decode(PostSearchData.self)
var ctx = try PostSearchPageContext(req, posts: responseData, searchType: .textSearch, formData: formData)
if let forumID = formData.forumID {
if let forumID = formData.forum {
let forumResponse = try await apiQuery(req, endpoint: "/forum/\(forumID)", query: [URLQueryItem(name: "limit", value: "0")])
ctx.forumData = try forumResponse.content.decode(ForumData.self)
}
if let categoryID = ctx.forumData?.categoryID ?? formData.categoryID {
if let categoryID = ctx.forumData?.categoryID ?? formData.category {
let catResponse = try await apiQuery(req, endpoint: "/forum/categories", query: [URLQueryItem(name: "cat", value: categoryID.uuidString)])
let catInfo = try catResponse.content.decode([CategoryData].self)
ctx.categoryData = catInfo.first
Expand Down
3 changes: 3 additions & 0 deletions docs/Swiftarr/API Changelist.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,6 @@ New Micro Karaoke feature. API changes include:
* `UserNotificationData` now includes `nextJoinedLFGID` and `nextJoinedLFGTime`.
* `SettingsAdminData` now includes `upcomingEventNotificationSeconds`, `upcomingEventNotificationSetting`, and `upcomingLFGNotificationSetting`.
* New `SocketNotificationType` of `joinedLFGStarting`.

## Jun 25, 2024
* `PostSearchData` now uses `Paginator` to report pagination data--previously the same data was reported via top-level ints.

0 comments on commit c62c521

Please sign in to comment.