backstage-plugin-qeta icon indicating copy to clipboard operation
backstage-plugin-qeta copied to clipboard

feat: Export event handlers to allow for customization

Open anestor6 opened this issue 2 years ago • 10 comments

I think it could useful to allow us to add custom functions to events like post question, post answer, post reply, upvote, etc. In our case, we used the patch-package library to add a POST requests to a backend route with a custom Slack integration with the question payload in the body to notify users when a question is asked / answer is posted. But it would be handy if these operations were exposed in the client to add a bit of customization on how we handle notifiable events. Perhaps in the QuestionContainer component and the AskForm, only thing would be to ensure these event handlers have all the appropriate information, for example, question title, content, time, reporter, entities, etc. And in the case of the reply, the reporter and the replier. Another thing for consideration would be on the editing of a question and how that argument would look. Thoughts? @drodil

anestor6 avatar Apr 04 '23 17:04 anestor6

Very good idea and pretty easy to implement! Do you have time to work on this? I am currently quite busy but can take a look if there's no activity from the community.

drodil avatar Apr 17 '23 09:04 drodil

How about if we add support for the events backend and publish the changes through that? See https://github.com/backstage/backstage/tree/master/plugins%2Fevents-backend

Or is there some need to have the callbacks in the frontend?

drodil avatar May 19 '23 05:05 drodil

@anestor6 I implemented the support for the events backend, see the docs/events.md for details. Would this be sufficient for your use case?

drodil avatar Jun 14 '23 11:06 drodil

Have yet to test this out / update. But just checking the docs. In the payload for events such as commenting or upvoting does the comment payload also contain the user profile info of the commenter? I see author (which i'm assuming is the question author). But in the case of sending notifications it'd be nice to have the info so you can say "Hi {author}, {commenter} left a comment!". Similarly for voting, etc...

anestor6 avatar Jun 29 '23 15:06 anestor6

It contains the user entity reference of the user who did the action as well as all information about the question (& answer). Maybe it would make sense to document different events in the events.md..

drodil avatar Jun 30 '23 04:06 drodil

Hey i'm sorry, i tried following the docs for the events plugin, but I was wondering if you could provide more implementation details in the docs for events.md. I've installed the events package and set up and events.ts file. I see this log when i run the app events info Registered /api/events/http/qeta to receive events type=plugin. But when i try to handle a route or setup a subscriber i'm getting nowhere. If you could elaborate in the docs a bit more it would help. Maybe an example for handling an event to the post-question event with some custom logic to log it perhaps?

Edit

There's actually a typo in the events-backend docs

In their docs:

export default async function createPlugin(
  env: PluginEnvironment,
): Promise<Router> {
  env.eventBroker.subscribe([
    {
      supportsEventTopics: ['publish.example'],
      onEvent: async (params: EventParams) => {
        env.logger.info(`receieved ${params.topic} event`);
      },
    },
  ]);
}

However supportEventTopics doesnt take string[] it takes type () => string[]

so to reference my example of simply logging a payload as an event arrives, here's a template for plugins/qeta.ts

export default async function createPlugin({
  logger,
  database,
  identity,
  config,
  eventBroker,
}: PluginEnvironment) {
  eventBroker.subscribe([
    {
      supportsEventTopics: () => ['qeta'],
      onEvent: async (params: EventParams) => {
        console.log(params.eventPayload);
        logger.info(`receieved ${params.topic} event`);
      },
    },
  ]);
  const db = await DatabaseQetaStore.create({
    database: database,
  });
  return createRouter({
    logger,
    database: db,
    identity,
    config,
    eventBroker,
  });
}

anestor6 avatar Jul 20 '23 17:07 anestor6

Going to add though that the docs say that for the comment-answer event it includes the answer payload, but it doesnt. Just question, comment, and author. Not question, answer, comment, and author

anestor6 avatar Jul 21 '23 18:07 anestor6

Hi, drodil, could you please add a deleted event? Because we have created an index on the search function. When deleting a question, existing events will not be triggered. We can only work on the permissions function. But this is not in compliance with the specification and will cause the coupling to be too strong. @drodil

LZING avatar Oct 11 '23 03:10 LZING

Hi @LZING and @anestor6 , both comment answer and delete events are already added. Do you think there's something still missing?

drodil avatar Jan 11 '24 10:01 drodil

Thank you drodil. The delete events has been implemented. We have updated it and used it. No problems have been found so far. Thank you.

LZING avatar Jan 16 '24 14:01 LZING

Can this be closed?

drodil avatar Oct 18 '24 18:10 drodil