Sengi-Windows-MacOS-Linux/src/app/components/floating-column/search/search.component.ts

114 lines
3.9 KiB
TypeScript
Raw Normal View History

2023-08-06 23:06:36 +02:00
import { Component, OnInit, Input, Output, EventEmitter, ViewChild, ElementRef } from '@angular/core';
2019-02-12 05:41:21 +01:00
import { HttpErrorResponse } from '@angular/common/http';
import { MastodonWrapperService } from '../../../services/mastodon-wrapper.service';
import { AccountInfo } from '../../../states/accounts.state';
2019-02-12 05:41:21 +01:00
import { Results, Account } from '../../../services/models/mastodon.interfaces';
2019-02-19 04:44:21 +01:00
import { ToolsService, OpenThreadEvent } from '../../../services/tools.service';
2019-02-12 05:41:21 +01:00
import { NotificationService } from '../../../services/notification.service';
import { StatusWrapper } from '../../../models/common.model';
2018-09-22 06:22:51 +02:00
@Component({
selector: 'app-search',
templateUrl: './search.component.html',
styleUrls: ['./search.component.scss']
2018-09-22 06:22:51 +02:00
})
export class SearchComponent implements OnInit {
@Input() searchHandle: string;
accounts: Account[] = [];
2018-11-03 05:57:39 +01:00
statuses: StatusWrapper[] = [];
hashtags: string[] = [];
2018-10-02 03:44:59 +02:00
isLoading: boolean;
@Output() browseAccountEvent = new EventEmitter<string>();
@Output() browseHashtagEvent = new EventEmitter<string>();
2019-02-19 04:44:21 +01:00
@Output() browseThreadEvent = new EventEmitter<OpenThreadEvent>();
2018-11-08 05:53:33 +01:00
2023-08-06 23:06:36 +02:00
@ViewChild('search') searchElement: ElementRef;
constructor(
2019-02-12 05:41:21 +01:00
private readonly notificationService: NotificationService,
2018-11-03 05:57:39 +01:00
private readonly toolsService: ToolsService,
private readonly mastodonService: MastodonWrapperService) { }
ngOnInit() {
2023-08-06 23:06:36 +02:00
this.searchElement.nativeElement.focus();
}
onSubmit(): boolean {
this.searchHandle
this.search(this.searchHandle);
return false;
}
browseHashtag(hashtag: string): boolean {
2018-11-08 05:53:33 +01:00
if (hashtag) {
this.browseHashtagEvent.next(hashtag);
2018-11-08 05:53:33 +01:00
}
return false;
}
2018-11-03 06:38:52 +01:00
2019-03-08 01:50:27 +01:00
browseThread(openThreadEvent: OpenThreadEvent): boolean {
if (openThreadEvent) {
this.browseThreadEvent.next(openThreadEvent);
}
2019-03-08 01:50:27 +01:00
return false;
}
2018-11-03 05:34:56 +01:00
2020-03-14 08:26:37 +01:00
browseAccount(account: string): boolean {
if (account) {
this.browseAccountEvent.next(account);
}
2018-11-08 05:53:33 +01:00
return false;
}
2020-03-14 18:07:06 +01:00
processAndBrowseAccount(account: Account): boolean {
if(account){
const fullHandle = this.toolsService.getAccountFullHandle(account);
this.browseAccountEvent.next(fullHandle);
}
return false;
}
2018-11-03 06:38:52 +01:00
private lastAccountUsed: AccountInfo;
private search(data: string) {
2020-03-14 08:26:37 +01:00
if (!data) return;
2020-02-15 02:14:42 +01:00
this.accounts.length = 0;
this.statuses.length = 0;
this.hashtags.length = 0;
2018-10-02 03:44:59 +02:00
this.isLoading = true;
2019-03-08 01:50:27 +01:00
this.lastAccountUsed = this.toolsService.getSelectedAccounts()[0];
2019-09-25 06:09:10 +02:00
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)
})
2019-03-08 01:50:27 +01:00
.then((results: Results) => {
if (results) {
this.accounts = results.accounts.slice(0, 7);
2019-03-08 01:50:27 +01:00
this.hashtags = results.hashtags;
if(data && data[0] === '#' && !this.hashtags.map(x => x.toLowerCase()).includes(data.replace('#', '').toLowerCase())){
this.hashtags.unshift(data.replace('#', ''));
}
2019-03-08 01:50:27 +01:00
for (let status of results.statuses) {
2020-04-01 08:29:51 +02:00
let cwPolicy = this.toolsService.checkContentWarning(status);
const statusWrapper = new StatusWrapper(cwPolicy.status, this.lastAccountUsed, cwPolicy.applyCw, cwPolicy.hide);
2019-03-08 01:50:27 +01:00
this.statuses.push(statusWrapper);
2018-10-01 02:45:20 +02:00
}
2019-03-08 01:50:27 +01:00
}
})
.catch((err: HttpErrorResponse) => {
2019-09-07 23:52:07 +02:00
this.notificationService.notifyHttpError(err, this.lastAccountUsed);
2019-03-08 01:50:27 +01:00
})
.then(() => { this.isLoading = false; });
}
2018-09-22 06:22:51 +02:00
}