fix search issues in 3.0.0rc1

This commit is contained in:
Nicolas Constant 2019-09-25 00:09:10 -04:00
parent 2d9b48dede
commit ebbd5c944f
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
6 changed files with 97 additions and 21 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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,7 +259,13 @@ export class StatusUserContextMenuComponent implements OnInit, OnDestroy {
let statusPromise: Promise<Status> = Promise.resolve(this.statusWrapper.status);
if (account.id !== this.statusWrapper.provider.id) {
statusPromise = this.mastodonService.search(account, this.statusWrapper.status.url, true)
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];
});

View File

@ -128,7 +128,12 @@ export class ThreadComponent implements OnInit, OnDestroy {
var statusPromise: Promise<Status> = 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];

View File

@ -128,9 +128,12 @@ export class MastodonService {
return this.httpClient.get<Status>(route, { headers: headers }).toPromise()
}
search(account: AccountInfo, query: string, resolve: boolean = false): Promise<Results> {
search(account: AccountInfo, query: string, version: 'v1' | 'v2', resolve: boolean = false): Promise<Results> {
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<Results>(route, { headers: headers }).toPromise()
}

View File

@ -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<InstanceInfo> {
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<string> {
if (this.accountAvatar[acc.id]) {
@ -60,7 +86,12 @@ export class ToolsService {
}
findAccount(account: AccountInfo, accountName: string): Promise<Account> {
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,9 +111,15 @@ export class ToolsService {
let statusPromise: Promise<Status> = Promise.resolve(originalStatus.status);
if (!isProvider) {
statusPromise = statusPromise.then((foreignStatus: Status) => {
statusPromise = statusPromise
.then((foreignStatus: Status) => {
const statusUrl = foreignStatus.url;
return this.mastodonService.search(account, statusUrl, true)
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];
});
@ -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
}