From 02d9d350d9ea664e96059029ad6912adee19f37e Mon Sep 17 00:00:00 2001
From: Steffen van Bergerem <svbergerem@omgsrsly.net>
Date: Mon, 28 Aug 2017 01:31:59 +0200
Subject: [PATCH] Move person extension for prefills to conversations form view

closes #7599
---
 Changelog.md                                      |  1 +
 app/assets/javascripts/app/pages/contacts.js      |  9 ++-------
 .../app/views/conversations_form_view.js          |  9 +++++++--
 .../javascripts/app/views/profile_header_view.js  |  7 +------
 spec/javascripts/app/pages/contacts_spec.js       |  9 ++-------
 .../app/views/conversations_form_view_spec.js     | 15 +++++++++++++--
 .../app/views/profile_header_view_spec.js         |  9 +++------
 spec/javascripts/jasmine_helpers/factory.js       |  8 +++++---
 8 files changed, 34 insertions(+), 33 deletions(-)

diff --git a/Changelog.md b/Changelog.md
index 315cc0976e..5adc427e2b 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -15,6 +15,7 @@
 * Fix S3 support [#7566](https://github.com/diaspora/diaspora/pull/7566)
 * Fix mixed username and timestamp with LTR/RTL scripts [#7575](https://github.com/diaspora/diaspora/pull/7575)
 * Prevent users from zooming in IE Mobile [#7589](https://github.com/diaspora/diaspora/pull/7589)
+* Fix recipient prefill on contacts and profile page [#7599](https://github.com/diaspora/diaspora/pull/7599)
 
 ## Features
 * Ask for confirmation when leaving a submittable comment field [#7530](https://github.com/diaspora/diaspora/pull/7530)
diff --git a/app/assets/javascripts/app/pages/contacts.js b/app/assets/javascripts/app/pages/contacts.js
index 64c17422d5..a7d21a0b08 100644
--- a/app/assets/javascripts/app/pages/contacts.js
+++ b/app/assets/javascripts/app/pages/contacts.js
@@ -80,14 +80,9 @@ app.pages.Contacts = Backbone.View.extend({
 
   showMessageModal: function(){
     $("#conversationModal").on("modal:loaded", function() {
-      var people = app.contacts.filter(function(contact) {
+      var people = _.pluck(app.contacts.filter(function(contact) {
         return contact.inAspect(app.aspect.get("id"));
-      }).map(function(contact) {
-        return _.extend({
-          avatar: contact.person.get("profile").avatar.small,
-          handle: contact.person.get("diaspora_id")
-        }, contact.person.attributes);
-      });
+      }), "person");
       new app.views.ConversationsForm({prefill: people});
     });
     app.helpers.showModal("#conversationModal");
diff --git a/app/assets/javascripts/app/views/conversations_form_view.js b/app/assets/javascripts/app/views/conversations_form_view.js
index a28b6d4265..a360f09175 100644
--- a/app/assets/javascripts/app/views/conversations_form_view.js
+++ b/app/assets/javascripts/app/views/conversations_form_view.js
@@ -52,8 +52,13 @@ app.views.ConversationsForm = app.views.Base.extend({
     this.setupAvatarFallback(personEl);
   },
 
-  prefill: function(handles) {
-    handles.forEach(this.addRecipient.bind(this));
+  prefill: function(people) {
+    people.forEach(function(person) {
+      this.addRecipient(_.extend({
+        avatar: person.get("profile").avatar.small,
+        handle: person.get("diaspora_id")
+      }, person.attributes));
+    }, this);
   },
 
   updateContactIdsListInput: function() {
diff --git a/app/assets/javascripts/app/views/profile_header_view.js b/app/assets/javascripts/app/views/profile_header_view.js
index 49fecd656e..14247369a5 100644
--- a/app/assets/javascripts/app/views/profile_header_view.js
+++ b/app/assets/javascripts/app/views/profile_header_view.js
@@ -81,12 +81,7 @@ app.views.ProfileHeader = app.views.Base.extend({
 
   showMessageModal: function(){
     $("#conversationModal").on("modal:loaded", function() {
-      new app.views.ConversationsForm({
-        prefill: [_.extend({
-          avatar: this.model.get("profile").avatar.small,
-          handle: this.model.get("diaspora_id")
-        }, this.model.attributes)]
-      });
+      new app.views.ConversationsForm({prefill: [this.model]});
     }.bind(this));
     app.helpers.showModal("#conversationModal");
   }
diff --git a/spec/javascripts/app/pages/contacts_spec.js b/spec/javascripts/app/pages/contacts_spec.js
index 8ac9c19bb5..fa9f001d2f 100644
--- a/spec/javascripts/app/pages/contacts_spec.js
+++ b/spec/javascripts/app/pages/contacts_spec.js
@@ -297,13 +297,8 @@ describe("app.pages.Contacts", function(){
       expect(app.views.ConversationsForm.prototype.initialize).toHaveBeenCalled();
 
       var prefill = app.views.ConversationsForm.prototype.initialize.calls.mostRecent().args[0].prefill;
-      var people = app.contacts.filter(function(contact) { return contact.inAspect(app.aspect.get("id")); });
-      expect(prefill.length).toBe(people.length);
-
-      var person = app.contacts.first().person;
-      expect(prefill[0].handle).toBe(person.get("diaspora_id"));
-      expect(prefill[0].name).toBe(person.get("name"));
-      expect(prefill[0].avatar).toBe(person.get("profile").avatar.small);
+      var contacts = app.contacts.filter(function(contact) { return contact.inAspect(app.aspect.get("id")); });
+      expect(_.pluck(prefill, "id")).toEqual(contacts.map(function(contact) { return contact.person.id; }));
     });
   });
 });
diff --git a/spec/javascripts/app/views/conversations_form_view_spec.js b/spec/javascripts/app/views/conversations_form_view_spec.js
index b0d0653509..419af34c53 100644
--- a/spec/javascripts/app/views/conversations_form_view_spec.js
+++ b/spec/javascripts/app/views/conversations_form_view_spec.js
@@ -85,7 +85,11 @@ describe("app.views.ConversationsForm", function() {
 
   describe("prefill", function() {
     beforeEach(function() {
-      this.prefills = [{name: "diaspora user"}, {name: "other diaspora user"}, {name: "user"}];
+      this.prefills = [
+        factory.personWithProfile({"diaspora_id": "alice@pod.tld"}),
+        factory.personWithProfile({"diaspora_id": "bob@pod.tld"}),
+        factory.personWithProfile({"diaspora_id": "carol@pod.tld"})
+      ];
     });
 
     it("calls addRecipient for each prefilled participant", function() {
@@ -95,7 +99,14 @@ describe("app.views.ConversationsForm", function() {
       var allArgsFlattened = app.views.ConversationsForm.prototype.addRecipient.calls.allArgs().map(function(arg) {
         return arg[0];
       });
-      expect(allArgsFlattened).toEqual(this.prefills);
+
+      expect(_.pluck(allArgsFlattened, "handle")).toEqual(
+        this.prefills.map(function(person) { return person.get("diaspora_id"); })
+      );
+
+      expect(_.pluck(allArgsFlattened, "avatar")).toEqual(
+        this.prefills.map(function(person) { return person.get("profile").avatar.small; })
+      );
     });
   });
 
diff --git a/spec/javascripts/app/views/profile_header_view_spec.js b/spec/javascripts/app/views/profile_header_view_spec.js
index 3488d84460..3b5d086a95 100644
--- a/spec/javascripts/app/views/profile_header_view_spec.js
+++ b/spec/javascripts/app/views/profile_header_view_spec.js
@@ -77,12 +77,9 @@ describe("app.views.ProfileHeader", function() {
       spyOn(app.views.ConversationsForm.prototype, "initialize");
       spyOn($.fn, "load").and.callFake(function(url, callback) { callback(); });
       this.view.showMessageModal();
-      expect(app.views.ConversationsForm.prototype.initialize).toHaveBeenCalled();
-      var prefill = app.views.ConversationsForm.prototype.initialize.calls.mostRecent().args[0].prefill;
-      expect(prefill.length).toBe(1);
-      expect(prefill[0].handle).toBe("my@pod");
-      expect(prefill[0].name).toBe("User Name");
-      expect(prefill[0].avatar).toBe("http://example.org/avatar.jpg");
+      expect(app.views.ConversationsForm.prototype.initialize).toHaveBeenCalledWith({
+        prefill: [this.model]
+      });
     });
   });
 });
diff --git a/spec/javascripts/jasmine_helpers/factory.js b/spec/javascripts/jasmine_helpers/factory.js
index 217c68952c..9b0d4ecfc5 100644
--- a/spec/javascripts/jasmine_helpers/factory.js
+++ b/spec/javascripts/jasmine_helpers/factory.js
@@ -138,9 +138,11 @@ var factory = {
       "full_name": "bob grimm",
       "gender": "robot",
       "id": id,
-      "image_url": "http://localhost:3000/assets/user/default.png",
-      "image_url_medium": "http://localhost:3000/assets/user/default.png",
-      "image_url_small": "http://localhost:3000/assets/user/default.png",
+      "avatar": {
+        "small": "http://localhost:3000/assets/user/default.png",
+        "medium": "http://localhost:3000/assets/user/default.png",
+        "large": "http://localhost:3000/assets/user/default.png"
+      },
       "last_name": "Grimm",
       "location": "Earth",
       "nsfw": false,
-- 
GitLab