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

View file

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

View file

@ -61,11 +61,20 @@ def setPreferences(data):
'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)
if 'username' in data:
u = state.user()
u.update_name()
u.save()
if change_username:
Changelog.record(u, 'editusername', data['username'])
if change_contact:
Changelog.record(state.user(), 'editcontact', data['contact'])
return settings.preferences
actions.register(setPreferences)

View file

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