openmedialibrary/oml/node/nodeapi.py

94 lines
2.7 KiB
Python
Raw Normal View History

2014-05-17 00:14:15 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
2014-09-02 22:32:44 +00:00
2014-05-17 00:14:15 +00:00
2014-05-04 17:26:43 +00:00
from changelog import Changelog
from user.models import User
from websocket import trigger_event
2014-08-12 08:16:57 +00:00
import state
2016-01-17 13:12:56 +00:00
import settings
2014-05-04 17:26:43 +00:00
2014-05-17 14:26:59 +00:00
import logging
2015-11-29 14:56:38 +00:00
logger = logging.getLogger(__name__)
2014-05-17 14:26:59 +00:00
2014-08-09 16:33:59 +00:00
def api_pullChanges(remote_id, user_id=None, from_=None, to=None):
2014-05-04 17:26:43 +00:00
if user_id and not from_ and not to:
from_ = user_id
user_id = None
if user_id and from_ and not to:
if isinstance(user_id, int):
to = from_
from_ = user_id
user_id = None
from_ = from_ or 0
if user_id:
return []
2016-01-13 09:58:06 +00:00
return Changelog.aggregated_changes(from_)
2014-05-04 17:26:43 +00:00
2014-08-09 16:33:59 +00:00
def api_requestPeering(user_id, username, message):
event = 'peering.request'
2014-05-04 17:26:43 +00:00
user = User.get_or_create(user_id)
if not user.peered:
2016-01-17 13:12:56 +00:00
pref = settings.preferences.get('receivedRequests')
if pref == 'reject':
return True
if user.pending == 'sent' or pref == 'accept':
2014-05-04 17:26:43 +00:00
user.info['message'] = message
user.update_peering(True, username)
2014-05-25 21:19:56 +00:00
user.update_name()
event = 'peering.accept'
2014-05-04 17:26:43 +00:00
else:
user.pending = 'received'
user.info['username'] = username
user.info['message'] = message
2014-05-25 21:19:56 +00:00
user.update_name()
2014-05-04 17:26:43 +00:00
user.save()
trigger_event(event, user.json())
2016-01-17 13:12:56 +00:00
if user.peered:
state.nodes.queue('add', user.id, True)
2014-05-04 17:26:43 +00:00
return True
return False
2014-08-09 16:33:59 +00:00
def api_acceptPeering(user_id, username, message):
2014-05-04 17:26:43 +00:00
user = User.get(user_id)
2016-02-10 14:02:32 +00:00
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 17:26:43 +00:00
return False
2014-08-09 16:33:59 +00:00
def api_rejectPeering(user_id, message):
2014-05-04 17:26:43 +00: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 17:26:43 +00:00
return True
return False
2014-08-09 16:33:59 +00:00
def api_removePeering(user_id, message):
2014-05-04 17:26:43 +00:00
user = User.get(user_id)
if user:
user.info['message'] = message
2014-05-12 23:43:27 +00:00
user.update_peering(False)
trigger_event('peering.remove', user.json())
2014-05-12 23:43:27 +00:00
return True
return False
2014-08-09 16:33:59 +00:00
def api_cancelPeering(user_id, message):
2014-05-12 23:43:27 +00: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 17:26:43 +00:00
return True
return False