Skip to content

Commit

Permalink
remove get() with string from Svelte calls (#169)
Browse files Browse the repository at this point in the history
* remove get() with string pt 1

* remove get() with string pt 2

* fix typo

* fix some null exceptions in get()

* fixup code style
  • Loading branch information
nolanlawson authored Apr 19, 2018
1 parent 18c3064 commit 8d5690d
Show file tree
Hide file tree
Showing 70 changed files with 454 additions and 391 deletions.
7 changes: 3 additions & 4 deletions routes/_actions/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ export async function clearProfileAndRelationship () {
}

export async function updateProfileAndRelationship (accountId) {
let instanceName = store.get('currentInstance')
let accessToken = store.get('accessToken')
let { currentInstance, accessToken } = store.get()

await Promise.all([
updateAccount(accountId, instanceName, accessToken),
updateRelationship(accountId, instanceName, accessToken)
updateAccount(accountId, currentInstance, accessToken),
updateRelationship(accountId, currentInstance, accessToken)
])
}
29 changes: 13 additions & 16 deletions routes/_actions/addInstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,23 @@ const REDIRECT_URI = (typeof location !== 'undefined'
? location.origin : 'https://pinafore.social') + '/settings/instances/add'

async function redirectToOauth () {
let instanceName = store.get('instanceNameInSearch')
let loggedInInstances = store.get('loggedInInstances')
instanceName = instanceName.replace(/^https?:\/\//, '').replace(/\/$/, '').replace('/$', '').toLowerCase()
if (Object.keys(loggedInInstances).includes(instanceName)) {
store.set({logInToInstanceError: `You've already logged in to ${instanceName}`})
let { instanceNameInSearch, loggedInInstances } = store.get()
instanceNameInSearch = instanceNameInSearch.replace(/^https?:\/\//, '').replace(/\/$/, '').replace('/$', '').toLowerCase()
if (Object.keys(loggedInInstances).includes(instanceNameInSearch)) {
store.set({logInToInstanceError: `You've already logged in to ${instanceNameInSearch}`})
return
}
let registrationPromise = registerApplication(instanceName, REDIRECT_URI)
let instanceInfo = await getInstanceInfo(instanceName)
await setInstanceInfoInDatabase(instanceName, instanceInfo) // cache for later
let registrationPromise = registerApplication(instanceNameInSearch, REDIRECT_URI)
let instanceInfo = await getInstanceInfo(instanceNameInSearch)
await setInstanceInfoInDatabase(instanceNameInSearch, instanceInfo) // cache for later
let instanceData = await registrationPromise
store.set({
currentRegisteredInstanceName: instanceName,
currentRegisteredInstanceName: instanceNameInSearch,
currentRegisteredInstance: instanceData
})
store.save()
let oauthUrl = generateAuthLink(
instanceName,
instanceNameInSearch,
instanceData.client_id,
REDIRECT_URI
)
Expand All @@ -48,28 +47,26 @@ export async function logInToInstance () {
(navigator.onLine
? `Is this a valid Mastodon instance? Is a browser extension blocking the request?`
: `Are you offline?`)
let { instanceNameInSearch } = store.get()
store.set({
logInToInstanceError: error,
logInToInstanceErrorForText: store.get('instanceNameInSearch')
logInToInstanceErrorForText: instanceNameInSearch
})
} finally {
store.set({logInToInstanceLoading: false})
}
}

async function registerNewInstance (code) {
let currentRegisteredInstanceName = store.get('currentRegisteredInstanceName')
let currentRegisteredInstance = store.get('currentRegisteredInstance')
let { currentRegisteredInstanceName, currentRegisteredInstance } = store.get()
let instanceData = await getAccessTokenFromAuthCode(
currentRegisteredInstanceName,
currentRegisteredInstance.client_id,
currentRegisteredInstance.client_secret,
code,
REDIRECT_URI
)
let loggedInInstances = store.get('loggedInInstances')
let loggedInInstancesInOrder = store.get('loggedInInstancesInOrder')
let instanceThemes = store.get('instanceThemes')
let { loggedInInstances, loggedInInstancesInOrder, instanceThemes } = store.get()
instanceThemes[currentRegisteredInstanceName] = 'default'
loggedInInstances[currentRegisteredInstanceName] = instanceData
if (!loggedInInstancesInOrder.includes(currentRegisteredInstanceName)) {
Expand Down
7 changes: 3 additions & 4 deletions routes/_actions/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import { toast } from '../_utils/toast'
import { updateProfileAndRelationship } from './accounts'

export async function setAccountBlocked (accountId, block, toastOnSuccess) {
let instanceName = store.get('currentInstance')
let accessToken = store.get('accessToken')
let { currentInstance, accessToken } = store.get()
try {
if (block) {
await blockAccount(instanceName, accessToken, accountId)
await blockAccount(currentInstance, accessToken, accountId)
} else {
await unblockAccount(instanceName, accessToken, accountId)
await unblockAccount(currentInstance, accessToken, accountId)
}
await updateProfileAndRelationship(accountId)
if (toastOnSuccess) {
Expand Down
38 changes: 20 additions & 18 deletions routes/_actions/compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { emit } from '../_utils/eventBus'
import { putMediaDescription } from '../_api/media'

export async function insertHandleForReply (statusId) {
let instanceName = store.get('currentInstance')
let status = await getStatusFromDatabase(instanceName, statusId)
let verifyCredentials = store.get('currentVerifyCredentials')
let { currentInstance } = store.get()
let status = await getStatusFromDatabase(currentInstance, statusId)
let { currentVerifyCredentials } = store.get()
let originalStatus = status.reblog || status
let accounts = [originalStatus.account].concat(originalStatus.mentions || [])
.filter(account => account.id !== verifyCredentials.id)
.filter(account => account.id !== currentVerifyCredentials.id)
if (!store.getComposeData(statusId, 'text') && accounts.length) {
store.setComposeData(statusId, {
text: accounts.map(account => `@${account.acct} `).join('')
Expand All @@ -23,9 +23,7 @@ export async function insertHandleForReply (statusId) {
export async function postStatus (realm, text, inReplyToId, mediaIds,
sensitive, spoilerText, visibility,
mediaDescriptions = [], inReplyToUuid) {
let instanceName = store.get('currentInstance')
let accessToken = store.get('accessToken')
let online = store.get('online')
let { currentInstance, accessToken, online } = store.get()

if (!online) {
toast.say('You cannot post while offline')
Expand All @@ -37,11 +35,11 @@ export async function postStatus (realm, text, inReplyToId, mediaIds,
})
try {
await Promise.all(mediaDescriptions.map(async (description, i) => {
return description && putMediaDescription(instanceName, accessToken, mediaIds[i], description)
return description && putMediaDescription(currentInstance, accessToken, mediaIds[i], description)
}))
let status = await postStatusToServer(instanceName, accessToken, text,
let status = await postStatusToServer(currentInstance, accessToken, text,
inReplyToId, mediaIds, sensitive, spoilerText, visibility)
addStatusOrNotification(instanceName, 'home', status)
addStatusOrNotification(currentInstance, 'home', status)
store.clearComposeData(realm)
emit('postedStatus', realm, inReplyToUuid)
} catch (e) {
Expand All @@ -61,12 +59,16 @@ export async function insertUsername (realm, username, startIndex, endIndex) {
}

export async function clickSelectedAutosuggestionUsername (realm) {
let selectionStart = store.get('composeSelectionStart')
let searchText = store.get('composeAutosuggestionSearchText')
let selection = store.get('composeAutosuggestionSelected') || 0
let account = store.get('composeAutosuggestionSearchResults')[selection]
let startIndex = selectionStart - searchText.length
let endIndex = selectionStart
let {
composeSelectionStart,
composeAutosuggestionSearchText,
composeAutosuggestionSelected,
composeAutosuggestionSearchResults
} = store.get()
composeAutosuggestionSelected = composeAutosuggestionSelected || 0
let account = composeAutosuggestionSearchResults[composeAutosuggestionSelected]
let startIndex = composeSelectionStart - composeAutosuggestionSearchText.length
let endIndex = composeSelectionStart
await insertUsername(realm, account.acct, startIndex, endIndex)
}

Expand Down Expand Up @@ -96,8 +98,8 @@ export function setReplyVisibility (realm, replyVisibility) {
if (typeof postPrivacy !== 'undefined') {
return // user has already set the postPrivacy
}
let verifyCredentials = store.get('currentVerifyCredentials')
let defaultVisibility = verifyCredentials.source.privacy
let { currentVerifyCredentials } = store.get()
let defaultVisibility = currentVerifyCredentials.source.privacy
let visibility = PRIVACY_LEVEL[replyVisibility] < PRIVACY_LEVEL[defaultVisibility]
? replyVisibility
: defaultVisibility
Expand Down
5 changes: 2 additions & 3 deletions routes/_actions/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { deleteStatus } from '../_api/delete'
import { toast } from '../_utils/toast'

export async function doDeleteStatus (statusId) {
let instanceName = store.get('currentInstance')
let accessToken = store.get('accessToken')
let { currentInstance, accessToken } = store.get()
try {
await deleteStatus(instanceName, accessToken, statusId)
await deleteStatus(currentInstance, accessToken, statusId)
toast.say('Status deleted.')
} catch (e) {
console.error(e)
Expand Down
21 changes: 13 additions & 8 deletions routes/_actions/emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ export async function updateCustomEmojiForInstance (instanceName) {
() => getCustomEmojiFromDatabase(instanceName),
emoji => setCustomEmojiInDatabase(instanceName, emoji),
emoji => {
let customEmoji = store.get('customEmoji')
let { customEmoji } = store.get()
customEmoji[instanceName] = emoji
store.set({customEmoji: customEmoji})
}
)
}

export function insertEmoji (realm, emoji) {
let idx = store.get('composeSelectionStart') || 0
let { composeSelectionStart } = store.get()
let idx = composeSelectionStart || 0
let oldText = store.getComposeData(realm, 'text') || ''
let pre = oldText.substring(0, idx)
let post = oldText.substring(idx)
Expand All @@ -37,11 +38,15 @@ export function insertEmojiAtPosition (realm, emoji, startIndex, endIndex) {
}

export async function clickSelectedAutosuggestionEmoji (realm) {
let selectionStart = store.get('composeSelectionStart')
let searchText = store.get('composeAutosuggestionSearchText')
let selection = store.get('composeAutosuggestionSelected') || 0
let emoji = store.get('composeAutosuggestionSearchResults')[selection]
let startIndex = selectionStart - searchText.length
let endIndex = selectionStart
let {
composeSelectionStart,
composeAutosuggestionSearchText,
composeAutosuggestionSelected,
composeAutosuggestionSearchResults
} = store.get()
composeAutosuggestionSelected = composeAutosuggestionSelected || 0
let emoji = composeAutosuggestionSearchResults[composeAutosuggestionSelected]
let startIndex = composeSelectionStart - composeAutosuggestionSearchText.length
let endIndex = composeSelectionStart
await insertEmojiAtPosition(realm, emoji, startIndex, endIndex)
}
16 changes: 8 additions & 8 deletions routes/_actions/favorite.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ import {
} from '../_database/timelines/updateStatus'

export async function setFavorited (statusId, favorited) {
if (!store.get('online')) {
let { online } = store.get()
if (!online) {
toast.say(`You cannot ${favorited ? 'favorite' : 'unfavorite'} while offline.`)
return
}
let instanceName = store.get('currentInstance')
let accessToken = store.get('accessToken')
let { currentInstance, accessToken } = store.get()
let networkPromise = favorited
? favoriteStatus(instanceName, accessToken, statusId)
: unfavoriteStatus(instanceName, accessToken, statusId)
store.setStatusFavorited(instanceName, statusId, favorited) // optimistic update
? favoriteStatus(currentInstance, accessToken, statusId)
: unfavoriteStatus(currentInstance, accessToken, statusId)
store.setStatusFavorited(currentInstance, statusId, favorited) // optimistic update
try {
await networkPromise
await setStatusFavoritedInDatabase(instanceName, statusId, favorited)
await setStatusFavoritedInDatabase(currentInstance, statusId, favorited)
} catch (e) {
console.error(e)
toast.say(`Failed to ${favorited ? 'favorite' : 'unfavorite'}. ` + (e.message || ''))
store.setStatusFavorited(instanceName, statusId, !favorited) // undo optimistic update
store.setStatusFavorited(currentInstance, statusId, !favorited) // undo optimistic update
}
}
9 changes: 4 additions & 5 deletions routes/_actions/follow.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ import {
} from '../_database/accountsAndRelationships'

export async function setAccountFollowed (accountId, follow, toastOnSuccess) {
let instanceName = store.get('currentInstance')
let accessToken = store.get('accessToken')
let { currentInstance, accessToken } = store.get()
try {
let account
if (follow) {
account = await followAccount(instanceName, accessToken, accountId)
account = await followAccount(currentInstance, accessToken, accountId)
} else {
account = await unfollowAccount(instanceName, accessToken, accountId)
account = await unfollowAccount(currentInstance, accessToken, accountId)
}
await updateProfileAndRelationship(accountId)
let relationship = await getRelationshipFromDatabase(instanceName, accountId)
let relationship = await getRelationshipFromDatabase(currentInstance, accountId)
if (toastOnSuccess) {
if (follow) {
if (account.locked && relationship.requested) {
Expand Down
28 changes: 16 additions & 12 deletions routes/_actions/instances.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ import {
} from '../_database/meta'

export function changeTheme (instanceName, newTheme) {
let instanceThemes = store.get('instanceThemes')
let { instanceThemes } = store.get()
instanceThemes[instanceName] = newTheme
store.set({instanceThemes: instanceThemes})
store.save()
if (instanceName === store.get('currentInstance')) {
let { currentInstance } = store.get()
if (instanceName === currentInstance) {
switchToTheme(newTheme)
}
}

export function switchToInstance (instanceName) {
let instanceThemes = store.get('instanceThemes')
let { instanceThemes } = store.get()
store.set({
currentInstance: instanceName,
searchResults: null,
Expand All @@ -35,11 +36,13 @@ export function switchToInstance (instanceName) {
}

export async function logOutOfInstance (instanceName) {
let loggedInInstances = store.get('loggedInInstances')
let instanceThemes = store.get('instanceThemes')
let loggedInInstancesInOrder = store.get('loggedInInstancesInOrder')
let composeData = store.get('composeData')
let currentInstance = store.get('currentInstance')
let {
loggedInInstances,
instanceThemes,
loggedInInstancesInOrder,
composeData,
currentInstance
} = store.get()
loggedInInstancesInOrder.splice(loggedInInstancesInOrder.indexOf(instanceName), 1)
let newInstance = instanceName === currentInstance
? loggedInInstancesInOrder[0]
Expand All @@ -64,13 +67,13 @@ export async function logOutOfInstance (instanceName) {
}

function setStoreVerifyCredentials (instanceName, thisVerifyCredentials) {
let verifyCredentials = store.get('verifyCredentials')
let { verifyCredentials } = store.get()
verifyCredentials[instanceName] = thisVerifyCredentials
store.set({verifyCredentials: verifyCredentials})
}

export async function updateVerifyCredentialsForInstance (instanceName) {
let loggedInInstances = store.get('loggedInInstances')
let { loggedInInstances } = store.get()
let accessToken = loggedInInstances[instanceName].access_token
await cacheFirstUpdateAfter(
() => getVerifyCredentials(instanceName, accessToken),
Expand All @@ -81,7 +84,8 @@ export async function updateVerifyCredentialsForInstance (instanceName) {
}

export async function updateVerifyCredentialsForCurrentInstance () {
await updateVerifyCredentialsForInstance(store.get('currentInstance'))
let { currentInstance } = store.get()
await updateVerifyCredentialsForInstance(currentInstance)
}

export async function updateInstanceInfo (instanceName) {
Expand All @@ -90,7 +94,7 @@ export async function updateInstanceInfo (instanceName) {
() => getInstanceInfoFromDatabase(instanceName),
info => setInstanceInfoInDatabase(instanceName, info),
info => {
let instanceInfos = store.get('instanceInfos')
let { instanceInfos } = store.get()
instanceInfos[instanceName] = info
store.set({instanceInfos: instanceInfos})
}
Expand Down
13 changes: 6 additions & 7 deletions routes/_actions/lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ import {
} from '../_database/meta'

export async function updateLists () {
let instanceName = store.get('currentInstance')
let accessToken = store.get('accessToken')
let { currentInstance, accessToken } = store.get()

await cacheFirstUpdateAfter(
() => getLists(instanceName, accessToken),
() => getListsFromDatabase(instanceName),
lists => setListsInDatabase(instanceName, lists),
() => getLists(currentInstance, accessToken),
() => getListsFromDatabase(currentInstance),
lists => setListsInDatabase(currentInstance, lists),
lists => {
let instanceLists = store.get('instanceLists')
instanceLists[instanceName] = lists
let { instanceLists } = store.get()
instanceLists[currentInstance] = lists
store.set({instanceLists: instanceLists})
}
)
Expand Down
5 changes: 2 additions & 3 deletions routes/_actions/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import { toast } from '../_utils/toast'
import { scheduleIdleTask } from '../_utils/scheduleIdleTask'

export async function doMediaUpload (realm, file) {
let instanceName = store.get('currentInstance')
let accessToken = store.get('accessToken')
let { currentInstance, accessToken } = store.get()
store.set({uploadingMedia: true})
try {
let response = await uploadMedia(instanceName, accessToken, file)
let response = await uploadMedia(currentInstance, accessToken, file)
let composeMedia = store.getComposeData(realm, 'media') || []
composeMedia.push({
data: response,
Expand Down
Loading

0 comments on commit 8d5690d

Please sign in to comment.