openmedialibrary/oml/node/nodeapi.py

79 lines
2.3 KiB
Python
Raw Normal View History

2014-05-17 02:14:15 +02:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
2014-09-03 00:32:44 +02:00
2014-05-17 02:14:15 +02:00
2014-05-04 19:26:43 +02:00
from user.models import User
from websocket import trigger_event
2014-08-12 10:16:57 +02:00
import state
2016-01-17 18:42:56 +05:30
import settings
2014-05-04 19:26:43 +02:00
2014-05-17 16:26:59 +02:00
import logging
2015-11-29 15:56:38 +01:00
logger = logging.getLogger(__name__)
2014-05-17 16:26:59 +02:00
2014-08-09 18:33:59 +02:00
def api_requestPeering(user_id, username, message):
event = 'peering.request'
2014-05-04 19:26:43 +02:00
user = User.get_or_create(user_id)
if not user.peered:
2016-01-17 18:42:56 +05:30
pref = settings.preferences.get('receivedRequests')
if pref == 'reject':
return True
if user.pending == 'sent' or pref == 'accept':
2014-05-04 19:26:43 +02:00
user.info['message'] = message
user.update_peering(True, username)
2014-05-25 23:19:56 +02:00
user.update_name()
event = 'peering.accept'
2014-05-04 19:26:43 +02:00
else:
user.pending = 'received'
user.info['username'] = username
user.info['message'] = message
2014-05-25 23:19:56 +02:00
user.update_name()
2014-05-04 19:26:43 +02:00
user.save()
trigger_event(event, user.json())
2016-01-17 18:42:56 +05:30
if user.peered:
state.nodes.queue('add', user.id, True)
2014-05-04 19:26:43 +02:00
return True
return False
2014-08-09 18:33:59 +02:00
def api_acceptPeering(user_id, username, message):
2014-05-04 19:26:43 +02:00
user = User.get(user_id)
2016-02-10 19:32:32 +05:30
if user:
logger.debug('incoming acceptPeering event: pending: %s', user.pending)
if user.pending == 'sent':
user.info['username'] = username
user.info['message'] = message
user.update_name()
user.update_peering(True, username)
state.nodes.queue('add', user.id, True)
trigger_event('peering.accept', user.json())
return True
elif user.peered:
return True
2014-05-04 19:26:43 +02:00
return False
2014-08-09 18:33:59 +02:00
def api_rejectPeering(user_id, message):
2014-05-04 19:26:43 +02:00
user = User.get(user_id)
if user:
user.info['message'] = message
user.update_peering(False)
trigger_event('peering.reject', user.json())
2014-05-04 19:26:43 +02:00
return True
return False
2014-08-09 18:33:59 +02:00
def api_removePeering(user_id, message):
2014-05-04 19:26:43 +02:00
user = User.get(user_id)
if user:
user.info['message'] = message
2014-05-13 01:43:27 +02:00
user.update_peering(False)
trigger_event('peering.remove', user.json())
2014-05-13 01:43:27 +02:00
return True
return False
2014-08-09 18:33:59 +02:00
def api_cancelPeering(user_id, message):
2014-05-13 01:43:27 +02:00
user = User.get(user_id)
if user:
user.info['message'] = message
user.update_peering(False)
trigger_event('peering.cancel', user.json())
2014-05-04 19:26:43 +02:00
return True
return False