Merge branch 'hotfix/21.56.9' into master
[gitter.git] / public / js / views / people / userPopoverView.js
blobda8c57938a67a8b9e41ef3240d28898b67bf2101
1 'use strict';
3 var Marionette = require('backbone.marionette');
4 var Backbone = require('backbone');
5 var Popover = require('../popover');
6 var template = require('./tmpl/userPopoverView.hbs');
7 var footerTemplate = require('./tmpl/userPopoverFooterView.hbs');
8 var appEvents = require('../../utils/appevents');
9 var context = require('gitter-web-client-context');
10 var SyncMixin = require('../../collections/sync-mixin');
11 var avatars = require('gitter-web-avatars');
12 const checkForMatrixUsername = require('gitter-web-users/lib/virtual-users/check-for-matrix-username');
13 const getGitterDmRoomUriByGitterUserIdAndOtherPersonMxid = require('gitter-web-matrix-bridge/lib/get-gitter-dm-room-uri-by-gitter-user-id-and-other-person-mxid');
15 module.exports = (function() {
16   var UserView = Marionette.ItemView.extend({
17     template: template,
18     modelEvents: {
19       change: 'render'
20     },
21     serializeData: function() {
22       var data = this.model.toJSON();
23       data.inactive = data.removed;
24       data.avatarUrl = data.avatarUrl || avatars.getForUser(data);
25       return data;
26     }
27   });
29   var UserPopoverFooterView = Marionette.ItemView.extend({
30     template: footerTemplate,
31     modelEvents: {
32       change: 'render'
33     },
34     events: {
35       'click #button-onetoone': function() {
36         this.parentPopover.hide();
37         var username = this.model.get('username');
38         appEvents.trigger('dispatchVueAction', 'changeDisplayedRoomByUrl', `/${username}`);
39       },
40       'click #button-matrix-dm': function() {
41         this.parentPopover.hide();
42         const userId = context.user().get('id');
43         const targetMxid = `@${this.model.get('username')}`;
44         const uri = getGitterDmRoomUriByGitterUserIdAndOtherPersonMxid(userId, targetMxid);
45         appEvents.trigger('dispatchVueAction', 'changeDisplayedRoomByUrl', `/${uri}`);
46       },
47       'click #button-mention': function() {
48         this.parentPopover.hide();
49         var username = this.model.get('username');
50         appEvents.trigger('input.append', '@' + username + ' ');
51       },
52       'click #button-remove': function() {
53         this.parentPopover.hide();
54         var username = this.model.get('username');
55         appEvents.trigger('command.room.remove', username);
56       }
57     },
58     serializeData: function() {
59       var data = this.model.toJSON();
60       var isntSelf = data.username !== context.user().get('username');
61       var inactive = data.removed;
62       var chatPrivately = data.has_gitter_login && isntSelf && !inactive;
63       const chatPrivatelyMatrixDm = checkForMatrixUsername(data.username);
64       var mentionable = isntSelf;
66       const removable =
67         // Can't remove yourself
68         isntSelf &&
69         // You can't remove Matrix users
70         !checkForMatrixUsername(data.username) &&
71         // Need to be an admin to remove someone
72         context.isTroupeAdmin();
74       // Special case
75       if (context.inOneToOneTroupeContext()) {
76         if (context.troupe().get('user').username === data.username) {
77           chatPrivately = false;
78         }
79       }
81       data.avatarUrl = data.avatarUrl || avatars.getForUser(data);
82       data.inactive = data.removed;
83       data.chatPrivately = chatPrivately;
84       data.chatPrivatelyMatrixDm = chatPrivatelyMatrixDm;
85       data.mentionable = mentionable;
86       data.removable = removable;
87       data.loaded = !!this.model.loaded;
88       return data;
89     }
90   });
92   var UserPopoverView = Popover.extend({
93     initialize: function(options) {
94       options.placement = 'horizontal';
95       options.minHeight = '88px';
97       var m;
98       if (this.model) {
99         m = this.model.toJSON();
100       } else {
101         m = {
102           username: options.username,
103           displayName: options.displayName
104         };
105       }
107       var username = m.username;
108       var ghModel = new Backbone.Model(m);
110       // No extra data to fetch for virtualUsers
111       if (!checkForMatrixUsername(username)) {
112         ghModel.sync = SyncMixin.sync; // XXX This is less than ideal
113         ghModel.url = '/v1/users/' + username;
114         ghModel.fetch(function() {
115           ghModel.loaded = true;
116         });
117       }
119       options.footerView = new UserPopoverFooterView({ model: ghModel });
121       Popover.prototype.initialize.apply(this, arguments);
122       this.view = new UserView({ model: ghModel, userCollection: options.userCollection });
123     }
124   });
126   return UserPopoverView;
127 })();