From 19ac4a54c2395c5f0cdca54017dfd3d16acfefd7 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 27 Feb 2022 20:21:39 -0600 Subject: [PATCH] normalizeAccount(): normalize Pleroma legacy fields --- .../__fixtures__/pleroma-2.2.2-account.json | 46 +++++++++++++++++++ .../normalizers/__tests__/account-test.js | 20 ++++++++ app/soapbox/normalizers/account.js | 30 +++++++++++- app/soapbox/normalizers/instance.js | 4 +- app/soapbox/normalizers/status.js | 4 +- app/soapbox/reducers/accounts.js | 25 ++++------ app/soapbox/utils/normalizers.js | 2 + app/soapbox/utils/pleroma.js | 10 ---- 8 files changed, 108 insertions(+), 33 deletions(-) create mode 100644 app/soapbox/__fixtures__/pleroma-2.2.2-account.json create mode 100644 app/soapbox/utils/normalizers.js delete mode 100644 app/soapbox/utils/pleroma.js diff --git a/app/soapbox/__fixtures__/pleroma-2.2.2-account.json b/app/soapbox/__fixtures__/pleroma-2.2.2-account.json new file mode 100644 index 000000000..7df005d65 --- /dev/null +++ b/app/soapbox/__fixtures__/pleroma-2.2.2-account.json @@ -0,0 +1,46 @@ +{ + "acct": "alex", + "avatar": "https://freespeechextremist.com/images/avi.png", + "avatar_static": "https://freespeechextremist.com/images/avi.png", + "bot": false, + "created_at": "2022-02-28T01:55:05.000Z", + "display_name": "Alex Gleason", + "emojis": [], + "fields": [], + "followers_count": 0, + "following_count": 0, + "header": "https://freespeechextremist.com/images/banner.png", + "header_static": "https://freespeechextremist.com/images/banner.png", + "id": "AGv8wCadU7DqWgMqNk", + "locked": false, + "note": "I'm testing out compatibility with an older Pleroma version", + "pleroma": { + "accepts_chat_messages": true, + "ap_id": "https://freespeechextremist.com/users/alex", + "background_image": null, + "confirmation_pending": false, + "favicon": null, + "hide_favorites": true, + "hide_followers": false, + "hide_followers_count": false, + "hide_follows": false, + "hide_follows_count": false, + "is_admin": false, + "is_moderator": false, + "relationship": {}, + "skip_thread_containment": false, + "tags": [] + }, + "source": { + "fields": [], + "note": "I'm testing out compatibility with an older Pleroma version", + "pleroma": { + "actor_type": "Person", + "discoverable": true + }, + "sensitive": false + }, + "statuses_count": 0, + "url": "https://freespeechextremist.com/users/alex", + "username": "alex" +} diff --git a/app/soapbox/normalizers/__tests__/account-test.js b/app/soapbox/normalizers/__tests__/account-test.js index 58d9b9f27..d6584f96b 100644 --- a/app/soapbox/normalizers/__tests__/account-test.js +++ b/app/soapbox/normalizers/__tests__/account-test.js @@ -16,4 +16,24 @@ describe('normalizeAccount()', () => { expect(result.get('birthday')).toEqual('1993-07-03'); }); + + it('normalizes Pleroma legacy fields', () => { + const account = fromJS(require('soapbox/__fixtures__/pleroma-2.2.2-account.json')); + const result = normalizeAccount(account); + + expect(result.getIn(['pleroma', 'is_active'])).toBe(true); + expect(result.getIn(['pleroma', 'is_confirmed'])).toBe(true); + expect(result.getIn(['pleroma', 'is_approved'])).toBe(true); + + expect(result.hasIn(['pleroma', 'confirmation_pending'])).toBe(false); + }); + + it('prefers new Pleroma fields', () => { + const account = fromJS(require('soapbox/__fixtures__/pleroma-account.json')); + const result = normalizeAccount(account); + + expect(result.getIn(['pleroma', 'is_active'])).toBe(true); + expect(result.getIn(['pleroma', 'is_confirmed'])).toBe(true); + expect(result.getIn(['pleroma', 'is_approved'])).toBe(true); + }); }); diff --git a/app/soapbox/normalizers/account.js b/app/soapbox/normalizers/account.js index eb623c803..da942473f 100644 --- a/app/soapbox/normalizers/account.js +++ b/app/soapbox/normalizers/account.js @@ -1,4 +1,25 @@ -export const normalizeAccount = account => { +import { Map as ImmutableMap } from 'immutable'; + +import { mergeDefined } from 'soapbox/utils/normalizers'; + +// https://gitlab.com/soapbox-pub/soapbox-fe/-/issues/549 +const normalizePleromaLegacyFields = account => { + return account.update('pleroma', ImmutableMap(), pleroma => { + return pleroma.withMutations(pleroma => { + const legacy = ImmutableMap({ + is_active: !pleroma.get('deactivated'), + is_confirmed: !pleroma.get('confirmation_pending'), + is_approved: !pleroma.get('approval_pending'), + }); + + pleroma.mergeWith(mergeDefined, legacy); + pleroma.deleteAll(['deactivated', 'confirmation_pending', 'approval_pending']); + }); + }); +}; + +// Normalize Pleroma/Fedibird birthday +const normalizeBirthday = account => { const birthday = [ account.getIn(['pleroma', 'birthday']), account.getIn(['other_settings', 'birthday']), @@ -6,3 +27,10 @@ export const normalizeAccount = account => { return account.set('birthday', birthday); }; + +export const normalizeAccount = account => { + return account.withMutations(account => { + normalizePleromaLegacyFields(account); + normalizeBirthday(account); + }); +}; diff --git a/app/soapbox/normalizers/instance.js b/app/soapbox/normalizers/instance.js index d95990670..155f6c8ae 100644 --- a/app/soapbox/normalizers/instance.js +++ b/app/soapbox/normalizers/instance.js @@ -1,6 +1,7 @@ import { Map as ImmutableMap } from 'immutable'; import { parseVersion, PLEROMA } from 'soapbox/utils/features'; +import { mergeDefined } from 'soapbox/utils/normalizers'; import { isNumber } from 'soapbox/utils/numbers'; // Use Mastodon defaults @@ -36,9 +37,6 @@ const pleromaToMastodonConfig = instance => { }); }; -// Use new value only if old value is undefined -const mergeDefined = (oldVal, newVal) => oldVal === undefined ? newVal : oldVal; - // Get the software's default attachment limit const getAttachmentLimit = software => software === PLEROMA ? Infinity : 4; diff --git a/app/soapbox/normalizers/status.js b/app/soapbox/normalizers/status.js index 8927546e8..ab6ce6312 100644 --- a/app/soapbox/normalizers/status.js +++ b/app/soapbox/normalizers/status.js @@ -1,6 +1,7 @@ import { Map as ImmutableMap, List as ImmutableList } from 'immutable'; import { accountToMention } from 'soapbox/utils/accounts'; +import { mergeDefined } from 'soapbox/utils/normalizers'; // Some backends can return null, or omit these required fields const baseStatus = ImmutableMap({ @@ -40,9 +41,6 @@ const basePoll = ImmutableMap({ votes_count: 0, }); -// Merger function for only overriding undefined values -const mergeDefined = (oldVal, newVal) => oldVal === undefined ? newVal : oldVal; - // Merge base status const mergeBase = status => { return status.mergeDeepWith(mergeDefined, baseStatus); diff --git a/app/soapbox/reducers/accounts.js b/app/soapbox/reducers/accounts.js index 924360dc9..c3cbcb9d6 100644 --- a/app/soapbox/reducers/accounts.js +++ b/app/soapbox/reducers/accounts.js @@ -31,7 +31,6 @@ import { CHATS_FETCH_SUCCESS, CHATS_EXPAND_SUCCESS, CHAT_FETCH_SUCCESS } from 's import { normalizeAccount as normalizeAccount2 } from 'soapbox/actions/importer/normalizer'; import { STREAMING_CHAT_UPDATE } from 'soapbox/actions/streaming'; import { normalizeAccount } from 'soapbox/normalizers/account'; -import { normalizePleromaUserFields } from 'soapbox/utils/pleroma'; import { ACCOUNT_IMPORT, @@ -41,24 +40,18 @@ import { const initialState = ImmutableMap(); -const normalizePleroma = account => { - if (!account.pleroma) return account; - account.pleroma = normalizePleromaUserFields(account.pleroma); - delete account.pleroma.chat_token; - return account; +const minifyAccount = account => { + return account.deleteAll([ + 'followers_count', + 'following_count', + 'statuses_count', + 'source', + ]); }; const fixAccount = (state, account) => { - const normalized = fromJS(normalizePleroma(account)).withMutations(account => { - account.deleteAll([ - 'followers_count', - 'following_count', - 'statuses_count', - 'source', - ]); - }); - - return state.set(account.id, normalizeAccount(normalized)); + const normalized = minifyAccount(normalizeAccount(fromJS(account))); + return state.set(account.id, normalized); }; const normalizeAccounts = (state, accounts) => { diff --git a/app/soapbox/utils/normalizers.js b/app/soapbox/utils/normalizers.js new file mode 100644 index 000000000..7d205f21c --- /dev/null +++ b/app/soapbox/utils/normalizers.js @@ -0,0 +1,2 @@ +// Use new value only if old value is undefined +export const mergeDefined = (oldVal, newVal) => oldVal === undefined ? newVal : oldVal; diff --git a/app/soapbox/utils/pleroma.js b/app/soapbox/utils/pleroma.js deleted file mode 100644 index dd8937938..000000000 --- a/app/soapbox/utils/pleroma.js +++ /dev/null @@ -1,10 +0,0 @@ -// https://gitlab.com/soapbox-pub/soapbox-fe/-/issues/549 -export const normalizePleromaUserFields = obj => { - obj.is_active = obj.is_active === undefined ? !obj.deactivated : obj.is_active; - obj.is_confirmed = obj.is_confirmed === undefined ? !obj.confirmation_pending : obj.is_confirmed; - obj.is_approved = obj.is_approved === undefined ? !obj.approval_pending : obj.is_approved; - delete obj.deactivated; - delete obj.confirmation_pending; - delete obj.approval_pending; - return obj; -};