Browse Source

Fix cyclic dependencies; front fucked up wtf

pull/100/head
squeaky otter 2 years ago
parent
commit
163b506759
Signed by: dashie GPG Key ID: C2D57B325840B755
  1. 8
      DEVELOPMENT.md
  2. 23
      flake_id.py
  3. 20
      front/src/backend/oauth/oauth.js
  4. 8
      front/src/boot/after_store.js
  5. 1
      front/src/components/login_form/login_form.vue
  6. 1
      front/src/modules/instance.js
  7. 4
      front/src/modules/tracks.js
  8. 2
      front/src/views/tracks/Upload.vue
  9. 2
      models.py
  10. 22
      utils.py

8
DEVELOPMENT.md

@ -31,10 +31,16 @@ Create `front/config/local.json` with:
```json
{
"target": "http://127.0.0.1:5000/"
"target": "http://reel2bits.dev.lan.sigpipe.me/"
}
```
Uses whatever dns you want, but it needs to match the AP_DOMAIN in `config.py`.
Also add that dns into your `/etc/hosts`.
And you need to run it proxified by nginx or whatever else.
Run the front with: `cd front && npm run dev`
Then access to the front on: http://localhost:8081

23
flake_id.py

@ -0,0 +1,23 @@
import time
import threading
import os
import random
def gen_flakeid():
# (64b ?) timestamp in ns
t = str(time.time_ns())
# (48b) "worker id", rand
w = random.getrandbits(48)
pid = os.getpid()
tid = threading.get_ident()
ptid = f"{pid}{tid}"
# (16b) sequence from PID+threadID
# might be possible to throw in the
# object id of the flask request
s = hash(ptid) & 65536
return int(f"{t}{w}{s}")

20
front/src/backend/oauth/oauth.js

@ -3,7 +3,7 @@ import { reduce } from 'lodash'
const REDIRECT_URI = `${window.location.origin}/oauth-callback`
export const getOrCreateApp = ({ clientId, clientSecret, instance, commit }) => {
export const getOrCreateApp = ({ clientId, clientSecret, commit }) => {
console.debug('getOrCreateApp')
if (clientId && clientSecret) {
console.debug('we already have clientId and clientSecret stored')
@ -11,7 +11,7 @@ export const getOrCreateApp = ({ clientId, clientSecret, instance, commit }) =>
}
console.debug('registering a new app')
const url = `${instance}/api/v1/apps`
const url = `/api/v1/apps`
const form = new window.FormData()
form.append('client_name', `reel2bit_front_${window.___reel2bitsfe_commit_hash}_${(new Date()).toISOString()}`)
@ -27,7 +27,7 @@ export const getOrCreateApp = ({ clientId, clientSecret, instance, commit }) =>
.then((app) => commit('setClientData', app) || app)
}
const login = ({ instance, clientId }) => {
const login = ({ clientId }) => {
console.debug('login')
const data = {
response_type: 'code',
@ -46,13 +46,13 @@ const login = ({ instance, clientId }) => {
}, false)
// Do the redirect
window.location.href = `${instance}/oauth/authorize?${dataString}`
window.location.href = `/oauth/authorize?${dataString}`
}
// Used on : login
const getTokenWithCredentials = ({ clientId, clientSecret, instance, username, password }) => {
const getTokenWithCredentials = ({ clientId, clientSecret, username, password }) => {
console.debug('getTokenWithCredentials')
const url = `${instance}/oauth/token`
const url = `/oauth/token`
const form = new window.FormData()
form.append('client_id', clientId)
@ -68,9 +68,9 @@ const getTokenWithCredentials = ({ clientId, clientSecret, instance, username, p
}).then((data) => data.json())
}
const getToken = ({ clientId, clientSecret, instance, code }) => {
const getToken = ({ clientId, clientSecret, code }) => {
console.debug('getToken')
const url = `${instance}/oauth/token`
const url = `/oauth/token`
const form = new window.FormData()
form.append('client_id', clientId)
@ -87,9 +87,9 @@ const getToken = ({ clientId, clientSecret, instance, code }) => {
}).then((data) => data.json())
}
export const getClientToken = ({ clientId, clientSecret, instance }) => {
export const getClientToken = ({ clientId, clientSecret }) => {
console.debug('getClientToken')
const url = `${instance}/oauth/token`
const url = `/oauth/token`
const form = new window.FormData()
form.append('client_id', clientId)

8
front/src/boot/after_store.js

@ -7,7 +7,7 @@ import { getOrCreateApp, getClientToken } from '../backend/oauth/oauth.js'
const getNodeInfo = async ({ store }) => {
try {
const res = await window.fetch(`${store.state.instance.instanceUrl}/nodeinfo/2.0`)
const res = await window.fetch(`/nodeinfo/2.0`)
if (res.ok) {
const data = await res.json()
@ -48,9 +48,9 @@ const checkOAuthToken = async ({ store }) => {
const getAppSecret = async ({ store }) => {
const { state, commit } = store
const { oauth, instance } = state
return getOrCreateApp({ ...oauth, instance: instance.instanceUrl, commit })
.then((app) => getClientToken({ ...app, instance: instance.instanceUrl }))
const { oauth } = state
return getOrCreateApp({ ...oauth, commit })
.then((app) => getClientToken({ ...app }))
.then((token) => {
commit('setAppToken', token.access_token)
})

1
front/src/components/login_form/login_form.vue

@ -49,7 +49,6 @@ export default {
const data = {
clientId,
oauth: this.oauth,
instance: this.instance.instanceUrl,
commit: this.$store.commit
}
this.error = false

1
front/src/modules/instance.js

@ -6,7 +6,6 @@ const defaultState = {
description: 'A reel2bits instance',
backendVersion: '',
frontendVersion: '',
instanceUrl: '',
tos: ''
}

4
front/src/modules/tracks.js

@ -9,6 +9,7 @@ export const mutations = {
uploadSuccess (state, data) {
state.uploadPending = false
state.uploadId = data.id
state.uploadSlug = data.slug
},
uploadFailure (state, errors) {
state.uploadPending = false
@ -19,7 +20,8 @@ export const mutations = {
export const defaultState = {
uploadPending: false,
uploadErrors: [],
uploadId: null
uploadId: null,
uploadSlug: null
}
export const getters = {

2
front/src/views/tracks/Upload.vue

@ -163,7 +163,7 @@ export default {
try {
console.debug('track upload: uploading')
await this.uploadTrack(this.track)
this.$router.push({ name: 'tracks-show', params: { username: this.$store.state.users.currentUser.screen_name, trackname: this.$store.state.tracks.uploadId } })
this.$router.push({ name: 'tracks-show', params: { username: this.$store.state.users.currentUser.screen_name, trackname: this.$store.state.tracks.uploadSlug } })
} catch (error) {
console.warn('Upload failed: ' + error)
}

2
models.py

@ -23,7 +23,7 @@ from little_boxes import activitypub as ap
from urllib.parse import urlparse
from authlib.flask.oauth2.sqla import OAuth2ClientMixin, OAuth2AuthorizationCodeMixin, OAuth2TokenMixin
import time
from utils import gen_flakeid
from flake_id import gen_flakeid
import uuid
db = SQLAlchemy()

22
utils.py

@ -3,9 +3,6 @@ import hashlib
import os
import subprocess
from os.path import splitext
import time
import random
import threading
from flask import current_app
from flask_security import current_user
@ -260,22 +257,3 @@ def get_hashed_filename(filename):
fs_fname = fs_fname.hexdigest()
return fs_fname + f_e
def gen_flakeid():
# (64b ?) timestamp in ns
t = str(time.time_ns())
# (48b) "worker id", rand
w = random.getrandbits(48)
pid = os.getpid()
tid = threading.get_ident()
ptid = f"{pid}{tid}"
# (16b) sequence from PID+threadID
# might be possible to throw in the
# object id of the flask request
s = hash(ptid) & 65536
return int(f"{t}{w}{s}")

Loading…
Cancel
Save