refs #125 Prepare convert methods for entities

This commit is contained in:
AkiraFukushima 2020-02-20 23:15:27 +09:00
parent 4e189bc04e
commit f3af5c3f68
14 changed files with 383 additions and 20 deletions

View file

@ -205,7 +205,7 @@ export default class Mastodon implements MegalodonInterface {
}
public async updateCredentials(
discoverable?: string | null,
discoverable?: boolean | null,
bot?: boolean | null,
display_name?: string | null,
note?: string | null,
@ -220,7 +220,7 @@ export default class Mastodon implements MegalodonInterface {
fields_attributes?: Array<{ name: string; value: string }>
): Promise<Response<Entity.Account>> {
let params = {}
if (discoverable) {
if (discoverable !== null) {
params = Object.assign(params, {
discoverable: discoverable
})

View file

@ -120,7 +120,7 @@ export interface MegalodonInterface {
* @return An account.
*/
updateCredentials(
discoverable?: string | null,
discoverable?: boolean | null,
bot?: boolean | null,
display_name?: string | null,
note?: string | null,

View file

@ -169,4 +169,147 @@ export default class Misskey {
reject(err)
})
}
// ======================================
// accounts
// ======================================
public async registerAccount(
_username: string,
_email: string,
_password: string,
_agreement: boolean,
_locale: string,
_reason?: string | null
): Promise<Response<Entity.Token>> {
return new Promise((_, reject) => {
const err = new NoImplementedError('misskey does not support')
reject(err)
})
}
/**
* POST /api/i
*/
public async verifyAccountCredentials(): Promise<Response<Entity.Account>> {
return this.client.post<MisskeyAPI.Entity.UserDetail>('/api/i').then(res => {
return Object.assign(res, {
data: MisskeyAPI.Converter.userDetail(res.data)
})
})
}
/**
* POST /api/i/update
*/
public async pudateCredentials(
_discoverable?: boolean | null,
bot?: boolean | null,
display_name?: string | null,
note?: string | null,
_avatar?: string | null,
_header?: string | null,
locked?: boolean | null,
source?: {
privacy?: string
sensitive?: boolean
language?: string
} | null,
_fields_attributes?: Array<{ name: string; value: string }>
): Promise<Response<Entity.Account>> {
let params = {}
if (bot !== null) {
params = Object.assign(params, {
isBot: bot
})
}
if (display_name) {
params = Object.assign(params, {
name: display_name
})
}
if (note) {
params = Object.assign(params, {
description: note
})
}
if (locked !== null) {
params = Object.assign(params, {
isLocked: locked
})
}
if (source) {
if (source.language) {
params = Object.assign(params, {
lang: source.language
})
}
if (source.sensitive) {
params = Object.assign(params, {
alwaysMarkNsfw: source.sensitive
})
}
}
return this.client.post<MisskeyAPI.Entity.UserDetail>('/api/i', params).then(res => {
return Object.assign(res, {
data: MisskeyAPI.Converter.userDetail(res.data)
})
})
}
/**
* POST /api/users/show
*/
public async getAccount(id: string): Promise<Response<Entity.Account>> {
return this.client
.post<MisskeyAPI.Entity.UserDetail>('/api/users/show', {
userId: id
})
.then(res => {
return Object.assign(res, {
data: MisskeyAPI.Converter.userDetail(res.data)
})
})
}
/**
* POST /api/users/notes
*/
public async getAccountStatuses(id: string): Promise<Response<Array<Entity.Status>>> {
return this.client
.post<Array<MisskeyAPI.Entity.Note>>('/api/users/notes', {
userId: id
})
.then(res => {
const statuses: Array<Entity.Status> = res.data.map(note => MisskeyAPI.Converter.note(note))
return Object.assign(res, {
data: statuses
})
})
}
public async getAccountFavourites(
_id: string,
_limit?: number | null,
_max_id?: string | null,
_since_id?: string | null
): Promise<Response<Array<Entity.Status>>> {
return new Promise((_, reject) => {
const err = new NoImplementedError('misskey does not support')
reject(err)
})
}
public async subscribeAccount(_id: string): Promise<Response<Entity.Relationship>> {
return new Promise((_, reject) => {
const err = new NoImplementedError('misskey does not support')
reject(err)
})
}
public async unsubscribeAccount(_id: string): Promise<Response<Entity.Relationship>> {
return new Promise((_, reject) => {
const err = new NoImplementedError('misskey does not support')
reject(err)
})
}
}

View file

@ -3,15 +3,144 @@ import { DEFAULT_UA } from '@/default'
import proxyAgent, { ProxyConfig } from '@/proxy_config'
import Response from '@/response'
import MisskeyEntity from './entity'
import MegalodonEntity from '@/entity'
namespace MisskeyAPI {
export namespace Entity {
export type App = MisskeyEntity.App
export type Emoji = MisskeyEntity.Emoji
export type File = MisskeyEntity.File
export type Note = MisskeyEntity.Note
export type User = MisskeyEntity.User
export type UserDetail = MisskeyEntity.UserDetail
export type UserKey = MisskeyEntity.UserKey
export type Session = MisskeyEntity.Session
}
export namespace Converter {
export const emoji = (e: Entity.Emoji): MegalodonEntity.Emoji => {
return {
shortcode: e.name,
static_url: e.url,
url: e.url,
visible_in_picker: true
}
}
export const user = (u: Entity.User): MegalodonEntity.Account => {
let acct = u.username
if (u.host) {
acct = `${u.username}@${u.host}`
}
return {
id: u.id,
username: u.username,
acct: acct,
display_name: u.name,
locked: false,
created_at: '',
followers_count: 0,
following_count: 0,
statuses_count: 0,
note: '',
url: '',
avatar: u.avatarUrl,
avatar_static: u.avatarColor,
header: '',
header_static: '',
emojis: u.emojis.map(e => emoji(e)),
moved: null,
fields: null,
bot: null
}
}
export const userDetail = (u: Entity.UserDetail): MegalodonEntity.Account => {
let acct = u.username
if (u.host) {
acct = `${u.username}@${u.host}`
}
return {
id: u.id,
username: u.username,
acct: acct,
display_name: u.name,
locked: u.isLocked,
created_at: u.createdAt,
followers_count: u.followersCount,
following_count: u.followingCount,
statuses_count: u.notesCount,
note: u.description,
url: acct,
avatar: u.avatarUrl,
avatar_static: u.avatarColor,
header: u.bannerUrl,
header_static: u.bannerColor,
emojis: u.emojis.map(e => emoji(e)),
moved: null,
fields: null,
bot: u.isBot
}
}
export const visibility = (v: 'public' | 'home' | 'followers' | 'direct'): 'public' | 'unlisted' | 'private' | 'direct' => {
switch (v) {
case 'public':
case 'direct':
return v
case 'home':
return 'unlisted'
case 'followers':
return 'private'
}
}
export const file = (f: Entity.File): MegalodonEntity.Attachment => {
return {
id: f.id,
type: 'image',
url: f.url,
remote_url: f.url,
preview_url: f.thumbnailUrl,
text_url: f.url,
meta: null,
description: null
}
}
export const note = (n: Entity.Note): MegalodonEntity.Status => {
return {
id: n.id,
uri: '',
url: '',
account: user(n.user),
in_reply_to_id: n.replyId,
in_reply_to_account_id: null,
reblog: n.renote ? note(n.renote) : null,
content: n.text,
created_at: n.createdAt,
emojis: n.emojis.map(e => emoji(e)),
replies_count: n.repliesCount,
reblogs_count: n.renoteCount,
favourites_count: 0,
reblogged: false,
favourited: false,
muted: false,
sensitive: n.files ? n.files.some(f => f.isSensitive) : false,
spoiler_text: '',
visibility: visibility(n.visibility),
media_attachments: n.files ? n.files.map(f => file(f)) : [],
mentions: [],
tags: [],
card: null,
poll: null,
application: null,
language: null,
pinned: null
}
}
}
export const DEFAULT_SCOPE = [
'read:account',
'write:account',

View file

@ -1,4 +1,4 @@
namespace Entity {
namespace MisskeyEntity {
export type App = {
id: string
name: string

View file

@ -0,0 +1,8 @@
namespace MisskeyEntity {
export type Emoji = {
name: string
host: string | null
url: string
aliases: Array<string>
}
}

View file

@ -0,0 +1,18 @@
namespace MisskeyEntity {
export type File = {
id: string
createdAt: string
name: string
type: string
md5: string
size: number
isSensitive: boolean
properties: {
width: number
height: number
avgColor: string
}
url: string
thumbnailUrl: string
}
}

View file

@ -0,0 +1,11 @@
/// <reference path="userDetail.ts" />
namespace MisskeyEntity {
export type Follower = {
id: string
createdAt: string
followeeId: string
followerId: string
follower: UserDetail
}
}

View file

@ -0,0 +1,26 @@
/// <reference path="user.ts" />
/// <reference path="emoji.ts" />
/// <reference path="file.ts" />
namespace MisskeyEntity {
export type Note = {
id: string
createdAt: string
text: string
cw: string | null
userId: string
user: User
replyId: string | null
renoteId: string | null
renote?: Note
repliesCount: number
renoteCount: number
viaMobile: boolean
visibility: 'public' | 'home' | 'followers' | 'direct'
reactions: { [key: string]: number }
tags: Array<string>
emojis: Array<Emoji>
fileIds: Array<string>
files: Array<File>
}
}

View file

@ -1,4 +1,4 @@
namespace Entity {
namespace MisskeyEntity {
export type Session = {
token: string
url: string

View file

@ -1,18 +1,13 @@
namespace Entity {
/// <reference path="emoji.ts" />
namespace MisskeyEntity {
export type User = {
id: string
username: string
name: string
host: string
description: string
createdAt: string
followersCount: number
followingCount: number
notesCount: number
isBot: boolean
isCat: boolean
isAdmim: boolean
isVerified: boolean
isLocked: boolean
username: string
host: string | null
avatarUrl: string
avatarColor: string
emojis: Array<Emoji>
}
}

View file

@ -0,0 +1,29 @@
/// <reference path="emoji.ts" />
namespace MisskeyEntity {
export type UserDetail = {
id: string
name: string
username: string
host: string | null
avatarUrl: string
avatarColor: string
isAdmin: boolean
isModerator: boolean
isBot: boolean
isCat: boolean
emojis: Array<Emoji>
createdAt: string
bannerUrl: string
bannerColor: string
isLocked: boolean
isSilenced: boolean
isSuspended: boolean
description: string
followersCount: number
followingCount: number
notesCount: number
avatarId: string
bannerId: string
}
}

View file

@ -1,6 +1,6 @@
/// <reference path="user.ts" />
namespace Entity {
namespace MisskeyEntity {
export type UserKey = {
accessToken: string
user: User

View file

@ -1,6 +1,10 @@
/// <reference path="entities/app.ts" />
/// <reference path="entities/emoji.ts" />
/// <reference path="entities/file.ts" />
/// <reference path="entities/note.ts" />
/// <reference path="entities/user.ts" />
/// <reference path="entities/userDetail.ts" />
/// <reference path="entities/userkey.ts" />
/// <reference path="entities/session.ts" />
export default Entity
export default MisskeyEntity