Sengi-Windows-MacOS-Linux/src/app/services/tools.service.ts

191 lines
7.1 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
import { Store } from '@ngxs/store';
import { AccountInfo } from '../states/accounts.state';
import { MastodonWrapperService } from './mastodon-wrapper.service';
2019-07-30 03:05:37 +02:00
import { Account, Results, Status, Emoji } from "./models/mastodon.interfaces";
import { StatusWrapper } from '../models/common.model';
2019-11-14 04:18:39 +01:00
import { AccountSettings, SaveAccountSettings, GlobalSettings, SaveSettings } from '../states/settings.state';
import { AppInfo, RegisteredAppsStateModel } from '../states/registered-apps.state';
@Injectable({
providedIn: 'root'
})
2019-07-30 03:20:12 +02:00
export class ToolsService {
2019-09-06 07:18:41 +02:00
private accountAvatar: { [id: string]: string; } = {};
2019-09-25 06:09:10 +02:00
private instanceInfos: { [id: string]: InstanceInfo } = {};
2019-09-06 07:18:41 +02:00
constructor(
private readonly mastodonService: MastodonWrapperService,
private readonly store: Store) { }
2019-09-25 06:09:10 +02:00
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;
2019-09-28 03:55:40 +02:00
} else if (instance.version.toLowerCase().includes('pixelfed')) {
type = InstanceType.Pixelfed;
2019-09-25 06:09:10 +02:00
}
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;
2019-09-28 03:55:40 +02:00
2019-09-25 06:09:10 +02:00
return instanceInfo;
});
}
}
2019-09-06 07:18:41 +02:00
getAvatar(acc: AccountInfo): Promise<string> {
if (this.accountAvatar[acc.id]) {
return Promise.resolve(this.accountAvatar[acc.id]);
} else {
return this.mastodonService.retrieveAccountDetails(acc)
.then((result: Account) => {
this.accountAvatar[acc.id] = result.avatar;
return result.avatar;
})
.catch((err) => {
return "";
});
}
}
getSelectedAccounts(): AccountInfo[] {
2019-09-30 01:21:43 +02:00
let regAccounts = <AccountInfo[]>this.store.snapshot().registeredaccounts.accounts;
return regAccounts.filter(x => x.isSelected);
}
2019-07-30 03:20:12 +02:00
2019-04-01 00:30:38 +02:00
getAccountSettings(account: AccountInfo): AccountSettings {
2019-09-30 01:21:43 +02:00
let accountsSettings = <AccountSettings[]>this.store.snapshot().globalsettings.settings.accountSettings;
2019-04-01 00:30:38 +02:00
let accountSettings = accountsSettings.find(x => x.accountId === account.id);
2019-07-30 03:20:12 +02:00
if (!accountSettings) {
2019-04-01 00:30:38 +02:00
accountSettings = new AccountSettings();
accountSettings.accountId = account.id;
2019-07-30 03:20:12 +02:00
this.saveAccountSettings(accountSettings);
2019-04-01 00:30:38 +02:00
}
2019-07-30 03:20:12 +02:00
if (!accountSettings.customStatusCharLength) {
accountSettings.customStatusCharLength = 500;
this.saveAccountSettings(accountSettings);
}
2019-04-01 00:30:38 +02:00
return accountSettings;
}
2019-07-30 03:20:12 +02:00
saveAccountSettings(accountSettings: AccountSettings) {
2019-04-01 00:30:38 +02:00
this.store.dispatch([
new SaveAccountSettings(accountSettings)
2019-11-14 04:18:39 +01:00
]);
}
getSettings(): GlobalSettings {
let settings = <GlobalSettings>this.store.snapshot().globalsettings.settings;
return settings;
}
saveSettings(settings: GlobalSettings){
this.store.dispatch([
new SaveSettings(settings)
]);
2019-04-01 00:30:38 +02:00
}
findAccount(account: AccountInfo, accountName: string): Promise<Account> {
2019-09-25 06:09:10 +02:00
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) => {
2019-07-30 03:20:12 +02:00
if (accountName[0] === '@') accountName = accountName.substr(1);
2018-11-03 19:39:32 +01:00
2019-04-03 06:08:50 +02:00
const foundAccount = result.accounts.find(
2019-07-07 17:29:30 +02:00
x => (x.acct.toLowerCase() === accountName.toLowerCase()
2019-07-30 03:20:12 +02:00
||
(x.acct.toLowerCase().split('@')[0] === accountName.toLowerCase().split('@')[0])
&& x.url.replace('https://', '').split('/')[0] === accountName.toLowerCase().split('@')[1])
);
return foundAccount;
});
}
2019-07-30 03:20:12 +02:00
getStatusUsableByAccount(account: AccountInfo, originalStatus: StatusWrapper): Promise<Status> {
2019-02-12 04:28:15 +01:00
const isProvider = originalStatus.provider.id === account.id;
let statusPromise: Promise<Status> = Promise.resolve(originalStatus.status);
if (!isProvider) {
2019-09-25 06:09:10 +02:00
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];
});
});
2019-02-12 04:28:15 +01:00
}
return statusPromise;
}
2019-04-01 00:30:38 +02:00
2019-07-12 01:02:15 +02:00
getAccountFullHandle(account: Account): string {
let fullHandle = account.acct.toLowerCase();
if (!fullHandle.includes('@')) {
fullHandle += `@${account.url.replace('https://', '').split('/')[0]}`;
}
return `@${fullHandle}`;
}
2019-07-30 03:05:37 +02:00
2019-07-30 03:20:12 +02:00
private emojiCache: { [id: string]: Emoji[] } = {};
getCustomEmojis(account: AccountInfo): Promise<Emoji[]> {
if (this.emojiCache[account.id]) {
return Promise.resolve(this.emojiCache[account.id]);
} else {
return this.mastodonService.getCustomEmojis(account)
.then(emojis => {
this.emojiCache[account.id] = emojis.filter(x => x.visible_in_picker);
return this.emojiCache[account.id];
});
}
}
2019-02-19 04:44:21 +01:00
}
2019-02-12 04:28:15 +01:00
2019-02-19 04:44:21 +01:00
export class OpenThreadEvent {
constructor(
public status: Status,
public sourceAccount: AccountInfo
) {
}
}
2019-09-25 06:09:10 +02:00
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,
2019-09-28 03:55:40 +02:00
Florence = 4,
Pixelfed = 5
2019-09-25 06:09:10 +02:00
}