DRY up group update sending
// FREEBIE
This commit is contained in:
parent
f00d118c93
commit
9ff95c7e61
3 changed files with 34 additions and 46 deletions
|
@ -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() {
|
leaveGroup: function() {
|
||||||
var now = Date.now();
|
var now = Date.now();
|
||||||
if (this.get('type') === 'group') {
|
if (this.get('type') === 'group') {
|
||||||
|
|
|
@ -101,42 +101,20 @@
|
||||||
textsecure.storage.groups.createNewGroup(members).then(function(group) {
|
textsecure.storage.groups.createNewGroup(members).then(function(group) {
|
||||||
return group.id;
|
return group.id;
|
||||||
}).then(function(groupId) {
|
}).then(function(groupId) {
|
||||||
var attributes = {
|
var now = Date.now();
|
||||||
|
var group = ConversationController.create({
|
||||||
id: groupId,
|
id: groupId,
|
||||||
groupId: groupId,
|
groupId: groupId,
|
||||||
type: 'group',
|
type: 'group',
|
||||||
name: name,
|
name: name,
|
||||||
avatar: avatarFile,
|
avatar: avatarFile,
|
||||||
members: members,
|
members: members,
|
||||||
active_at: Date.now()
|
active_at: now,
|
||||||
};
|
});
|
||||||
var group = ConversationController.create(attributes);
|
|
||||||
group.save().then(function() {
|
group.save().then(function() {
|
||||||
this.trigger('open', group);
|
this.trigger('open', group);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
var now = Date.now();
|
group.updateGroup();
|
||||||
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});
|
|
||||||
});
|
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
},
|
},
|
||||||
|
|
|
@ -74,25 +74,7 @@
|
||||||
this.model.trigger('change:avatar');
|
this.model.trigger('change:avatar');
|
||||||
}
|
}
|
||||||
|
|
||||||
var message = this.model.messageCollection.add({
|
this.model.updateGroup(group_update);
|
||||||
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.goBack();
|
this.goBack();
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue