Skip to content

Commit

Permalink
chore(core): migrate device token to notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleSamtoshi committed Jan 22, 2024
1 parent 9935d94 commit 01dfcf3
Show file tree
Hide file tree
Showing 9 changed files with 1,060 additions and 2 deletions.
1 change: 1 addition & 0 deletions core/api/src/domain/notifications/index.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type NotificationChannel =

type NotificationSettings = Record<NotificationChannel, NotificationChannelSettings> & {
language: UserLanguageOrEmpty
pushDeviceTokens: DeviceToken[]
}

type NotificationChannelSettings = {
Expand Down
1 change: 1 addition & 0 deletions core/api/src/services/notifications/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const grpcNotificationSettingsToNotificationSettings = (
enabled: pushSettings.getEnabled(),
disabledCategories,
},
pushDeviceTokens: settings.getPushDeviceTokensList() as DeviceToken[],
}

return notificationSettings
Expand Down
16 changes: 16 additions & 0 deletions core/api/src/services/notifications/grpc-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import {
GetNotificationSettingsResponse,
UpdateUserLocaleRequest,
UpdateUserLocaleResponse,
AddPushDeviceTokenRequest,
AddPushDeviceTokenResponse,
RemovePushDeviceTokenRequest,
RemovePushDeviceTokenResponse,
} from "./proto/notifications_pb"

import { NOTIFICATIONS_HOST, NOTIFICATIONS_PORT } from "@/config"
Expand Down Expand Up @@ -73,3 +77,15 @@ export const updateUserLocale = promisify<
Metadata,
UpdateUserLocaleResponse
>(notificationsClient.updateUserLocale.bind(notificationsClient))

export const addPushDeviceToken = promisify<
AddPushDeviceTokenRequest,
Metadata,
AddPushDeviceTokenResponse
>(notificationsClient.addPushDeviceToken.bind(notificationsClient))

export const removePushDeviceToken = promisify<
RemovePushDeviceTokenRequest,
Metadata,
RemovePushDeviceTokenResponse
>(notificationsClient.removePushDeviceToken.bind(notificationsClient))
56 changes: 56 additions & 0 deletions core/api/src/services/notifications/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import {
import { createPushNotificationContent } from "./create-push-notification-content"

import {
AddPushDeviceTokenRequest,
DisableNotificationCategoryRequest,
DisableNotificationChannelRequest,
EnableNotificationCategoryRequest,
EnableNotificationChannelRequest,
GetNotificationSettingsRequest,
RemovePushDeviceTokenRequest,
UpdateUserLocaleRequest,
} from "./proto/notifications_pb"

Expand Down Expand Up @@ -409,6 +411,60 @@ export const NotificationsService = (): INotificationsService => {
}
}

const addPushDeviceToken = async ({
userId,
deviceToken,
}: {
userId: UserId
deviceToken: DeviceToken
}): Promise<NotificationSettings | NotificationsServiceError> => {
try {
const request = new AddPushDeviceTokenRequest()
request.setUserId(userId)
request.setDeviceToken(deviceToken)

const response = await notificationsGrpc.addPushDeviceToken(
request,
notificationsGrpc.notificationsMetadata,
)

const notificationSettings = grpcNotificationSettingsToNotificationSettings(
response.getNotificationSettings(),
)

return notificationSettings
} catch (err) {
return new UnknownNotificationsServiceError(err)
}
}

const removePushDeviceToken = async ({
userId,
deviceToken,
}: {
userId: UserId
deviceToken: DeviceToken
}): Promise<NotificationSettings | NotificationsServiceError> => {
try {
const request = new RemovePushDeviceTokenRequest()
request.setUserId(userId)
request.setDeviceToken(deviceToken)

const response = await notificationsGrpc.removePushDeviceToken(
request,
notificationsGrpc.notificationsMetadata,
)

const notificationSettings = grpcNotificationSettingsToNotificationSettings(
response.getNotificationSettings(),
)

return notificationSettings
} catch (err) {
return new UnknownNotificationsServiceError(err)
}
}

const updateUserLanguage = async ({
userId,
language,
Expand Down
21 changes: 21 additions & 0 deletions core/api/src/services/notifications/proto/notifications.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ service NotificationsService {
rpc DisableNotificationCategory (DisableNotificationCategoryRequest) returns (DisableNotificationCategoryResponse) {}
rpc GetNotificationSettings (GetNotificationSettingsRequest) returns (GetNotificationSettingsResponse) {}
rpc UpdateUserLocale (UpdateUserLocaleRequest) returns (UpdateUserLocaleResponse) {}
rpc AddPushDeviceToken (AddPushDeviceTokenRequest) returns (AddPushDeviceTokenResponse) {}
rpc RemovePushDeviceToken (RemovePushDeviceTokenRequest) returns (RemovePushDeviceTokenResponse) {}
}

enum NotificationChannel {
Expand Down Expand Up @@ -48,6 +50,7 @@ message EnableNotificationChannelResponse {
message NotificationSettings {
ChannelNotificationSettings push = 1;
optional string locale = 2;
repeated string push_device_tokens = 3;
}

message ChannelNotificationSettings {
Expand Down Expand Up @@ -100,3 +103,21 @@ message UpdateUserLocaleRequest {
message UpdateUserLocaleResponse {
NotificationSettings notification_settings = 1;
}

message AddPushDeviceTokenRequest {
string user_id = 1;
string device_token = 2;
}

message AddPushDeviceTokenResponse {
NotificationSettings notification_settings = 1;
}

message RemovePushDeviceTokenRequest {
string user_id = 1;
string device_token = 2;
}

message RemovePushDeviceTokenResponse {
NotificationSettings notification_settings = 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ interface INotificationsServiceService extends grpc.ServiceDefinition<grpc.Untyp
disableNotificationCategory: INotificationsServiceService_IDisableNotificationCategory;
getNotificationSettings: INotificationsServiceService_IGetNotificationSettings;
updateUserLocale: INotificationsServiceService_IUpdateUserLocale;
addPushDeviceToken: INotificationsServiceService_IAddPushDeviceToken;
removePushDeviceToken: INotificationsServiceService_IRemovePushDeviceToken;
}

interface INotificationsServiceService_IShouldSendNotification extends grpc.MethodDefinition<notifications_pb.ShouldSendNotificationRequest, notifications_pb.ShouldSendNotificationResponse> {
Expand Down Expand Up @@ -81,6 +83,24 @@ interface INotificationsServiceService_IUpdateUserLocale extends grpc.MethodDefi
responseSerialize: grpc.serialize<notifications_pb.UpdateUserLocaleResponse>;
responseDeserialize: grpc.deserialize<notifications_pb.UpdateUserLocaleResponse>;
}
interface INotificationsServiceService_IAddPushDeviceToken extends grpc.MethodDefinition<notifications_pb.AddPushDeviceTokenRequest, notifications_pb.AddPushDeviceTokenResponse> {
path: "/services.notifications.v1.NotificationsService/AddPushDeviceToken";
requestStream: false;
responseStream: false;
requestSerialize: grpc.serialize<notifications_pb.AddPushDeviceTokenRequest>;
requestDeserialize: grpc.deserialize<notifications_pb.AddPushDeviceTokenRequest>;
responseSerialize: grpc.serialize<notifications_pb.AddPushDeviceTokenResponse>;
responseDeserialize: grpc.deserialize<notifications_pb.AddPushDeviceTokenResponse>;
}
interface INotificationsServiceService_IRemovePushDeviceToken extends grpc.MethodDefinition<notifications_pb.RemovePushDeviceTokenRequest, notifications_pb.RemovePushDeviceTokenResponse> {
path: "/services.notifications.v1.NotificationsService/RemovePushDeviceToken";
requestStream: false;
responseStream: false;
requestSerialize: grpc.serialize<notifications_pb.RemovePushDeviceTokenRequest>;
requestDeserialize: grpc.deserialize<notifications_pb.RemovePushDeviceTokenRequest>;
responseSerialize: grpc.serialize<notifications_pb.RemovePushDeviceTokenResponse>;
responseDeserialize: grpc.deserialize<notifications_pb.RemovePushDeviceTokenResponse>;
}

export const NotificationsServiceService: INotificationsServiceService;

Expand All @@ -92,6 +112,8 @@ export interface INotificationsServiceServer extends grpc.UntypedServiceImplemen
disableNotificationCategory: grpc.handleUnaryCall<notifications_pb.DisableNotificationCategoryRequest, notifications_pb.DisableNotificationCategoryResponse>;
getNotificationSettings: grpc.handleUnaryCall<notifications_pb.GetNotificationSettingsRequest, notifications_pb.GetNotificationSettingsResponse>;
updateUserLocale: grpc.handleUnaryCall<notifications_pb.UpdateUserLocaleRequest, notifications_pb.UpdateUserLocaleResponse>;
addPushDeviceToken: grpc.handleUnaryCall<notifications_pb.AddPushDeviceTokenRequest, notifications_pb.AddPushDeviceTokenResponse>;
removePushDeviceToken: grpc.handleUnaryCall<notifications_pb.RemovePushDeviceTokenRequest, notifications_pb.RemovePushDeviceTokenResponse>;
}

export interface INotificationsServiceClient {
Expand All @@ -116,6 +138,12 @@ export interface INotificationsServiceClient {
updateUserLocale(request: notifications_pb.UpdateUserLocaleRequest, callback: (error: grpc.ServiceError | null, response: notifications_pb.UpdateUserLocaleResponse) => void): grpc.ClientUnaryCall;
updateUserLocale(request: notifications_pb.UpdateUserLocaleRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: notifications_pb.UpdateUserLocaleResponse) => void): grpc.ClientUnaryCall;
updateUserLocale(request: notifications_pb.UpdateUserLocaleRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: notifications_pb.UpdateUserLocaleResponse) => void): grpc.ClientUnaryCall;
addPushDeviceToken(request: notifications_pb.AddPushDeviceTokenRequest, callback: (error: grpc.ServiceError | null, response: notifications_pb.AddPushDeviceTokenResponse) => void): grpc.ClientUnaryCall;
addPushDeviceToken(request: notifications_pb.AddPushDeviceTokenRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: notifications_pb.AddPushDeviceTokenResponse) => void): grpc.ClientUnaryCall;
addPushDeviceToken(request: notifications_pb.AddPushDeviceTokenRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: notifications_pb.AddPushDeviceTokenResponse) => void): grpc.ClientUnaryCall;
removePushDeviceToken(request: notifications_pb.RemovePushDeviceTokenRequest, callback: (error: grpc.ServiceError | null, response: notifications_pb.RemovePushDeviceTokenResponse) => void): grpc.ClientUnaryCall;
removePushDeviceToken(request: notifications_pb.RemovePushDeviceTokenRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: notifications_pb.RemovePushDeviceTokenResponse) => void): grpc.ClientUnaryCall;
removePushDeviceToken(request: notifications_pb.RemovePushDeviceTokenRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: notifications_pb.RemovePushDeviceTokenResponse) => void): grpc.ClientUnaryCall;
}

export class NotificationsServiceClient extends grpc.Client implements INotificationsServiceClient {
Expand All @@ -141,4 +169,10 @@ export class NotificationsServiceClient extends grpc.Client implements INotifica
public updateUserLocale(request: notifications_pb.UpdateUserLocaleRequest, callback: (error: grpc.ServiceError | null, response: notifications_pb.UpdateUserLocaleResponse) => void): grpc.ClientUnaryCall;
public updateUserLocale(request: notifications_pb.UpdateUserLocaleRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: notifications_pb.UpdateUserLocaleResponse) => void): grpc.ClientUnaryCall;
public updateUserLocale(request: notifications_pb.UpdateUserLocaleRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: notifications_pb.UpdateUserLocaleResponse) => void): grpc.ClientUnaryCall;
public addPushDeviceToken(request: notifications_pb.AddPushDeviceTokenRequest, callback: (error: grpc.ServiceError | null, response: notifications_pb.AddPushDeviceTokenResponse) => void): grpc.ClientUnaryCall;
public addPushDeviceToken(request: notifications_pb.AddPushDeviceTokenRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: notifications_pb.AddPushDeviceTokenResponse) => void): grpc.ClientUnaryCall;
public addPushDeviceToken(request: notifications_pb.AddPushDeviceTokenRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: notifications_pb.AddPushDeviceTokenResponse) => void): grpc.ClientUnaryCall;
public removePushDeviceToken(request: notifications_pb.RemovePushDeviceTokenRequest, callback: (error: grpc.ServiceError | null, response: notifications_pb.RemovePushDeviceTokenResponse) => void): grpc.ClientUnaryCall;
public removePushDeviceToken(request: notifications_pb.RemovePushDeviceTokenRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: notifications_pb.RemovePushDeviceTokenResponse) => void): grpc.ClientUnaryCall;
public removePushDeviceToken(request: notifications_pb.RemovePushDeviceTokenRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: notifications_pb.RemovePushDeviceTokenResponse) => void): grpc.ClientUnaryCall;
}
66 changes: 66 additions & 0 deletions core/api/src/services/notifications/proto/notifications_grpc_pb.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@ var grpc = require('@grpc/grpc-js');
var notifications_pb = require('./notifications_pb.js');
var google_protobuf_struct_pb = require('google-protobuf/google/protobuf/struct_pb.js');

function serialize_services_notifications_v1_AddPushDeviceTokenRequest(arg) {
if (!(arg instanceof notifications_pb.AddPushDeviceTokenRequest)) {
throw new Error('Expected argument of type services.notifications.v1.AddPushDeviceTokenRequest');
}
return Buffer.from(arg.serializeBinary());
}

function deserialize_services_notifications_v1_AddPushDeviceTokenRequest(buffer_arg) {
return notifications_pb.AddPushDeviceTokenRequest.deserializeBinary(new Uint8Array(buffer_arg));
}

function serialize_services_notifications_v1_AddPushDeviceTokenResponse(arg) {
if (!(arg instanceof notifications_pb.AddPushDeviceTokenResponse)) {
throw new Error('Expected argument of type services.notifications.v1.AddPushDeviceTokenResponse');
}
return Buffer.from(arg.serializeBinary());
}

function deserialize_services_notifications_v1_AddPushDeviceTokenResponse(buffer_arg) {
return notifications_pb.AddPushDeviceTokenResponse.deserializeBinary(new Uint8Array(buffer_arg));
}

function serialize_services_notifications_v1_DisableNotificationCategoryRequest(arg) {
if (!(arg instanceof notifications_pb.DisableNotificationCategoryRequest)) {
throw new Error('Expected argument of type services.notifications.v1.DisableNotificationCategoryRequest');
Expand Down Expand Up @@ -115,6 +137,28 @@ function deserialize_services_notifications_v1_GetNotificationSettingsResponse(b
return notifications_pb.GetNotificationSettingsResponse.deserializeBinary(new Uint8Array(buffer_arg));
}

function serialize_services_notifications_v1_RemovePushDeviceTokenRequest(arg) {
if (!(arg instanceof notifications_pb.RemovePushDeviceTokenRequest)) {
throw new Error('Expected argument of type services.notifications.v1.RemovePushDeviceTokenRequest');
}
return Buffer.from(arg.serializeBinary());
}

function deserialize_services_notifications_v1_RemovePushDeviceTokenRequest(buffer_arg) {
return notifications_pb.RemovePushDeviceTokenRequest.deserializeBinary(new Uint8Array(buffer_arg));
}

function serialize_services_notifications_v1_RemovePushDeviceTokenResponse(arg) {
if (!(arg instanceof notifications_pb.RemovePushDeviceTokenResponse)) {
throw new Error('Expected argument of type services.notifications.v1.RemovePushDeviceTokenResponse');
}
return Buffer.from(arg.serializeBinary());
}

function deserialize_services_notifications_v1_RemovePushDeviceTokenResponse(buffer_arg) {
return notifications_pb.RemovePushDeviceTokenResponse.deserializeBinary(new Uint8Array(buffer_arg));
}

function serialize_services_notifications_v1_ShouldSendNotificationRequest(arg) {
if (!(arg instanceof notifications_pb.ShouldSendNotificationRequest)) {
throw new Error('Expected argument of type services.notifications.v1.ShouldSendNotificationRequest');
Expand Down Expand Up @@ -238,6 +282,28 @@ var NotificationsServiceService = exports.NotificationsServiceService = {
responseSerialize: serialize_services_notifications_v1_UpdateUserLocaleResponse,
responseDeserialize: deserialize_services_notifications_v1_UpdateUserLocaleResponse,
},
addPushDeviceToken: {
path: '/services.notifications.v1.NotificationsService/AddPushDeviceToken',
requestStream: false,
responseStream: false,
requestType: notifications_pb.AddPushDeviceTokenRequest,
responseType: notifications_pb.AddPushDeviceTokenResponse,
requestSerialize: serialize_services_notifications_v1_AddPushDeviceTokenRequest,
requestDeserialize: deserialize_services_notifications_v1_AddPushDeviceTokenRequest,
responseSerialize: serialize_services_notifications_v1_AddPushDeviceTokenResponse,
responseDeserialize: deserialize_services_notifications_v1_AddPushDeviceTokenResponse,
},
removePushDeviceToken: {
path: '/services.notifications.v1.NotificationsService/RemovePushDeviceToken',
requestStream: false,
responseStream: false,
requestType: notifications_pb.RemovePushDeviceTokenRequest,
responseType: notifications_pb.RemovePushDeviceTokenResponse,
requestSerialize: serialize_services_notifications_v1_RemovePushDeviceTokenRequest,
requestDeserialize: deserialize_services_notifications_v1_RemovePushDeviceTokenRequest,
responseSerialize: serialize_services_notifications_v1_RemovePushDeviceTokenResponse,
responseDeserialize: deserialize_services_notifications_v1_RemovePushDeviceTokenResponse,
},
};

exports.NotificationsServiceClient = grpc.makeGenericClientConstructor(NotificationsServiceService);
Loading

0 comments on commit 01dfcf3

Please sign in to comment.