add editpeer to changelog, record name/contact changes of peers

This commit is contained in:
j 2016-01-21 11:44:01 +05:30
parent bae70fd829
commit 8267fa8b39
4 changed files with 73 additions and 42 deletions

View file

@ -31,9 +31,7 @@ class Changelog(db.Model):
editcontact string editcontact string
addpeer peerid peername addpeer peerid peername
removepeer peerid peername removepeer peerid peername
editpeer peerid {username: string, contact: string}
editmeta key, value data (i.e. 'isbn', '0000000000', {title: 'Example'})
resetmeta key, value
''' '''
__tablename__ = 'changelog' __tablename__ = 'changelog'
id = sa.Column(sa.Integer(), primary_key=True) id = sa.Column(sa.Integer(), primary_key=True)
@ -81,6 +79,8 @@ class Changelog(db.Model):
@classmethod @classmethod
def apply_changes(cls, user, changes): def apply_changes(cls, user, changes):
trigger = changes trigger = changes
if trigger:
trigger_event('change', {});
for change in changes: for change in changes:
if user.id in state.removepeer: if user.id in state.removepeer:
user.cleanup() user.cleanup()
@ -243,12 +243,15 @@ class Changelog(db.Model):
user.update_name() user.update_name()
if old != user.nickname: if old != user.nickname:
List.rename_user(old, user.nickname) List.rename_user(old, user.nickname)
Changelog.record(state.user(), 'editpeer', self.id, self.json(['username']))
user.save() user.save()
return True return True
def action_editcontact(self, user, timestamp, contact): def action_editcontact(self, user, timestamp, contact):
user.info['contact'] = contact if user.info.get('contact') != contact:
user.save() user.info['contact'] = contact
user.save()
Changelog.record(state.user(), 'editpeer', user.id, user.json(['contact']))
return True return True
def action_addpeer(self, user, timestamp, peerid, username): def action_addpeer(self, user, timestamp, peerid, username):
@ -272,30 +275,31 @@ class Changelog(db.Model):
#fixme, remove from User table if no other connection exists #fixme, remove from User table if no other connection exists
return True return True
def action_editmeta(self, user, timestamp, key, value, data): 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()
return True return True
'''>> Metadata no longer tracked per isbn'''
from item.models import Metadata def action_editmeta(self, user, timestamp, key, value, data):
m = Metadata.get(key, value)
if not m or m.timestamp < timestamp:
if not m:
m = Metadata.get_or_create(key, value)
if m.edit(data, False):
m.update_items()
return True return True
def action_resetmeta(self, user, timestamp, key, value): def action_resetmeta(self, user, timestamp, key, value):
return True return True
from item.models import Metadata
m = Metadata.get(key, value)
if m and m.timestamp < timestamp:
m.reset()
return True
@classmethod @classmethod
def aggregated_changes(cls, since=None, user_id=None): def aggregated_changes(cls, since=None, user_id=None):
from item.models import Item from item.models import Item
from user.models import List from user.models import List
from user.models import User
if not user_id: if not user_id:
user_id = settings.USER_ID user_id = settings.USER_ID
qs = cls.query.filter_by(user_id=user_id) qs = cls.query.filter_by(user_id=user_id)
@ -304,6 +308,8 @@ class Changelog(db.Model):
qs = qs.filter(Changelog.revision>=since) qs = qs.filter(Changelog.revision>=since)
changes = {} changes = {}
orderlists = False orderlists = False
editcontact = False
editusername = False
for c in qs.order_by('timestamp'): for c in qs.order_by('timestamp'):
revision = c.revision revision = c.revision
timestamp = c.timestamp timestamp = c.timestamp
@ -397,28 +403,32 @@ class Changelog(db.Model):
if listitems: if listitems:
action[list_id] = [revision, timestamp, listitems] action[list_id] = [revision, timestamp, listitems]
elif op == 'editusername': elif op == 'editusername':
old_name = data[1] editusername = True
new_name = data[2]
#fixme merge multiple edits
action[old_name] = [revision, timestamp, new_name]
elif op == 'editcontact': elif op == 'editcontact':
old_contact = data[1] editcontact = True
new_contact = data[2]
#fixme merge multiple edits
action[old_contact] = [revision, timestamp, new_contact]
pass
elif op == 'addpeer': elif op == 'addpeer':
peer = data[1] peer_id = data[1]
username = data[2] username = data[2]
action[peer] = [revision, timestamp, username] if len(peer_id) == 16:
if peer in changes.get('removepeer', []): peer = User.get(peer_id)
del changes['removepeer'][peer] if peer:
username = peer.json()['username']
action[peer_id] = [revision, timestamp, username]
if peer_id in changes.get('removepeer', []):
del changes['removepeer'][peer_id]
elif op == 'removepeer': elif op == 'removepeer':
peer = data[1] peer_id = data[1]
if peer in changes.get('addpeer', []): if peer_id in changes.get('addpeer', []):
del changes['addpeer'][peer] del changes['addpeer'][peer_id]
else: else:
action[peer] = [revision, timestamp] 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]
elif op == 'editmeta': elif op == 'editmeta':
pass pass
elif op == 'resetmeta': elif op == 'resetmeta':
@ -441,6 +451,11 @@ class Changelog(db.Model):
ids = [l.name for l in List.query.filter_by(user_id=user_id,type='static').order_by('index_') if l.name] 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: if len(ids) > 1:
_changes.append([-1, timestamp, json.dumps(['orderlists', ids])]) _changes.append([-1, timestamp, json.dumps(['orderlists', ids])])
userinfo = state.user().json()
if editusername:
_changes.append([-1, timestamp, json.dumps(['editusername', userinfo['username']])])
if editcontact:
_changes.append([-1, timestamp, json.dumps(['editcontact', userinfo['contact']])])
if _changes: if _changes:
r = revision r = revision
for c in reversed(_changes): for c in reversed(_changes):

View file

@ -94,7 +94,7 @@ if 'modules' in release and 'openmedialibrary' in release['modules']:
else: else:
MINOR_VERSION = 'git' MINOR_VERSION = 'git'
NODE_PROTOCOL="0.5" NODE_PROTOCOL="0.6"
VERSION="%s.%s" % (NODE_PROTOCOL, MINOR_VERSION) VERSION="%s.%s" % (NODE_PROTOCOL, MINOR_VERSION)
USER_AGENT = 'OpenMediaLibrary/%s' % VERSION USER_AGENT = 'OpenMediaLibrary/%s' % VERSION

View file

@ -61,11 +61,20 @@ def setPreferences(data):
'sub.key': value 'sub.key': value
} }
''' '''
change_contact = 'contact' in data and \
data['contact'] != settings.preferences['contact']
change_username = 'username' in data and \
data['username'] != settings.preferences['username']
update_dict(settings.preferences, data) update_dict(settings.preferences, data)
if 'username' in data: if 'username' in data:
u = state.user() u = state.user()
u.update_name() u.update_name()
u.save() u.save()
if change_username:
Changelog.record(u, 'editusername', data['username'])
if change_contact:
Changelog.record(state.user(), 'editcontact', data['contact'])
return settings.preferences return settings.preferences
actions.register(setPreferences) actions.register(setPreferences)

View file

@ -78,7 +78,7 @@ class User(db.Model):
l.save() l.save()
return l return l
def json(self): def json(self, keys=None):
j = {} j = {}
if self.info: if self.info:
j.update(self.info) j.update(self.info)
@ -88,8 +88,14 @@ class User(db.Model):
j['peered'] = self.peered j['peered'] = self.peered
j['online'] = self.is_online() j['online'] = self.is_online()
j['nickname'] = self.info.get('nickname') j['nickname'] = self.info.get('nickname')
j['username'] = self.info.get('username') if self.id != settings.USER_ID else settings.preferences['username'] j['username'] = self.info.get('username')
j['name'] = self.name j['name'] = self.name
if self.id == settings.USER_ID:
j['username'] = settings.preferences['username']
j['contact'] = settings.preferences['contact']
if keys:
for k in set(j) - set(keys):
del j[k]
return j return j
def export_library(self): def export_library(self):
@ -283,9 +289,10 @@ class List(db.Model):
from item.models import Item from item.models import Item
for item_id in items: for item_id in items:
i = Item.get(item_id) i = Item.get(item_id)
if i in self.items: if i:
self.items.remove(i) if i in self.items:
i.update() self.items.remove(i)
i.update()
state.db.session.add(self) state.db.session.add(self)
state.db.session.commit() state.db.session.commit()
if self.user_id == settings.USER_ID and self.name != '': if self.user_id == settings.USER_ID and self.name != '':