openmedialibrary/oml/node/nodeapi.py

91 lines
2.7 KiB
Python
Raw Normal View History

2014-05-17 00:14:15 +00:00
# -*- coding: utf-8 -*-
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 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_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):
2019-01-30 12:30:38 +00:00
user = User.get(user_id, for_update=True)
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):
2019-01-30 12:30:38 +00:00
user = User.get(user_id, for_update=True)
2014-05-04 17:26:43 +00:00
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):
2019-01-30 12:30:38 +00:00
user = User.get(user_id, for_update=True)
2014-05-04 17:26:43 +00:00
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):
2019-01-30 12:30:38 +00:00
user = User.get(user_id, for_update=True)
2014-05-12 23:43:27 +00:00
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
def api_upload(user_id, items):
from user.models import List
peer = User.get(user_id)
if peer:
2019-01-28 10:09:00 +00:00
l = List.get_or_create(':Inbox')
if l:
2019-01-28 10:09:00 +00:00
logger.debug('%s added items to inbox: %s', user_id, items)
l.add_items(items)
2019-01-19 11:42:01 +00:00
trigger_event('change', {})
return True
return False