diff --git a/src/app/components/create-status/autosuggest/autosuggest.component.ts b/src/app/components/create-status/autosuggest/autosuggest.component.ts index 95b5d18e..cd4097fd 100644 --- a/src/app/components/create-status/autosuggest/autosuggest.component.ts +++ b/src/app/components/create-status/autosuggest/autosuggest.component.ts @@ -64,7 +64,12 @@ export class AutosuggestComponent implements OnInit, OnDestroy { this.lastPatternUsed = pattern; this.lastPatternUsedWtType = value; - this.mastodonService.search(selectedAccount, pattern, false) + this.toolsService.getInstanceInfo(selectedAccount) + .then(instance => { + let version: 'v1' | 'v2' = 'v1'; + if(instance.major >= 3) version = 'v2'; + return this.mastodonService.search(selectedAccount, pattern, version, false); + }) .then((results: Results) => { if (this.lastPatternUsed !== pattern) return; diff --git a/src/app/components/floating-column/search/search.component.ts b/src/app/components/floating-column/search/search.component.ts index 9b5985c0..137646b9 100644 --- a/src/app/components/floating-column/search/search.component.ts +++ b/src/app/components/floating-column/search/search.component.ts @@ -69,7 +69,12 @@ export class SearchComponent implements OnInit { this.lastAccountUsed = this.toolsService.getSelectedAccounts()[0]; - this.mastodonService.search(this.lastAccountUsed, data, true) + this.toolsService.getInstanceInfo(this.lastAccountUsed) + .then(instance => { + let version: 'v1' | 'v2' = 'v1'; + if (instance.major >= 3) version = 'v2'; + return this.mastodonService.search(this.lastAccountUsed, data, version, true) + }) .then((results: Results) => { if (results) { this.accounts = results.accounts.slice(0, 5); diff --git a/src/app/components/stream/status/action-bar/status-user-context-menu/status-user-context-menu.component.ts b/src/app/components/stream/status/action-bar/status-user-context-menu/status-user-context-menu.component.ts index 76b46b9c..28edbbf3 100644 --- a/src/app/components/stream/status/action-bar/status-user-context-menu/status-user-context-menu.component.ts +++ b/src/app/components/stream/status/action-bar/status-user-context-menu/status-user-context-menu.component.ts @@ -47,14 +47,14 @@ export class StatusUserContextMenuComponent implements OnInit, OnDestroy { this.accounts$ = this.store.select(state => state.registeredaccounts.accounts); } - ngOnInit() { + ngOnInit() { if (this.statusWrapper) { const status = this.statusWrapper.status; if (status.reblog) { this.displayedStatus = status.reblog; } else { this.displayedStatus = status; - } + } } this.accountSub = this.accounts$.subscribe((accounts: AccountInfo[]) => { @@ -63,7 +63,7 @@ export class StatusUserContextMenuComponent implements OnInit, OnDestroy { }); let account: Account; - if(this.statusWrapper) { + if (this.statusWrapper) { account = this.displayedStatus.account; } else { account = this.displayedAccount; @@ -82,7 +82,7 @@ export class StatusUserContextMenuComponent implements OnInit, OnDestroy { ngOnDestroy(): void { - if(this.accountSub) this.accountSub.unsubscribe(); + if (this.accountSub) this.accountSub.unsubscribe(); } public onContextMenu($event: MouseEvent): void { @@ -259,10 +259,16 @@ export class StatusUserContextMenuComponent implements OnInit, OnDestroy { let statusPromise: Promise = Promise.resolve(this.statusWrapper.status); if (account.id !== this.statusWrapper.provider.id) { - statusPromise = this.mastodonService.search(account, this.statusWrapper.status.url, true) - .then((result: Results) => { - return result.statuses[0]; - }); + statusPromise = + this.toolsService.getInstanceInfo(account) + .then(instance => { + let version: 'v1' | 'v2' = 'v1'; + if (instance.major >= 3) version = 'v2'; + return this.mastodonService.search(account, this.statusWrapper.status.url, version, true); + }) + .then((result: Results) => { + return result.statuses[0]; + }); } return statusPromise; diff --git a/src/app/components/stream/thread/thread.component.ts b/src/app/components/stream/thread/thread.component.ts index 17f72e96..1fe454bd 100644 --- a/src/app/components/stream/thread/thread.component.ts +++ b/src/app/components/stream/thread/thread.component.ts @@ -128,7 +128,12 @@ export class ThreadComponent implements OnInit, OnDestroy { var statusPromise: Promise = Promise.resolve(status); if (sourceAccount.id !== currentAccount.id) { - statusPromise = this.mastodonService.search(currentAccount, status.uri, true) + statusPromise = this.toolsService.getInstanceInfo(currentAccount) + .then(instance => { + let version: 'v1' | 'v2' = 'v1'; + if (instance.major >= 3) version = 'v2'; + return this.mastodonService.search(currentAccount, status.uri, version, true); + }) .then((result: Results) => { if (result.statuses.length === 1) { const retrievedStatus = result.statuses[0]; diff --git a/src/app/services/mastodon.service.ts b/src/app/services/mastodon.service.ts index 3f3a9d48..8e3aacec 100644 --- a/src/app/services/mastodon.service.ts +++ b/src/app/services/mastodon.service.ts @@ -128,9 +128,12 @@ export class MastodonService { return this.httpClient.get(route, { headers: headers }).toPromise() } - search(account: AccountInfo, query: string, resolve: boolean = false): Promise { + search(account: AccountInfo, query: string, version: 'v1' | 'v2', resolve: boolean = false): Promise { if (query[0] === '#') query = query.substr(1); - const route = `https://${account.instance}${this.apiRoutes.search}?q=${query}&resolve=${resolve}`; + let searchRoute = this.apiRoutes.search; + if(version === 'v2') searchRoute = this.apiRoutes.searchV2; + + const route = `https://${account.instance}${searchRoute}?q=${query}&resolve=${resolve}`; const headers = new HttpHeaders({ 'Authorization': `Bearer ${account.token.access_token}` }); return this.httpClient.get(route, { headers: headers }).toPromise() } diff --git a/src/app/services/tools.service.ts b/src/app/services/tools.service.ts index 077ac0f6..356603c6 100644 --- a/src/app/services/tools.service.ts +++ b/src/app/services/tools.service.ts @@ -12,11 +12,37 @@ import { AccountSettings, SaveAccountSettings } from '../states/settings.state'; }) export class ToolsService { private accountAvatar: { [id: string]: string; } = {}; + private instanceInfos: { [id: string]: InstanceInfo } = {}; constructor( private readonly mastodonService: MastodonService, private readonly store: Store) { } + getInstanceInfo(acc: AccountInfo): Promise { + if (this.instanceInfos[acc.instance]) { + return Promise.resolve(this.instanceInfos[acc.instance]); + } else { + return this.mastodonService.getInstance(acc.instance) + .then(instance => { + var type = InstanceType.Mastodon; + if (instance.version.toLowerCase().includes('pleroma')) { + type = InstanceType.Pleroma; + } else if (instance.version.toLowerCase().includes('+glitch')) { + type = InstanceType.GlitchSoc; + } else if (instance.version.toLowerCase().includes('+florence')) { + type = InstanceType.Florence; + } + + var splittedVersion = instance.version.split('.'); + var major = +splittedVersion[0]; + var minor = +splittedVersion[1]; + + var instanceInfo = new InstanceInfo(type, major, minor); + this.instanceInfos[acc.instance] = instanceInfo; + return instanceInfo; + }); + } + } getAvatar(acc: AccountInfo): Promise { if (this.accountAvatar[acc.id]) { @@ -60,7 +86,12 @@ export class ToolsService { } findAccount(account: AccountInfo, accountName: string): Promise { - return this.mastodonService.search(account, accountName, true) + return this.getInstanceInfo(account) + .then(instance => { + let version: 'v1' | 'v2' = 'v1'; + if (instance.major >= 3) version = 'v2'; + return this.mastodonService.search(account, accountName, version, true); + }) .then((result: Results) => { if (accountName[0] === '@') accountName = accountName.substr(1); @@ -80,13 +111,19 @@ export class ToolsService { let statusPromise: Promise = Promise.resolve(originalStatus.status); if (!isProvider) { - statusPromise = statusPromise.then((foreignStatus: Status) => { - const statusUrl = foreignStatus.url; - return this.mastodonService.search(account, statusUrl, true) - .then((results: Results) => { - return results.statuses[0]; - }); - }); + statusPromise = statusPromise + .then((foreignStatus: Status) => { + const statusUrl = foreignStatus.url; + return this.getInstanceInfo(account) + .then(instance => { + let version: 'v1' | 'v2' = 'v1'; + if (instance.major >= 3) version = 'v2'; + return this.mastodonService.search(account, statusUrl, version, true); + }) + .then((results: Results) => { + return results.statuses[0]; + }); + }); } return statusPromise; @@ -121,3 +158,18 @@ export class OpenThreadEvent { ) { } } + +export class InstanceInfo { + constructor( + public readonly type: InstanceType, + public readonly major: number, + public readonly minor: number) { + } +} + +export enum InstanceType { + Mastodon = 1, + Pleroma = 2, + GlitchSoc = 3, + Florence = 4 +} \ No newline at end of file