Skip to content

Commit

Permalink
Wrapp mentions in special tags before creating an annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Feb 10, 2025
1 parent aa75693 commit dc7b080
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/sidebar/services/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import type {
SavedAnnotation,
} from '../../types/api';
import type { AnnotationEventType, SidebarSettings } from '../../types/config';
import { parseAccountID } from '../helpers/account-id';
import * as metadata from '../helpers/annotation-metadata';
import { wrapMentions } from '../helpers/mentions';
import {
defaultPermissions,
privatePermissions,
Expand Down Expand Up @@ -46,10 +48,16 @@ export class AnnotationsService {
private _applyDraftChanges(annotation: Annotation): Annotation {
const changes: Partial<Annotation> = {};
const draft = this._store.getDraft(annotation);
const authority =
parseAccountID(this._store.profile().userid)?.provider ??
this._store.defaultAuthority();
const mentionsEnabled = this._store.isFeatureEnabled('at_mentions');

if (draft) {
changes.tags = draft.tags;
changes.text = draft.text;
changes.text = mentionsEnabled
? wrapMentions(draft.text, authority)
: draft.text;
changes.permissions = draft.isPrivate
? privatePermissions(annotation.user)
: sharedPermissions(annotation.user, annotation.group);
Expand Down
38 changes: 38 additions & 0 deletions src/sidebar/services/test/annotations-test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sinon from 'sinon';

import * as fixtures from '../../test/annotation-fixtures';
import { AnnotationsService, $imports } from '../annotations';

Expand Down Expand Up @@ -67,6 +69,8 @@ describe('AnnotationsService', () => {
selectTab: sinon.stub(),
setExpanded: sinon.stub(),
updateFlagStatus: sinon.stub(),
defaultAuthority: sinon.stub().returns('hypothes.is'),
isFeatureEnabled: sinon.stub().returns(false),
};

setLoggedIn(true);
Expand Down Expand Up @@ -506,6 +510,40 @@ describe('AnnotationsService', () => {
});
});

[
{
profile: { userid: 'acct:[email protected]' },
mentionsEnabled: false,
expectedText: 'hello @bob',
},
{
profile: { userid: 'acct:[email protected]' },
mentionsEnabled: true,
expectedText:
'hello <a data-hyp-mention="" data-userid="acct:[email protected]">@bob</a>',
},
{
profile: { userid: 'acct:foo' },
mentionsEnabled: true,
expectedText:
'hello <a data-hyp-mention="" data-userid="acct:[email protected]">@bob</a>',
},
].forEach(({ profile, mentionsEnabled, expectedText }) => {
it('wraps mentions in tags when feature is enabled', async () => {
fakeStore.isFeatureEnabled.returns(mentionsEnabled);
fakeStore.profile.returns(profile);
fakeStore.getDraft.returns({ text: 'hello @bob' });

await svc.save(fixtures.defaultAnnotation());

assert.calledWith(
fakeApi.annotation.create,
{},
sinon.match({ text: expectedText }),
);
});
});

context('successful save', () => {
it('copies over internal app-specific keys to the annotation object', () => {
fakeMetadata.isSaved.returns(true);
Expand Down

0 comments on commit dc7b080

Please sign in to comment.