diff --git a/src/app/services/instances-info.service.ts b/src/app/services/instances-info.service.ts index d7cf2aac..3d64fc5e 100644 --- a/src/app/services/instances-info.service.ts +++ b/src/app/services/instances-info.service.ts @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core'; import { VisibilityEnum } from './mastodon.service'; import { MastodonWrapperService } from './mastodon-wrapper.service'; -import { Instance, Account } from './models/mastodon.interfaces'; +import { Instance, Instancev1, Instancev2, Account } from './models/mastodon.interfaces'; import { AccountInfo } from '../states/accounts.state'; @Injectable({ @@ -19,11 +19,20 @@ export class InstancesInfoService { if (!this.cachedMaxInstanceChar[instance]) { this.cachedMaxInstanceChar[instance] = this.mastodonService.getInstance(instance) .then((instance: Instance) => { - if (instance.max_toot_chars) { - return instance.max_toot_chars; + if (+instance.version.split('.')[0] >= 4) { + const instanceV2 = instance; + if (instanceV2 + && instanceV2.configuration + && instanceV2.configuration.statuses + && instanceV2.configuration.statuses.max_characters) + return instanceV2.configuration.statuses.max_characters; } else { - return this.defaultMaxChars; + const instanceV1 = instance; + if (instanceV1 && instanceV1.max_toot_chars) + return instanceV1.max_toot_chars; } + + return this.defaultMaxChars; }) .catch(() => { return this.defaultMaxChars; diff --git a/src/app/services/mastodon.service.ts b/src/app/services/mastodon.service.ts index 14adb701..6215c058 100644 --- a/src/app/services/mastodon.service.ts +++ b/src/app/services/mastodon.service.ts @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core'; import { HttpHeaders, HttpClient, HttpResponse } from '@angular/common/http'; import { ApiRoutes } from './models/api.settings'; -import { Account, Status, Results, Context, Relationship, Instance, Attachment, Notification, List, Poll, Emoji, Conversation, ScheduledStatus, Tag } from "./models/mastodon.interfaces"; +import { Account, Status, Results, Context, Relationship, Instance, Attachment, Notification, List, Poll, Emoji, Conversation, ScheduledStatus, Tag, Instancev2, Instancev1 } from "./models/mastodon.interfaces"; import { AccountInfo } from '../states/accounts.state'; import { StreamTypeEnum, StreamElement } from '../states/streams.state'; @@ -14,10 +14,10 @@ export class MastodonService { getInstance(instance: string): Promise { let route = `https://${instance}${this.apiRoutes.getInstancev2}`; - return this.httpClient.get(route).toPromise() + return this.httpClient.get(route).toPromise() .catch(err => { route = `https://${instance}${this.apiRoutes.getInstance}`; - return this.httpClient.get(route).toPromise(); + return this.httpClient.get(route).toPromise(); }); } diff --git a/src/app/services/models/mastodon.interfaces.ts b/src/app/services/models/mastodon.interfaces.ts index 087b1494..a8539ebd 100644 --- a/src/app/services/models/mastodon.interfaces.ts +++ b/src/app/services/models/mastodon.interfaces.ts @@ -110,21 +110,43 @@ export interface Error { error: string; } + + export interface Instance { - uri: string; title: string; - description: string; - email: string; version: string; + description: string; +} + +export interface Instancev1 extends Instance { + uri: string; + email: string; urls: InstanceUrls; contact_account: Account; max_toot_chars: number; } +export interface Instancev2 extends Instance { + configuration: Instancev2Configuration +} + +export interface Instancev2Configuration { + urls: Instancev2Urls; + statuses: Instancev2Statuses +} + export interface InstanceUrls { streaming_api: string; } +export interface Instancev2Urls { + streaming: string; +} + +export interface Instancev2Statuses { + max_characters: number; +} + export interface Mention { url: string; username: string; diff --git a/src/app/services/tools.service.ts b/src/app/services/tools.service.ts index b48deabf..ea1da0d6 100644 --- a/src/app/services/tools.service.ts +++ b/src/app/services/tools.service.ts @@ -3,7 +3,7 @@ import { Store } from '@ngxs/store'; import { AccountInfo } from '../states/accounts.state'; import { MastodonWrapperService } from './mastodon-wrapper.service'; -import { Account, Results, Status, Emoji } from "./models/mastodon.interfaces"; +import { Account, Results, Status, Emoji, Instancev2, Instancev1 } from "./models/mastodon.interfaces"; import { StatusWrapper } from '../models/common.model'; import { AccountSettings, SaveAccountSettings, GlobalSettings, SaveSettings, ContentWarningPolicy, SaveContentWarningPolicy, ContentWarningPolicyEnum, TimeLineModeEnum, TimeLineHeaderEnum } from '../states/settings.state'; import { SettingsService } from './settings.service'; @@ -94,7 +94,19 @@ export class ToolsService { const minor = +splittedVersion[1]; let streamingApi = ""; - if(instance.urls) streamingApi = instance.urls.streaming_api; + + if (major >= 4) { + const instanceV2 = instance; + + if (instanceV2 + && instanceV2.configuration + && instanceV2.configuration.urls) + streamingApi = instanceV2.configuration.urls.streaming; + } else { + const instanceV1 = instance; + if (instanceV1 && instanceV1.urls) + streamingApi = instanceV1.urls.streaming_api; + } let instanceInfo = new InstanceInfo(type, major, minor, streamingApi); this.instanceInfos[acc.instance] = instanceInfo;