Skip to content

Commit dc7b080

Browse files
committed
Wrapp mentions in special tags before creating an annotation
1 parent aa75693 commit dc7b080

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/sidebar/services/annotations.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import type {
66
SavedAnnotation,
77
} from '../../types/api';
88
import type { AnnotationEventType, SidebarSettings } from '../../types/config';
9+
import { parseAccountID } from '../helpers/account-id';
910
import * as metadata from '../helpers/annotation-metadata';
11+
import { wrapMentions } from '../helpers/mentions';
1012
import {
1113
defaultPermissions,
1214
privatePermissions,
@@ -46,10 +48,16 @@ export class AnnotationsService {
4648
private _applyDraftChanges(annotation: Annotation): Annotation {
4749
const changes: Partial<Annotation> = {};
4850
const draft = this._store.getDraft(annotation);
51+
const authority =
52+
parseAccountID(this._store.profile().userid)?.provider ??
53+
this._store.defaultAuthority();
54+
const mentionsEnabled = this._store.isFeatureEnabled('at_mentions');
4955

5056
if (draft) {
5157
changes.tags = draft.tags;
52-
changes.text = draft.text;
58+
changes.text = mentionsEnabled
59+
? wrapMentions(draft.text, authority)
60+
: draft.text;
5361
changes.permissions = draft.isPrivate
5462
? privatePermissions(annotation.user)
5563
: sharedPermissions(annotation.user, annotation.group);

src/sidebar/services/test/annotations-test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import sinon from 'sinon';
2+
13
import * as fixtures from '../../test/annotation-fixtures';
24
import { AnnotationsService, $imports } from '../annotations';
35

@@ -67,6 +69,8 @@ describe('AnnotationsService', () => {
6769
selectTab: sinon.stub(),
6870
setExpanded: sinon.stub(),
6971
updateFlagStatus: sinon.stub(),
72+
defaultAuthority: sinon.stub().returns('hypothes.is'),
73+
isFeatureEnabled: sinon.stub().returns(false),
7074
};
7175

7276
setLoggedIn(true);
@@ -506,6 +510,40 @@ describe('AnnotationsService', () => {
506510
});
507511
});
508512

513+
[
514+
{
515+
profile: { userid: 'acct:[email protected]' },
516+
mentionsEnabled: false,
517+
expectedText: 'hello @bob',
518+
},
519+
{
520+
profile: { userid: 'acct:[email protected]' },
521+
mentionsEnabled: true,
522+
expectedText:
523+
'hello <a data-hyp-mention="" data-userid="acct:[email protected]">@bob</a>',
524+
},
525+
{
526+
profile: { userid: 'acct:foo' },
527+
mentionsEnabled: true,
528+
expectedText:
529+
'hello <a data-hyp-mention="" data-userid="acct:[email protected]">@bob</a>',
530+
},
531+
].forEach(({ profile, mentionsEnabled, expectedText }) => {
532+
it('wraps mentions in tags when feature is enabled', async () => {
533+
fakeStore.isFeatureEnabled.returns(mentionsEnabled);
534+
fakeStore.profile.returns(profile);
535+
fakeStore.getDraft.returns({ text: 'hello @bob' });
536+
537+
await svc.save(fixtures.defaultAnnotation());
538+
539+
assert.calledWith(
540+
fakeApi.annotation.create,
541+
{},
542+
sinon.match({ text: expectedText }),
543+
);
544+
});
545+
});
546+
509547
context('successful save', () => {
510548
it('copies over internal app-specific keys to the annotation object', () => {
511549
fakeMetadata.isSaved.returns(true);

0 commit comments

Comments
 (0)