| 
									
										
										
										
											2023-09-16 18:52:42 +02:00
										 |  |  | import { Validator } from "../Validator" | 
					
						
							|  |  |  | import { Translation } from "../../i18n/Translation" | 
					
						
							|  |  |  | import Translations from "../../i18n/Translations" | 
					
						
							| 
									
										
										
										
											2023-08-10 14:10:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default class FediverseValidator extends Validator { | 
					
						
							| 
									
										
										
										
											2024-01-21 02:54:49 +01:00
										 |  |  |     public static readonly usernameAtServer: RegExp = /^@?(\w+)@((\w|-|\.)+)$/ | 
					
						
							| 
									
										
										
										
											2023-08-10 14:10:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     constructor() { | 
					
						
							| 
									
										
										
										
											2023-09-16 18:52:42 +02:00
										 |  |  |         super( | 
					
						
							|  |  |  |             "fediverse", | 
					
						
							|  |  |  |             "Validates fediverse addresses and normalizes them into `@username@server`-format" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-08-10 14:10:06 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Returns an `@username@host` | 
					
						
							|  |  |  |      * @param s | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     reformat(s: string): string { | 
					
						
							| 
									
										
										
										
											2024-02-20 12:28:01 +01:00
										 |  |  |         s = s.trim() | 
					
						
							| 
									
										
										
										
											2023-09-16 18:52:42 +02:00
										 |  |  |         if (!s.startsWith("@")) { | 
					
						
							|  |  |  |             s = "@" + s | 
					
						
							| 
									
										
										
										
											2023-08-10 14:10:06 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         if (s.match(FediverseValidator.usernameAtServer)) { | 
					
						
							|  |  |  |             return s | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             const url = new URL(s) | 
					
						
							|  |  |  |             const path = url.pathname | 
					
						
							|  |  |  |             if (path.match(/^\/\w+$/)) { | 
					
						
							| 
									
										
										
										
											2023-09-16 18:52:42 +02:00
										 |  |  |                 return `@${path.substring(1)}@${url.hostname}` | 
					
						
							| 
									
										
										
										
											2023-08-10 14:10:06 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } catch (e) { | 
					
						
							|  |  |  |             // Nothing to do here
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return undefined | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-09-16 18:52:42 +02:00
										 |  |  |     getFeedback(s: string): Translation | undefined { | 
					
						
							| 
									
										
										
										
											2024-02-20 12:28:01 +01:00
										 |  |  |         s = s.trim() | 
					
						
							| 
									
										
										
										
											2023-09-16 18:52:42 +02:00
										 |  |  |         const match = s.match(FediverseValidator.usernameAtServer) | 
					
						
							|  |  |  |         console.log("Match:", match) | 
					
						
							|  |  |  |         if (match) { | 
					
						
							|  |  |  |             const host = match[2] | 
					
						
							|  |  |  |             try { | 
					
						
							| 
									
										
										
										
											2023-09-21 01:53:34 +02:00
										 |  |  |                 new URL("https://" + host) | 
					
						
							| 
									
										
										
										
											2023-09-16 18:52:42 +02:00
										 |  |  |                 return undefined | 
					
						
							|  |  |  |             } catch (e) { | 
					
						
							|  |  |  |                 return Translations.t.validation.fediverse.invalidHost.Subs({ host }) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-08-10 14:10:06 +02:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2023-09-16 18:52:42 +02:00
										 |  |  |             const url = new URL(s) | 
					
						
							|  |  |  |             const path = url.pathname | 
					
						
							|  |  |  |             if (path.match(/^\/\w+$/)) { | 
					
						
							|  |  |  |                 return undefined | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-08-10 14:10:06 +02:00
										 |  |  |         } catch (e) { | 
					
						
							| 
									
										
										
										
											2023-09-16 18:52:42 +02:00
										 |  |  |             // Nothing to do here
 | 
					
						
							| 
									
										
										
										
											2023-08-10 14:10:06 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-09-16 18:52:42 +02:00
										 |  |  |         return Translations.t.validation.fediverse.feedback | 
					
						
							| 
									
										
										
										
											2023-08-10 14:10:06 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     isValid(s): boolean { | 
					
						
							| 
									
										
										
										
											2023-09-16 18:52:42 +02:00
										 |  |  |         return this.getFeedback(s) === undefined | 
					
						
							| 
									
										
										
										
											2023-08-10 14:10:06 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } |