Parse Mentions as Immutable.Record
This commit is contained in:
parent
9afd43a42d
commit
8decaa2d9f
5 changed files with 31 additions and 43 deletions
|
@ -32,30 +32,34 @@ describe('normalizeStatus', () => {
|
||||||
it('adds mention to self in self-reply on Mastodon', () => {
|
it('adds mention to self in self-reply on Mastodon', () => {
|
||||||
const status = fromJS(require('soapbox/__fixtures__/mastodon-reply-to-self.json'));
|
const status = fromJS(require('soapbox/__fixtures__/mastodon-reply-to-self.json'));
|
||||||
|
|
||||||
const expected = fromJS([{
|
const expected = {
|
||||||
id: '106801667066418367',
|
id: '106801667066418367',
|
||||||
username: 'benis911',
|
username: 'benis911',
|
||||||
acct: 'benis911',
|
acct: 'benis911',
|
||||||
url: 'https://mastodon.social/@benis911',
|
url: 'https://mastodon.social/@benis911',
|
||||||
}]);
|
};
|
||||||
|
|
||||||
const result = normalizeStatus(status).get('mentions');
|
const result = normalizeStatus(status).mentions;
|
||||||
|
|
||||||
expect(result).toEqual(expected);
|
expect(result.size).toBe(1);
|
||||||
|
expect(result.get(0).toJS()).toMatchObject(expected);
|
||||||
|
expect(result.get(0).id).toEqual('106801667066418367');
|
||||||
|
expect(ImmutableRecord.isRecord(result.get(0))).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('normalizes mentions with only acct', () => {
|
it('normalizes mentions with only acct', () => {
|
||||||
const status = fromJS({ mentions: [{ acct: 'alex@gleasonator.com' }] });
|
const status = fromJS({ mentions: [{ acct: 'alex@gleasonator.com' }] });
|
||||||
|
|
||||||
const expected = fromJS([{
|
const expected = [{
|
||||||
|
id: '',
|
||||||
acct: 'alex@gleasonator.com',
|
acct: 'alex@gleasonator.com',
|
||||||
username: 'alex',
|
username: 'alex',
|
||||||
url: '',
|
url: '',
|
||||||
}]);
|
}];
|
||||||
|
|
||||||
const result = normalizeStatus(status).get('mentions');
|
const result = normalizeStatus(status).get('mentions');
|
||||||
|
|
||||||
expect(result).toEqual(expected);
|
expect(result.toJS()).toEqual(expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('normalizes Mitra attachments', () => {
|
it('normalizes Mitra attachments', () => {
|
||||||
|
|
|
@ -97,6 +97,13 @@ const normalizeLocation = (account: ImmutableMap<string, any>) => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Set username from acct, if applicable
|
||||||
|
const fixUsername = (account: ImmutableMap<string, any>) => {
|
||||||
|
return account.update('username', username => (
|
||||||
|
username || (account.get('acct') || '').split('@')[0]
|
||||||
|
));
|
||||||
|
};
|
||||||
|
|
||||||
export const normalizeAccount = (account: ImmutableMap<string, any>): IAccount => {
|
export const normalizeAccount = (account: ImmutableMap<string, any>): IAccount => {
|
||||||
return AccountRecord(
|
return AccountRecord(
|
||||||
account.withMutations(account => {
|
account.withMutations(account => {
|
||||||
|
@ -104,6 +111,7 @@ export const normalizeAccount = (account: ImmutableMap<string, any>): IAccount =
|
||||||
normalizeVerified(account);
|
normalizeVerified(account);
|
||||||
normalizeBirthday(account);
|
normalizeBirthday(account);
|
||||||
normalizeLocation(account);
|
normalizeLocation(account);
|
||||||
|
fixUsername(account);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
@ -4,8 +4,8 @@ import {
|
||||||
Record as ImmutableRecord,
|
Record as ImmutableRecord,
|
||||||
} from 'immutable';
|
} from 'immutable';
|
||||||
|
|
||||||
|
import { normalizeAccount } from 'soapbox/normalizers/account';
|
||||||
import { IStatus } from 'soapbox/types';
|
import { IStatus } from 'soapbox/types';
|
||||||
import { accountToMention } from 'soapbox/utils/accounts';
|
|
||||||
import { mergeDefined } from 'soapbox/utils/normalizers';
|
import { mergeDefined } from 'soapbox/utils/normalizers';
|
||||||
|
|
||||||
const StatusRecord = ImmutableRecord({
|
const StatusRecord = ImmutableRecord({
|
||||||
|
@ -79,6 +79,14 @@ const AttachmentRecord = ImmutableRecord({
|
||||||
url: '',
|
url: '',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// https://docs.joinmastodon.org/entities/mention/
|
||||||
|
const MentionRecord = ImmutableRecord({
|
||||||
|
id: '',
|
||||||
|
acct: '',
|
||||||
|
username: '',
|
||||||
|
url: '',
|
||||||
|
});
|
||||||
|
|
||||||
// Ensure attachments have required fields
|
// Ensure attachments have required fields
|
||||||
// https://docs.joinmastodon.org/entities/attachment/
|
// https://docs.joinmastodon.org/entities/attachment/
|
||||||
const normalizeAttachment = (attachment: ImmutableMap<string, any>) => {
|
const normalizeAttachment = (attachment: ImmutableMap<string, any>) => {
|
||||||
|
@ -104,13 +112,7 @@ const normalizeAttachments = (status: ImmutableMap<string, any>) => {
|
||||||
|
|
||||||
// Normalize mentions
|
// Normalize mentions
|
||||||
const normalizeMention = (mention: ImmutableMap<string, any>) => {
|
const normalizeMention = (mention: ImmutableMap<string, any>) => {
|
||||||
const base = ImmutableMap({
|
return MentionRecord(normalizeAccount(mention));
|
||||||
acct: '',
|
|
||||||
username: (mention.get('acct') || '').split('@')[0],
|
|
||||||
url: '',
|
|
||||||
});
|
|
||||||
|
|
||||||
return mention.mergeWith(mergeDefined, base);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const normalizeMentions = (status: ImmutableMap<string, any>) => {
|
const normalizeMentions = (status: ImmutableMap<string, any>) => {
|
||||||
|
@ -184,8 +186,8 @@ const addSelfMention = (status: ImmutableMap<string, any>) => {
|
||||||
const isSelfReply = accountId === status.get('in_reply_to_account_id');
|
const isSelfReply = accountId === status.get('in_reply_to_account_id');
|
||||||
const hasSelfMention = accountId === status.getIn(['mentions', 0, 'id']);
|
const hasSelfMention = accountId === status.getIn(['mentions', 0, 'id']);
|
||||||
|
|
||||||
if (isSelfReply && !hasSelfMention) {
|
if (isSelfReply && !hasSelfMention && accountId) {
|
||||||
const mention = accountToMention(status.get('account'));
|
const mention = normalizeMention(status.get('account'));
|
||||||
return status.update('mentions', ImmutableList(), mentions => (
|
return status.update('mentions', ImmutableList(), mentions => (
|
||||||
ImmutableList([mention]).concat(mentions)
|
ImmutableList([mention]).concat(mentions)
|
||||||
));
|
));
|
||||||
|
|
|
@ -6,7 +6,6 @@ import {
|
||||||
isStaff,
|
isStaff,
|
||||||
isAdmin,
|
isAdmin,
|
||||||
isModerator,
|
isModerator,
|
||||||
accountToMention,
|
|
||||||
} from '../accounts';
|
} from '../accounts';
|
||||||
|
|
||||||
describe('getDomain', () => {
|
describe('getDomain', () => {
|
||||||
|
@ -116,19 +115,3 @@ describe('isModerator', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('accountToMention', () => {
|
|
||||||
it('converts the account to a mention', () => {
|
|
||||||
const account = fromJS(require('soapbox/__fixtures__/pleroma-account.json'));
|
|
||||||
|
|
||||||
const expected = fromJS({
|
|
||||||
id: '9v5bmRalQvjOy0ECcC',
|
|
||||||
username: 'alex',
|
|
||||||
acct: 'alex',
|
|
||||||
url: 'https://gleasonator.com/users/alex',
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = accountToMention(account);
|
|
||||||
expect(result).toEqual(expected);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
|
@ -62,12 +62,3 @@ export const isLocal = (account: ImmutableMap<string, any>): boolean => {
|
||||||
};
|
};
|
||||||
|
|
||||||
export const isRemote = (account: ImmutableMap<string, any>): boolean => !isLocal(account);
|
export const isRemote = (account: ImmutableMap<string, any>): boolean => !isLocal(account);
|
||||||
|
|
||||||
export const accountToMention = (account: ImmutableMap<string, any>): ImmutableMap<string, any> => {
|
|
||||||
return ImmutableMap({
|
|
||||||
id: account.get('id'),
|
|
||||||
username: account.get('username'),
|
|
||||||
acct: account.get('acct'),
|
|
||||||
url: account.get('url'),
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
Loading…
Reference in a new issue