From 9ff95c7e6148da3eeff4a7b53b259f3a2bda4fa2 Mon Sep 17 00:00:00 2001 From: lilia Date: Mon, 21 Sep 2015 19:12:06 -0700 Subject: [PATCH] DRY up group update sending // FREEBIE --- js/models/conversations.js | 28 +++++++++++++++++++++++++++ js/views/new_conversation_view.js | 32 +++++-------------------------- js/views/new_group_update_view.js | 20 +------------------ 3 files changed, 34 insertions(+), 46 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index 96d10f4e..735194a1 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -146,6 +146,34 @@ }, + updateGroup: function(group_update) { + if (this.isPrivate()) { + throw new Error("Called update group on private conversation"); + } + if (group_update === undefined) { + group_update = this.pick(['name', 'avatar', 'members']); + } + var now = Date.now(); + var message = this.messageCollection.add({ + conversationId : this.id, + type : 'outgoing', + sent_at : now, + received_at : now, + group_update : group_update + }); + message.save(); + textsecure.messaging.updateGroup( + this.id, + this.get('name'), + this.get('avatar'), + this.get('members') + ).catch(function(errors) { + message.save({errors: errors.map(function(e){return e.error;})}); + }).then(function() { + message.save({sent: true}); + }); + }, + leaveGroup: function() { var now = Date.now(); if (this.get('type') === 'group') { diff --git a/js/views/new_conversation_view.js b/js/views/new_conversation_view.js index 74eeb6bb..31330acc 100644 --- a/js/views/new_conversation_view.js +++ b/js/views/new_conversation_view.js @@ -101,42 +101,20 @@ textsecure.storage.groups.createNewGroup(members).then(function(group) { return group.id; }).then(function(groupId) { - var attributes = { + var now = Date.now(); + var group = ConversationController.create({ id: groupId, groupId: groupId, type: 'group', name: name, avatar: avatarFile, members: members, - active_at: Date.now() - }; - var group = ConversationController.create(attributes); + active_at: now, + }); group.save().then(function() { this.trigger('open', group); }.bind(this)); - var now = Date.now(); - var message = group.messageCollection.add({ - conversationId : group.id, - type : 'outgoing', - sent_at : now, - received_at : now, - group_update : { - name: group.get('name'), - avatar: group.get('avatar'), - joined: group.get('members') - } - }); - message.save(); - textsecure.messaging.updateGroup( - group.id, - group.get('name'), - group.get('avatar'), - group.get('members') - ).catch(function(errors) { - message.save({errors: errors.map(function(e){return e.error;})}); - }).then(function() { - message.save({sent: true}); - }); + group.updateGroup(); }.bind(this)); }.bind(this)); }, diff --git a/js/views/new_group_update_view.js b/js/views/new_group_update_view.js index 10eb0a43..dbd88c4a 100644 --- a/js/views/new_group_update_view.js +++ b/js/views/new_group_update_view.js @@ -74,25 +74,7 @@ this.model.trigger('change:avatar'); } - var message = this.model.messageCollection.add({ - conversationId : this.model.id, - type : 'outgoing', - sent_at : now, - received_at : now, - group_update : group_update - }); - message.save(); - textsecure.messaging.updateGroup( - this.model.id, - this.model.get('name'), - this.model.get('avatar'), - this.model.get('members') - ).catch(function(errors) { - message.save({errors: errors.map(function(e){return e.error;})}); - }).then(function() { - message.save({sent: true}); - }); - + this.model.updateGroup(group_update); this.goBack(); }.bind(this)); }