-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mastodon): implement mastodon watcher
- Loading branch information
Showing
5 changed files
with
188 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import * as masto from "masto"; | ||
|
||
import { BaseWatcher, BaseWatcherOptions, PartialUser } from "@watchers/base"; | ||
|
||
export interface MastodonWatcherOptions extends BaseWatcherOptions<MastodonWatcher> { | ||
url: string; | ||
accessToken: string; | ||
} | ||
|
||
export class MastodonWatcher extends BaseWatcher<"Mastodon"> { | ||
private client: masto.mastodon.Client | null = null; | ||
private account: masto.mastodon.v1.Account | null = null; | ||
|
||
constructor(private readonly options: MastodonWatcherOptions) { | ||
super("Mastodon"); | ||
} | ||
|
||
public async initialize(): Promise<void> { | ||
this.client = await masto.login({ | ||
url: this.options.url, | ||
accessToken: this.options.accessToken, | ||
}); | ||
|
||
this.account = await this.client.v1.accounts.lookup({ | ||
acct: "@[email protected]", | ||
}); | ||
|
||
return Promise.resolve(undefined); | ||
} | ||
|
||
protected async getFollowers(): Promise<PartialUser[]> { | ||
if (!this.client || !this.account) { | ||
throw new Error("Mastodon watcher has not initialized"); | ||
} | ||
|
||
const { id, followersCount } = this.account; | ||
const result: PartialUser[] = []; | ||
for await (const followers of this.client.v1.accounts.listFollowers(id, { limit: 80 })) { | ||
const partialUsers = followers.map(follower => ({ | ||
profileUrl: follower.url, | ||
uniqueId: follower.acct, | ||
displayName: follower.displayName, | ||
userId: follower.id, | ||
})); | ||
|
||
result.push(...partialUsers); | ||
if (result.length >= followersCount) { | ||
break; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} |
Oops, something went wrong.