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({
21 serializeData: function() {
22 var data = this.model.toJSON();
23 data.inactive = data.removed;
24 data.avatarUrl = data.avatarUrl || avatars.getForUser(data);
29 var UserPopoverFooterView = Marionette.ItemView.extend({
30 template: footerTemplate,
35 'click #button-onetoone': function() {
36 this.parentPopover.hide();
37 var username = this.model.get('username');
38 appEvents.trigger('dispatchVueAction', 'changeDisplayedRoomByUrl', `/${username}`);
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}`);
47 'click #button-mention': function() {
48 this.parentPopover.hide();
49 var username = this.model.get('username');
50 appEvents.trigger('input.append', '@' + username + ' ');
52 'click #button-remove': function() {
53 this.parentPopover.hide();
54 var username = this.model.get('username');
55 appEvents.trigger('command.room.remove', username);
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;
67 // Can't remove yourself
69 // You can't remove Matrix users
70 !checkForMatrixUsername(data.username) &&
71 // Need to be an admin to remove someone
72 context.isTroupeAdmin();
75 if (context.inOneToOneTroupeContext()) {
76 if (context.troupe().get('user').username === data.username) {
77 chatPrivately = false;
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;
92 var UserPopoverView = Popover.extend({
93 initialize: function(options) {
94 options.placement = 'horizontal';
95 options.minHeight = '88px';
99 m = this.model.toJSON();
102 username: options.username,
103 displayName: options.displayName
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;
119 options.footerView = new UserPopoverFooterView({ model: ghModel });
121 Popover.prototype.initialize.apply(this, arguments);
122 this.view = new UserView({ model: ghModel, userCollection: options.userCollection });
126 return UserPopoverView;