Skip to content

Commit

Permalink
moving socket sessions from db to memory
Browse files Browse the repository at this point in the history
  • Loading branch information
F-Node-Karlsruhe committed Jul 1, 2021
1 parent 3d6f0d1 commit dca185e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 28 deletions.
2 changes: 0 additions & 2 deletions admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from database.db import db
from database.models.access import Access
from database.models.authors import Author
from database.models.sessions import Session
from database.models.slugs import Slug


Expand Down Expand Up @@ -52,5 +51,4 @@ class PkModelView(AuthenticatedModelView):

admin.add_view(PkModelView(Access, db.session, name='Access', category='Database'))
admin.add_view(PkModelView(Author, db.session, name='Authors', category='Database'))
admin.add_view(PkModelView(Session, db.session, name='Sessions', category='Database'))
admin.add_view(PkModelView(Slug, db.session, name='Posts', category='Database'))
4 changes: 2 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
get_access,
get_slug_data,
get_author_address,
set_session,
set_socket_session,
access_expired)


Expand Down Expand Up @@ -124,7 +124,7 @@ def proxy(slug):
@socketio.on('await_payment')
def await_payment(data):

set_session(data['user_token_hash'], request.sid)
set_socket_session(data['user_token_hash'], request.sid)



Expand Down
7 changes: 0 additions & 7 deletions database/models/sessions.py

This file was deleted.

25 changes: 10 additions & 15 deletions database/operations.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from datetime import datetime
from database.db import db

from database.models.access import Access
from database.models.authors import Author
from database.models.slugs import Slug
from database.models.sessions import Session

from services.ghost_api import slug_exists, get_post_data
from config import DEFAULT_IOTA_ADDRESS, AUTHOR_ADDRESSES

socket_sessions = {}


def check_slug(slug, iota_listener):

Expand Down Expand Up @@ -99,25 +99,20 @@ def get_slug_price_for_hash(user_token_hash):



def set_session(user_token_hash, session_id):

session = Session.query.get(user_token_hash)

if not session:
def set_socket_session(user_token_hash, session_id):

session = Session(token_hash=user_token_hash, session_id=session_id)
socket_sessions[user_token_hash] = session_id


db.session.add(session)
db.session.commit()
def get_socket_session(user_token_hash):

else:
try:

session.session_id = session_id
db.session.commit()
return socket_sessions[user_token_hash]

def get_socket_session(user_token_hash):
except KeyError:

return Session.query.get(user_token_hash)
return None


def add_access(user_token_hash, exp_date=None):
Expand Down
4 changes: 2 additions & 2 deletions services/iota.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def unlock_content(self, user_token_hash, exp_time=None):
if socket_session:

# emit pamyent received event to the user
self.socketio.emit('payment_received', room=socket_session.session_id)
self.socketio.emit('payment_received', room=socket_session)


def payment_valid(self, message, user_token_hash):
Expand Down Expand Up @@ -165,7 +165,7 @@ def manual_payment_check(self, app, address, user_token_hash):
if socket_session:

# emit pamyent not found
self.socketio.emit('payment_not_found', room=socket_session.session_id)
self.socketio.emit('payment_not_found', room=socket_session)

self.manual_payment_checks.remove(user_token_hash)

Expand Down

0 comments on commit dca185e

Please sign in to comment.