Skip to content
Snippets Groups Projects
Commit 2b3db8f3 authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Filter contacts by blocked and banned status

parent f243d5ba
Branches
Tags
1 merge request!29Blocked and banned contacts
......@@ -12,6 +12,8 @@ extension Contact: FetchableRecord, PersistableRecord {
case photo
case authStatus
case isRecent
case isBlocked
case isBanned
case createdAt
}
......@@ -58,6 +60,14 @@ extension Contact: FetchableRecord, PersistableRecord {
request = request.filter(Column.isRecent == isRecent)
}
if let isBlocked = query.isBlocked {
request = request.filter(Column.isBlocked == isBlocked)
}
if let isBanned = query.isBanned {
request = request.filter(Column.isBanned == isBanned)
}
switch query.sortBy {
case .username(desc: false):
request = request.order(Column.username)
......
......@@ -173,6 +173,14 @@ extension Contact {
/// If `true`, only recent contacts are included.
/// If `false`, only non-recent contacts are included.
/// If `nil` (default), the filter is not used.
/// - isBlocked: Filter by `isBlocked` status.
/// If `true`, only blocked contacts are included.
/// If `false`, only non-blocked contacts are included.
/// If `nil` (default), the filter is not used.
/// - isBanned: Filter by `isBanned` status.
/// If `true`, only banned contacts are included.
/// If `false`, only non-banned contacts are included.
/// If `nil` (default), the filter is not used.
/// - sortBy: Sort order (defaults to `.username()`).
public init(
id: Set<Contact.ID>? = nil,
......@@ -180,6 +188,8 @@ extension Contact {
text: String? = nil,
authStatus: Set<AuthStatus>? = nil,
isRecent: Bool? = nil,
isBlocked: Bool? = nil,
isBanned: Bool? = nil,
sortBy: SortOrder = .username()
) {
self.id = id
......@@ -187,6 +197,8 @@ extension Contact {
self.text = text
self.authStatus = authStatus
self.isRecent = isRecent
self.isBlocked = isBlocked
self.isBanned = isBanned
self.sortBy = sortBy
}
......@@ -220,6 +232,20 @@ extension Contact {
/// If `nil`, the filter is not used.
public var isRecent: Bool?
/// Filter by `isBlocked` status
///
/// If `true`, only blocked contacts are included.
/// If `false`, only non-blocked contacts are included.
/// If `nil`, the filter is not used.
public var isBlocked: Bool?
/// Filter by `isBanned` status
///
/// If `true`, only banned contacts are included.
/// If `false`, only non-banned contacts are included.
/// If `nil`, the filter is not used.
public var isBanned: Bool?
/// Contacts sort order
public var sortBy: SortOrder
}
......
......@@ -452,4 +452,68 @@ final class ContactGRDBTests: XCTestCase {
contactF,
])
}
func testFetchingByBlockedStatus() throws {
// Mock up contacts:
let contactA = try db.saveContact(.stub("A").withBlocked(false))
let contactB = try db.saveContact(.stub("B").withBlocked(true))
let contactC = try db.saveContact(.stub("C").withBlocked(false))
let contactD = try db.saveContact(.stub("D").withBlocked(true))
// Fetch blocked contacts:
XCTAssertNoDifference(try db.fetchContacts(.init(isBlocked: true)), [
contactB,
contactD,
])
// Fetch not blocked contacts:
XCTAssertNoDifference(try db.fetchContacts(.init(isBlocked: false)), [
contactA,
contactC,
])
// Fetch contacts regardless blocked status:
XCTAssertNoDifference(try db.fetchContacts(.init(isBlocked: nil)), [
contactA,
contactB,
contactC,
contactD,
])
}
func testFetchingByBannedStatus() throws {
// Mock up contacts:
let contactA = try db.saveContact(.stub("A").withBanned(false))
let contactB = try db.saveContact(.stub("B").withBanned(true))
let contactC = try db.saveContact(.stub("C").withBanned(false))
let contactD = try db.saveContact(.stub("D").withBanned(true))
// Fetch banned contacts:
XCTAssertNoDifference(try db.fetchContacts(.init(isBanned: true)), [
contactB,
contactD,
])
// Fetch not banned contacts:
XCTAssertNoDifference(try db.fetchContacts(.init(isBanned: false)), [
contactA,
contactC,
])
// Fetch contacts regardless banned status:
XCTAssertNoDifference(try db.fetchContacts(.init(isBanned: nil)), [
contactA,
contactB,
contactC,
contactD,
])
}
}
......@@ -62,6 +62,18 @@ extension Contact {
contact.createdAt = createdAt
return contact
}
func withBlocked(_ isBlocked: Bool) -> Contact {
var contact = self
contact.isBlocked = isBlocked
return contact
}
func withBanned(_ isBanned: Bool) -> Contact {
var contact = self
contact.isBanned = isBanned
return contact
}
}
extension Group {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment