Google Cloud Pub/Sub 仍然在停止用户的所有事件后发送通知。

huangapple go评论77阅读模式
英文:

Google Cloud Pub Sub still sending notifications even after stopping all events for the user

问题

在我的节点服务器中,我正在获取用户的Gmail收件箱权限,并使用Google Cloud的Pub/Sub向它们添加监视器。

以下是代码部分:

async addWatcherToGmailInbox({
    gmailInboxRefreshToken,
  }: {
    gmailInboxRefreshToken: string;
  }): Promise<boolean> {
    const auth = await this.loadAuth(gmailInboxRefreshToken);
    const gmail = google.gmail({
      version: 'v1',
      auth,
    });
    await gmail.users.watch({
      userId: 'me',
      requestBody: {
        labelIds: ['INBOX'],
        topicName: 'projects/<project-name>/topics/<topic-name>',
      },
    });
    return true;
  }

但是当用户从我的平台取消链接他的Gmail帐户时,我首先停止通知,然后撤销令牌。但不知何故,在我的Pub/Sub订阅者的"on"事件中仍然收到通知。

停止通知的代码:

async removeWatcherFromGmailInbox({
    gmailInboxRefreshToken,
  }: {
    gmailInboxRefreshToken: string;
  }): Promise<boolean> {
    const auth = await this.loadAuth(gmailInboxRefreshToken);
    const gmail = google.gmail({
      version: 'v1',
      auth,
    });
    gmail.users.stop({
      userId: 'me',
    });
    return true;
  }

我Pub/Sub订阅者事件中获取通知的部分:

const pubsub = new PubSub({
      projectId: <project-name>,
    });

    this.logger.log('Google Pubsub initialized, fetching topics...');

    const subs = await pubsub.getSubscriptions();
    const sub = subs[0][0];

    // Receive callbacks for new messages on the subscription
    sub.on('message', (message) => {
      this.logger.log('Received message:', message.data.toString());

      const data = JSON.parse(message.data.toString());
    });

    // Receive callbacks for errors on the subscription
    sub.on('error', (error) => {
      this.logger.error('Received error:', error);
    });

当用户希望撤销令牌时,我漏掉了什么来停止通知?

注意:我确实记录了停止函数的响应。它返回一个空的正文,就像在文档中提到的那样。

英文:

In my node server, I am taking user's gmail inbox permissions and adding a watcher to all of them using google cloud's pub sub.

Here's the code:

async addWatcherToGmailInbox({
    gmailInboxRefreshToken,
  }: {
    gmailInboxRefreshToken: string;
  }): Promise&lt;boolean&gt; {
    const auth = await this.loadAuth(gmailInboxRefreshToken);
    const gmail = google.gmail({
      version: &#39;v1&#39;,
      auth,
    });
    await gmail.users.watch({
      userId: &#39;me&#39;,
      requestBody: {
        labelIds: [&#39;INBOX&#39;],
        topicName: &#39;projects/&lt;project-name&gt;/topics/&lt;topic-name&gt;&#39;,
      },
    });
    return true;
  }

But when user unlinks his gmail account from my platform, I stop the notifications first and then revoke the token. But somehow, I still get notifications in my pub sub subscriber on event.

Stopping notifications code:

async removeWatcherFromGmailInbox({
    gmailInboxRefreshToken,
  }: {
    gmailInboxRefreshToken: string;
  }): Promise&lt;boolean&gt; {
    const auth = await this.loadAuth(gmailInboxRefreshToken);
    const gmail = google.gmail({
      version: &#39;v1&#39;,
      auth,
    });
    gmail.users.stop({
      userId: &#39;me&#39;,
    });
    return true;
  }

My pub sub subscriber event where I'm getting notifications:

const pubsub = new PubSub({
      projectId: &lt;project-name&gt;,
    });

    this.logger.log(&#39;Google Pubsub initialized, fetching topics...&#39;);

    const subs = await pubsub.getSubscriptions();
    const sub = subs[0][0];

    // Receive callbacks for new messages on the subscription
    sub.on(&#39;message&#39;, (message) =&gt; {
      this.logger.log(&#39;Received message:&#39;, message.data.toString());

      const data = JSON.parse(message.data.toString());
    });

    // Receive callbacks for errors on the subscription
    sub.on(&#39;error&#39;, (error) =&gt; {
      this.logger.error(&#39;Received error:&#39;, error);
    });

What am I missing to stop notifications for a user when he/she wants to revoke the token.

Note: I did log the response of stop function. It returns empty body just like it's mentioned in the docs.

答案1

得分: 1

你可以停止发布主题中的消息,但订阅中仍然存在消息,PubSub会尝试将它们传递直到确认或消息过期。您还可以清除订阅。

英文:

You can stop to publish message in topic, but there is still messages in the subscription and PubSub try to deliver them until ack or message expiration. You can also purge the subscription

huangapple
  • 本文由 发表于 2023年6月16日 11:49:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定