1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
<!--
- @copyright Copyright (c) 2020 Georg Ehrke <oc.list@georgehrke.com>
- @author Georg Ehrke <oc.list@georgehrke.com>
-
- @license GNU AGPL version 3 or any later version
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-->
<template>
<li>
<div class="user-status-menu-item">
<!-- Username display -->
<span
v-if="!inline"
class="user-status-menu-item__header"
:title="displayName">
{{ displayName }}
</span>
<!-- Status modal toggle -->
<toggle :is="inline ? 'button' : 'a'"
:class="{'user-status-menu-item__toggle--inline': inline}"
class="user-status-menu-item__toggle"
href="#"
@click.prevent.stop="openModal">
<span :class="statusIcon" class="user-status-menu-item__toggle-icon" />
{{ visibleMessage }}
</toggle>
</div>
<!-- Status management modal -->
<SetStatusModal
v-if="isModalOpen"
@close="closeModal" />
</li>
</template>
<script>
import { getCurrentUser } from '@nextcloud/auth'
import debounce from 'debounce'
import { sendHeartbeat } from './services/heartbeatService'
import OnlineStatusMixin from './mixins/OnlineStatusMixin'
export default {
name: 'UserStatus',
components: {
SetStatusModal: () => import(/* webpackChunkName: 'user-status-modal' */'./components/SetStatusModal'),
},
mixins: [OnlineStatusMixin],
props: {
inline: {
type: Boolean,
default: false,
},
},
data() {
return {
isModalOpen: false,
heartbeatInterval: null,
setAwayTimeout: null,
mouseMoveListener: null,
isAway: false,
}
},
computed: {
/**
* The display-name of the current user
*
* @returns {String}
*/
displayName() {
return getCurrentUser().displayName
},
},
/**
* Loads the current user's status from initial state
* and stores it in Vuex
*/
mounted() {
this.$store.dispatch('loadStatusFromInitialState')
if (OC.config.session_keepalive) {
// Send the latest status to the server every 5 minutes
this.heartbeatInterval = setInterval(this._backgroundHeartbeat.bind(this), 1000 * 60 * 5)
this.setAwayTimeout = () => {
this.isAway = true
}
// Catch mouse movements, but debounce to once every 30 seconds
this.mouseMoveListener = debounce(() => {
const wasAway = this.isAway
this.isAway = false
// Reset the two minute counter
clearTimeout(this.setAwayTimeout)
// If the user did not move the mouse within two minutes,
// mark them as away
setTimeout(this.setAwayTimeout, 1000 * 60 * 2)
if (wasAway) {
this._backgroundHeartbeat()
}
}, 1000 * 2, true)
window.addEventListener('mousemove', this.mouseMoveListener, {
capture: true,
passive: true,
})
this._backgroundHeartbeat()
}
},
/**
* Some housekeeping before destroying the component
*/
beforeDestroy() {
window.removeEventListener('mouseMove', this.mouseMoveListener)
clearInterval(this.heartbeatInterval)
},
methods: {
/**
* Opens the modal to set a custom status
*/
openModal() {
this.isModalOpen = true
},
/**
* Closes the modal
*/
closeModal() {
this.isModalOpen = false
},
/**
* Sends the status heartbeat to the server
*
* @returns {Promise<void>}
* @private
*/
async _backgroundHeartbeat() {
await sendHeartbeat(this.isAway)
await this.$store.dispatch('reFetchStatusFromServer')
},
},
}
</script>
<style lang="scss">
$max-width-user-status: 200px;
.user-status-menu-item {
&__header {
display: block;
overflow: hidden;
box-sizing: border-box;
max-width: $max-width-user-status;
padding: 10px 12px 5px 38px;
text-align: left;
white-space: nowrap;
text-overflow: ellipsis;
opacity: 1;
color: var(--color-text-maxcontrast);
}
&__toggle {
&-icon {
width: 16px;
height: 16px;
margin-right: 10px;
opacity: 1 !important;
background-size: 16px;
}
// In dashboard
&--inline {
width: auto;
min-width: 44px;
height: 44px;
margin: 0;
border: 0;
border-radius: var(--border-radius-pill);
background-color: var(--color-background-translucent);
font-size: inherit;
font-weight: normal;
-webkit-backdrop-filter: var(--background-blur);
backdrop-filter: var(--background-blur);
&:active,
&:hover,
&:focus {
background-color: var(--color-background-hover);
}
}
}
}
li {
list-style-type: none;
}
</style>
|