2018-10-31 05:02:03 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { Store } from '@ngxs/store';
|
|
|
|
|
|
|
|
import { AccountInfo } from '../states/accounts.state';
|
2019-10-02 06:14:40 +02:00
|
|
|
import { MastodonWrapperService } from './mastodon-wrapper.service';
|
2019-07-30 03:05:37 +02:00
|
|
|
import { Account, Results, Status, Emoji } from "./models/mastodon.interfaces";
|
2019-03-06 04:46:50 +01:00
|
|
|
import { StatusWrapper } from '../models/common.model';
|
2020-04-01 08:29:51 +02:00
|
|
|
import { AccountSettings, SaveAccountSettings, GlobalSettings, SaveSettings, ContentWarningPolicy, SaveContentWarningPolicy, ContentWarningPolicyEnum } from '../states/settings.state';
|
2019-10-03 06:05:00 +02:00
|
|
|
import { AppInfo, RegisteredAppsStateModel } from '../states/registered-apps.state';
|
2018-10-31 05:02:03 +01:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
2020-04-01 08:29:51 +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
|
|
|
|
2018-11-03 02:35:33 +01:00
|
|
|
constructor(
|
2019-10-02 06:14:40 +02:00
|
|
|
private readonly mastodonService: MastodonWrapperService,
|
2018-11-03 02:35:33 +01:00
|
|
|
private readonly store: Store) { }
|
2018-10-31 05:02:03 +01:00
|
|
|
|
2020-04-01 08:29:51 +02:00
|
|
|
checkContentWarning(status: Status): StatusWithCwPolicyResult {
|
|
|
|
if(!status) {
|
|
|
|
return new StatusWithCwPolicyResult(status, false, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
let applyCw = false;
|
|
|
|
let hideStatus = false;
|
|
|
|
|
|
|
|
let cwPolicy = this.getSettings().contentWarningPolicy;
|
|
|
|
|
|
|
|
let splittedContent = [];
|
|
|
|
if ((cwPolicy.policy === ContentWarningPolicyEnum.HideAll && cwPolicy.addCwOnContent.length > 0)
|
|
|
|
|| (cwPolicy.policy === ContentWarningPolicyEnum.AddOnAllContent && cwPolicy.removeCwOnContent.length > 0)
|
|
|
|
|| (cwPolicy.hideCompletlyContent && cwPolicy.hideCompletlyContent.length > 0)) {
|
|
|
|
let parser = new DOMParser();
|
|
|
|
let dom = parser.parseFromString((status.content + ' ' + status.spoiler_text).replace("<br/>", " ").replace("<br>", " ").replace(/\n/g, ' '), 'text/html')
|
|
|
|
let contentToParse = dom.body.textContent;
|
|
|
|
splittedContent = contentToParse.toLowerCase().split(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cwPolicy.policy === ContentWarningPolicyEnum.None && (status.sensitive || status.spoiler_text)) {
|
|
|
|
applyCw = true;
|
|
|
|
} else if (cwPolicy.policy === ContentWarningPolicyEnum.HideAll) {
|
|
|
|
let detected = cwPolicy.addCwOnContent.filter(x => splittedContent.find(y => y == x || y == `#${x}`));
|
|
|
|
if (!detected || detected.length === 0) {
|
|
|
|
applyCw = false;
|
|
|
|
} else {
|
|
|
|
if (!status.spoiler_text) {
|
|
|
|
status.spoiler_text = detected.join(' ');
|
|
|
|
}
|
|
|
|
applyCw = true;
|
|
|
|
}
|
|
|
|
} else if (cwPolicy.policy === ContentWarningPolicyEnum.AddOnAllContent) {
|
|
|
|
let detected = cwPolicy.removeCwOnContent.filter(x => splittedContent.find(y => y == x || y == `#${x}`));
|
|
|
|
|
|
|
|
if (detected && detected.length > 0) {
|
|
|
|
applyCw = false;
|
|
|
|
} else {
|
|
|
|
applyCw = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cwPolicy.hideCompletlyContent && cwPolicy.hideCompletlyContent.length > 0) {
|
|
|
|
let detected = cwPolicy.hideCompletlyContent.filter(x => splittedContent.find(y => y == x || y == `#${x}`));
|
|
|
|
if (detected && detected.length > 0) {
|
|
|
|
hideStatus = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new StatusWithCwPolicyResult(status, applyCw, hideStatus);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-10-31 05:02:03 +01:00
|
|
|
|
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 "";
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 05:02:03 +01:00
|
|
|
getSelectedAccounts(): AccountInfo[] {
|
2019-09-30 01:21:43 +02:00
|
|
|
let regAccounts = <AccountInfo[]>this.store.snapshot().registeredaccounts.accounts;
|
2018-10-31 05:02:03 +01:00
|
|
|
return regAccounts.filter(x => x.isSelected);
|
|
|
|
}
|
2019-07-30 03:20:12 +02:00
|
|
|
|
2019-11-17 06:57:58 +01:00
|
|
|
getAccountById(accountId: string): AccountInfo {
|
|
|
|
let regAccounts = <AccountInfo[]>this.store.snapshot().registeredaccounts.accounts;
|
|
|
|
return regAccounts.find(x => x.id === accountId);
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-06-23 23:49:19 +02:00
|
|
|
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;
|
2020-03-28 22:04:36 +01:00
|
|
|
|
2020-04-01 08:29:51 +02:00
|
|
|
if (!settings.contentWarningPolicy) {
|
2020-03-28 22:04:36 +01:00
|
|
|
var newCwPolicy = new ContentWarningPolicy();
|
|
|
|
this.saveContentWarningPolicy(newCwPolicy);
|
|
|
|
return <GlobalSettings>this.store.snapshot().globalsettings.settings;
|
|
|
|
}
|
|
|
|
|
2019-11-14 04:18:39 +01:00
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
|
2020-03-12 01:54:32 +01:00
|
|
|
saveSettings(settings: GlobalSettings) {
|
2019-11-14 04:18:39 +01:00
|
|
|
this.store.dispatch([
|
|
|
|
new SaveSettings(settings)
|
|
|
|
]);
|
2019-04-01 00:30:38 +02:00
|
|
|
}
|
2018-10-31 05:02:03 +01:00
|
|
|
|
2020-04-01 08:29:51 +02:00
|
|
|
saveContentWarningPolicy(cwSettings: ContentWarningPolicy) {
|
2020-03-28 22:04:36 +01:00
|
|
|
this.store.dispatch([
|
|
|
|
new SaveContentWarningPolicy(cwSettings)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-11-03 02:35:33 +01:00
|
|
|
findAccount(account: AccountInfo, accountName: string): Promise<Account> {
|
2020-03-12 01:54:32 +01:00
|
|
|
let findAccountFunc = (result: Results) => {
|
|
|
|
if (accountName[0] === '@') accountName = accountName.substr(1);
|
|
|
|
|
|
|
|
const foundAccount = result.accounts.find(
|
|
|
|
x => (x.acct.toLowerCase() === accountName.toLowerCase()
|
|
|
|
||
|
|
|
|
(x.acct.toLowerCase().split('@')[0] === accountName.toLowerCase().split('@')[0])
|
|
|
|
&& x.url.replace('https://', '').split('/')[0] === accountName.toLowerCase().split('@')[1])
|
|
|
|
);
|
|
|
|
return foundAccount;
|
|
|
|
};
|
|
|
|
|
|
|
|
let searchVersion: 'v1' | 'v2' = 'v1';
|
2019-09-25 06:09:10 +02:00
|
|
|
return this.getInstanceInfo(account)
|
|
|
|
.then(instance => {
|
2020-03-12 01:54:32 +01:00
|
|
|
//let version: 'v1' | 'v2' = 'v1';
|
|
|
|
if (instance.major >= 3) searchVersion = 'v2';
|
|
|
|
return this.mastodonService.search(account, accountName, searchVersion, true);
|
2019-09-25 06:09:10 +02:00
|
|
|
})
|
2020-03-12 01:54:32 +01:00
|
|
|
.then((results: Results) => {
|
|
|
|
return findAccountFunc(results);
|
|
|
|
})
|
|
|
|
.then((foundAccount: Account) => {
|
|
|
|
if (foundAccount != null) return Promise.resolve(foundAccount);
|
|
|
|
|
|
|
|
let fullName = `https://${accountName.split('@')[1]}/@${accountName.split('@')[0]}`;
|
|
|
|
return this.mastodonService.search(account, fullName, searchVersion, true)
|
|
|
|
.then((results: Results) => {
|
|
|
|
return findAccountFunc(results);
|
|
|
|
});
|
2018-11-03 02:35:33 +01:00
|
|
|
});
|
2020-03-12 01:54:32 +01:00
|
|
|
}
|
2018-11-03 02:35:33 +01:00
|
|
|
|
2019-07-30 03:20:12 +02:00
|
|
|
getStatusUsableByAccount(account: AccountInfo, originalStatus: StatusWrapper): Promise<Status> {
|
2020-04-07 04:06:36 +02:00
|
|
|
let isProvider = false;
|
|
|
|
if(!originalStatus.isRemote){
|
|
|
|
isProvider = originalStatus.provider.id === account.id;
|
|
|
|
}
|
2019-02-12 04:28:15 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
) {
|
|
|
|
}
|
2018-10-31 05:02:03 +01:00
|
|
|
}
|
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
|
2020-04-01 08:29:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export class StatusWithCwPolicyResult {
|
|
|
|
constructor(public status: Status, public applyCw: boolean, public hide: boolean) {
|
|
|
|
}
|
2019-09-25 06:09:10 +02:00
|
|
|
}
|