Skip to content

Commit eb4b8a7

Browse files
committed
DEV: Adds age support to discourse-cakeday plugin
Enables staff/admins to see the age of a given user, directly from the user card. Hides the year in json files for non staff/admin users Added an input element to user preferences, for year input. Added sitesettings to enable new feature, and select order of input fields MM-DD-YYYY vs. DD-MM-YYYY TODO: Enable user to choose age visibility for non staff/admins
1 parent 96f6553 commit eb4b8a7

File tree

14 files changed

+147
-28
lines changed

14 files changed

+147
-28
lines changed

assets/javascripts/discourse/connectors/user-card-post-names/user-card-cakeday.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import {
33
cakedayBirthday,
44
cakedayBirthdayTitle,
55
cakedayTitle,
6+
userAge,
7+
userAgeTitle,
68
} from "discourse/plugins/discourse-cakeday/discourse/lib/cakeday";
79

810
export default {
@@ -17,5 +19,9 @@ export default {
1719
"cakedayBirthdayTitle",
1820
cakedayBirthdayTitle(args.user, this.currentUser)
1921
);
22+
const isStaff = this.currentUser && this.currentUser.staff;
23+
const isAdmin = this.currentUser && this.currentUser.admin;
24+
if (isAdmin || isStaff)
25+
component.set("userAgeTitle", userAgeTitle(args.user, this.currentUser));
2026
},
2127
};

assets/javascripts/discourse/connectors/user-custom-preferences/user-date-of-birth-input.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
export default {
22
setupComponent(args, component) {
3+
const year = 1904;
34
const months = moment.months().map((month, index) => {
45
return { name: month, value: index + 1 };
56
});
67

78
const days = Array.from(Array(31).keys()).map((x) => (x + 1).toString());
89

910
const dateOfBirth = args.model.get("date_of_birth");
11+
const userBirthdayYear = dateOfBirth
12+
? (moment(dateOfBirth, "YYYY-MM-DD").year() != year ? moment(dateOfBirth, "YYYY-MM-DD").year() : null)
13+
: null;
1014
const userBirthdayMonth = dateOfBirth
1115
? moment(dateOfBirth, "YYYY-MM-DD").month() + 1
1216
: null;
@@ -15,22 +19,28 @@ export default {
1519
: null;
1620

1721
component.setProperties({
22+
year,
1823
months,
1924
days,
25+
userBirthdayYear,
2026
userBirthdayMonth,
2127
userBirthdayDay,
2228
});
2329

2430
const updateBirthday = function () {
2531
let date = "";
2632

27-
if (component.userBirthdayMonth && component.userBirthdayDay) {
33+
if (component.userBirthdayYear && component.userBirthdayMonth && component.userBirthdayDay) {
34+
date = `${component.userBirthdayYear}-${component.userBirthdayMonth}-${component.userBirthdayDay}`;
35+
}
36+
else if (component.userBirthdayMonth && component.userBirthdayDay) {
2837
date = `1904-${component.userBirthdayMonth}-${component.userBirthdayDay}`;
2938
}
3039

3140
args.model.set("date_of_birth", date);
3241
};
3342

43+
component.addObserver("userBirthdayYear", updateBirthday);
3444
component.addObserver("userBirthdayMonth", updateBirthday);
3545
component.addObserver("userBirthdayDay", updateBirthday);
3646
},

assets/javascripts/discourse/initializers/cakeday.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,20 @@ function initializeCakeday(api) {
3030
});
3131
},
3232

33-
@observes("userBirthdayMonth", "userBirthdayDay")
33+
@observes("userBirthdayYear", "userBirthdayMonth", "userBirthdayDay")
3434
_setUserDateOfBirth() {
35+
const userBirthdayYear = this.get("userBirthdayYear");
3536
const userBirthdayMonth = this.get("userBirthdayMonth");
3637
const userBirthdayDay = this.get("userBirthdayDay");
3738
const user = this.get("model");
3839
let date = "";
3940

40-
if (userBirthdayMonth !== "" && userBirthdayDay !== "") {
41+
if (userBirthdayYear !== "" && userBirthdayMonth !== "" && userBirthdayDay !== "") {
42+
date = `${this.get("userBirthdayYear")}-${this.get("userBirthdayMonth")}-${this.get(
43+
"userBirthdayDay"
44+
)}`;
45+
}
46+
else if (userBirthdayMonth !== "" && userBirthdayDay !== "") {
4147
date = `1904-${this.get("userBirthdayMonth")}-${this.get(
4248
"userBirthdayDay"
4349
)}`;
@@ -46,6 +52,11 @@ function initializeCakeday(api) {
4652
user.set("date_of_birth", date);
4753
},
4854

55+
@discourseComputed("model.date_of_birth")
56+
userBirthdayYear(dateOfBirth) {
57+
return moment(dateOfBirth, "YYYY-MM-DD").year() + 1;
58+
},
59+
4960
@discourseComputed("model.date_of_birth")
5061
userBirthdayMonth(dateOfBirth) {
5162
return moment(dateOfBirth, "YYYY-MM-DD").month() + 1;

assets/javascripts/discourse/lib/cakeday.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ export function cakedayBirthday(dateOfBirth) {
3030
return isSameDay(dateOfBirth);
3131
}
3232

33+
export function userAge(dateOfBirth) {
34+
return dateOfBirth
35+
? (moment(dateOfBirth, "YYYY-MM-DD").year() != 1904 ? moment().diff(dateOfBirth, 'years') : null)
36+
: null;
37+
}
38+
39+
export function userAgeTitle(user, currentUser) {
40+
return (user.date_of_birth && moment(user.date_of_birth, "YYYY-MM-DD").year() != 1904) ? I18n.t("cakeday.age", {age: userAge(user.date_of_birth)}) : null;
41+
}
42+
3343
export function cakedayTitle(user, currentUser) {
3444
if (isSameUser(user, currentUser)) {
3545
return "user.anniversary.user_title";

assets/javascripts/discourse/templates/connectors/user-card-post-names/user-card-cakeday.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
{{emoji-images list=siteSettings.cakeday_emoji title=cakedayTitle}}
1010
{{/if}}
1111
{{/if}}
12+
{{userAgeTitle}}

assets/javascripts/discourse/templates/connectors/user-custom-preferences/user-date-of-birth-input.hbs

Lines changed: 70 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,76 @@
22
<div class="control-group">
33
<label class="control-label">{{i18n "user.date_of_birth.label"}}</label>
44
<div class="controls">
5-
{{combo-box
6-
content=months
7-
value=userBirthdayMonth
8-
valueAttribute="value"
9-
valueProperty="value"
10-
none="cakeday.none"
11-
options=(hash
12-
clearable=true
13-
autoInsertNoneItem=false
14-
)
15-
onChange=(action (mut userBirthdayMonth))
16-
}}
17-
18-
{{combo-box
19-
content=days
20-
value=userBirthdayDay
21-
valueProperty=null
22-
nameProperty=null
23-
none="cakeday.none"
24-
options=(hash
25-
clearable=true
26-
autoInsertNoneItem=false
27-
)
28-
onChange=(action (mut userBirthdayDay))
29-
}}
5+
{{#if siteSettings.cakeday_birthday_formatdmy}}
6+
{{combo-box
7+
content=days
8+
value=userBirthdayDay
9+
valueProperty=null
10+
nameProperty=null
11+
none="cakeday.dd"
12+
options=(hash
13+
clearable=true
14+
autoInsertNoneItem=false
15+
)
16+
onChange=(action (mut userBirthdayDay))
17+
}}
18+
{{#if siteSettings.cakeday_birthday_show_year}}
19+
-
20+
{{/if}}
21+
{{combo-box
22+
content=months
23+
value=userBirthdayMonth
24+
valueAttribute="value"
25+
valueProperty="value"
26+
none="cakeday.mm"
27+
options=(hash
28+
clearable=true
29+
autoInsertNoneItem=false
30+
)
31+
onChange=(action (mut userBirthdayMonth))
32+
}}
33+
{{else}}
34+
{{combo-box
35+
content=months
36+
value=userBirthdayMonth
37+
valueAttribute="value"
38+
valueProperty="value"
39+
none="cakeday.mm"
40+
options=(hash
41+
clearable=true
42+
autoInsertNoneItem=false
43+
)
44+
onChange=(action (mut userBirthdayMonth))
45+
}}
46+
{{#if siteSettings.cakeday_birthday_show_year}}
47+
-
48+
{{/if}}
49+
{{combo-box
50+
content=days
51+
value=userBirthdayDay
52+
valueProperty=null
53+
nameProperty=null
54+
none="cakeday.dd"
55+
options=(hash
56+
clearable=true
57+
autoInsertNoneItem=false
58+
)
59+
onChange=(action (mut userBirthdayDay))
60+
}}
61+
{{/if}}
62+
{{#if siteSettings.cakeday_birthday_show_year}}
63+
-
64+
{{input
65+
type="number"
66+
class="year"
67+
content=year
68+
value=userBirthdayYear
69+
min=1905
70+
max=2904
71+
placeholder=(i18n "user.date_of_birth.yyyy")
72+
onChange=(action (mut userBirthdayYear))
73+
}}
74+
{{/if}}
3075
</div>
3176
</div>
3277
{{/if}}

assets/stylesheets/mobile/user-date-of-birth-input.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
.mobile-view .user-custom-preferences-outlet.user-date-of-birth-input {
2+
input.year {
3+
width: 62px;
4+
}
15
.user-custom-preferences-outlet.user-date-of-birth-input {
26
select {
37
width: 49%;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.user-custom-preferences-outlet.user-date-of-birth-input {
2+
input.year {
3+
width: 80px;
4+
}
5+
}

config/locales/client.da.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ da:
1111
user_title: "I dag er det din fødselsdag!"
1212
title: "I dag er det min fødselsdag!"
1313
label: "Fødselsdato"
14+
yyyy: "ÅÅÅÅ"
15+
show_age: "Vis alder til brugere"
1416
anniversary:
1517
user_title: "I dag er årsdagen hvor du blev en del af vores fælleskab!"
1618
title: "I dag er årsdagen for at jeg blev en del af dette fælleskab!"
@@ -20,6 +22,7 @@ da:
2022
tomorrow: "I morgen"
2123
upcoming: "Kommende"
2224
all: "Alle"
25+
age: "%{age} år"
2326
birthdays:
2427
title: "Fødselsdage"
2528
month:

config/locales/client.en.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@ en:
55
user_title: "Today is your birthday!"
66
title: "Today is my birthday!"
77
label: "Date of Birth"
8+
yyyy: "YYYY"
89
anniversary:
910
user_title: "Today is the anniversary of the day you joined our community!"
1011
title: "Today is the anniversary of the day I joined this community!"
1112
cakeday:
1213
none: " "
14+
dd: "DD"
15+
mm: "MM"
1316
title: Cakeday
1417
today: "Today"
1518
tomorrow: "Tomorrow"
1619
upcoming: "Upcoming"
1720
all: "All"
21+
age: "%{age} years"
1822
birthdays:
1923
title: "Birthdays"
2024
month:

config/locales/server.da.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
da:
88
site_settings:
99
cakeday_enabled: "Vis cakeday humørikon[er] ved siden af navnet på brugeren, på den dato, hvor de blev medlem af Discourse."
10+
cakeday_birthday_show_year: "Vis Kagedag års vælger, for at tillade brugere at indtaste deres alder."
11+
cakeday_birthday_formatdmy: "Vis Kagedag vælgere som DD-MM-ÅÅÅÅ, i stedet for MM-DD-ÅÅÅÅ"
1012
cakeday_emoji: "De humørikon[er], der vises ved siden af navnet på brugeren, på den dato, hvor de blev medlem af Discourse. Flere humørikoner kan specificeres ved: smile|cake|smile"
1113
cakeday_birthday_enabled: "Vis fødselsdags humørikon[er] ved siden af navnet på brugeren, på deres fødselsdag."
1214
cakeday_birthday_emoji: "De humørikon[er], der vises ved siden af navnet på brugeren, på deres fødselsdag. Flere humørikoner kan specificeres ved: smile|cake|smile"

config/locales/server.en.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
en:
22
site_settings:
33
cakeday_enabled: "Show cakeday emoji[s] beside the user's name on the date they joined Discourse."
4+
cakeday_birthday_show_year: "Show cakeday year selector, to allow users to input their age."
5+
cakeday_birthday_formatdmy: "Show cakeday selectors as DD-MM-YYYY, instead of MM-DD-YYYY"
46
cakeday_emoji: "The emoji[s] that will be shown beside the user's name on the date that they joined Discourse. Multiple emojis can be specified by: smile|cake|smile"
57
cakeday_birthday_enabled: "Show birthday emoji[s] beside the user's name on their birthday."
68
cakeday_birthday_emoji: "The emoji[s] that will be shown beside the user's name on their birthday. Multiple emojis can be specified by: smile|cake|smile"

config/settings.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ plugins:
1111
cakeday_birthday_emoji:
1212
default: 'birthday'
1313
client: true
14+
cakeday_birthday_show_year:
15+
default: false
16+
client: true
17+
cakeday_birthday_formatdmy:
18+
default: false
19+
client: true

plugin.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
register_asset 'stylesheets/cakeday.scss'
1111
register_asset 'stylesheets/emoji-images.scss'
12+
register_asset 'stylesheets/user-date-of-birth-input.scss'
1213
register_asset 'stylesheets/mobile/user-date-of-birth-input.scss'
1314

1415
register_svg_icon "birthday-cake" if respond_to?(:register_svg_icon)
@@ -61,6 +62,15 @@ class ::User
6162

6263
add_to_serializer(:user_card, :date_of_birth, false) do
6364
object.date_of_birth
65+
if object.date_of_birth != nil
66+
if (scope.is_staff? || scope.is_admin?)
67+
object.date_of_birth
68+
else
69+
Date.new(1904, object.date_of_birth.month, object.date_of_birth.day)
70+
end
71+
else
72+
nil
73+
end
6474
end
6575

6676
add_to_serializer(:user_card, :include_date_of_birth?) do

0 commit comments

Comments
 (0)