|
@@ -0,0 +1,112 @@
|
|
|
|
|
+import { createStreamingAPIClient, createRestAPIClient, mastodon } from "masto";
|
|
|
|
|
+import { CronJob } from "cron";
|
|
|
|
|
+
|
|
|
|
|
+import PostgresClient from "../../libs/postgres-client";
|
|
|
|
|
+
|
|
|
|
|
+import config from "../../config";
|
|
|
|
|
+import Emojis from "../../enums/emojis";
|
|
|
|
|
+import LogLevels from "../../enums/log-levels";
|
|
|
|
|
+
|
|
|
|
|
+export default class Anniversaries {
|
|
|
|
|
+ private readonly _mastodonStreamingClient: mastodon.streaming.Client;
|
|
|
|
|
+ private readonly _mastodonRestClient: mastodon.rest.Client;
|
|
|
|
|
+ private readonly _postgresClient: PostgresClient;
|
|
|
|
|
+
|
|
|
|
|
+ constructor(accessToken = config.MASTODON_TEST_ACCESS_TOKEN) {
|
|
|
|
|
+ this._mastodonStreamingClient = createStreamingAPIClient({ streamingApiUrl: config.MASTODON_STREAMING_URL, accessToken });
|
|
|
|
|
+ this._mastodonRestClient = createRestAPIClient({ url: config.MASTODON_API_URL, accessToken });
|
|
|
|
|
+ this._postgresClient = new PostgresClient();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private async publish(status: string): Promise<void> {
|
|
|
|
|
+ let result: mastodon.v1.Status;
|
|
|
|
|
+
|
|
|
|
|
+ console.log("Sending status\n", status);
|
|
|
|
|
+ result = await this._mastodonRestClient.v1.statuses.create({ status });
|
|
|
|
|
+
|
|
|
|
|
+ if (config.LOG_LEVEL === LogLevels.DEBUG) {
|
|
|
|
|
+ console.log("Result", result);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public async start(): Promise<void> {
|
|
|
|
|
+ try {
|
|
|
|
|
+ let from = new Date();
|
|
|
|
|
+ let to = new Date();
|
|
|
|
|
+ to.setDate(from.getDate() + 1)
|
|
|
|
|
+
|
|
|
|
|
+ if (config.DEVELOP) {
|
|
|
|
|
+ from = new Date("2022-11-21");
|
|
|
|
|
+ to = new Date("2022-11-22");
|
|
|
|
|
+
|
|
|
|
|
+ const query = `
|
|
|
|
|
+ select a.username, a.display_name, u.created_at
|
|
|
|
|
+ from users u
|
|
|
|
|
+ join accounts a on u.account_id = a.id
|
|
|
|
|
+ where a.actor_type = 'Person'
|
|
|
|
|
+ and a.suspended_at is null
|
|
|
|
|
+ and u.created_at >= ${from}
|
|
|
|
|
+ and u.created_at < ${to}
|
|
|
|
|
+ order by created_at asc;
|
|
|
|
|
+ `;
|
|
|
|
|
+ console.log(query);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const anniversaries = await this._postgresClient.sql`
|
|
|
|
|
+ select a.username, a.display_name, u.created_at
|
|
|
|
|
+ from users u
|
|
|
|
|
+ join accounts a on u.account_id = a.id
|
|
|
|
|
+ where a.actor_type = 'Person'
|
|
|
|
|
+ and a.suspended_at is null
|
|
|
|
|
+ and u.created_at >= ${from}
|
|
|
|
|
+ and u.created_at < ${to}
|
|
|
|
|
+ order by created_at asc;
|
|
|
|
|
+ `;
|
|
|
|
|
+
|
|
|
|
|
+ if (config.LOG_LEVEL == LogLevels.DEBUG) {
|
|
|
|
|
+ console.debug(anniversaries);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (anniversaries.length == 0) {
|
|
|
|
|
+ console.log("No hay aniversarios");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ let status = `¡Se encienden las velitas y arrancan los festejos! ${Emojis.CAKE}${Emojis.SPARKLES}\n`;
|
|
|
|
|
+ status += `Hoy en #MastodonCL ${Emojis.CL_FLAG} celebramos el aniversario de la llegada a nuestra instancia de:\n\n`;
|
|
|
|
|
+
|
|
|
|
|
+ anniversaries.forEach((user: any) => {
|
|
|
|
|
+ const createdAt = new Date(user.created_at);
|
|
|
|
|
+ const today = new Date();
|
|
|
|
|
+ const years = today.getFullYear() - createdAt.getFullYear();
|
|
|
|
|
+
|
|
|
|
|
+ status += `- ${Emojis.PERSON} ${user.display_name || user.username} (@${user.username}) quien cumple ${years} año${years > 1 ? "s" : ""} \n`;
|
|
|
|
|
+ });
|
|
|
|
|
+ status += `\nGracias por acompañarnos ${Emojis.HEART}${Emojis.HUG} en este viaje a través del #Fediverso ${Emojis.ROCKET}. Que sea un muy feliz #Aniversario y que se cumplan muchos más!`;
|
|
|
|
|
+
|
|
|
|
|
+ this.publish(status);
|
|
|
|
|
+
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ console.error(error);
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ await this._postgresClient.disconnect();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+try {
|
|
|
|
|
+ const anniversaries = new Anniversaries(config.DEVELOP ? config.MASTODON_TEST_ACCESS_TOKEN : config.MASTODON_KEY_ANNIVERSARIES);
|
|
|
|
|
+ new CronJob(
|
|
|
|
|
+ "0 0 12 * * *",
|
|
|
|
|
+ () => anniversaries.start(),
|
|
|
|
|
+ null,
|
|
|
|
|
+ true,
|
|
|
|
|
+ config.DEFAULT_TIMEZONE
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ if (config.LOG_LEVEL == LogLevels.DEBUG) {
|
|
|
|
|
+ anniversaries.start();
|
|
|
|
|
+ }
|
|
|
|
|
+} catch (error) {
|
|
|
|
|
+ console.error(error);
|
|
|
|
|
+}
|