aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRigel Kent <sendmemail@rigelk.eu>2020-04-11 15:58:44 +0200
committerRigel Kent <sendmemail@rigelk.eu>2020-04-11 16:05:50 +0200
commit41a94d07f00f53d4624205fa4b23553a11e305dc (patch)
treef8031f19fdd6f918d315a863ffc464cfe6c4c30c
parent62eacb420945020d354037c5d5cdf2885529f52a (diff)
downloadPeerTube-41a94d07f00f53d4624205fa4b23553a11e305dc.tar.gz
PeerTube-41a94d07f00f53d4624205fa4b23553a11e305dc.tar.zst
PeerTube-41a94d07f00f53d4624205fa4b23553a11e305dc.zip
Refactor from-now pipe to display the two most significant intervals
-rw-r--r--client/src/app/shared/angular/from-now.pipe.ts89
-rw-r--r--client/src/app/videos/+video-watch/comment/video-comments.component.ts2
2 files changed, 67 insertions, 24 deletions
diff --git a/client/src/app/shared/angular/from-now.pipe.ts b/client/src/app/shared/angular/from-now.pipe.ts
index 3a9a76411..c3c0efbfc 100644
--- a/client/src/app/shared/angular/from-now.pipe.ts
+++ b/client/src/app/shared/angular/from-now.pipe.ts
@@ -1,5 +1,6 @@
1import { Pipe, PipeTransform } from '@angular/core' 1import { Pipe, PipeTransform } from '@angular/core'
2import { I18n } from '@ngx-translate/i18n-polyfill' 2import { I18n } from '@ngx-translate/i18n-polyfill'
3import { findIndex } from 'lodash-es'
3 4
4// Thanks: https://stackoverflow.com/questions/3177836/how-to-format-time-since-xxx-e-g-4-minutes-ago-similar-to-stack-exchange-site 5// Thanks: https://stackoverflow.com/questions/3177836/how-to-format-time-since-xxx-e-g-4-minutes-ago-similar-to-stack-exchange-site
5@Pipe({ name: 'myFromNow' }) 6@Pipe({ name: 'myFromNow' })
@@ -10,31 +11,73 @@ export class FromNowPipe implements PipeTransform {
10 transform (arg: number | Date | string) { 11 transform (arg: number | Date | string) {
11 const argDate = new Date(arg) 12 const argDate = new Date(arg)
12 const seconds = Math.floor((Date.now() - argDate.getTime()) / 1000) 13 const seconds = Math.floor((Date.now() - argDate.getTime()) / 1000)
14 let intervals = [
15 {
16 unit: 31536000, // 1 year
17 singular: (i: number) => this.i18n('{{i}} year', { i }),
18 plural: (i: number) => this.i18n('{{i}} years', { i })
19 },
20 {
21 unit: 2592000, // 1 month
22 max: 11,
23 singular: (i: number) => this.i18n('{{i}} month', { i }),
24 plural: (i: number) => this.i18n('{{i}} months', { i })
25 },
26 {
27 unit: 604800, // 1 week
28 max: 3,
29 singular: (i: number) => this.i18n('{{i}} week', { i }),
30 plural: (i: number) => this.i18n('{{i}} weeks', { i })
31 },
32 {
33 unit: 86400, // 1 day
34 max: 6,
35 singular: (i: number) => this.i18n('{{i}} day', { i }),
36 plural: (i: number) => this.i18n('{{i}} days', { i })
37 },
38 {
39 unit: 3600, // 1 hour
40 max: 23,
41 singular: (i: number) => this.i18n('{{i}} hour', { i }),
42 plural: (i: number) => this.i18n('{{i}} hours', { i })
43 },
44 {
45 unit: 60, // 1 min
46 max: 59,
47 singular: (i: number) => this.i18n('{{i}} min', { i }),
48 plural: (i: number) => this.i18n('{{i}} min', { i })
49 }
50 ]
51 .map(i => ({ ...i, interval: Math.floor(seconds / i.unit) })) // absolute interval
52 .map((i, index, array) => ({ // interval relative to remainder
53 ...i,
54 interval: index === 0
55 ? i.interval
56 : Math.floor((seconds - array[index - 1].interval * array[index - 1].unit) / i.unit)
57 }))
58 .map(i => ({ // value, interval put in its translated text wrt max value
59 ...i,
60 value: (i.interval > 1
61 ? i.plural
62 : i.singular
63 )(Math.min(i.max, i.interval)) // respect the max value
64 }))
13 65
14 let interval = Math.floor(seconds / 31536000) 66 // only keep the first two intervals with enough seconds to be considered
15 if (interval > 1) { 67 const big_interval_index = findIndex(intervals, i => i.interval >= 1)
16 return this.i18n('{{interval}} years ago', { interval }) 68 intervals = intervals
17 } 69 .slice(big_interval_index, big_interval_index + 2)
18 70 .filter(i => i.interval >= 1)
19 interval = Math.floor(seconds / 2592000)
20 if (interval > 1) return this.i18n('{{interval}} months ago', { interval })
21 if (interval === 1) return this.i18n('{{interval}} month ago', { interval })
22
23 interval = Math.floor(seconds / 604800)
24 if (interval > 1) return this.i18n('{{interval}} weeks ago', { interval })
25 if (interval === 1) return this.i18n('{{interval}} week ago', { interval })
26 71
27 interval = Math.floor(seconds / 86400) 72 if (intervals.length === 0) {
28 if (interval > 1) return this.i18n('{{interval}} days ago', { interval }) 73 return this.i18n('just now')
29 if (interval === 1) return this.i18n('{{interval}} day ago', { interval }) 74 }
30
31 interval = Math.floor(seconds / 3600)
32 if (interval > 1) return this.i18n('{{interval}} hours ago', { interval })
33 if (interval === 1) return this.i18n('{{interval}} hour ago', { interval })
34
35 interval = Math.floor(seconds / 60)
36 if (interval >= 1) return this.i18n('{{interval}} min ago', { interval })
37 75
38 return this.i18n('{{interval}} sec ago', { interval: Math.max(0, seconds) }) 76 return intervals.length == 1
77 ? this.i18n('{{interval}} ago', { interval: intervals[0].value })
78 : this.i18n('{{big_interval}} {{small_interval}} ago', {
79 big_interval: intervals[0].value,
80 small_interval: intervals[1].value
81 })
39 } 82 }
40} 83}
diff --git a/client/src/app/videos/+video-watch/comment/video-comments.component.ts b/client/src/app/videos/+video-watch/comment/video-comments.component.ts
index f1408effb..c6c28e3f7 100644
--- a/client/src/app/videos/+video-watch/comment/video-comments.component.ts
+++ b/client/src/app/videos/+video-watch/comment/video-comments.component.ts
@@ -169,7 +169,7 @@ export class VideoCommentsComponent implements OnInit, OnChanges, OnDestroy {
169 let message = 'Do you really want to delete this comment?' 169 let message = 'Do you really want to delete this comment?'
170 170
171 if (commentToDelete.isLocal) { 171 if (commentToDelete.isLocal) {
172 message += this.i18n(' The deletion will be sent to remote instances, so they remove the comment too.') 172 message += this.i18n(' The deletion will be sent to remote instances so they can reflect the change.')
173 } else { 173 } else {
174 message += this.i18n(' It is a remote comment, so the deletion will only be effective on your instance.') 174 message += this.i18n(' It is a remote comment, so the deletion will only be effective on your instance.')
175 } 175 }