Skip to content

Comment API Endpoints #4194

@carlossierra311

Description

@carlossierra311

Describe the feature you'd like

I @ssddanbrown. I am working on generating notifications when a new comment is made on a page. I am trying to achieve this using your webhooks implementation and the pages API, through an Azure Function App. But in doing so, I found there's no way to get information on page comments, including users that previously commented on the page, or users that have favorited the page, which I would like to include as recipients of the notification.

I'd like to have access to the following information as part of the data sent by the "commented_on" webhook and/or the page API:

  • new comment's commenter id
  • new comment's id
  • new comment's text
  • original comment id (meaning, the comment that the new comment is replying to)
  • list of page comments (with commenter id, comment id, comment text)
  • ids of people that have favorited the page

In case anyone else coming here is interested, here is my code so far (which is working, but will send notifications only to the person which last updated the page that was commented on):

Details

const request = require('request-promise');
const nodemailer = require('nodemailer');

const BOOKSTACK_URL = process.env['BOOKSTACK_URL'];
const BOOKSTACK_API_URL = BOOKSTACK_URL + '/api';

// Your BookStack API credentials
const API_TOKEN_ID = process.env['BOOKSTACK_API_TOKEN_ID'];
const API_TOKEN_SECRET = process.env['BOOKSTACK_API_TOKEN_SECRET'];

// Email server configuration
const EMAIL_HOST = process.env['EMAIL_HOST'];
const EMAIL_PORT = process.env['EMAIL_PORT'];
const EMAIL_USERNAME = process.env['EMAIL_USERNAME'];
const EMAIL_PASSWORD = process.env['EMAIL_PASSWORD'];
const EMAIL_FROM = process.env['EMAIL_FROM'];

// Function to get the email of the user who last updated the page
async function get_last_updated_user_email(page_id) {
    // Get the page information
    const page_url = BOOKSTACK_API_URL + '/pages/' + page_id;
    const page_data = await request.get({
        url: page_url,
        headers: { 'Authorization': 'Token ' + API_TOKEN_ID + ':' + API_TOKEN_SECRET },
        json: true
    });

    // Get the user who last updated the page
    const user_id = page_data.updated_by.id;
    const user_url = BOOKSTACK_API_URL + '/users/' + user_id;
    const user_data = await request.get({
        url: user_url,
        headers: { 'Authorization': 'Token ' + API_TOKEN_ID + ':' + API_TOKEN_SECRET },
        json: true
    });

    return user_data.email;
}

// Function to send a notification email
async function send_notification_email(to_email, subject, message, page_url) {
    // Configure the email transport
    const transport = nodemailer.createTransport({
        host: EMAIL_HOST,
        port: EMAIL_PORT,
        auth: {
            user: EMAIL_USERNAME,
            pass: EMAIL_PASSWORD
        }
    });

    // Define the email message
    const email_message = {
        from: EMAIL_FROM,
        to: to_email,
        subject: subject,
        html: `<p>${message}:</p><p><a href="${page_url}">${page_url}</a></p>`,
    };

    // Send the email
    await transport.sendMail(email_message);
}

// Function to handle the webhook event
async function handle_webhook_event(event_data) {
    // Get the page ID from the webhook event data
    const page_url = event_data.url;
    const page_id = event_data.related_item.id;

    // Get the email of the user who last updated the page
    const last_updated_user_email = await get_last_updated_user_email(page_id);

    // Send the notification email
    const subject = 'New comment on BookStack';
    const message = 'A new comment was added to ' + event_data.related_item.name;
    await send_notification_email(last_updated_user_email, subject, message, page_url);
}

module.exports = async function (context, req) {
    const event_data = req.body;

    // Only handle the "commented_on" event
    if (event_data.event === 'commented_on') {
        await handle_webhook_event(event_data);
    }

    context.res = {
        status: 204,
        body: ''
    };
};

Describe the benefits this would bring to existing BookStack users

It will allow BookStack to have a full comment notification system that allows for proper and timely interaction around a page's content and comments. Otherwise, comments will go unnoticed most of the time, lowering the value and usability of that feature.

Can the goal of this request already be achieved via other means?

Not that I'm aware of.

Have you searched for an existing open/closed issue?

  • I have searched for existing issues and none cover my fundemental request

How long have you been using BookStack?

6 months to 1 year

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions