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

Laushinka/let people unsubscribe 4761 #5281

Merged
merged 4 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions components/server/src/container-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import { IAnalyticsWriter } from '@gitpod/gitpod-protocol/lib/analytics';
import { HeadlessLogServiceClient } from '@gitpod/content-service/lib/headless-log_grpc_pb';
import { ProjectsService } from './projects/projects-service';
import { EnvConfig, Config } from './config';
import { NewsletterSubscriptionController } from './user/newsletter-subscription-controller';

export const productionContainerModule = new ContainerModule((bind, unbind, isBound, rebind) => {
bind(Env).toSelf().inSingletonScope();
Expand Down Expand Up @@ -220,4 +221,6 @@ export const productionContainerModule = new ContainerModule((bind, unbind, isBo
bind(HeadlessLogController).toSelf().inSingletonScope();

bind(ProjectsService).toSelf().inSingletonScope();

bind(NewsletterSubscriptionController).toSelf().inSingletonScope();
});
3 changes: 3 additions & 0 deletions components/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { CodeSyncService } from './code-sync/code-sync-service';
import { increaseHttpRequestCounter, observeHttpRequestDuration } from './prometheus-metrics';
import { OAuthController } from './oauth-server/oauth-controller';
import { HeadlessLogController } from './workspace/headless-log-controller';
import { NewsletterSubscriptionController } from './user/newsletter-subscription-controller';
import { Config } from './config';

@injectable()
Expand Down Expand Up @@ -73,6 +74,7 @@ export class Server<C extends GitpodClient, S extends GitpodServer> {

@inject(HostContextProvider) protected readonly hostCtxProvider: HostContextProvider;
@inject(OAuthController) protected readonly oauthController: OAuthController;
@inject(NewsletterSubscriptionController) protected readonly newsletterSubscriptionController: NewsletterSubscriptionController;

protected readonly eventEmitter = new EventEmitter();
protected app?: express.Application;
Expand Down Expand Up @@ -260,6 +262,7 @@ export class Server<C extends GitpodClient, S extends GitpodServer> {
app.use('/workspace-download', this.workspaceDownloadService.apiRouter);
app.use('/code-sync', this.codeSyncService.apiRouter);
app.use('/headless-logs', this.headlessLogController.apiRouter);
app.use(this.newsletterSubscriptionController.apiRouter);
app.use("/version", (req: express.Request, res: express.Response, next: express.NextFunction) => {
res.send(this.env.version);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* Copyright (c) 2021 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License-AGPL.txt in the project root for license information.
*/

import * as express from 'express';
import { inject, injectable } from "inversify";
import { UserDB } from "@gitpod/gitpod-db/lib";
import { IAnalyticsWriter } from "@gitpod/gitpod-protocol/lib/analytics";

@injectable()
export class NewsletterSubscriptionController {
@inject(UserDB) protected readonly userDb: UserDB;
@inject(IAnalyticsWriter) protected readonly analytics: IAnalyticsWriter;

get apiRouter(): express.Router {
const router = express.Router();

router.get("/unsubscribe", async (req: express.Request, res: express.Response) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we would like to use this API to subscribe newsletter users so that they are automatically attached to their user accounts if a the email for the newsletter signup matches any user's primary email. is it ok for you to extend the scope of this PR to include a /subscribe endpoint that signs up a known or unknown user for a specified subscription list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to keep the existing scope of this ticket for reasons of: 1) planning and 2) transparency :) When tickets are approved and put to in progress, I assume there are already plans and estimations of how much effort the current ticket/board would take. Changing the scope might change the planning for the rest of the board. Secondly, a changing scope almost at the end of the work might not be known to the stakeholders of the board.
However, I am still learning how the team usually manages their projects so I will tag @JanKoehnlein here for his thoughts.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's open another ticket for @jakobhero's request. I feel like it has to be discussed in terms of security and GDPR as well.

const email: string = req.query.email;
const newsletterType: string = req.query.type;
const acceptedNewsletterTypes: string[] = ["changelog", "devx"];
const newsletterProperties: {[key:string]: {[key: string]: string}} = {
changelog: {
property: "unsubscribed_changelog",
value: "allowsChangelogMail"
},
devx: {
property: "unsubscribed_devx",
value: "allowsDevXMail"
}
}

if (!acceptedNewsletterTypes.includes(newsletterType)) {
res.sendStatus(422);
return;
}

try {
// Not all newsletter subscribers are users,
// therefore the email address is our starting point
const user = (await this.userDb.findUsersByEmail(email))[0];
const successPageUrl: string = 'https://www.gitpod.io/unsubscribe';

if (user && user.additionalData && user.additionalData.emailNotificationSettings) {
await this.userDb.updateUserPartial({
...user,
additionalData: {
...user.additionalData,
emailNotificationSettings: {
...user.additionalData.emailNotificationSettings,
[newsletterProperties[newsletterType].value]: false
}
}
});
this.analytics.identify({
userId: user.id,
traits: {
[newsletterProperties[newsletterType].property]: true
}
});
res.redirect(successPageUrl);
}

else {
this.analytics.identify({
userId: email,
traits: {
[newsletterProperties[newsletterType].property]: true
}
});
res.redirect(successPageUrl);
}
} catch (error) {
res.send({
err: error.status,
message: error.message
});
return;
}
})

return router;
}
}