| 
									
										
										
										
											2018-01-07 16:00:42 -08:00
										 |  |  | <:Head> | 
					
						
							| 
									
										
										
										
											2018-01-13 12:12:17 -08:00
										 |  |  |   <title>Add an Instance</title> | 
					
						
							| 
									
										
										
										
											2018-01-07 16:00:42 -08:00
										 |  |  | </:Head> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <Layout page='settings'> | 
					
						
							| 
									
										
										
										
											2018-01-13 12:12:17 -08:00
										 |  |  |   <SettingsLayout page='settings/instances/add' label="Add an Instance"> | 
					
						
							|  |  |  |     <h1>Add an Instance</h1> | 
					
						
							| 
									
										
										
										
											2018-01-07 16:00:42 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 11:11:35 -08:00
										 |  |  |     {{#if $isUserLoggedIn}} | 
					
						
							| 
									
										
										
										
											2018-01-13 12:12:17 -08:00
										 |  |  |     <p>Connect to an instance to log in.</p> | 
					
						
							| 
									
										
										
										
											2018-01-13 11:11:35 -08:00
										 |  |  |     {{else}} | 
					
						
							| 
									
										
										
										
											2018-01-13 12:12:17 -08:00
										 |  |  |     <p>Log in to an instance to start using Pinafore.</p> | 
					
						
							| 
									
										
										
										
											2018-01-13 11:11:35 -08:00
										 |  |  |     {{/if}} | 
					
						
							| 
									
										
										
										
											2018-01-07 16:00:42 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 12:12:17 -08:00
										 |  |  |     <form class="add-new-instance" on:submit='onSubmit(event)'> | 
					
						
							|  |  |  |       <label for="instanceInput">Instance name:</label> | 
					
						
							|  |  |  |       <input type="text" id="instanceInput" bind:value='$instanceNameInSearch' placeholder=''> | 
					
						
							|  |  |  |       <button class="primary" type="submit" id="submitButton">Add instance</button> | 
					
						
							|  |  |  |     </form> | 
					
						
							| 
									
										
										
										
											2018-01-07 16:00:42 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 11:11:35 -08:00
										 |  |  |     {{#if !$isUserLoggedIn}} | 
					
						
							| 
									
										
										
										
											2018-01-13 14:19:51 -08:00
										 |  |  |     <p>Don't have an instance? <a rel="noopener" target="_blank" href="https://joinmastodon.org">Join Mastodon!</a></p> | 
					
						
							| 
									
										
										
										
											2018-01-13 11:11:35 -08:00
										 |  |  |     {{/if}} | 
					
						
							| 
									
										
										
										
											2018-01-08 17:44:29 -08:00
										 |  |  |   </SettingsLayout> | 
					
						
							| 
									
										
										
										
											2018-01-07 16:00:42 -08:00
										 |  |  | </Layout> | 
					
						
							|  |  |  | <style> | 
					
						
							| 
									
										
										
										
											2018-01-07 22:00:16 -08:00
										 |  |  |   @media (max-width: 767px) { | 
					
						
							|  |  |  |     input { | 
					
						
							|  |  |  |       width: 90%; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-07 16:00:42 -08:00
										 |  |  |   input { | 
					
						
							| 
									
										
										
										
											2018-01-07 22:00:16 -08:00
										 |  |  |     width: 250px; | 
					
						
							| 
									
										
										
										
											2018-01-07 16:00:42 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 11:11:35 -08:00
										 |  |  |   form.add-new-instance { | 
					
						
							| 
									
										
										
										
											2018-01-12 09:01:46 -08:00
										 |  |  |     background: var(--form-bg); | 
					
						
							| 
									
										
										
										
											2018-01-07 16:00:42 -08:00
										 |  |  |     padding: 5px 10px 15px; | 
					
						
							| 
									
										
										
										
											2018-01-12 09:01:46 -08:00
										 |  |  |     margin: 20px auto; | 
					
						
							|  |  |  |     border: 1px solid var(--form-border); | 
					
						
							| 
									
										
										
										
											2018-01-07 16:00:42 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 11:11:35 -08:00
										 |  |  |   form.add-new-instance label, form.add-new-instance input, form.add-new-instance button { | 
					
						
							| 
									
										
										
										
											2018-01-07 16:00:42 -08:00
										 |  |  |     display: block; | 
					
						
							|  |  |  |     margin: 20px 5px; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | </style> | 
					
						
							|  |  |  | <script> | 
					
						
							| 
									
										
										
										
											2018-01-13 12:12:17 -08:00
										 |  |  |   import Layout from '../../_components/Layout.html'; | 
					
						
							|  |  |  |   import SettingsLayout from '../_components/SettingsLayout.html' | 
					
						
							| 
									
										
										
										
											2018-01-13 14:19:51 -08:00
										 |  |  |   import { registerApplication, generateAuthLink, getAccessTokenFromAuthCode } from '../../_utils/mastodon/oauth' | 
					
						
							| 
									
										
										
										
											2018-01-13 12:12:17 -08:00
										 |  |  |   import { store } from '../../_utils/store' | 
					
						
							| 
									
										
										
										
											2018-01-07 22:13:15 -08:00
										 |  |  |   import { goto } from 'sapper/runtime.js' | 
					
						
							| 
									
										
										
										
											2018-01-07 16:00:42 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 19:23:05 -08:00
										 |  |  |   const REDIRECT_URI = (typeof location !== 'undefined' ? | 
					
						
							|  |  |  |       location.origin : 'https://pinafore.social') + '/settings/instances/add' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-07 16:00:42 -08:00
										 |  |  |   export default { | 
					
						
							| 
									
										
										
										
											2018-01-07 22:00:16 -08:00
										 |  |  |     oncreate: function () { | 
					
						
							|  |  |  |       if (process.browser) { | 
					
						
							|  |  |  |         (async () => { | 
					
						
							|  |  |  |           let params = new URLSearchParams(location.search) | 
					
						
							|  |  |  |           if (params.has('code')) { | 
					
						
							| 
									
										
										
										
											2018-01-08 10:13:42 -08:00
										 |  |  |             this.onReceivedOauthCode(params.get('code')) | 
					
						
							| 
									
										
										
										
											2018-01-07 22:00:16 -08:00
										 |  |  |           } | 
					
						
							|  |  |  |         })() | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2018-01-07 16:00:42 -08:00
										 |  |  |     components: { | 
					
						
							| 
									
										
										
										
											2018-01-08 17:44:29 -08:00
										 |  |  |       Layout, | 
					
						
							|  |  |  |       SettingsLayout | 
					
						
							| 
									
										
										
										
											2018-01-07 16:00:42 -08:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2018-01-08 10:13:42 -08:00
										 |  |  |     store: () => store, | 
					
						
							| 
									
										
										
										
											2018-01-07 16:00:42 -08:00
										 |  |  |     methods: { | 
					
						
							| 
									
										
										
										
											2018-01-13 18:59:49 -08:00
										 |  |  |       onSubmit: async function(event) { | 
					
						
							|  |  |  |         event.preventDefault() | 
					
						
							|  |  |  |         let instanceName = this.store.get('instanceNameInSearch') | 
					
						
							|  |  |  |         instanceName = instanceName.replace(/^https?:\/\//, '').replace('/$', '') | 
					
						
							|  |  |  |         // TODO: show toast error if you're already logged into this instance | 
					
						
							| 
									
										
										
										
											2018-01-13 19:23:05 -08:00
										 |  |  |         let instanceData = await registerApplication(instanceName, REDIRECT_URI) | 
					
						
							| 
									
										
										
										
											2018-01-13 18:59:49 -08:00
										 |  |  |         // TODO: handle error | 
					
						
							|  |  |  |         this.store.set({ | 
					
						
							|  |  |  |           currentRegisteredInstanceName: instanceName, | 
					
						
							|  |  |  |           currentRegisteredInstance: instanceData | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         this.store.save() | 
					
						
							| 
									
										
										
										
											2018-01-13 19:23:05 -08:00
										 |  |  |         let oauthUrl = generateAuthLink( | 
					
						
							|  |  |  |           instanceName, | 
					
						
							|  |  |  |           instanceData.client_id, | 
					
						
							|  |  |  |           REDIRECT_URI | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-01-13 18:59:49 -08:00
										 |  |  |         document.location.href = oauthUrl | 
					
						
							|  |  |  |       }, | 
					
						
							| 
									
										
										
										
											2018-01-08 10:13:42 -08:00
										 |  |  |       onReceivedOauthCode: async function(code) { | 
					
						
							| 
									
										
										
										
											2018-01-12 22:24:54 -08:00
										 |  |  |         let currentRegisteredInstanceName = this.store.get('currentRegisteredInstanceName') | 
					
						
							|  |  |  |         let currentRegisteredInstance = this.store.get('currentRegisteredInstance') | 
					
						
							| 
									
										
										
										
											2018-01-13 14:19:51 -08:00
										 |  |  |         let instanceData = await getAccessTokenFromAuthCode( | 
					
						
							| 
									
										
										
										
											2018-01-12 22:24:54 -08:00
										 |  |  |           currentRegisteredInstanceName, | 
					
						
							|  |  |  |           currentRegisteredInstance.client_id, | 
					
						
							|  |  |  |           currentRegisteredInstance.client_secret, | 
					
						
							| 
									
										
										
										
											2018-01-13 19:23:05 -08:00
										 |  |  |           code, | 
					
						
							|  |  |  |           REDIRECT_URI | 
					
						
							| 
									
										
										
										
											2018-01-13 14:19:51 -08:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2018-01-12 22:24:54 -08:00
										 |  |  |         // TODO: handle error | 
					
						
							|  |  |  |         let loggedInInstances = this.store.get('loggedInInstances') | 
					
						
							|  |  |  |         let loggedInInstancesInOrder = this.store.get('loggedInInstancesInOrder') | 
					
						
							| 
									
										
										
										
											2018-01-13 20:07:11 -08:00
										 |  |  |         let instanceThemes = this.store.get('instanceThemes') | 
					
						
							|  |  |  |         instanceThemes[currentRegisteredInstanceName] = 'default' | 
					
						
							| 
									
										
										
										
											2018-01-12 22:24:54 -08:00
										 |  |  |         loggedInInstances[currentRegisteredInstanceName] = instanceData | 
					
						
							|  |  |  |         if (!loggedInInstancesInOrder.includes(currentRegisteredInstanceName)) { | 
					
						
							|  |  |  |           loggedInInstancesInOrder.push(currentRegisteredInstanceName) | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-01-08 10:13:42 -08:00
										 |  |  |         this.store.set({ | 
					
						
							| 
									
										
										
										
											2018-01-12 22:26:20 -08:00
										 |  |  |           instanceNameInSearch: '', | 
					
						
							| 
									
										
										
										
											2018-01-13 18:59:49 -08:00
										 |  |  |           currentRegisteredInstanceName: null, | 
					
						
							|  |  |  |           currentRegisteredInstance: null, | 
					
						
							| 
									
										
										
										
											2018-01-12 22:26:20 -08:00
										 |  |  |           loggedInInstances: loggedInInstances, | 
					
						
							|  |  |  |           currentInstance: currentRegisteredInstanceName, | 
					
						
							| 
									
										
										
										
											2018-01-13 20:07:11 -08:00
										 |  |  |           loggedInInstancesInOrder: loggedInInstancesInOrder, | 
					
						
							|  |  |  |           instanceThemes: instanceThemes | 
					
						
							| 
									
										
										
										
											2018-01-08 10:13:42 -08:00
										 |  |  |         }) | 
					
						
							|  |  |  |         this.store.save() | 
					
						
							|  |  |  |         goto('/') | 
					
						
							|  |  |  |       }, | 
					
						
							| 
									
										
										
										
											2018-01-07 16:00:42 -08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | </script> |