From f05a212a8251f2bfbf1ed07b530ef21bff4b18bf Mon Sep 17 00:00:00 2001 From: j Date: Thu, 17 Jan 2019 23:49:23 +0530 Subject: [PATCH] add Public list, peers can add items to that list --- oml/node/nodeapi.py | 11 +++++++++++ oml/node/server.py | 10 +++++----- oml/nodes.py | 9 +++++++-- oml/tasks.py | 3 +++ oml/user/api.py | 14 +++++++++++--- oml/user/models.py | 11 +++++++++++ static/js/folders.js | 4 ++-- static/js/utils.js | 6 +++++- 8 files changed, 55 insertions(+), 13 deletions(-) diff --git a/oml/node/nodeapi.py b/oml/node/nodeapi.py index 4d9bc8b..7355b94 100644 --- a/oml/node/nodeapi.py +++ b/oml/node/nodeapi.py @@ -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 diff --git a/oml/node/server.py b/oml/node/server.py index 73b2e45..4673441 100644 --- a/oml/node/server.py +++ b/oml/node/server.py @@ -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() diff --git a/oml/nodes.py b/oml/nodes.py index ea89cf7..60e3596 100644 --- a/oml/nodes.py +++ b/oml/nodes.py @@ -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 diff --git a/oml/tasks.py b/oml/tasks.py index fa75d42..af02c3c 100644 --- a/oml/tasks.py +++ b/oml/tasks.py @@ -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: diff --git a/oml/user/api.py b/oml/user/api.py index f824282..6ecd50b 100644 --- a/oml/user/api.py +++ b/oml/user/api.py @@ -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 {} diff --git a/oml/user/models.py b/oml/user/models.py index f5c812d..0e09306 100644 --- a/oml/user/models.py +++ b/oml/user/models.py @@ -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)) diff --git a/static/js/folders.js b/static/js/folders.js index 02bf4ce..cd5aa46 100644 --- a/static/js/folders.js +++ b/static/js/folders.js @@ -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); diff --git a/static/js/utils.js b/static/js/utils.js index 930f04c..d61736f 100644 --- a/static/js/utils.js +++ b/static/js/utils.js @@ -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
to your own {1}.', [actionText, drag.target.type == 'library' ? 'library' : 'lists']