add Public list, peers can add items to that list
This commit is contained in:
parent
dd0e22a979
commit
f05a212a82
8 changed files with 55 additions and 13 deletions
|
@ -76,3 +76,14 @@ def api_cancelPeering(user_id, message):
|
||||||
trigger_event('peering.cancel', user.json())
|
trigger_event('peering.cancel', user.json())
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def api_upload(user_id, items):
|
||||||
|
from user.models import List
|
||||||
|
peer = User.get(user_id)
|
||||||
|
if peer:
|
||||||
|
l = List.get_or_create(':Public')
|
||||||
|
if l:
|
||||||
|
l.add_items(items)
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
|
@ -86,8 +86,8 @@ def api_call(action, user_id, args):
|
||||||
content = getattr(nodeapi, 'api_' + action)(user_id, *args)
|
content = getattr(nodeapi, 'api_' + action)(user_id, *args)
|
||||||
else:
|
else:
|
||||||
if u and u.pending:
|
if u and u.pending:
|
||||||
logger.debug('ignore request from pending peer[%s] %s (%s)',
|
logger.debug('ignore request from pending peer[%s] %s (%s) (pending state: %s)',
|
||||||
user_id, action, args)
|
user_id, action, args, u.pending)
|
||||||
content = {}
|
content = {}
|
||||||
else:
|
else:
|
||||||
content = None
|
content = None
|
||||||
|
@ -191,13 +191,13 @@ class Handler(http.server.SimpleHTTPRequestHandler):
|
||||||
u = user.models.User.get(user_id)
|
u = user.models.User.get(user_id)
|
||||||
if not u:
|
if not u:
|
||||||
return self._denied()
|
return self._denied()
|
||||||
if u.pending:
|
|
||||||
logger.debug('ignore request from pending peer[%s]', user_id)
|
|
||||||
return self._denied()
|
|
||||||
if not u.peered and u.pending == 'sent':
|
if not u.peered and u.pending == 'sent':
|
||||||
u.update_peering(True)
|
u.update_peering(True)
|
||||||
state.nodes.queue('add', u.id, True)
|
state.nodes.queue('add', u.id, True)
|
||||||
trigger_event('peering.accept', u.json())
|
trigger_event('peering.accept', u.json())
|
||||||
|
if u.pending:
|
||||||
|
logger.debug('ignore request from pending peer[%s] changelog (pending sate: %s)', user_id, u.pending)
|
||||||
|
return self._denied()
|
||||||
if not u.peered:
|
if not u.peered:
|
||||||
return self._denied()
|
return self._denied()
|
||||||
path = changelog_path()
|
path = changelog_path()
|
||||||
|
|
|
@ -132,7 +132,7 @@ class Node(Thread):
|
||||||
logger.debug('403: %s (%s)', url, self.user_id)
|
logger.debug('403: %s (%s)', url, self.user_id)
|
||||||
if state.tasks:
|
if state.tasks:
|
||||||
state.tasks.queue('peering', (self.user_id, False))
|
state.tasks.queue('peering', (self.user_id, False))
|
||||||
del self._nodes[self.user_id]
|
del self._nodes._nodes[self.user_id]
|
||||||
self.online = False
|
self.online = False
|
||||||
return None
|
return None
|
||||||
logger.debug('urllib2.HTTPError %s %s', e, e.code)
|
logger.debug('urllib2.HTTPError %s %s', e, e.code)
|
||||||
|
@ -260,7 +260,7 @@ class Node(Thread):
|
||||||
logger.debug('pullChanges 403: %s (%s)', url, self.user_id)
|
logger.debug('pullChanges 403: %s (%s)', url, self.user_id)
|
||||||
if state.tasks:
|
if state.tasks:
|
||||||
state.tasks.queue('peering', (self.user_id, False))
|
state.tasks.queue('peering', (self.user_id, False))
|
||||||
del self._nodes[self.user_id]
|
del self._nodes._nodes[self.user_id]
|
||||||
self.online = False
|
self.online = False
|
||||||
else:
|
else:
|
||||||
logger.debug('unknown http errpr %s %s (%s)', e.code, url, self.user_id)
|
logger.debug('unknown http errpr %s %s (%s)', e.code, url, self.user_id)
|
||||||
|
@ -461,6 +461,11 @@ class Node(Thread):
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def upload(self, items):
|
||||||
|
r = self.request('upload', items)
|
||||||
|
return bool(r)
|
||||||
|
|
||||||
|
|
||||||
class Nodes(Thread):
|
class Nodes(Thread):
|
||||||
_nodes = {}
|
_nodes = {}
|
||||||
local = None
|
local = None
|
||||||
|
|
|
@ -43,6 +43,7 @@ class Tasks(Thread):
|
||||||
from user.models import (
|
from user.models import (
|
||||||
export_list, update_user_peering,
|
export_list, update_user_peering,
|
||||||
add_local_info, remove_local_info,
|
add_local_info, remove_local_info,
|
||||||
|
upload
|
||||||
)
|
)
|
||||||
shutdown = False
|
shutdown = False
|
||||||
while not shutdown:
|
while not shutdown:
|
||||||
|
@ -81,6 +82,8 @@ class Tasks(Thread):
|
||||||
item.scan.import_folder()
|
item.scan.import_folder()
|
||||||
elif action == 'syncmetadata':
|
elif action == 'syncmetadata':
|
||||||
sync_metadata(data)
|
sync_metadata(data)
|
||||||
|
elif action == 'upload':
|
||||||
|
upload(data)
|
||||||
else:
|
else:
|
||||||
trigger_event('error', {'error': 'unknown action'})
|
trigger_event('error', {'error': 'unknown action'})
|
||||||
if DEUBG_TASKS:
|
if DEUBG_TASKS:
|
||||||
|
|
|
@ -178,6 +178,7 @@ def getLists(data):
|
||||||
}
|
}
|
||||||
'''
|
'''
|
||||||
from item.models import Item
|
from item.models import Item
|
||||||
|
from user.models import List
|
||||||
lists = []
|
lists = []
|
||||||
lists.append({
|
lists.append({
|
||||||
'id': '',
|
'id': '',
|
||||||
|
@ -186,6 +187,7 @@ def getLists(data):
|
||||||
'type': 'libraries',
|
'type': 'libraries',
|
||||||
'user': None,
|
'user': None,
|
||||||
})
|
})
|
||||||
|
List.get_or_create(':Public')
|
||||||
for u in models.User.query.filter((models.User.peered==True)|(models.User.id==settings.USER_ID)):
|
for u in models.User.query.filter((models.User.peered==True)|(models.User.id==settings.USER_ID)):
|
||||||
lists += u.lists_json()
|
lists += u.lists_json()
|
||||||
return {
|
return {
|
||||||
|
@ -286,7 +288,7 @@ def removeList(data):
|
||||||
}
|
}
|
||||||
'''
|
'''
|
||||||
l = models.List.get(data['id'])
|
l = models.List.get(data['id'])
|
||||||
if l:
|
if l and l.name != 'Public':
|
||||||
l.remove()
|
l.remove()
|
||||||
return {}
|
return {}
|
||||||
actions.register(removeList, cache=False)
|
actions.register(removeList, cache=False)
|
||||||
|
@ -305,10 +307,16 @@ def addListItems(data):
|
||||||
i = Item.get(item_id)
|
i = Item.get(item_id)
|
||||||
i.queue_download()
|
i.queue_download()
|
||||||
i.update()
|
i.update()
|
||||||
elif data['list']:
|
elif data['list'] and (data['list'].startswith(':') or data['list'].endswith(':Public')):
|
||||||
l = models.List.get_or_create(data['list'])
|
l = models.List.get_or_create(data['list'])
|
||||||
if l:
|
if l:
|
||||||
l.add_items(data['items'])
|
if l.name == 'Public' and l.user_id != settings.USER_ID:
|
||||||
|
state.tasks.queue('upload', {
|
||||||
|
'user': l.user_id,
|
||||||
|
'items': data['items']
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
l.add_items(data['items'])
|
||||||
state.cache.clear('group:')
|
state.cache.clear('group:')
|
||||||
return l.json()
|
return l.json()
|
||||||
return {}
|
return {}
|
||||||
|
|
|
@ -622,3 +622,14 @@ def add_local_info(data):
|
||||||
u.save()
|
u.save()
|
||||||
if state.nodes:
|
if state.nodes:
|
||||||
state.nodes.queue('add', u.id)
|
state.nodes.queue('add', u.id)
|
||||||
|
|
||||||
|
|
||||||
|
def upload(data):
|
||||||
|
delay = 60
|
||||||
|
with db.session():
|
||||||
|
u = User.get(data['user'])
|
||||||
|
if u:
|
||||||
|
if u.is_online() and state.nodes._nodes[u.id].upload(data['items']):
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
state.main.call_later(delay, lambda: state.tasks.queue('upload', data))
|
||||||
|
|
|
@ -229,8 +229,8 @@ oml.ui.folders = function() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'delete': function() {
|
'delete': function(data) {
|
||||||
!index && oml.ui.deleteListDialog().open();
|
!index && !Ox.contains(data.ids, ':Public') && oml.ui.deleteListDialog().open();
|
||||||
},
|
},
|
||||||
key_control_d: function() {
|
key_control_d: function() {
|
||||||
oml.addList(ui._list);
|
oml.addList(ui._list);
|
||||||
|
|
|
@ -334,6 +334,10 @@ oml.enableDragAndDrop = function($list, canMove) {
|
||||||
data.type == 'library'
|
data.type == 'library'
|
||||||
&& drag.source.user != ''
|
&& drag.source.user != ''
|
||||||
&& data.user == ''
|
&& data.user == ''
|
||||||
|
) || (
|
||||||
|
data.type == 'static'
|
||||||
|
&& data.name == 'Public'
|
||||||
|
&& drag.source.user == ''
|
||||||
),
|
),
|
||||||
selected: data.id == ui._list
|
selected: data.id == ui._list
|
||||||
}, data);
|
}, data);
|
||||||
|
@ -503,7 +507,7 @@ oml.enableDragAndDrop = function($list, canMove) {
|
||||||
Ox._(itemText[0] == '"' ? '' : 'These ') + itemText,
|
Ox._(itemText[0] == '"' ? '' : 'These ') + itemText,
|
||||||
targetText
|
targetText
|
||||||
]);
|
]);
|
||||||
} else if (drag.target.user != '') {
|
} else if (drag.target.user != '' && drag.target.name != 'Public') {
|
||||||
text = Ox._(
|
text = Ox._(
|
||||||
'You can only {0} books<br>to your own {1}.',
|
'You can only {0} books<br>to your own {1}.',
|
||||||
[actionText, drag.target.type == 'library' ? 'library' : 'lists']
|
[actionText, drag.target.type == 'library' ? 'library' : 'lists']
|
||||||
|
|
Loading…
Reference in a new issue