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: Export event handlers to allow for customization #55

Open
anestor6 opened this issue Apr 4, 2023 · 10 comments
Open

feat: Export event handlers to allow for customization #55

anestor6 opened this issue Apr 4, 2023 · 10 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@anestor6
Copy link
Contributor

anestor6 commented Apr 4, 2023

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

@drodil
Copy link
Owner

drodil commented Apr 17, 2023

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
Copy link
Owner

drodil commented May 19, 2023

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 added a commit that referenced this issue Jun 14, 2023
@drodil
Copy link
Owner

drodil commented Jun 14, 2023

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

@anestor6
Copy link
Contributor Author

anestor6 commented Jun 29, 2023

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...

@drodil
Copy link
Owner

drodil commented Jun 30, 2023

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..

@anestor6
Copy link
Contributor Author

anestor6 commented Jul 20, 2023

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
Copy link
Contributor Author

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

@LZING
Copy link

LZING commented Oct 11, 2023

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

@drodil
Copy link
Owner

drodil commented Jan 11, 2024

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

@LZING
Copy link

LZING commented Jan 16, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants