add Public list, peers can add items to that list

This commit is contained in:
j 2019-01-17 23:49:23 +05:30
parent dd0e22a979
commit f05a212a82
8 changed files with 55 additions and 13 deletions

View file

@ -76,3 +76,14 @@ def api_cancelPeering(user_id, message):
trigger_event('peering.cancel', user.json())
return True
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

View file

@ -86,8 +86,8 @@ def api_call(action, user_id, args):
content = getattr(nodeapi, 'api_' + action)(user_id, *args)
else:
if u and u.pending:
logger.debug('ignore request from pending peer[%s] %s (%s)',
user_id, action, args)
logger.debug('ignore request from pending peer[%s] %s (%s) (pending state: %s)',
user_id, action, args, u.pending)
content = {}
else:
content = None
@ -191,13 +191,13 @@ class Handler(http.server.SimpleHTTPRequestHandler):
u = user.models.User.get(user_id)
if not u:
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':
u.update_peering(True)
state.nodes.queue('add', u.id, True)
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:
return self._denied()
path = changelog_path()

View file

@ -132,7 +132,7 @@ class Node(Thread):
logger.debug('403: %s (%s)', url, self.user_id)
if state.tasks:
state.tasks.queue('peering', (self.user_id, False))
del self._nodes[self.user_id]
del self._nodes._nodes[self.user_id]
self.online = False
return None
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)
if state.tasks:
state.tasks.queue('peering', (self.user_id, False))
del self._nodes[self.user_id]
del self._nodes._nodes[self.user_id]
self.online = False
else:
logger.debug('unknown http errpr %s %s (%s)', e.code, url, self.user_id)
@ -461,6 +461,11 @@ class Node(Thread):
else:
return False
def upload(self, items):
r = self.request('upload', items)
return bool(r)
class Nodes(Thread):
_nodes = {}
local = None

View file

@ -43,6 +43,7 @@ class Tasks(Thread):
from user.models import (
export_list, update_user_peering,
add_local_info, remove_local_info,
upload
)
shutdown = False
while not shutdown:
@ -81,6 +82,8 @@ class Tasks(Thread):
item.scan.import_folder()
elif action == 'syncmetadata':
sync_metadata(data)
elif action == 'upload':
upload(data)
else:
trigger_event('error', {'error': 'unknown action'})
if DEUBG_TASKS:

View file

@ -178,6 +178,7 @@ def getLists(data):
}
'''
from item.models import Item
from user.models import List
lists = []
lists.append({
'id': '',
@ -186,6 +187,7 @@ def getLists(data):
'type': 'libraries',
'user': None,
})
List.get_or_create(':Public')
for u in models.User.query.filter((models.User.peered==True)|(models.User.id==settings.USER_ID)):
lists += u.lists_json()
return {
@ -286,7 +288,7 @@ def removeList(data):
}
'''
l = models.List.get(data['id'])
if l:
if l and l.name != 'Public':
l.remove()
return {}
actions.register(removeList, cache=False)
@ -305,10 +307,16 @@ def addListItems(data):
i = Item.get(item_id)
i.queue_download()
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'])
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:')
return l.json()
return {}

View file

@ -622,3 +622,14 @@ def add_local_info(data):
u.save()
if state.nodes:
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))

View file

@ -229,8 +229,8 @@ oml.ui.folders = function() {
);
}
},
'delete': function() {
!index && oml.ui.deleteListDialog().open();
'delete': function(data) {
!index && !Ox.contains(data.ids, ':Public') && oml.ui.deleteListDialog().open();
},
key_control_d: function() {
oml.addList(ui._list);

View file

@ -334,6 +334,10 @@ oml.enableDragAndDrop = function($list, canMove) {
data.type == 'library'
&& drag.source.user != ''
&& data.user == ''
) || (
data.type == 'static'
&& data.name == 'Public'
&& drag.source.user == ''
),
selected: data.id == ui._list
}, data);
@ -503,7 +507,7 @@ oml.enableDragAndDrop = function($list, canMove) {
Ox._(itemText[0] == '"' ? '' : 'These ') + itemText,
targetText
]);
} else if (drag.target.user != '') {
} else if (drag.target.user != '' && drag.target.name != 'Public') {
text = Ox._(
'You can only {0} books<br>to your own {1}.',
[actionText, drag.target.type == 'library' ? 'library' : 'lists']