Merge pull request #210 from h3poteto/add/converter

Add converter to convert entity between each SNS and megalodon
This commit is contained in:
AkiraFukushima 2020-02-21 21:29:14 +09:00 committed by GitHub
commit 8629b913a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
38 changed files with 1091 additions and 214 deletions

File diff suppressed because it is too large Load diff

View file

@ -5,6 +5,8 @@ import Response from '../response'
import { RequestCanceledError } from '../cancel'
import proxyAgent, { ProxyConfig } from '../proxy_config'
import { NO_REDIRECT, DEFAULT_SCOPE, DEFAULT_UA } from '../default'
import MastodonEntity from './entity'
import MegalodonEntity from '../entity'
namespace MastodonAPI {
/**
@ -341,5 +343,77 @@ namespace MastodonAPI {
return streaming
}
}
export namespace Entity {
export type Account = MastodonEntity.Account
export type Activity = MastodonEntity.Activity
export type Application = MastodonEntity.Application
export type Attachment = MastodonEntity.Attachment
export type Card = MastodonEntity.Card
export type Context = MastodonEntity.Context
export type Conversation = MastodonEntity.Conversation
export type Emoji = MastodonEntity.Emoji
export type FeaturedTag = MastodonEntity.FeaturedTag
export type Field = MastodonEntity.Field
export type Filter = MastodonEntity.Filter
export type History = MastodonEntity.History
export type IdentityProof = MastodonEntity.IdentityProof
export type Instance = MastodonEntity.Instance
export type List = MastodonEntity.List
export type Marker = MastodonEntity.Marker
export type Mention = MastodonEntity.Mention
export type Notification = MastodonEntity.Notification
export type Poll = MastodonEntity.Poll
export type PollOption = MastodonEntity.PollOption
export type Preferences = MastodonEntity.Preferences
export type PushSubscription = MastodonEntity.PushSubscription
export type Relationship = MastodonEntity.Relationship
export type Report = MastodonEntity.Report
export type Results = MastodonEntity.Results
export type ScheduledStatus = MastodonEntity.ScheduledStatus
export type Source = MastodonEntity.Source
export type Stats = MastodonEntity.Stats
export type Status = MastodonEntity.Status
export type StatusParams = MastodonEntity.StatusParams
export type Tag = MastodonEntity.Tag
export type Token = MastodonEntity.Token
export type URLs = MastodonEntity.URLs
}
export namespace Converter {
export const account = (a: Entity.Account): MegalodonEntity.Account => a
export const activity = (a: Entity.Activity): MegalodonEntity.Activity => a
export const application = (a: Entity.Application): MegalodonEntity.Application => a
export const attachment = (a: Entity.Attachment): MegalodonEntity.Attachment => a
export const card = (c: Entity.Card): MegalodonEntity.Card => c
export const context = (c: Entity.Context): MegalodonEntity.Context => c
export const conversation = (c: Entity.Conversation): MegalodonEntity.Conversation => c
export const emoji = (e: Entity.Emoji): MegalodonEntity.Emoji => e
export const featured_tag = (e: Entity.FeaturedTag): MegalodonEntity.FeaturedTag => e
export const field = (f: Entity.Field): MegalodonEntity.Field => f
export const filter = (f: Entity.Filter): MegalodonEntity.Filter => f
export const history = (h: Entity.History): MegalodonEntity.History => h
export const identity_proof = (i: Entity.IdentityProof): MegalodonEntity.IdentityProof => i
export const instance = (i: Entity.Instance): MegalodonEntity.Instance => i
export const list = (l: Entity.List): MegalodonEntity.List => l
export const marker = (m: Entity.Marker): MegalodonEntity.Marker => m
export const mention = (m: Entity.Mention): MegalodonEntity.Mention => m
export const notification = (n: Entity.Notification): MegalodonEntity.Notification => n
export const poll = (p: Entity.Poll): MegalodonEntity.Poll => p
export const poll_option = (p: Entity.PollOption): MegalodonEntity.PollOption => p
export const preferences = (p: Entity.Preferences): MegalodonEntity.Preferences => p
export const push_subscription = (p: Entity.PushSubscription): MegalodonEntity.PushSubscription => p
export const relationship = (r: Entity.Relationship): MegalodonEntity.Relationship => r
export const report = (r: Entity.Report): MegalodonEntity.Report => r
export const results = (r: Entity.Results): MegalodonEntity.Results => r
export const scheduled_status = (s: Entity.ScheduledStatus): MegalodonEntity.ScheduledStatus => s
export const source = (s: Entity.Source): MegalodonEntity.Source => s
export const stats = (s: Entity.Stats): MegalodonEntity.Stats => s
export const status = (s: Entity.Status): MegalodonEntity.Status => s
export const status_params = (s: Entity.StatusParams): MegalodonEntity.StatusParams => s
export const tag = (t: Entity.Tag): MegalodonEntity.Tag => t
export const token = (t: Entity.Token): MegalodonEntity.Token => t
export const urls = (u: Entity.URLs): MegalodonEntity.URLs => u
}
}
export default MastodonAPI

View file

@ -0,0 +1,27 @@
/// <reference path="emoji.ts" />
/// <reference path="source.ts" />
namespace MastodonEntity {
export type Account = {
id: string
username: string
acct: string
display_name: string
locked: boolean
created_at: string
followers_count: number
following_count: number
statuses_count: number
note: string
url: string
avatar: string
avatar_static: string
header: string
header_static: string
emojis: Array<Emoji>
moved: Account | null
fields: object | null
bot: boolean | null
source?: Source
}
}

View file

@ -0,0 +1,8 @@
namespace MastodonEntity {
export type Activity = {
week: string
statuses: string
logins: string
registrations: string
}
}

View file

@ -0,0 +1,7 @@
namespace MastodonEntity {
export type Application = {
name: string
website?: string | null
vapid_key?: string | null
}
}

View file

@ -0,0 +1,12 @@
namespace MastodonEntity {
export type Attachment = {
id: string
type: 'unknown' | 'image' | 'gifv' | 'video'
url: string
remote_url: string | null
preview_url: string
text_url: string | null
meta: object | null
description: string | null
}
}

View file

@ -0,0 +1,16 @@
namespace MastodonEntity {
export type Card = {
url: string
title: string
description: string
image: string | null
type: 'link' | 'photo' | 'video' | 'rich'
author_name: string | null
author_url: string | null
provider_name: string | null
provider_url: string | null
html: string | null
width: number | null
height: number | null
}
}

View file

@ -0,0 +1,8 @@
/// <reference path="status.ts" />
namespace MastodonEntity {
export type Context = {
ancestors: Array<Status>
descendants: Array<Status>
}
}

View file

@ -0,0 +1,11 @@
/// <reference path="account.ts" />
/// <reference path="status.ts" />
namespace MastodonEntity {
export type Conversation = {
id: string
accounts: Array<Account>
last_status: Status | null
unread: boolean
}
}

View file

@ -0,0 +1,8 @@
namespace MastodonEntity {
export type Emoji = {
shortcode: string
static_url: string
url: string
visible_in_picker: boolean
}
}

View file

@ -0,0 +1,8 @@
namespace MastodonEntity {
export type FeaturedTag = {
id: string
name: string
statuses_count: number
last_status_at: string
}
}

View file

@ -0,0 +1,7 @@
namespace MastodonEntity {
export type Field = {
name: string
value: string
verified_at: string | null
}
}

View file

@ -0,0 +1,10 @@
namespace MastodonEntity {
export type Filter = {
id: string
phrase: string
context: Array<string>
expires_at: string | null
irreversible: boolean
whole_word: boolean
}
}

View file

@ -0,0 +1,7 @@
namespace MastodonEntity {
export type History = {
day: string
uses: number
accounts: number
}
}

View file

@ -0,0 +1,9 @@
namespace MastodonEntity {
export type IdentityProof = {
provider: string
provider_username: string
updated_at: string
proof_url: string
profile_url: string
}
}

View file

@ -0,0 +1,20 @@
/// <reference path="account.ts" />
/// <reference path="urls.ts" />
/// <reference path="stats.ts" />
namespace MastodonEntity {
export type Instance = {
uri: string
title: string
description: string
email: string
version: string
thumbnail: string | null
urls: URLs
stats: Stats
languages: Array<string>
contact_account: Account | null
max_toot_chars?: number
registrations?: boolean
}
}

View file

@ -0,0 +1,6 @@
namespace MastodonEntity {
export type List = {
id: string
title: string
}
}

View file

@ -0,0 +1,14 @@
namespace MastodonEntity {
export type Marker = {
home: {
last_read_id: string
version: number
updated_at: string
}
notifications: {
last_read_id: string
version: number
updated_at: string
}
}
}

View file

@ -0,0 +1,8 @@
namespace MastodonEntity {
export type Mention = {
id: string
username: string
url: string
acct: string
}
}

View file

@ -0,0 +1,12 @@
/// <reference path="account.ts" />
/// <reference path="status.ts" />
namespace MastodonEntity {
export type Notification = {
account: Account
created_at: string
id: string
status: Status | null
type: 'mention' | 'reblog' | 'favourite' | 'follow'
}
}

View file

@ -0,0 +1,13 @@
/// <reference path="poll_option.ts" />
namespace MastodonEntity {
export type Poll = {
id: string
expires_at: string | null
expired: boolean
multiple: boolean
votes_count: number
options: Array<PollOption>
voted: boolean
}
}

View file

@ -0,0 +1,6 @@
namespace MastodonEntity {
export type PollOption = {
title: string
votes_count: number | null
}
}

View file

@ -0,0 +1,9 @@
namespace MastodonEntity {
export type Preferences = {
'posting:default:visibility': 'public' | 'unlisted' | 'private' | 'direct'
'posting:default:sensitive': boolean
'posting:default:language': string | null
'reading:expand:media': 'default' | 'show_all' | 'hide_all'
'reading:expand:spoilers': boolean
}
}

View file

@ -0,0 +1,8 @@
namespace MastodonEntity {
export type PushSubscription = {
id: string
endpoint: string
server_key: string
alerts: object
}
}

View file

@ -0,0 +1,14 @@
namespace MastodonEntity {
export type Relationship = {
id: string
following: boolean
followed_by: boolean
blocking: boolean
muting: boolean
muting_notifications: boolean
requested: boolean
domain_blocking: boolean
showing_reblogs: boolean
endorsed: boolean
}
}

View file

@ -0,0 +1,9 @@
namespace MastodonEntity {
export type Report = {
id: string
action_taken: string
comment: string
account_id: string
status_ids: Array<string>
}
}

View file

@ -0,0 +1,11 @@
/// <reference path="account.ts" />
/// <reference path="status.ts" />
/// <reference path="tag.ts" />
namespace MastodonEntity {
export type Results = {
accounts: Array<Account>
statuses: Array<Status>
hashtags: Array<Tag>
}
}

View file

@ -0,0 +1,10 @@
// <reference path="attachment.ts" />
namespace MastodonEntity {
export type ScheduledStatus = {
id: string
scheduled_at: string
params: object
media_attachments: Array<Attachment>
}
}

View file

@ -0,0 +1,9 @@
namespace MastodonEntity {
export type Source = {
privacy: string | null
sensitive: boolean | null
language: string | null
note: string
fields: object
}
}

View file

@ -0,0 +1,7 @@
namespace MastodonEntity {
export type Stats = {
user_count: number
status_count: number
domain_count: number
}
}

View file

@ -0,0 +1,40 @@
/// <reference path="account.ts" />
/// <reference path="application.ts" />
/// <reference path="mention.ts" />
/// <reference path="tag.ts" />
/// <reference path="attachment.ts" />
/// <reference path="emoji.ts" />
/// <reference path="card.ts" />
/// <reference path="poll.ts" />
namespace MastodonEntity {
export type Status = {
id: string
uri: string
url: string
account: Account
in_reply_to_id: string | null
in_reply_to_account_id: string | null
reblog: Status | null
content: string
created_at: string
emojis: Emoji[]
replies_count: number
reblogs_count: number
favourites_count: number
reblogged: boolean | null
favourited: boolean | null
muted: boolean | null
sensitive: boolean
spoiler_text: string
visibility: 'public' | 'unlisted' | 'private' | 'direct'
media_attachments: Array<Attachment>
mentions: Array<Mention>
tags: Array<Tag>
card: Card | null
poll: Poll | null
application: Application | null
language: string | null
pinned: boolean | null
}
}

View file

@ -0,0 +1,12 @@
namespace MastodonEntity {
export type StatusParams = {
text: string
in_reply_to_id: string | null
media_ids: Array<string> | null
sensitive: boolean | null
spoiler_text: string | null
visibility: 'public' | 'unlisted' | 'private' | 'direct'
scheduled_at: string | null
application_id: string
}
}

View file

@ -0,0 +1,9 @@
/// <reference path="history.ts" />
namespace MastodonEntity {
export type Tag = {
name: string
url: string
history: Array<History> | null
}
}

View file

@ -0,0 +1,8 @@
namespace MastodonEntity {
export type Token = {
access_token: string
token_type: string
scope: string
created_at: number
}
}

View file

@ -0,0 +1,5 @@
namespace MastodonEntity {
export type URLs = {
streaming_api: string
}
}

35
src/mastodon/entity.ts Normal file
View file

@ -0,0 +1,35 @@
/// <reference path="./entities/account.ts" />
/// <reference path="./entities/activity.ts" />
/// <reference path="./entities/application.ts" />
/// <reference path="./entities/attachment.ts" />
/// <reference path="./entities/card.ts" />
/// <reference path="./entities/context.ts" />
/// <reference path="./entities/conversation.ts" />
/// <reference path="./entities/emoji.ts" />
/// <reference path="./entities/featured_tag.ts" />
/// <reference path="./entities/field.ts" />
/// <reference path="./entities/filter.ts" />
/// <reference path="./entities/history.ts" />
/// <reference path="./entities/identity_proof.ts" />
/// <reference path="./entities/instance.ts" />
/// <reference path="./entities/list.ts" />
/// <reference path="./entities/marker.ts" />
/// <reference path="./entities/mention.ts" />
/// <reference path="./entities/notification.ts" />
/// <reference path="./entities/poll.ts" />
/// <reference path="./entities/poll_option.ts" />
/// <reference path="./entities/preferences.ts" />
/// <reference path="./entities/push_subscription.ts" />
/// <reference path="./entities/relationship.ts" />
/// <reference path="./entities/report.ts" />
/// <reference path="./entities/results.ts" />
/// <reference path="./entities/scheduled_status.ts" />
/// <reference path="./entities/source.ts" />
/// <reference path="./entities/stats.ts" />
/// <reference path="./entities/status.ts" />
/// <reference path="./entities/status_params.ts" />
/// <reference path="./entities/tag.ts" />
/// <reference path="./entities/token.ts" />
/// <reference path="./entities/urls.ts" />
export default MastodonEntity

View file

@ -3,12 +3,13 @@ import Mastodon from './mastodon'
import StreamListener from './stream_listener'
import Response from './response'
import Entity from './entity'
import PleromaAPI from './pleroma/api_client'
export default class Pleroma extends Mastodon implements MegalodonInterface {
// ======================================
// accounts
// ======================================
public getAccountFavourites(
public async getAccountFavourites(
id: string,
limit?: number | null,
max_id?: string | null,
@ -30,15 +31,27 @@ export default class Pleroma extends Mastodon implements MegalodonInterface {
since_id: since_id
})
}
return this.client.get<Array<Entity.Status>>(`/api/v1/pleroma/accounts/${id}/favourites`, params)
return this.client.get<Array<PleromaAPI.Entity.Status>>(`/api/v1/pleroma/accounts/${id}/favourites`, params).then(res => {
return Object.assign(res, {
data: res.data.map(s => PleromaAPI.Converter.status(s))
})
})
}
public subscribeAccount(id: string): Promise<Response<Entity.Relationship>> {
return this.client.post<Entity.Relationship>(`/api/v1/pleroma/accounts/${id}/subscribe`)
public async subscribeAccount(id: string): Promise<Response<Entity.Relationship>> {
return this.client.post<PleromaAPI.Entity.Relationship>(`/api/v1/pleroma/accounts/${id}/subscribe`).then(res => {
return Object.assign(res, {
data: PleromaAPI.Converter.relationship(res.data)
})
})
}
public unsubscribeAccount(id: string): Promise<Response<Entity.Relationship>> {
return this.client.post<Entity.Relationship>(`/api/v1/pleroma/accounts/${id}/unsubscribe`)
public async unsubscribeAccount(id: string): Promise<Response<Entity.Relationship>> {
return this.client.post<PleromaAPI.Entity.Relationship>(`/api/v1/pleroma/accounts/${id}/unsubscribe`).then(res => {
return Object.assign(res, {
data: PleromaAPI.Converter.relationship(res.data)
})
})
}
// ======================================

16
src/pleroma/api_client.ts Normal file
View file

@ -0,0 +1,16 @@
import MastodonAPI from '../mastodon/api_client'
import MegalodonEntity from '../entity'
namespace PleromaAPI {
export namespace Entity {
export type Status = MastodonAPI.Entity.Status
export type Relationship = MastodonAPI.Entity.Relationship
}
export namespace Converter {
export const status = (s: Entity.Status): MegalodonEntity.Status => s
export const relationship = (r: Entity.Relationship): MegalodonEntity.Relationship => r
}
}
export default PleromaAPI