DRY up group update sending

// FREEBIE
This commit is contained in:
lilia 2015-09-21 19:12:06 -07:00
parent f00d118c93
commit 9ff95c7e61
3 changed files with 34 additions and 46 deletions

View file

@ -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') {

View file

@ -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));
},

View file

@ -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));
}