Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ implement getPreferences and getDefaultPostPrivacy for Firefish #1959

Merged
1 change: 1 addition & 0 deletions megalodon/src/entities/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ export type Account = {
source?: Source
role?: Role
mute_expires_at?: string
always_mark_nsfw?: boolean
}
20 changes: 17 additions & 3 deletions megalodon/src/firefish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -953,12 +953,26 @@ export default class Firefish implements MegalodonInterface {
// accounts/preferences
// ======================================
public async getPreferences(): Promise<Response<Entity.Preferences>> {
return new Promise((_, reject) => {
const err = new NoImplementedError('firefish does not support')
reject(err)
return this.client.post<FirefishAPI.Entity.UserDetailMe>('/api/i').then(async res => {
return Object.assign(res, {
data: FirefishAPI.Converter.userPreferences(res.data, await this.getDefaultPostPrivacy())
})
})
}

public async getDefaultPostPrivacy(): Promise<'public' | 'unlisted' | 'private' | 'direct'> {
ThatOneCalculator marked this conversation as resolved.
Show resolved Hide resolved
return this.client
.post<string>('/api/i/registry/get-unsecure', {
key: 'defaultNoteVisibility',
scope: ['client', 'base']
})
.then(res => {
if (!res.data || (res.data != 'public' && res.data != 'home' && res.data != 'followers' && res.data != 'specified')) return 'public'
return FirefishAPI.Converter.visibility(res.data)
})
.catch(_ => 'public')
}

// ======================================
// accounts/followed_tags
// ======================================
Expand Down
20 changes: 20 additions & 0 deletions megalodon/src/firefish/api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace FirefishAPI {
export type Relation = FirefishEntity.Relation
export type User = FirefishEntity.User
export type UserDetail = FirefishEntity.UserDetail
export type UserDetailMe = FirefishEntity.UserDetailMe
export type Session = FirefishEntity.Session
export type Stats = FirefishEntity.Stats
export type Instance = FirefishEntity.Instance
Expand Down Expand Up @@ -133,6 +134,25 @@ namespace FirefishAPI {
}
}

export const userDetailMe = (u: Entity.UserDetailMe): MegalodonEntity.Account => {
const account = userDetail(u)
account.always_mark_nsfw = u.alwaysMarkNsfw
return account
}

export const userPreferences = (
u: FirefishAPI.Entity.UserDetailMe,
v: 'public' | 'unlisted' | 'private' | 'direct'
): MegalodonEntity.Preferences => {
return {
'reading:expand:media': 'default',
'reading:expand:spoilers': false,
'posting:default:language': u.lang,
'posting:default:sensitive': u.alwaysMarkNsfw,
'posting:default:visibility': v
}
}

export const visibility = (
v: 'public' | 'home' | 'followers' | 'specified' | 'hidden'
): 'public' | 'unlisted' | 'private' | 'direct' => {
Expand Down
4 changes: 4 additions & 0 deletions megalodon/src/firefish/entities/userDetail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ export type UserDetail = {
isMuted?: boolean
isRenoteMuted?: boolean
}

export type UserDetailMe = UserDetail & {
alwaysMarkNsfw: boolean
}
1 change: 1 addition & 0 deletions megalodon/src/firefish/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export namespace Entity {
export type Stats = stats.Stats
export type User = user.User
export type UserDetail = user_detail.UserDetail
export type UserDetailMe = user_detail.UserDetailMe
}

export default Entity
Loading