2014-05-04 17:26:43 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
2014-09-02 22:32:44 +00:00
|
|
|
|
2017-06-03 20:50:14 +00:00
|
|
|
import os
|
2014-05-04 17:26:43 +00:00
|
|
|
from datetime import datetime
|
2014-08-12 08:16:57 +00:00
|
|
|
import json
|
2014-05-04 17:26:43 +00:00
|
|
|
|
2014-08-12 08:16:57 +00:00
|
|
|
import sqlalchemy as sa
|
2016-07-03 11:51:51 +00:00
|
|
|
from sqlalchemy.sql.expression import text
|
2014-05-04 17:26:43 +00:00
|
|
|
|
2017-08-19 16:19:59 +00:00
|
|
|
from utils import datetime2ts, ts2datetime, makefolder
|
2014-08-12 08:16:57 +00:00
|
|
|
from websocket import trigger_event
|
2014-08-09 15:03:16 +00:00
|
|
|
import db
|
2014-08-12 08:16:57 +00:00
|
|
|
import settings
|
2014-05-04 17:26:43 +00:00
|
|
|
import state
|
|
|
|
|
2014-08-12 08:16:57 +00:00
|
|
|
import logging
|
2015-11-29 14:56:25 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2014-05-17 14:26:59 +00:00
|
|
|
|
2017-06-03 20:50:14 +00:00
|
|
|
|
|
|
|
def changelog_path():
|
|
|
|
return os.path.join(settings.data_path, 'peers', '%s.log' % settings.USER_ID)
|
|
|
|
|
|
|
|
def next_revision():
|
|
|
|
settings.server['revision'] = settings.server.get('revision', -1) + 1
|
|
|
|
return settings.server['revision']
|
|
|
|
|
|
|
|
def add_record(action, *args, **kwargs):
|
|
|
|
if '_ts' in kwargs:
|
|
|
|
timestamp = kwargs['_ts']
|
|
|
|
del kwargs['_ts']
|
|
|
|
else:
|
|
|
|
timestamp = None
|
|
|
|
if not timestamp:
|
|
|
|
timestamp = datetime.utcnow()
|
|
|
|
timestamp = datetime2ts(timestamp)
|
|
|
|
revision = next_revision()
|
|
|
|
|
|
|
|
data = [revision, timestamp, [action] + list(args)]
|
|
|
|
data = json.dumps(data, ensure_ascii=False)
|
|
|
|
|
|
|
|
path = changelog_path()
|
|
|
|
if os.path.exists(path):
|
|
|
|
mode = 'a'
|
|
|
|
state.changelog_size = os.path.getsize(path)
|
|
|
|
else:
|
|
|
|
mode = 'w'
|
|
|
|
state.changelog_size = 0
|
2017-08-19 16:19:59 +00:00
|
|
|
makefolder(path)
|
2017-06-03 20:50:14 +00:00
|
|
|
with open(path, mode) as fd:
|
|
|
|
fd.write(data + '\n')
|
|
|
|
state.changelog_size = os.path.getsize(path)
|
|
|
|
#logger.debug('record change: %s', data)
|
|
|
|
|
|
|
|
def changelog_size():
|
|
|
|
if state.changelog_size is None:
|
|
|
|
path = changelog_path()
|
|
|
|
if not os.path.exists(path):
|
|
|
|
return 0
|
|
|
|
return os.path.getsize(path)
|
|
|
|
else:
|
|
|
|
return state.changelog_size
|
|
|
|
|
2014-05-04 17:26:43 +00:00
|
|
|
class Changelog(db.Model):
|
|
|
|
'''
|
|
|
|
additem itemid metadata from file (info) + OLID
|
|
|
|
edititem itemid name->id (i.e. olid-> OL...M)
|
|
|
|
removeitem itemid
|
|
|
|
addlist name
|
|
|
|
editlist name {name: newname}
|
|
|
|
orderlists [name, name, name]
|
|
|
|
removelist name
|
2014-05-12 12:57:47 +00:00
|
|
|
addlistitems listname [ids]
|
|
|
|
removelistitems listname [ids]
|
2014-05-04 17:26:43 +00:00
|
|
|
editusername username
|
|
|
|
editcontact string
|
|
|
|
addpeer peerid peername
|
|
|
|
removepeer peerid peername
|
2016-01-21 06:14:01 +00:00
|
|
|
editpeer peerid {username: string, contact: string}
|
2014-05-04 17:26:43 +00:00
|
|
|
'''
|
2014-08-09 15:03:16 +00:00
|
|
|
__tablename__ = 'changelog'
|
|
|
|
id = sa.Column(sa.Integer(), primary_key=True)
|
2014-05-04 17:26:43 +00:00
|
|
|
|
2014-08-09 15:03:16 +00:00
|
|
|
created = sa.Column(sa.DateTime())
|
|
|
|
timestamp = sa.Column(sa.BigInteger())
|
2014-05-04 17:26:43 +00:00
|
|
|
|
2014-08-09 15:03:16 +00:00
|
|
|
user_id = sa.Column(sa.String(43))
|
|
|
|
revision = sa.Column(sa.BigInteger())
|
|
|
|
data = sa.Column(sa.Text())
|
|
|
|
sig = sa.Column(sa.String(96))
|
2014-05-04 17:26:43 +00:00
|
|
|
|
|
|
|
@classmethod
|
2016-01-24 06:35:58 +00:00
|
|
|
def record(cls, user, action, *args, **kwargs):
|
2017-06-03 20:50:14 +00:00
|
|
|
return add_record(action, *args, **kwargs)
|
2014-05-04 17:26:43 +00:00
|
|
|
|
2014-05-16 14:30:16 +00:00
|
|
|
@classmethod
|
2016-01-24 08:22:18 +00:00
|
|
|
def apply_changes(cls, user_, changes, first=False):
|
|
|
|
from user.models import User
|
|
|
|
user = user_
|
2015-11-26 00:26:10 +00:00
|
|
|
trigger = changes
|
2016-01-21 06:14:01 +00:00
|
|
|
if trigger:
|
2016-01-23 16:49:34 +00:00
|
|
|
trigger_event('change', {})
|
|
|
|
if first:
|
|
|
|
items = set()
|
|
|
|
lists = {}
|
|
|
|
peers = set()
|
|
|
|
for change in changes:
|
|
|
|
if change[2][0] == 'additem':
|
|
|
|
items.add(change[2][1])
|
|
|
|
if change[2][0] == 'addlist':
|
|
|
|
lists[change[2][1]] = set()
|
|
|
|
if change[2][0] == 'addlistitems':
|
|
|
|
if not change[2][1] in lists:
|
|
|
|
lists[change[2][1]] = set()
|
|
|
|
for i in change[2][2]:
|
|
|
|
lists[change[2][1]].add(i)
|
|
|
|
if change[2][0] == 'addpeer':
|
|
|
|
peers.add(change[2][1])
|
|
|
|
for i in user.library.items:
|
|
|
|
if i.id not in items and user in i.users:
|
|
|
|
i.users.remove(user)
|
|
|
|
if i.users:
|
|
|
|
i.update()
|
|
|
|
else:
|
|
|
|
i.delete()
|
|
|
|
for name in lists:
|
|
|
|
qs = user.lists.filter_by(name=name)
|
|
|
|
if qs.count():
|
|
|
|
l = qs[0]
|
|
|
|
for i in l.get_items():
|
|
|
|
if i.id not in lists[name]:
|
2016-01-24 08:22:18 +00:00
|
|
|
if i.id in l.items:
|
|
|
|
l.items.remove(i.id)
|
|
|
|
for peer in User.query:
|
2016-01-23 16:49:34 +00:00
|
|
|
if user.id in peer.info.get('users', {}) and peer.id not in peers:
|
|
|
|
del peer.info['users'][user.id]
|
|
|
|
peer.save()
|
|
|
|
|
2014-05-16 14:30:16 +00:00
|
|
|
for change in changes:
|
2016-02-04 13:12:37 +00:00
|
|
|
if state.shutdown:
|
|
|
|
return False
|
2016-01-14 13:36:07 +00:00
|
|
|
if user.id in state.removepeer:
|
|
|
|
del state.removepeer[user.id]
|
|
|
|
return False
|
2015-12-25 14:58:41 +00:00
|
|
|
if not cls.apply_change(user, change, trigger=False):
|
|
|
|
logger.debug('FAIL %s', change)
|
|
|
|
trigger = False
|
|
|
|
break
|
|
|
|
return False
|
2015-11-26 00:26:10 +00:00
|
|
|
if trigger:
|
2016-07-03 11:51:51 +00:00
|
|
|
trigger_event('change', {})
|
2014-05-16 14:30:16 +00:00
|
|
|
return True
|
|
|
|
|
2014-05-04 17:26:43 +00:00
|
|
|
@classmethod
|
2015-11-26 00:26:10 +00:00
|
|
|
def apply_change(cls, user, change, trigger=True):
|
|
|
|
revision, timestamp, data = change
|
2016-07-03 11:51:51 +00:00
|
|
|
last = cls.query.filter_by(user_id=user.id).order_by(text('-revision')).first()
|
2014-05-04 17:26:43 +00:00
|
|
|
next_revision = last.revision + 1 if last else 0
|
2015-12-01 14:15:18 +00:00
|
|
|
if revision >= next_revision:
|
2015-11-26 00:26:10 +00:00
|
|
|
c = cls()
|
|
|
|
c.created = datetime.utcnow()
|
|
|
|
c.timestamp = timestamp
|
|
|
|
c.user_id = user.id
|
|
|
|
c.revision = revision
|
2016-01-23 16:49:34 +00:00
|
|
|
c.data = json.dumps(data)
|
|
|
|
logger.debug('apply change from %s: %s(%s)', user.name, data[0], data[1:])
|
|
|
|
if getattr(c, 'action_' + data[0])(user, timestamp, *data[1:]):
|
2015-11-26 00:26:10 +00:00
|
|
|
logger.debug('change applied')
|
|
|
|
state.db.session.add(c)
|
|
|
|
state.db.session.commit()
|
|
|
|
if trigger:
|
2016-07-03 11:51:51 +00:00
|
|
|
trigger_event('change', {})
|
2015-11-26 00:26:10 +00:00
|
|
|
return True
|
2016-01-12 07:32:39 +00:00
|
|
|
else:
|
|
|
|
logger.debug('could not apply change')
|
2014-05-04 17:26:43 +00:00
|
|
|
else:
|
2014-05-17 14:26:59 +00:00
|
|
|
logger.debug('revsion does not match! got %s expecting %s', revision, next_revision)
|
2014-05-04 17:26:43 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.data
|
|
|
|
|
|
|
|
def json(self):
|
2014-05-20 17:03:31 +00:00
|
|
|
timestamp = self.timestamp or datetime2ts(self.created)
|
2016-01-23 16:49:34 +00:00
|
|
|
return [self.revision, timestamp, json.loads(self.data)]
|
2014-05-04 17:26:43 +00:00
|
|
|
|
|
|
|
def action_additem(self, user, timestamp, itemid, info):
|
|
|
|
from item.models import Item
|
|
|
|
i = Item.get(itemid)
|
2015-12-01 08:55:43 +00:00
|
|
|
if i:
|
2014-05-19 09:38:41 +00:00
|
|
|
if user not in i.users:
|
2016-01-06 18:06:48 +00:00
|
|
|
i.add_user(user)
|
2014-05-19 09:38:41 +00:00
|
|
|
i.update()
|
2015-12-01 08:55:43 +00:00
|
|
|
else:
|
2014-05-04 17:26:43 +00:00
|
|
|
i = Item.get_or_create(itemid, info)
|
2014-05-20 17:03:31 +00:00
|
|
|
i.modified = ts2datetime(timestamp)
|
2015-11-26 00:26:10 +00:00
|
|
|
if user not in i.users:
|
2016-01-06 18:06:48 +00:00
|
|
|
i.add_user(user)
|
2015-11-26 00:26:10 +00:00
|
|
|
i.update()
|
2014-05-04 17:26:43 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def action_edititem(self, user, timestamp, itemid, meta):
|
2016-01-11 13:43:54 +00:00
|
|
|
from user.models import Metadata
|
|
|
|
m = Metadata.get_or_create(user.id, itemid)
|
2016-01-19 10:05:16 +00:00
|
|
|
m.edit(meta, modified=ts2datetime(timestamp))
|
2014-05-04 17:26:43 +00:00
|
|
|
from item.models import Item
|
|
|
|
i = Item.get(itemid)
|
2016-01-13 09:58:06 +00:00
|
|
|
if i:
|
2016-01-19 10:05:16 +00:00
|
|
|
i.sync_metadata()
|
2016-01-24 07:44:43 +00:00
|
|
|
if state.tasks:
|
|
|
|
#state.tasks.queue('syncmetadata', [i.id])
|
|
|
|
state.tasks.queue('getpreview', i.id)
|
2014-05-04 17:26:43 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def action_removeitem(self, user, timestamp, itemid):
|
|
|
|
from item.models import Item
|
2016-01-11 13:43:54 +00:00
|
|
|
from user.models import Metadata
|
2014-05-04 17:26:43 +00:00
|
|
|
i = Item.get(itemid)
|
2015-12-01 08:55:43 +00:00
|
|
|
if i:
|
|
|
|
if user in i.users:
|
|
|
|
i.users.remove(user)
|
|
|
|
if i.users:
|
|
|
|
i.update()
|
|
|
|
else:
|
|
|
|
i.delete()
|
2016-01-12 05:44:16 +00:00
|
|
|
Metadata.query.filter_by(user_id=user.id, item_id=itemid).delete()
|
2014-05-04 17:26:43 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def action_addlist(self, user, timestamp, name, query=None):
|
|
|
|
from user.models import List
|
2016-01-07 11:09:52 +00:00
|
|
|
if name == '':
|
|
|
|
return True
|
2016-01-22 07:08:41 +00:00
|
|
|
l = List.get_or_create(user.id, name)
|
2016-01-13 16:16:31 +00:00
|
|
|
trigger_event('addlist', {'id': l.public_id, 'user': user.id})
|
2014-05-04 17:26:43 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def action_editlist(self, user, timestamp, name, new):
|
|
|
|
from user.models import List
|
|
|
|
l = List.get_or_create(user.id, name)
|
|
|
|
if 'name' in new:
|
|
|
|
l.name = new['name']
|
|
|
|
l.save()
|
2016-01-13 16:16:31 +00:00
|
|
|
trigger_event('editlist', {'id': l.public_id, 'user': user.id})
|
2014-05-04 17:26:43 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def action_orderlists(self, user, timestamp, lists):
|
|
|
|
from user.models import List
|
2014-05-25 18:06:12 +00:00
|
|
|
idx = 0
|
2014-05-04 17:26:43 +00:00
|
|
|
for name in lists:
|
|
|
|
l = List.get_or_create(user.id, name)
|
2014-05-25 18:06:12 +00:00
|
|
|
l.index_ = idx
|
2014-05-04 17:26:43 +00:00
|
|
|
l.save()
|
2014-05-25 18:06:12 +00:00
|
|
|
idx += 1
|
2016-01-13 16:16:31 +00:00
|
|
|
trigger_event('orderlists', {'user': user.id})
|
2014-05-04 17:26:43 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def action_removelist(self, user, timestamp, name):
|
|
|
|
from user.models import List
|
|
|
|
l = List.get(user.id, name)
|
|
|
|
if l:
|
|
|
|
l.remove()
|
2016-01-13 09:58:06 +00:00
|
|
|
trigger_event('removelist', {'id': l.public_id})
|
2014-05-04 17:26:43 +00:00
|
|
|
return True
|
|
|
|
|
2014-05-12 12:57:47 +00:00
|
|
|
def action_addlistitems(self, user, timestamp, name, ids):
|
2014-05-04 17:26:43 +00:00
|
|
|
from user.models import List
|
2014-05-12 12:57:47 +00:00
|
|
|
l = List.get_or_create(user.id, name)
|
|
|
|
l.add_items(ids)
|
2014-05-04 17:26:43 +00:00
|
|
|
return True
|
|
|
|
|
2014-05-12 23:43:27 +00:00
|
|
|
def action_removelistitems(self, user, timestamp, name, ids):
|
2014-05-04 17:26:43 +00:00
|
|
|
from user.models import List
|
|
|
|
l = List.get(user.id, name)
|
2014-05-12 12:57:47 +00:00
|
|
|
if l:
|
|
|
|
l.remove_items(ids)
|
2014-05-04 17:26:43 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def action_editusername(self, user, timestamp, username):
|
2014-05-25 18:06:12 +00:00
|
|
|
from user.models import List
|
|
|
|
old = user.nickname
|
2014-05-04 17:26:43 +00:00
|
|
|
user.info['username'] = username
|
2014-05-25 12:16:04 +00:00
|
|
|
user.update_name()
|
2014-05-25 18:06:12 +00:00
|
|
|
if old != user.nickname:
|
|
|
|
List.rename_user(old, user.nickname)
|
2016-01-21 06:14:01 +00:00
|
|
|
Changelog.record(state.user(), 'editpeer', self.id, self.json(['username']))
|
2014-05-04 17:26:43 +00:00
|
|
|
user.save()
|
|
|
|
return True
|
|
|
|
|
|
|
|
def action_editcontact(self, user, timestamp, contact):
|
2016-01-21 06:14:01 +00:00
|
|
|
if user.info.get('contact') != contact:
|
|
|
|
user.info['contact'] = contact
|
|
|
|
user.save()
|
|
|
|
Changelog.record(state.user(), 'editpeer', user.id, user.json(['contact']))
|
2014-05-04 17:26:43 +00:00
|
|
|
return True
|
|
|
|
|
2014-05-12 23:43:27 +00:00
|
|
|
def action_addpeer(self, user, timestamp, peerid, username):
|
2015-11-29 14:56:25 +00:00
|
|
|
if len(peerid) == 16:
|
|
|
|
from user.models import User
|
|
|
|
if not 'users' in user.info:
|
|
|
|
user.info['users'] = {}
|
|
|
|
user.info['users'][peerid] = username
|
|
|
|
user.save()
|
|
|
|
peer = User.get_or_create(peerid)
|
|
|
|
if not 'username' in peer.info:
|
|
|
|
peer.info['username'] = username
|
|
|
|
peer.update_name()
|
|
|
|
peer.save()
|
2014-05-04 17:26:43 +00:00
|
|
|
return True
|
|
|
|
|
2014-05-12 23:43:27 +00:00
|
|
|
def action_removepeer(self, user, timestamp, peerid):
|
2014-05-04 17:26:43 +00:00
|
|
|
if 'users' in user.info and peerid in user.info['users']:
|
|
|
|
del user.info['users'][peerid]
|
|
|
|
user.save()
|
|
|
|
#fixme, remove from User table if no other connection exists
|
|
|
|
return True
|
2014-05-21 00:02:21 +00:00
|
|
|
|
2016-01-21 06:14:01 +00:00
|
|
|
def action_editpeer(self, user, timestamp, peerid, data):
|
|
|
|
if len(peerid) == 16:
|
|
|
|
from user.models import User
|
|
|
|
peer = User.get_or_create(peerid)
|
|
|
|
update = False
|
|
|
|
if not peer.peered:
|
|
|
|
for key in ('username', 'contact'):
|
|
|
|
if key in data and peer.info.get(key) != data[key]:
|
|
|
|
peer.info[key] = data[key]
|
|
|
|
update = True
|
|
|
|
if update:
|
|
|
|
peer.save()
|
2016-01-11 13:43:54 +00:00
|
|
|
return True
|
2016-01-21 06:14:01 +00:00
|
|
|
|
2016-01-13 09:58:06 +00:00
|
|
|
@classmethod
|
|
|
|
def aggregated_changes(cls, since=None, user_id=None):
|
2016-01-13 15:19:48 +00:00
|
|
|
from item.models import Item
|
2016-01-13 09:58:06 +00:00
|
|
|
from user.models import List
|
2016-01-21 06:14:01 +00:00
|
|
|
from user.models import User
|
2016-01-13 09:58:06 +00:00
|
|
|
if not user_id:
|
|
|
|
user_id = settings.USER_ID
|
|
|
|
qs = cls.query.filter_by(user_id=user_id)
|
|
|
|
qs = Changelog.query.filter_by(user_id=user_id)
|
|
|
|
if since:
|
|
|
|
qs = qs.filter(Changelog.revision>=since)
|
|
|
|
changes = {}
|
|
|
|
orderlists = False
|
2016-01-21 06:14:01 +00:00
|
|
|
editcontact = False
|
|
|
|
editusername = False
|
2016-01-13 09:58:06 +00:00
|
|
|
for c in qs.order_by('timestamp'):
|
|
|
|
revision = c.revision
|
|
|
|
timestamp = c.timestamp
|
|
|
|
data = json.loads(c.data)
|
|
|
|
op = data[0]
|
|
|
|
if op in ('editmeta', 'resetmeta'):
|
|
|
|
continue
|
|
|
|
action = changes.setdefault(op, {})
|
|
|
|
if op == 'additem':
|
|
|
|
item_id = data[1]
|
|
|
|
info = data[2]
|
|
|
|
action[item_id] = [revision, timestamp, info]
|
|
|
|
if item_id in changes.get('removeitem', []):
|
|
|
|
del changes['removeitem'][item_id]
|
2016-01-13 15:19:48 +00:00
|
|
|
i = Item.get(item_id)
|
|
|
|
if i:
|
|
|
|
changes.setdefault('edititem', {})[item_id] = [revision+1, timestamp, i.meta]
|
2016-01-13 09:58:06 +00:00
|
|
|
elif op == 'edititem':
|
|
|
|
item_id = data[1]
|
|
|
|
meta = data[2]
|
|
|
|
if not item_id in action:
|
|
|
|
action[item_id] = [revision, timestamp, meta]
|
|
|
|
else:
|
|
|
|
action[item_id][0] = revision
|
|
|
|
action[item_id][1] = timestamp
|
|
|
|
action[item_id][2].update(meta)
|
|
|
|
elif op == 'removeitem':
|
|
|
|
item_id = data[1]
|
|
|
|
if item_id in changes.get('additem', []):
|
|
|
|
del changes['additem'][item_id]
|
|
|
|
else:
|
|
|
|
action[item_id] = [revision, timestamp]
|
|
|
|
if item_id in changes.get('edititem', []):
|
|
|
|
del changes['edititem'][item_id]
|
|
|
|
elif op == 'addlist':
|
|
|
|
list_id = data[1]
|
|
|
|
if list_id:
|
|
|
|
ids = data[2] if len(data) > 2 else []
|
|
|
|
action[list_id] = [revision, timestamp, ids]
|
|
|
|
elif op == 'editlist':
|
|
|
|
old_id = data[1]
|
|
|
|
new_id = data[2]['name']
|
|
|
|
r = revision
|
|
|
|
if old_id not in changes.get('addlist', []):
|
2016-01-13 16:16:31 +00:00
|
|
|
action[old_id] = [revision, timestamp, {'name': new_id}]
|
2016-01-13 09:58:06 +00:00
|
|
|
r += 1
|
|
|
|
for a in ('addlist', 'addlistitems', 'removelistitems'):
|
|
|
|
if a in changes and old_id in changes[a]:
|
|
|
|
changes[a][new_id] = changes[a].pop(old_id)
|
|
|
|
changes[a][new_id][0] = r
|
|
|
|
elif op == 'orderlists':
|
|
|
|
orderlists = True
|
|
|
|
elif op == 'removelist':
|
|
|
|
list_id = data[1]
|
|
|
|
if list_id not in changes.get('addlist', []):
|
|
|
|
action[list_id] = [revision, timestamp]
|
|
|
|
for a in ('addlist', 'addlistitems', 'removelistitems'):
|
|
|
|
if a in changes and list_id in changes[a]:
|
|
|
|
del changes[a][list_id]
|
|
|
|
elif op == 'addlistitems':
|
|
|
|
list_id = data[1]
|
|
|
|
if not list_id:
|
|
|
|
continue
|
|
|
|
listitems = data[2]
|
|
|
|
if list_id not in action:
|
|
|
|
action[list_id] = [revision, timestamp, []]
|
|
|
|
action[list_id][0] = revision
|
|
|
|
action[list_id][1] = timestamp
|
|
|
|
action[list_id][2] += listitems
|
|
|
|
#remove from removelistitems!
|
|
|
|
if list_id in changes.get('remvelistitems', {}):
|
|
|
|
changes['remvelistitems'][list_id] = [
|
|
|
|
i for i in changes['remvelistitems'][list_id] if i not in listitems
|
|
|
|
]
|
|
|
|
elif op == 'removelistitems':
|
|
|
|
list_id = data[1]
|
|
|
|
listitems = data[2]
|
|
|
|
#remove from additemlists
|
|
|
|
removed = []
|
|
|
|
if list_id in changes.get('addlistitems',{}):
|
|
|
|
removed = [
|
|
|
|
i for i in changes['addlistitems'][list_id] if i in listitems
|
|
|
|
]
|
|
|
|
changes['addlistitems'][list_id] = [
|
|
|
|
i for i in changes['addlistitems'][list_id] if i not in listitems
|
|
|
|
]
|
|
|
|
#remove remaining items
|
|
|
|
listitems = [
|
|
|
|
i for i in listitems if i not in removed
|
|
|
|
]
|
|
|
|
if listitems:
|
|
|
|
action[list_id] = [revision, timestamp, listitems]
|
|
|
|
elif op == 'editusername':
|
2016-01-21 06:14:01 +00:00
|
|
|
editusername = True
|
2016-01-13 09:58:06 +00:00
|
|
|
elif op == 'editcontact':
|
2016-01-21 06:14:01 +00:00
|
|
|
editcontact = True
|
2016-01-13 09:58:06 +00:00
|
|
|
elif op == 'addpeer':
|
2016-01-21 06:14:01 +00:00
|
|
|
peer_id = data[1]
|
2016-01-13 15:19:48 +00:00
|
|
|
username = data[2]
|
2016-01-21 06:14:01 +00:00
|
|
|
if len(peer_id) == 16:
|
|
|
|
peer = User.get(peer_id)
|
|
|
|
if peer:
|
2016-03-13 17:45:08 +00:00
|
|
|
username = peer.json().get('username', 'anonymous')
|
2016-01-21 06:14:01 +00:00
|
|
|
action[peer_id] = [revision, timestamp, username]
|
|
|
|
if peer_id in changes.get('removepeer', []):
|
|
|
|
del changes['removepeer'][peer_id]
|
2016-01-13 09:58:06 +00:00
|
|
|
elif op == 'removepeer':
|
2016-01-21 06:14:01 +00:00
|
|
|
peer_id = data[1]
|
|
|
|
if peer_id in changes.get('addpeer', []):
|
|
|
|
del changes['addpeer'][peer_id]
|
2016-01-13 09:58:06 +00:00
|
|
|
else:
|
2016-01-21 06:14:01 +00:00
|
|
|
action[peer_id] = [revision, timestamp]
|
|
|
|
elif op == 'editpeer':
|
|
|
|
peer_id = data[1]
|
|
|
|
if not peer_id in action:
|
|
|
|
peer = User.get(peer_id)
|
|
|
|
if peer:
|
|
|
|
data = peer.json(['username', 'contact'])
|
|
|
|
action[peer_id] = [revision, timestamp, data]
|
2016-01-13 09:58:06 +00:00
|
|
|
else:
|
2016-02-07 12:54:07 +00:00
|
|
|
logger.debug('unknonw action %s', data)
|
2016-01-13 09:58:06 +00:00
|
|
|
_changes = []
|
|
|
|
for op in list(changes):
|
|
|
|
if not changes[op]:
|
|
|
|
del changes[op]
|
|
|
|
else:
|
|
|
|
for id in changes[op]:
|
|
|
|
data = changes[op][id]
|
|
|
|
rv = data[0]
|
|
|
|
ts = data[1]
|
|
|
|
data = [op, id] + data[2:]
|
2016-01-23 16:49:34 +00:00
|
|
|
_changes.append([rv, ts, data])
|
2016-01-13 09:58:06 +00:00
|
|
|
_changes.sort(key=lambda change: (change[0], change[1]))
|
|
|
|
if orderlists:
|
|
|
|
ids = [l.name for l in List.query.filter_by(user_id=user_id,type='static').order_by('index_') if l.name]
|
|
|
|
if len(ids) > 1:
|
2016-01-23 16:49:34 +00:00
|
|
|
_changes.append([-1, timestamp, ['orderlists', ids]])
|
2016-01-21 06:14:01 +00:00
|
|
|
userinfo = state.user().json()
|
|
|
|
if editusername:
|
2016-01-23 16:49:34 +00:00
|
|
|
_changes.append([-1, timestamp, ['editusername', userinfo['username']]])
|
2016-01-21 06:14:01 +00:00
|
|
|
if editcontact:
|
2016-01-23 16:49:34 +00:00
|
|
|
_changes.append([-1, timestamp, ['editcontact', userinfo['contact']]])
|
2016-01-13 09:58:06 +00:00
|
|
|
if _changes:
|
2016-01-13 16:16:31 +00:00
|
|
|
r = revision
|
|
|
|
for c in reversed(_changes):
|
2016-01-13 09:58:06 +00:00
|
|
|
c[0] = r
|
2016-01-13 16:16:31 +00:00
|
|
|
r -= 1
|
2016-01-13 09:58:06 +00:00
|
|
|
return _changes
|