Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated the SQL for checking session id prefixes #1048

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,11 @@ internal extension LibSession {
let threadIdsToRemove: [String] = try SessionThread
.filter(!syncedContactIds.contains(SessionThread.Columns.id))
.filter(SessionThread.Columns.variant == SessionThread.Variant.contact)
.filter(!SessionThread.Columns.id.like("\(SessionId.Prefix.blinded15.rawValue)%"))
.filter(!SessionThread.Columns.id.like("\(SessionId.Prefix.blinded25.rawValue)%"))
.filter(
/// Only want to include include standard contact conversations (not blinded conversations)
ClosedGroup.Columns.threadId > SessionId.Prefix.standard.rawValue &&
ClosedGroup.Columns.threadId < SessionId.Prefix.standard.endOfRangeString
)
.select(.id)
.asRequest(of: String.self)
.fetchAll(db)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,6 @@ extension LibSession {
let thread: TypedTableAlias<SessionThread> = TypedTableAlias()
let keyPair: TypedTableAlias<ClosedGroupKeyPair> = TypedTableAlias()

let prefixLiteral: SQL = SQL(stringLiteral: "\(SessionId.Prefix.standard.rawValue)%")
let keyPairThreadIdColumnLiteral: SQL = SQL(stringLiteral: ClosedGroupKeyPair.Columns.threadId.name)
let receivedTimestampColumnLiteral: SQL = SQL(stringLiteral: ClosedGroupKeyPair.Columns.receivedTimestamp.name)
let threadIdColumnLiteral: SQL = SQL(stringLiteral: DisappearingMessagesConfiguration.Columns.threadId.name)
Expand Down Expand Up @@ -861,7 +860,10 @@ extension LibSession {
) AS \(LegacyGroupInfo.lastKeyPairKey) ON \(LegacyGroupInfo.lastKeyPairKey).\(keyPairThreadIdColumnLiteral) = \(closedGroup[.threadId])
LEFT JOIN \(DisappearingMessagesConfiguration.self) AS \(LegacyGroupInfo.disappearingConfigKey) ON \(LegacyGroupInfo.disappearingConfigKey).\(threadIdColumnLiteral) = \(closedGroup[.threadId])

WHERE \(SQL("\(closedGroup[.threadId]) LIKE '\(prefixLiteral)'"))
WHERE (
\(closedGroup[.threadId]) > \(SessionId.Prefix.standard.rawValue) AND
\(closedGroup[.threadId]) < \(SessionId.Prefix.standard.endOfRangeString)
)
"""

let legacyGroupInfoNoMembers: [LegacyGroupInfo] = try request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ extension MessageReceiver {
currentUserPublicKey: currentUserPublicKey,
legacyGroupIds: try ClosedGroup
.select(.threadId)
.filter(!ClosedGroup.Columns.threadId.like("\(SessionId.Prefix.group.rawValue)%"))
.filter(
ClosedGroup.Columns.threadId > SessionId.Prefix.standard.rawValue &&
ClosedGroup.Columns.threadId < SessionId.Prefix.standard.endOfRangeString
)
.joining(
required: ClosedGroup.members
.filter(GroupMember.Columns.profileId == currentUserPublicKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,14 @@ extension MessageReceiver {
.select(.id)
.filter(SessionThread.Columns.variant == SessionThread.Variant.contact)
.filter(
SessionThread.Columns.id.like("\(SessionId.Prefix.blinded15.rawValue)%") ||
SessionThread.Columns.id.like("\(SessionId.Prefix.blinded25.rawValue)%")
(
ClosedGroup.Columns.threadId > SessionId.Prefix.blinded15.rawValue &&
ClosedGroup.Columns.threadId < SessionId.Prefix.blinded15.endOfRangeString
) ||
(
ClosedGroup.Columns.threadId > SessionId.Prefix.blinded25.rawValue &&
ClosedGroup.Columns.threadId < SessionId.Prefix.blinded25.endOfRangeString
)
)
.asRequest(of: String.self)
.fetchSet(db))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ extension MessageSender {
currentUserPublicKey: userPublicKey,
legacyGroupIds: try ClosedGroup
.select(.threadId)
.filter(!ClosedGroup.Columns.threadId.like("\(SessionId.Prefix.group.rawValue)%"))
.filter(
ClosedGroup.Columns.threadId > SessionId.Prefix.standard.rawValue &&
ClosedGroup.Columns.threadId < SessionId.Prefix.standard.endOfRangeString
)
.joining(
required: ClosedGroup.members
.filter(GroupMember.Columns.profileId == userPublicKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ public enum PushNotificationAPI {
currentUserPublicKey: currentUserPublicKey,
legacyGroupIds: try ClosedGroup
.select(.threadId)
.filter(!ClosedGroup.Columns.threadId.like("\(SessionId.Prefix.group.rawValue)%"))
.filter(
ClosedGroup.Columns.threadId > SessionId.Prefix.standard.rawValue &&
ClosedGroup.Columns.threadId < SessionId.Prefix.standard.endOfRangeString
)
.joining(
required: ClosedGroup.members
.filter(GroupMember.Columns.profileId == currentUserPublicKey)
Expand Down Expand Up @@ -167,7 +170,10 @@ public enum PushNotificationAPI {
getUserHexEncodedPublicKey(db, using: dependencies),
try ClosedGroup
.select(.threadId)
.filter(!ClosedGroup.Columns.threadId.like("\(SessionId.Prefix.group.rawValue)%"))
.filter(
ClosedGroup.Columns.threadId > SessionId.Prefix.standard.rawValue &&
ClosedGroup.Columns.threadId < SessionId.Prefix.standard.endOfRangeString
)
.asRequest(of: String.self)
.fetchSet(db)
)
Expand Down
14 changes: 14 additions & 0 deletions SessionUtilitiesKit/General/SessionId.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ public struct SessionId: Equatable, Hashable, CustomStringConvertible {
case .some: throw SessionIdError.invalidSessionId // Should be covered by above cases
}
}

/// In SQLite it's more efficient to check if an indexed column is `{column} > '05' AND {column} < '06'` then it is to use
/// a wildcard like`{column} LIKE '05%'` as the wildcard can't use the index, so this variable is here to streamline this behaviour
///
/// stringlint:ignore_contents
public var endOfRangeString: String {
switch self {
case .standard: return "06"
case .blinded15: return "16"
case .blinded25: return "26"
case .unblinded: return "01"
case .group: return "04"
}
}
}

public let prefix: Prefix
Expand Down