Skip to content

Commit

Permalink
Merge pull request h3poteto#1688 from h3poteto/fix/account
Browse files Browse the repository at this point in the history
Fix account entity
  • Loading branch information
h3poteto authored Apr 24, 2023
2 parents aa340b2 + fe7c66e commit c04f14c
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 29 deletions.
2 changes: 2 additions & 0 deletions megalodon/src/entities/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Entity {
acct: string
display_name: string
locked: boolean
discoverable?: boolean
group: boolean | null
created_at: string
followers_count: number
following_count: number
Expand Down
16 changes: 10 additions & 6 deletions megalodon/src/friendica.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,19 +478,23 @@ export default class Friendica implements MegalodonInterface {
/** Helper function to optionally follow Link headers as pagination */
private async urlToAccounts(url: string, params: Record<string, string>, get_all: boolean, sleep_ms: number) {
const res = await this.client.get<Array<FriendicaAPI.Entity.Account>>(url, params)
res.data = res.data.map(a => FriendicaAPI.Converter.account(a))
if (get_all && res.headers.link) {
let parsed = parseLinkHeader(res.headers.link)
let converted = Object.assign({}, res, {
data: res.data.map(a => FriendicaAPI.Converter.account(a))
})
if (get_all && converted.headers.link) {
let parsed = parseLinkHeader(converted.headers.link)
while (parsed?.next) {
const nextRes = await this.client.get<Array<FriendicaEntity.Account>>(parsed?.next.url, undefined, undefined, true)
res.data.push(...nextRes.data.map(a => FriendicaAPI.Converter.account(a)))
converted = Object.assign({}, converted, {
data: [...converted.data, ...nextRes.data.map(a => FriendicaAPI.Converter.account(a))]
})
parsed = parseLinkHeader(nextRes.headers.link)
if (sleep_ms) {
await new Promise<void>(res => setTimeout(res, sleep_ms))
await new Promise<void>(converted => setTimeout(converted, sleep_ms))
}
}
}
return res
return converted
}

/**
Expand Down
6 changes: 4 additions & 2 deletions megalodon/src/friendica/entities/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace FriendicaEntity {
acct: string
display_name: string
locked: boolean
discoverable?: boolean
group: boolean | null
created_at: string
followers_count: number
following_count: number
Expand All @@ -21,7 +23,7 @@ namespace FriendicaEntity {
emojis: Array<Emoji>
moved: Account | null
fields: Array<Field>
bot: boolean | null
source?: Source
bot: boolean
source: Source
}
}
14 changes: 9 additions & 5 deletions megalodon/src/mastodon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,15 +554,19 @@ export default class Mastodon implements MegalodonInterface {
/** Helper function to optionally follow Link headers as pagination */
private async urlToAccounts(url: string, params: Record<string, string>, get_all: boolean, sleep_ms: number) {
const res = await this.client.get<Array<MastodonAPI.Entity.Account>>(url, params)
res.data = res.data.map(a => MastodonAPI.Converter.account(a))
if (get_all && res.headers.link) {
let parsed = parseLinkHeader(res.headers.link)
let converted = Object.assign({}, res, {
data: res.data.map(a => MastodonAPI.Converter.account(a))
})
if (get_all && converted.headers.link) {
let parsed = parseLinkHeader(converted.headers.link)
while (parsed?.next) {
const nextRes = await this.client.get<Array<MastodonEntity.Account>>(parsed?.next.url, undefined, undefined, true)
res.data.push(...nextRes.data.map(a => MastodonAPI.Converter.account(a)))
converted = Object.assign({}, converted, {
data: [...converted.data, ...nextRes.data.map(a => MastodonAPI.Converter.account(a))]
})
parsed = parseLinkHeader(nextRes.headers.link)
if (sleep_ms) {
await new Promise<void>(res => setTimeout(res, sleep_ms))
await new Promise<void>(converted => setTimeout(converted, sleep_ms))
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions megalodon/src/mastodon/entities/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace MastodonEntity {
acct: string
display_name: string
locked: boolean
discoverable?: boolean
group: boolean | null
created_at: string
followers_count: number
following_count: number
Expand All @@ -21,7 +23,7 @@ namespace MastodonEntity {
emojis: Array<Emoji>
moved: Account | null
fields: Array<Field>
bot: boolean | null
source?: Source
bot: boolean
source: Source
}
}
2 changes: 2 additions & 0 deletions megalodon/src/misskey/api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ namespace MisskeyAPI {
acct: acct,
display_name: u.name,
locked: false,
group: null,
created_at: '',
followers_count: 0,
following_count: 0,
Expand Down Expand Up @@ -88,6 +89,7 @@ namespace MisskeyAPI {
acct: acct,
display_name: u.name,
locked: u.isLocked,
group: null,
created_at: u.createdAt,
followers_count: u.followersCount,
following_count: u.followingCount,
Expand Down
69 changes: 61 additions & 8 deletions megalodon/src/pleroma/api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,32 @@ namespace PleromaAPI {
}
}

export const account = (a: Entity.Account): MegalodonEntity.Account => a
export const account = (a: Entity.Account): MegalodonEntity.Account => {
return {
id: a.id,
username: a.username,
acct: a.acct,
display_name: a.display_name,
locked: a.locked,
discoverable: a.discoverable,
group: null,
created_at: a.created_at,
followers_count: a.followers_count,
following_count: a.following_count,
statuses_count: a.statuses_count,
note: a.note,
url: a.url,
avatar: a.avatar,
avatar_static: a.avatar_static,
header: a.header,
header_static: a.header_static,
emojis: a.emojis,
moved: a.moved ? account(a.moved) : null,
fields: a.fields,
bot: a.bot,
source: a.source
}
}
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
Expand Down Expand Up @@ -180,7 +205,7 @@ namespace PleromaAPI {
if (n.status && n.emoji) {
return {
id: n.id,
account: n.account,
account: account(n.account),
created_at: n.created_at,
status: status(n.status),
emoji: n.emoji,
Expand All @@ -189,23 +214,23 @@ namespace PleromaAPI {
} else if (n.status) {
return {
id: n.id,
account: n.account,
account: account(n.account),
created_at: n.created_at,
status: status(n.status),
type: decodeNotificationType(n.type)
}
} else if (n.target) {
return {
id: n.id,
account: n.account,
account: account(n.account),
created_at: n.created_at,
target: n.target,
target: account(n.target),
type: decodeNotificationType(n.type)
}
} else {
return {
id: n.id,
account: n.account,
account: account(n.account),
created_at: n.created_at,
type: decodeNotificationType(n.type)
}
Expand All @@ -215,7 +240,19 @@ namespace PleromaAPI {
export const pollOption = (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 reaction = (r: Entity.Reaction): MegalodonEntity.Reaction => r
export const reaction = (r: Entity.Reaction): MegalodonEntity.Reaction => {
const p = {
count: r.count,
me: r.me,
name: r.name
}
if (r.accounts) {
return Object.assign({}, p, {
accounts: r.accounts.map(a => account(a))
})
}
return p
}
export const relationship = (r: Entity.Relationship): MegalodonEntity.Relationship => ({
id: r.id,
following: r.following,
Expand All @@ -230,7 +267,23 @@ namespace PleromaAPI {
endorsed: r.endorsed,
notifying: r.subscribing
})
export const report = (r: Entity.Report): MegalodonEntity.Report => r
export const report = (r: Entity.Report): MegalodonEntity.Report => {
const p = {
id: r.id,
action_taken: r.action_taken,
category: r.category,
comment: r.comment,
forwarded: r.forwarded,
status_ids: r.status_ids,
rule_ids: r.rule_ids
}
if (r.target_account) {
return Object.assign({}, p, {
target_account: account(r.target_account)
})
}
return p
}
export const results = (r: Entity.Results): MegalodonEntity.Results => ({
accounts: Array.isArray(r.accounts) ? r.accounts.map(a => account(a)) : [],
statuses: Array.isArray(r.statuses) ? r.statuses.map(s => status(s)) : [],
Expand Down
5 changes: 3 additions & 2 deletions megalodon/src/pleroma/entities/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace PleromaEntity {
acct: string
display_name: string
locked: boolean
discoverable?: boolean
created_at: string
followers_count: number
following_count: number
Expand All @@ -21,7 +22,7 @@ namespace PleromaEntity {
emojis: Array<Emoji>
moved: Account | null
fields: Array<Field>
bot: boolean | null
source?: Source
bot: boolean
source: Source
}
}
10 changes: 9 additions & 1 deletion megalodon/test/integration/mastodon.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const account: MastodonEntity.Account = {
acct: 'h3poteto@pleroma.io',
display_name: 'h3poteto',
locked: false,
group: false,
created_at: '2019-03-26T21:30:32',
followers_count: 10,
following_count: 10,
Expand All @@ -25,7 +26,14 @@ const account: MastodonEntity.Account = {
emojis: [],
moved: null,
fields: [],
bot: false
bot: false,
source: {
privacy: null,
sensitive: false,
language: null,
note: 'test',
fields: []
}
}

const status: MastodonEntity.Status = {
Expand Down
10 changes: 9 additions & 1 deletion megalodon/test/integration/mastodon/api_client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const account: Entity.Account = {
acct: 'h3poteto@pleroma.io',
display_name: 'h3poteto',
locked: false,
group: false,
created_at: '2019-03-26T21:30:32',
followers_count: 10,
following_count: 10,
Expand All @@ -24,7 +25,14 @@ const account: Entity.Account = {
emojis: [],
moved: null,
fields: [],
bot: false
bot: false,
source: {
privacy: null,
sensitive: false,
language: null,
note: 'test',
fields: []
}
}

const status: Entity.Status = {
Expand Down
9 changes: 8 additions & 1 deletion megalodon/test/integration/pleroma.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ const account: PleromaEntity.Account = {
emojis: [],
moved: null,
fields: [],
bot: false
bot: false,
source: {
privacy: null,
sensitive: false,
language: null,
note: 'test',
fields: []
}
}

const status: PleromaEntity.Status = {
Expand Down
1 change: 1 addition & 0 deletions megalodon/test/unit/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const account: Entity.Account = {
acct: 'h3poteto@pleroma.io',
display_name: 'h3poteto',
locked: false,
group: false,
created_at: '2019-03-26T21:30:32',
followers_count: 10,
following_count: 10,
Expand Down
9 changes: 8 additions & 1 deletion megalodon/test/unit/pleroma/api_client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ const account: PleromaEntity.Account = {
emojis: [],
moved: null,
fields: [],
bot: false
bot: false,
source: {
privacy: null,
sensitive: false,
language: null,
note: 'test',
fields: []
}
}

describe('api_client', () => {
Expand Down
1 change: 1 addition & 0 deletions megalodon/test/unit/webo_socket.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const account: Entity.Account = {
acct: 'h3poteto@pleroma.io',
display_name: 'h3poteto',
locked: false,
group: false,
created_at: '2019-03-26T21:30:32',
followers_count: 10,
following_count: 10,
Expand Down

0 comments on commit c04f14c

Please sign in to comment.