Skip to content

Commit

Permalink
Tune birthday
Browse files Browse the repository at this point in the history
  • Loading branch information
mei23 committed Nov 3, 2021
1 parent 5186dfe commit 7809187
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 15 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@
"request-stats": "3.0.0",
"rimraf": "3.0.2",
"rndstr": "1.0.0",
"s-age": "1.1.2",
"sharp": "0.28.3",
"showdown": "1.9.1",
"showdown-highlightjs-extension": "0.1.2",
Expand Down
4 changes: 2 additions & 2 deletions src/client/app/desktop/views/home/user/user.header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<script lang="ts">
import Vue from 'vue';
import i18n from '../../../../i18n';
import * as age from 's-age';
import { calcAge } from '../../../../../../misc/calc-age';
import XUserMenu from '../../../../common/views/components/user-menu.vue';
import XListMenu from '../../../../common/views/components/list-menu.vue';
import XIntegrations from '../../../../common/views/components/integrations.vue';
Expand Down Expand Up @@ -91,7 +91,7 @@ export default Vue.extend({
},
age(): number {
return age(this.user.profile.birthday);
return calcAge(this.user.profile.birthday);
}
},
mounted() {
Expand Down
4 changes: 2 additions & 2 deletions src/client/app/mobile/views/pages/user/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
<script lang="ts">
import Vue from 'vue';
import i18n from '../../../../i18n';
import * as age from 's-age';
import { calcAge } from '../../../../../../misc/calc-age';
import parseAcct from '../../../../../../misc/acct/parse';
import Progress from '../../../../common/scripts/loading';
import XUserMenu from '../../../../common/views/components/user-menu.vue';
Expand All @@ -113,7 +113,7 @@ export default Vue.extend({
},
computed: {
age(): number {
return age(this.user.profile.birthday);
return calcAge(this.user.profile.birthday);
},
avator(): string {
return this.$store.state.device.disableShowingAnimatedImages
Expand Down
31 changes: 31 additions & 0 deletions src/misc/calc-age.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export function calcAge(dt: Date | number | string) {
const date = new Date(dt);
let y = date.getFullYear();
let m0 = date.getMonth();
let d = date.getDate();

console.log(`${date} ${dt}`);

if (date.toString() === 'Invalid Date' && typeof dt === 'string') {
const m = dt.match(/^([+-]?\d{4,})-(\d{2})-(\d{2})$/);
console.log(JSON.stringify(m));
if (m) {
y = Number(m[1]);
m0 = Number(m[2]) - 1;
d = Number(m[3]);
}
}

const now = new Date();

const yearDiff = now.getFullYear() - y;
const monthDiff = now.getMonth() - m0;
const pastDate = now.getDate() < d;

// compare months. if same month, compare days
if (monthDiff < 0 || (monthDiff === 0 && pastDate)) {
return yearDiff - 1;
} else {
return yearDiff;
}
}
2 changes: 1 addition & 1 deletion src/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export function isValidLocation(location: string): boolean {

export function isValidBirthday(birthday: string): boolean {
// eslint-disable-next-line no-useless-escape
return typeof birthday == 'string' && /^([0-9]{4})\-([0-9]{2})-([0-9]{2})$/.test(birthday);
return typeof birthday == 'string' && /^([0-9]{4,8})\-([0-9]{2})-([0-9]{2})$/.test(birthday);
}
//#endregion

Expand Down
4 changes: 2 additions & 2 deletions src/remote/activitypub/models/person.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export async function createPerson(uri: string, resolver?: Resolver): Promise<IR

const movedToUserId = await resolveAnotherUser(uri, person.movedTo);

const bday = person['vcard:bday']?.match(/^\d{4}-\d{2}-\d{2}/);
const bday = person['vcard:bday']?.match(/^[0-9]{4,8}-\d{2}-\d{2}/);

// Create user
let user: IRemoteUser | undefined;
Expand Down Expand Up @@ -342,7 +342,7 @@ export async function updatePerson(uri: string, resolver?: Resolver, hint?: IAct

const movedToUserId = await resolveAnotherUser(uri, person.movedTo);

const bday = person['vcard:bday']?.match(/^\d{4}-\d{2}-\d{2}/);
const bday = person['vcard:bday']?.match(/^[0-9]{4,8}-\d{2}-\d{2}/);

const updates = {
lastFetchedAt: new Date(),
Expand Down
4 changes: 2 additions & 2 deletions src/server/api/endpoints/i/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { sendDeleteActivity } from '../../../../services/suspend-user';
import { doPostUnsuspend } from '../../../../services/unsuspend-user';
import { normalizeTag } from '../../../../misc/normalize-tag';
import config from '../../../../config';
const age = require('s-age');
import { calcAge } from '../../../../misc/calc-age';

export const meta = {
desc: {
Expand Down Expand Up @@ -245,7 +245,7 @@ export default define(meta, async (ps, user, app) => {
if (typeof config.minimumAge === 'number' && ps.birthday != null) {
const d = new Date(ps.birthday);
if (d?.toString() !== 'Invalid Date') {
const a = age(d);
const a = calcAge(d);
if (a < config.minimumAge && a >= 0) {
ps.birthday = null;
}
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7935,11 +7935,6 @@ run-parallel@^1.1.9:
resolved "https://registry.yarnpkg.com/run-parallel/-/run-parallel-1.1.9.tgz#c9dd3a7cf9f4b2c4b6244e173a6ed866e61dd679"
integrity sha512-DEqnSRTDw/Tc3FXf49zedI638Z9onwUotBMiUFKmrO2sdFKIbXamXGQ3Axd4qgphxKB4kw/qP1w5kTxnfU1B9Q==

s-age@1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/s-age/-/s-age-1.1.2.tgz#c0cf15233ccc93f41de92ea42c36d957977d1ea2"
integrity sha512-aSN2TlF39WLoZA/6cgYSJZhKt63kJ4EaadejPWjWY9/h4rksIqvfWY3gfd+3uAegSM1IXsA9aWeEhJtkxkFQtA==

safe-buffer@*, safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@^5.1.2, safe-buffer@~5.2.0:
version "5.2.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
Expand Down

0 comments on commit 7809187

Please sign in to comment.