-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Feature: 3rd-Party Server URLs #7
Draft
pythcon
wants to merge
14
commits into
revoltchat:main
Choose a base branch
from
pythcon:3rd-party-server-urls
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
949e61f
Added 3rd Party URL Selection
pythcon 6955eda
Added server url retrieval and fixed encoding errors
pythcon 0fbae13
Added 3rd Party URL Selection
pythcon 3ec27c8
Added: URL selector component
pythcon 3076bc2
Added current connected server host
pythcon 47bdd4d
Adjusted ViewState to read from stored apiInfo instead of hardcoded url
pythcon bf32c98
Adjusted viewstate call; Styled button to clearing cache and session
pythcon cbe91f7
Changed from null object to empty string for url
pythcon fb2578c
fix: handle optional URL safely in login request
pythcon 6724eaf
Added guard logic with customer server url support for account creation
pythcon 65683d1
Added custom server urls to registration page
pythcon 260b8e9
Added ServerUrlSelector component
pythcon 6d71ab1
Fixed serverurl clear on sign out
pythcon 9414115
Feature: Added api validation and additional /api endpoint validation
pythcon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
// | ||
// ServerUrlSelector.swift | ||
// Revolt | ||
// | ||
// Created by pythcon on 12/7/24. | ||
// | ||
|
||
import SwiftUI | ||
import Types | ||
|
||
struct ServerUrlSelector: View { | ||
@EnvironmentObject var viewState: ViewState | ||
@Environment(\.colorScheme) var colorScheme | ||
|
||
private let officialServer = "https://api.revolt.chat" | ||
@State private var showCustomServer = false | ||
@State private var customDomain: String = "" | ||
@State private var isValidating = false | ||
@State private var validationError: String? = nil | ||
@State private var validationSuccess: String? = nil | ||
@State private var connectionStatus: ConnectionStatus = .untested | ||
@FocusState private var isTextFieldFocused: Bool | ||
|
||
enum ConnectionStatus { | ||
case untested | ||
case testing | ||
case success | ||
case failed | ||
} | ||
|
||
private func validateAndUpdateApiInfo(_ domain: String) { | ||
if domain.isEmpty { return } | ||
|
||
isValidating = true | ||
connectionStatus = .testing | ||
validationError = nil | ||
validationSuccess = nil | ||
|
||
let baseUrl: String | ||
if domain == officialServer { | ||
baseUrl = officialServer | ||
} else { | ||
// First try the domain as-is | ||
let cleanDomain = domain.hasSuffix("/") ? String(domain.dropLast()) : domain | ||
baseUrl = (domain.starts(with: "http://") || domain.starts(with: "https://")) ? cleanDomain : "https://" + cleanDomain | ||
} | ||
|
||
// Function to try validation with a URL | ||
func tryValidation(url: String) async -> Bool { | ||
let tempHttp = HTTPClient(token: nil, baseURL: url) | ||
do { | ||
let fetchedApiInfo = try await tempHttp.fetchApiInfo().get() | ||
// Validate that this is actually a Revolt API | ||
guard | ||
!fetchedApiInfo.revolt.isEmpty, // Check revolt version exists | ||
fetchedApiInfo.features.january != nil, // Check for required features | ||
fetchedApiInfo.features.autumn != nil, // Check for required features | ||
!fetchedApiInfo.ws.isEmpty, // Check websocket URL exists | ||
!fetchedApiInfo.app.isEmpty // Check app URL exists | ||
else { | ||
return false | ||
} | ||
|
||
await MainActor.run { | ||
viewState.apiInfo = fetchedApiInfo | ||
viewState.userSettingsStore.store.serverUrl = url | ||
|
||
if url != baseUrl { | ||
customDomain = url // Update textbox if we added /api | ||
} | ||
|
||
isValidating = false | ||
validationSuccess = "Successfully connected to server" | ||
connectionStatus = .success | ||
} | ||
return true | ||
} catch { | ||
return false | ||
} | ||
} | ||
|
||
Task { | ||
// Try original URL first | ||
if await tryValidation(url: baseUrl) { | ||
return | ||
} | ||
|
||
// If not official server and first attempt failed, try with /api | ||
if domain != officialServer { | ||
let apiUrl = baseUrl + "/api" | ||
if await tryValidation(url: apiUrl) { | ||
return | ||
} | ||
} | ||
|
||
// If both attempts failed, show error | ||
await MainActor.run { | ||
isValidating = false | ||
validationError = "Unable to connect to server" | ||
connectionStatus = .failed | ||
} | ||
} | ||
} | ||
|
||
private var statusIcon: some View { | ||
Group { | ||
switch connectionStatus { | ||
case .untested: | ||
Image(systemName: "link.circle.fill") | ||
.foregroundStyle(.gray) | ||
case .testing: | ||
ProgressView() | ||
.controlSize(.small) | ||
case .success: | ||
Image(systemName: "checkmark.circle.fill") | ||
.foregroundStyle(.green) | ||
case .failed: | ||
Image(systemName: "x.circle.fill") | ||
.foregroundStyle(.red) | ||
} | ||
} | ||
} | ||
|
||
var body: some View { | ||
VStack(alignment: .leading) { | ||
HStack { | ||
Text("Server") | ||
.font(.caption) | ||
.foregroundStyle(.secondary) | ||
Spacer() | ||
Button(action: { | ||
withAnimation { | ||
showCustomServer.toggle() | ||
if !showCustomServer { | ||
validateAndUpdateApiInfo(officialServer) | ||
viewState.userSettingsStore.store.serverUrl = officialServer | ||
} | ||
} | ||
}) { | ||
Text(showCustomServer ? "Use Official" : "Use Custom") | ||
.font(.caption) | ||
.foregroundStyle(viewState.theme.accent) | ||
} | ||
} | ||
|
||
if showCustomServer { | ||
HStack { | ||
TextField( | ||
"Domain (e.g. example.com)", | ||
text: $customDomain | ||
) | ||
.textContentType(.URL) | ||
.keyboardType(.URL) | ||
.disabled(isValidating) | ||
.focused($isTextFieldFocused) | ||
.onChange(of: isTextFieldFocused) { oldValue, newValue in | ||
if !newValue { | ||
validateAndUpdateApiInfo(customDomain) | ||
} | ||
} | ||
.onChange(of: customDomain) { oldValue, newValue in | ||
print("onChange: \(oldValue) -> \(newValue)") | ||
print("connectionStatus: \(connectionStatus)") | ||
print("validationSuccess: \(validationSuccess)") | ||
if oldValue != newValue { | ||
connectionStatus = .untested | ||
validationError = nil | ||
validationSuccess = nil | ||
} | ||
} | ||
|
||
Button(action: { | ||
i | ||
';f !customDomain.isEmpty { | ||
validateAndUpdateApiInfo(customDomain) | ||
} | ||
}) { | ||
statusIcon | ||
} | ||
.disabled(connectionStatus == .testing || customDomain.isEmpty) | ||
} | ||
.padding() | ||
.background((colorScheme == .light) ? Color(white: 0.851) : Color(white: 0.2)) | ||
.clipShape(.rect(cornerRadius: 5)) | ||
|
||
if let error = validationError { | ||
Text(error) | ||
.font(.caption) | ||
.foregroundStyle(.red) | ||
} else if let success = validationSuccess { | ||
Text(success) | ||
.font(.caption) | ||
.foregroundStyle(.green) | ||
} | ||
} else { | ||
Text("Official Revolt Server") | ||
.padding() | ||
.frame(maxWidth: .infinity, alignment: .leading) | ||
.background((colorScheme == .light) ? Color(white: 0.851) : Color(white: 0.2)) | ||
.clipShape(.rect(cornerRadius: 5)) | ||
} | ||
} | ||
.padding(.bottom) | ||
.onAppear { | ||
if viewState.userSettingsStore.store.serverUrl.isEmpty { | ||
viewState.userSettingsStore.store.serverUrl = officialServer | ||
validateAndUpdateApiInfo(officialServer) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change was added due to an infinite loop in waiting for notifications. When logging into the official server or a 3rd party, the request would make it to the API, but the user would never get the "Accept Notifications" prompt.