You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by GitBox <gi...@apache.org> on 2021/10/28 09:12:26 UTC

[GitHub] [james-project] chibenwa commented on a change in pull request #713: JAMES-3539 Cassandra implement for PushSubscriptionRepository

chibenwa commented on a change in pull request #713:
URL: https://github.com/apache/james-project/pull/713#discussion_r738191687



##########
File path: server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/pushsubscription/CassandraPushSubscriptionRepository.java
##########
@@ -0,0 +1,144 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.cassandra.pushsubscription;
+
+import static org.apache.james.jmap.api.model.PushSubscription.EXPIRES_TIME_MAX_DAY;
+
+import java.time.Clock;
+import java.time.ZonedDateTime;
+import java.util.Optional;
+import java.util.Set;
+
+import javax.inject.Inject;
+
+import org.apache.james.core.Username;
+import org.apache.james.jmap.api.model.DeviceClientIdInvalidException;
+import org.apache.james.jmap.api.model.ExpireTimeInvalidException;
+import org.apache.james.jmap.api.model.PushSubscription;
+import org.apache.james.jmap.api.model.PushSubscriptionCreationRequest;
+import org.apache.james.jmap.api.model.PushSubscriptionExpiredTime;
+import org.apache.james.jmap.api.model.PushSubscriptionId;
+import org.apache.james.jmap.api.model.PushSubscriptionNotFoundException;
+import org.apache.james.jmap.api.model.TypeName;
+import org.apache.james.jmap.api.pushsubscription.PushSubscriptionRepository;
+import org.reactivestreams.Publisher;
+
+import reactor.core.publisher.Mono;
+import scala.Option;
+import scala.jdk.javaapi.CollectionConverters;
+import scala.jdk.javaapi.OptionConverters;
+
+public class CassandraPushSubscriptionRepository implements PushSubscriptionRepository {
+    private final CassandraPushSubscriptionDAO dao;
+    private final Clock clock;
+
+    @Inject
+    public CassandraPushSubscriptionRepository(CassandraPushSubscriptionDAO dao, Clock clock) {
+        this.dao = dao;
+        this.clock = clock;
+    }
+
+    @Override
+    public Publisher<PushSubscription> save(Username username, PushSubscriptionCreationRequest request) {
+        return Mono.just(request)
+            .handle((req, sink) -> {
+                if (isInThePast(req.expires())) {
+                    sink.error(new ExpireTimeInvalidException(req.expires().get().value(), "expires must be greater than now"));
+                }
+                if (!isUniqueDeviceClientId(username, req.deviceClientId())) {
+                    sink.error(new DeviceClientIdInvalidException(req.deviceClientId(), "deviceClientId must be unique"));
+                }
+            })
+            .thenReturn(PushSubscription.from(request,
+                evaluateExpiresTime(OptionConverters.toJava(request.expires().map(PushSubscriptionExpiredTime::value)))))
+            .doOnNext(subscription -> dao.insert(username, subscription));
+    }
+
+    @Override
+    public Publisher<Void> updateExpireTime(Username username, PushSubscriptionId id, ZonedDateTime newExpire) {
+        return Mono.just(newExpire)
+            .handle((inputTime, sink) -> {
+                if (newExpire.isBefore(ZonedDateTime.now(clock))) {
+                    sink.error(new ExpireTimeInvalidException(inputTime, "expires must be greater than now"));
+                }
+            })
+            .then(Mono.from(dao.selectAll(username).filter(subscription -> subscription.id().equals(id)))
+                .doOnNext(subscription -> dao.insert(username, subscription.withExpires(evaluateExpiresTime(Optional.of(newExpire)))))
+                .switchIfEmpty(Mono.error(() -> new PushSubscriptionNotFoundException(id)))
+                .then());
+    }
+
+    @Override
+    public Publisher<Void> updateTypes(Username username, PushSubscriptionId id, Set<TypeName> types) {
+        return Mono.from(dao.selectAll(username).filter(subscription -> subscription.id().equals(id)))
+            .doOnNext(subscription -> subscription.withTypes(CollectionConverters.asScala(types).toSeq()))
+            .flatMap(newPushSubscription -> dao.insert(username, newPushSubscription))
+            .switchIfEmpty(Mono.error(() -> new PushSubscriptionNotFoundException(id)));
+    }
+
+    @Override
+    public Publisher<Void> validateVerificationCode(Username username, PushSubscriptionId id) {
+        return Mono.from(dao.selectAll(username).filter(subscription -> subscription.id().equals(id)))
+            .doOnNext(PushSubscription::validated)

Review comment:
       ```suggestion
               .map(PushSubscription::validated)
   ```

##########
File path: server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/change/TypeStateFactoryTest.scala
##########
@@ -28,7 +30,7 @@ import scala.jdk.CollectionConverters._
 
 class TypeStateFactoryTest {
   val ALL: Set[TypeName] = Set(EmailTypeName, MailboxTypeName, ThreadTypeName, IdentityTypeName, EmailSubmissionTypeName, EmailDeliveryTypeName, VacationResponseTypeName)
-  val factory: TypeStateFactory = TypeStateFactory(ALL.asJava)
+  val factory: TypeStateFactory = change.TypeStateFactory(ALL.asJava)

Review comment:
       Oups

##########
File path: server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/pushsubscription/CassandraPushSubscriptionRepository.java
##########
@@ -0,0 +1,144 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.cassandra.pushsubscription;
+
+import static org.apache.james.jmap.api.model.PushSubscription.EXPIRES_TIME_MAX_DAY;
+
+import java.time.Clock;
+import java.time.ZonedDateTime;
+import java.util.Optional;
+import java.util.Set;
+
+import javax.inject.Inject;
+
+import org.apache.james.core.Username;
+import org.apache.james.jmap.api.model.DeviceClientIdInvalidException;
+import org.apache.james.jmap.api.model.ExpireTimeInvalidException;
+import org.apache.james.jmap.api.model.PushSubscription;
+import org.apache.james.jmap.api.model.PushSubscriptionCreationRequest;
+import org.apache.james.jmap.api.model.PushSubscriptionExpiredTime;
+import org.apache.james.jmap.api.model.PushSubscriptionId;
+import org.apache.james.jmap.api.model.PushSubscriptionNotFoundException;
+import org.apache.james.jmap.api.model.TypeName;
+import org.apache.james.jmap.api.pushsubscription.PushSubscriptionRepository;
+import org.reactivestreams.Publisher;
+
+import reactor.core.publisher.Mono;
+import scala.Option;
+import scala.jdk.javaapi.CollectionConverters;
+import scala.jdk.javaapi.OptionConverters;
+
+public class CassandraPushSubscriptionRepository implements PushSubscriptionRepository {
+    private final CassandraPushSubscriptionDAO dao;
+    private final Clock clock;
+
+    @Inject
+    public CassandraPushSubscriptionRepository(CassandraPushSubscriptionDAO dao, Clock clock) {
+        this.dao = dao;
+        this.clock = clock;
+    }
+
+    @Override
+    public Publisher<PushSubscription> save(Username username, PushSubscriptionCreationRequest request) {
+        return Mono.just(request)
+            .handle((req, sink) -> {
+                if (isInThePast(req.expires())) {
+                    sink.error(new ExpireTimeInvalidException(req.expires().get().value(), "expires must be greater than now"));
+                }
+                if (!isUniqueDeviceClientId(username, req.deviceClientId())) {
+                    sink.error(new DeviceClientIdInvalidException(req.deviceClientId(), "deviceClientId must be unique"));
+                }
+            })
+            .thenReturn(PushSubscription.from(request,
+                evaluateExpiresTime(OptionConverters.toJava(request.expires().map(PushSubscriptionExpiredTime::value)))))
+            .doOnNext(subscription -> dao.insert(username, subscription));
+    }
+
+    @Override
+    public Publisher<Void> updateExpireTime(Username username, PushSubscriptionId id, ZonedDateTime newExpire) {
+        return Mono.just(newExpire)
+            .handle((inputTime, sink) -> {
+                if (newExpire.isBefore(ZonedDateTime.now(clock))) {
+                    sink.error(new ExpireTimeInvalidException(inputTime, "expires must be greater than now"));
+                }
+            })
+            .then(Mono.from(dao.selectAll(username).filter(subscription -> subscription.id().equals(id)))
+                .doOnNext(subscription -> dao.insert(username, subscription.withExpires(evaluateExpiresTime(Optional.of(newExpire)))))
+                .switchIfEmpty(Mono.error(() -> new PushSubscriptionNotFoundException(id)))
+                .then());
+    }
+
+    @Override
+    public Publisher<Void> updateTypes(Username username, PushSubscriptionId id, Set<TypeName> types) {
+        return Mono.from(dao.selectAll(username).filter(subscription -> subscription.id().equals(id)))
+            .doOnNext(subscription -> subscription.withTypes(CollectionConverters.asScala(types).toSeq()))

Review comment:
       ```suggestion
               .map(subscription -> subscription.withTypes(CollectionConverters.asScala(types).toSeq()))
   ```

##########
File path: server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/pushsubscription/CassandraPushSubscriptionRepository.java
##########
@@ -0,0 +1,144 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.cassandra.pushsubscription;
+
+import static org.apache.james.jmap.api.model.PushSubscription.EXPIRES_TIME_MAX_DAY;
+
+import java.time.Clock;
+import java.time.ZonedDateTime;
+import java.util.Optional;
+import java.util.Set;
+
+import javax.inject.Inject;
+
+import org.apache.james.core.Username;
+import org.apache.james.jmap.api.model.DeviceClientIdInvalidException;
+import org.apache.james.jmap.api.model.ExpireTimeInvalidException;
+import org.apache.james.jmap.api.model.PushSubscription;
+import org.apache.james.jmap.api.model.PushSubscriptionCreationRequest;
+import org.apache.james.jmap.api.model.PushSubscriptionExpiredTime;
+import org.apache.james.jmap.api.model.PushSubscriptionId;
+import org.apache.james.jmap.api.model.PushSubscriptionNotFoundException;
+import org.apache.james.jmap.api.model.TypeName;
+import org.apache.james.jmap.api.pushsubscription.PushSubscriptionRepository;
+import org.reactivestreams.Publisher;
+
+import reactor.core.publisher.Mono;
+import scala.Option;
+import scala.jdk.javaapi.CollectionConverters;
+import scala.jdk.javaapi.OptionConverters;
+
+public class CassandraPushSubscriptionRepository implements PushSubscriptionRepository {
+    private final CassandraPushSubscriptionDAO dao;
+    private final Clock clock;
+
+    @Inject
+    public CassandraPushSubscriptionRepository(CassandraPushSubscriptionDAO dao, Clock clock) {
+        this.dao = dao;
+        this.clock = clock;
+    }
+
+    @Override
+    public Publisher<PushSubscription> save(Username username, PushSubscriptionCreationRequest request) {
+        return Mono.just(request)
+            .handle((req, sink) -> {
+                if (isInThePast(req.expires())) {
+                    sink.error(new ExpireTimeInvalidException(req.expires().get().value(), "expires must be greater than now"));
+                }
+                if (!isUniqueDeviceClientId(username, req.deviceClientId())) {
+                    sink.error(new DeviceClientIdInvalidException(req.deviceClientId(), "deviceClientId must be unique"));
+                }
+            })
+            .thenReturn(PushSubscription.from(request,
+                evaluateExpiresTime(OptionConverters.toJava(request.expires().map(PushSubscriptionExpiredTime::value)))))
+            .doOnNext(subscription -> dao.insert(username, subscription));
+    }
+
+    @Override
+    public Publisher<Void> updateExpireTime(Username username, PushSubscriptionId id, ZonedDateTime newExpire) {
+        return Mono.just(newExpire)
+            .handle((inputTime, sink) -> {
+                if (newExpire.isBefore(ZonedDateTime.now(clock))) {
+                    sink.error(new ExpireTimeInvalidException(inputTime, "expires must be greater than now"));
+                }
+            })
+            .then(Mono.from(dao.selectAll(username).filter(subscription -> subscription.id().equals(id)))
+                .doOnNext(subscription -> dao.insert(username, subscription.withExpires(evaluateExpiresTime(Optional.of(newExpire)))))
+                .switchIfEmpty(Mono.error(() -> new PushSubscriptionNotFoundException(id)))
+                .then());

Review comment:
       ```suggestion
               .then(Mono.from(dao.selectAll(username)
                    .filter(subscription -> subscription.id().equals(id)))
                   .flatMap(subscription -> dao.insert(username, subscription.withExpires(evaluateExpiresTime(Optional.of(newExpire)))))
                   .switchIfEmpty(Mono.error(() -> new PushSubscriptionNotFoundException(id)))
                   .then());
   ```
   
   flatMap vs doOnNext
   
   Also line breaks makes it more readable

##########
File path: server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/pushsubscription/CassandraPushSubscriptionDAO.java
##########
@@ -0,0 +1,175 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.cassandra.pushsubscription;
+
+import static com.datastax.driver.core.querybuilder.QueryBuilder.bindMarker;
+import static com.datastax.driver.core.querybuilder.QueryBuilder.delete;
+import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;
+import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto;
+import static com.datastax.driver.core.querybuilder.QueryBuilder.select;
+import static org.apache.james.jmap.cassandra.pushsubscription.tables.CassandraPushSubscriptionTable.DEVICE_CLIENT_ID;
+import static org.apache.james.jmap.cassandra.pushsubscription.tables.CassandraPushSubscriptionTable.ENCRYPT_AUTH_SECRET;
+import static org.apache.james.jmap.cassandra.pushsubscription.tables.CassandraPushSubscriptionTable.ENCRYPT_PUBLIC_KEY;
+import static org.apache.james.jmap.cassandra.pushsubscription.tables.CassandraPushSubscriptionTable.EXPIRES;
+import static org.apache.james.jmap.cassandra.pushsubscription.tables.CassandraPushSubscriptionTable.ID;
+import static org.apache.james.jmap.cassandra.pushsubscription.tables.CassandraPushSubscriptionTable.TABLE_NAME;
+import static org.apache.james.jmap.cassandra.pushsubscription.tables.CassandraPushSubscriptionTable.TYPES;
+import static org.apache.james.jmap.cassandra.pushsubscription.tables.CassandraPushSubscriptionTable.URL;
+import static org.apache.james.jmap.cassandra.pushsubscription.tables.CassandraPushSubscriptionTable.USER;
+import static org.apache.james.jmap.cassandra.pushsubscription.tables.CassandraPushSubscriptionTable.VALIDATED;
+import static org.apache.james.jmap.cassandra.pushsubscription.tables.CassandraPushSubscriptionTable.VERIFICATION_CODE;
+import static org.apache.james.jmap.cassandra.pushsubscription.tables.CassandraPushSubscriptionTable.ZONE;
+
+import java.time.ZoneId;
+import java.time.ZonedDateTime;
+import java.util.Date;
+import java.util.Set;
+
+import javax.inject.Inject;
+
+import org.apache.james.backends.cassandra.utils.CassandraAsyncExecutor;
+import org.apache.james.core.Username;
+import org.apache.james.jmap.api.change.TypeStateFactory;
+import org.apache.james.jmap.api.model.DeviceClientId;
+import org.apache.james.jmap.api.model.PushSubscription;
+import org.apache.james.jmap.api.model.PushSubscriptionExpiredTime;
+import org.apache.james.jmap.api.model.PushSubscriptionId;
+import org.apache.james.jmap.api.model.PushSubscriptionKeys;
+import org.apache.james.jmap.api.model.PushSubscriptionServerURL;
+import org.apache.james.jmap.api.model.TypeName;
+import org.apache.james.jmap.api.model.VerificationCode;
+
+import com.datastax.driver.core.BoundStatement;
+import com.datastax.driver.core.PreparedStatement;
+import com.datastax.driver.core.Row;
+import com.datastax.driver.core.Session;
+import com.google.common.collect.ImmutableSet;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+import scala.Option;
+import scala.Some;
+import scala.collection.immutable.Seq;
+import scala.jdk.javaapi.CollectionConverters;
+
+public class CassandraPushSubscriptionDAO {
+    private final TypeStateFactory typeStateFactory;
+    private final CassandraAsyncExecutor executor;
+    private final PreparedStatement insert;
+    private final PreparedStatement selectAll;
+    private final PreparedStatement deleteOne;
+
+    @Inject
+    public CassandraPushSubscriptionDAO(Session session, TypeStateFactory typeStateFactory) {
+        executor = new CassandraAsyncExecutor(session);
+
+        insert = session.prepare(insertInto(TABLE_NAME)
+            .value(USER, bindMarker(USER))
+            .value(DEVICE_CLIENT_ID, bindMarker(DEVICE_CLIENT_ID))
+            .value(ID, bindMarker(ID))
+            .value(EXPIRES, bindMarker(EXPIRES))
+            .value(ZONE, bindMarker(ZONE))
+            .value(TYPES, bindMarker(TYPES))
+            .value(URL, bindMarker(URL))
+            .value(VERIFICATION_CODE, bindMarker(VERIFICATION_CODE))
+            .value(ENCRYPT_PUBLIC_KEY, bindMarker(ENCRYPT_PUBLIC_KEY))
+            .value(ENCRYPT_AUTH_SECRET, bindMarker(ENCRYPT_AUTH_SECRET))
+            .value(VALIDATED, bindMarker(VALIDATED)));
+
+        selectAll = session.prepare(select()
+            .from(TABLE_NAME)
+            .where(eq(USER, bindMarker(USER))));
+
+        deleteOne = session.prepare(delete()
+            .from(TABLE_NAME)
+            .where(eq(USER, bindMarker(USER)))
+            .and(eq(DEVICE_CLIENT_ID, bindMarker(DEVICE_CLIENT_ID))));
+
+        this.typeStateFactory = typeStateFactory;
+    }
+
+    public Mono<Void> insert(Username username, PushSubscription subscription) {
+        Set<String> typeNames = CollectionConverters.asJava(subscription.types()
+            .map(TypeName::asString)
+            .toSet());
+
+        BoundStatement insertSubscription = insert.bind()
+            .setString(USER, username.asString())
+            .setString(DEVICE_CLIENT_ID, subscription.deviceClientId())
+            .setUUID(ID, subscription.id().value())
+            .setTimestamp(EXPIRES, Date.from(subscription.expires().value().toInstant()))
+            .setString(ZONE, subscription.expires().value().getZone().toString())
+            .setSet(TYPES, typeNames)
+            .setString(URL, subscription.url().value().toString())
+            .setString(VERIFICATION_CODE, subscription.verificationCode())
+            .setBool(VALIDATED, subscription.validated());
+        if (subscription.keys().isDefined()) {
+            insertSubscription.setString(ENCRYPT_PUBLIC_KEY, subscription.keys().get().p256dh())
+                .setString(ENCRYPT_AUTH_SECRET, subscription.keys().get().auth());
+        }
+
+        return executor.executeVoid(insertSubscription);
+    }
+
+    public Flux<PushSubscription> selectAll(Username username) {
+        return executor.executeRows(selectAll.bind().setString(USER, username.asString()))
+            .map(this::toPushSubscription);
+    }
+
+    public Mono<Void> deleteOne(Username username, String deviceClientId) {
+        return executor.executeVoid(deleteOne.bind()
+            .setString(USER, username.asString())
+            .setString(DEVICE_CLIENT_ID, deviceClientId));
+    }
+
+    private PushSubscription toPushSubscription(Row row) {
+        return PushSubscription.apply(
+            PushSubscriptionId.apply(row.getUUID(ID)),
+            DeviceClientId.apply(row.getString(DEVICE_CLIENT_ID)),
+            PushSubscriptionServerURL.from(row.getString(URL)).get(),
+            toKeys(row),
+            VerificationCode.apply(row.getString(VERIFICATION_CODE)),
+            row.getBool(VALIDATED),
+            toExpires(row),
+            toTypes(row));
+    }
+
+    private Option<PushSubscriptionKeys> toKeys(Row row) {
+        String p256dh = row.getString(ENCRYPT_PUBLIC_KEY);
+        String auth = row.getString(ENCRYPT_AUTH_SECRET);
+        if (!p256dh.isEmpty() && !auth.isEmpty()) {
+            return Some.apply(PushSubscriptionKeys.apply(p256dh, auth));
+        } else {
+            return Option.empty();
+        }
+    }

Review comment:
       Do we have tests for storing subscription...
   
    - [ ] with keys?
    - [ ]  Without keys?
    - [ ] With a key with an empty auth?
    - [ ] With a key with an empty p256dh ?
   
   Can you add those tests with dummy values in the contract?

##########
File path: server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/change/StateChangeEventSerializerTest.scala
##########
@@ -83,7 +85,7 @@ object StateChangeEventSerializerTest {
 
 class StateChangeEventSerializerTest {
   val typeNameSet: Set[TypeName] = Set(EmailTypeName, MailboxTypeName, ThreadTypeName, IdentityTypeName, EmailSubmissionTypeName, EmailDeliveryTypeName, VacationResponseTypeName)
-  val typeStateFactory: TypeStateFactory = TypeStateFactory(typeNameSet.asJava)
+  val typeStateFactory: TypeStateFactory = change.TypeStateFactory(typeNameSet.asJava)

Review comment:
       Oups

##########
File path: server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/pushsubscription/CassandraPushSubscriptionRepository.java
##########
@@ -0,0 +1,144 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.cassandra.pushsubscription;
+
+import static org.apache.james.jmap.api.model.PushSubscription.EXPIRES_TIME_MAX_DAY;
+
+import java.time.Clock;
+import java.time.ZonedDateTime;
+import java.util.Optional;
+import java.util.Set;
+
+import javax.inject.Inject;
+
+import org.apache.james.core.Username;
+import org.apache.james.jmap.api.model.DeviceClientIdInvalidException;
+import org.apache.james.jmap.api.model.ExpireTimeInvalidException;
+import org.apache.james.jmap.api.model.PushSubscription;
+import org.apache.james.jmap.api.model.PushSubscriptionCreationRequest;
+import org.apache.james.jmap.api.model.PushSubscriptionExpiredTime;
+import org.apache.james.jmap.api.model.PushSubscriptionId;
+import org.apache.james.jmap.api.model.PushSubscriptionNotFoundException;
+import org.apache.james.jmap.api.model.TypeName;
+import org.apache.james.jmap.api.pushsubscription.PushSubscriptionRepository;
+import org.reactivestreams.Publisher;
+
+import reactor.core.publisher.Mono;
+import scala.Option;
+import scala.jdk.javaapi.CollectionConverters;
+import scala.jdk.javaapi.OptionConverters;
+
+public class CassandraPushSubscriptionRepository implements PushSubscriptionRepository {
+    private final CassandraPushSubscriptionDAO dao;
+    private final Clock clock;
+
+    @Inject
+    public CassandraPushSubscriptionRepository(CassandraPushSubscriptionDAO dao, Clock clock) {
+        this.dao = dao;
+        this.clock = clock;
+    }
+
+    @Override
+    public Publisher<PushSubscription> save(Username username, PushSubscriptionCreationRequest request) {
+        return Mono.just(request)
+            .handle((req, sink) -> {
+                if (isInThePast(req.expires())) {
+                    sink.error(new ExpireTimeInvalidException(req.expires().get().value(), "expires must be greater than now"));
+                }
+                if (!isUniqueDeviceClientId(username, req.deviceClientId())) {
+                    sink.error(new DeviceClientIdInvalidException(req.deviceClientId(), "deviceClientId must be unique"));
+                }
+            })
+            .thenReturn(PushSubscription.from(request,
+                evaluateExpiresTime(OptionConverters.toJava(request.expires().map(PushSubscriptionExpiredTime::value)))))
+            .doOnNext(subscription -> dao.insert(username, subscription));

Review comment:
       ```suggestion
               .flatMap(subscription -> dao.insert(username, subscription));
   ```
   
   Overwize you loose the Mono...

##########
File path: server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/pushsubscription/CassandraPushSubscriptionRepository.java
##########
@@ -0,0 +1,144 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.cassandra.pushsubscription;
+
+import static org.apache.james.jmap.api.model.PushSubscription.EXPIRES_TIME_MAX_DAY;
+
+import java.time.Clock;
+import java.time.ZonedDateTime;
+import java.util.Optional;
+import java.util.Set;
+
+import javax.inject.Inject;
+
+import org.apache.james.core.Username;
+import org.apache.james.jmap.api.model.DeviceClientIdInvalidException;
+import org.apache.james.jmap.api.model.ExpireTimeInvalidException;
+import org.apache.james.jmap.api.model.PushSubscription;
+import org.apache.james.jmap.api.model.PushSubscriptionCreationRequest;
+import org.apache.james.jmap.api.model.PushSubscriptionExpiredTime;
+import org.apache.james.jmap.api.model.PushSubscriptionId;
+import org.apache.james.jmap.api.model.PushSubscriptionNotFoundException;
+import org.apache.james.jmap.api.model.TypeName;
+import org.apache.james.jmap.api.pushsubscription.PushSubscriptionRepository;
+import org.reactivestreams.Publisher;
+
+import reactor.core.publisher.Mono;
+import scala.Option;
+import scala.jdk.javaapi.CollectionConverters;
+import scala.jdk.javaapi.OptionConverters;
+
+public class CassandraPushSubscriptionRepository implements PushSubscriptionRepository {
+    private final CassandraPushSubscriptionDAO dao;
+    private final Clock clock;
+
+    @Inject
+    public CassandraPushSubscriptionRepository(CassandraPushSubscriptionDAO dao, Clock clock) {
+        this.dao = dao;
+        this.clock = clock;
+    }
+
+    @Override
+    public Publisher<PushSubscription> save(Username username, PushSubscriptionCreationRequest request) {
+        return Mono.just(request)
+            .handle((req, sink) -> {
+                if (isInThePast(req.expires())) {
+                    sink.error(new ExpireTimeInvalidException(req.expires().get().value(), "expires must be greater than now"));
+                }
+                if (!isUniqueDeviceClientId(username, req.deviceClientId())) {
+                    sink.error(new DeviceClientIdInvalidException(req.deviceClientId(), "deviceClientId must be unique"));
+                }
+            })
+            .thenReturn(PushSubscription.from(request,
+                evaluateExpiresTime(OptionConverters.toJava(request.expires().map(PushSubscriptionExpiredTime::value)))))
+            .doOnNext(subscription -> dao.insert(username, subscription));
+    }
+
+    @Override
+    public Publisher<Void> updateExpireTime(Username username, PushSubscriptionId id, ZonedDateTime newExpire) {
+        return Mono.just(newExpire)
+            .handle((inputTime, sink) -> {
+                if (newExpire.isBefore(ZonedDateTime.now(clock))) {
+                    sink.error(new ExpireTimeInvalidException(inputTime, "expires must be greater than now"));
+                }
+            })
+            .then(Mono.from(dao.selectAll(username).filter(subscription -> subscription.id().equals(id)))
+                .doOnNext(subscription -> dao.insert(username, subscription.withExpires(evaluateExpiresTime(Optional.of(newExpire)))))
+                .switchIfEmpty(Mono.error(() -> new PushSubscriptionNotFoundException(id)))
+                .then());
+    }
+
+    @Override
+    public Publisher<Void> updateTypes(Username username, PushSubscriptionId id, Set<TypeName> types) {
+        return Mono.from(dao.selectAll(username).filter(subscription -> subscription.id().equals(id)))
+            .doOnNext(subscription -> subscription.withTypes(CollectionConverters.asScala(types).toSeq()))
+            .flatMap(newPushSubscription -> dao.insert(username, newPushSubscription))
+            .switchIfEmpty(Mono.error(() -> new PushSubscriptionNotFoundException(id)));
+    }
+
+    @Override
+    public Publisher<Void> validateVerificationCode(Username username, PushSubscriptionId id) {
+        return Mono.from(dao.selectAll(username).filter(subscription -> subscription.id().equals(id)))
+            .doOnNext(PushSubscription::validated)
+            .flatMap(newPushSubscription -> dao.insert(username, newPushSubscription))
+            .switchIfEmpty(Mono.error(() -> new PushSubscriptionNotFoundException(id)));
+    }
+
+    @Override
+    public Publisher<Void> revoke(Username username, PushSubscriptionId id) {
+        return Mono.from(dao.selectAll(username).filter(subscription -> subscription.id().equals(id)))
+            .flatMap(subscription -> dao.deleteOne(username, subscription.deviceClientId()))

Review comment:
       ```
   dao.selectAll(username).filter(subscription -> subscription.id().equals(id))
   ```
   
   This piece of code is duplicated a lot. Can we share it via a `retrieveById` method?

##########
File path: server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/pushsubscription/CassandraPushSubscriptionDAO.java
##########
@@ -0,0 +1,175 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.cassandra.pushsubscription;
+
+import static com.datastax.driver.core.querybuilder.QueryBuilder.bindMarker;
+import static com.datastax.driver.core.querybuilder.QueryBuilder.delete;
+import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;
+import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto;
+import static com.datastax.driver.core.querybuilder.QueryBuilder.select;
+import static org.apache.james.jmap.cassandra.pushsubscription.tables.CassandraPushSubscriptionTable.DEVICE_CLIENT_ID;
+import static org.apache.james.jmap.cassandra.pushsubscription.tables.CassandraPushSubscriptionTable.ENCRYPT_AUTH_SECRET;
+import static org.apache.james.jmap.cassandra.pushsubscription.tables.CassandraPushSubscriptionTable.ENCRYPT_PUBLIC_KEY;
+import static org.apache.james.jmap.cassandra.pushsubscription.tables.CassandraPushSubscriptionTable.EXPIRES;
+import static org.apache.james.jmap.cassandra.pushsubscription.tables.CassandraPushSubscriptionTable.ID;
+import static org.apache.james.jmap.cassandra.pushsubscription.tables.CassandraPushSubscriptionTable.TABLE_NAME;
+import static org.apache.james.jmap.cassandra.pushsubscription.tables.CassandraPushSubscriptionTable.TYPES;
+import static org.apache.james.jmap.cassandra.pushsubscription.tables.CassandraPushSubscriptionTable.URL;
+import static org.apache.james.jmap.cassandra.pushsubscription.tables.CassandraPushSubscriptionTable.USER;
+import static org.apache.james.jmap.cassandra.pushsubscription.tables.CassandraPushSubscriptionTable.VALIDATED;
+import static org.apache.james.jmap.cassandra.pushsubscription.tables.CassandraPushSubscriptionTable.VERIFICATION_CODE;
+import static org.apache.james.jmap.cassandra.pushsubscription.tables.CassandraPushSubscriptionTable.ZONE;
+
+import java.time.ZoneId;
+import java.time.ZonedDateTime;
+import java.util.Date;
+import java.util.Set;
+
+import javax.inject.Inject;
+
+import org.apache.james.backends.cassandra.utils.CassandraAsyncExecutor;
+import org.apache.james.core.Username;
+import org.apache.james.jmap.api.change.TypeStateFactory;
+import org.apache.james.jmap.api.model.DeviceClientId;
+import org.apache.james.jmap.api.model.PushSubscription;
+import org.apache.james.jmap.api.model.PushSubscriptionExpiredTime;
+import org.apache.james.jmap.api.model.PushSubscriptionId;
+import org.apache.james.jmap.api.model.PushSubscriptionKeys;
+import org.apache.james.jmap.api.model.PushSubscriptionServerURL;
+import org.apache.james.jmap.api.model.TypeName;
+import org.apache.james.jmap.api.model.VerificationCode;
+
+import com.datastax.driver.core.BoundStatement;
+import com.datastax.driver.core.PreparedStatement;
+import com.datastax.driver.core.Row;
+import com.datastax.driver.core.Session;
+import com.google.common.collect.ImmutableSet;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+import scala.Option;
+import scala.Some;
+import scala.collection.immutable.Seq;
+import scala.jdk.javaapi.CollectionConverters;
+
+public class CassandraPushSubscriptionDAO {
+    private final TypeStateFactory typeStateFactory;
+    private final CassandraAsyncExecutor executor;
+    private final PreparedStatement insert;
+    private final PreparedStatement selectAll;
+    private final PreparedStatement deleteOne;
+
+    @Inject
+    public CassandraPushSubscriptionDAO(Session session, TypeStateFactory typeStateFactory) {
+        executor = new CassandraAsyncExecutor(session);
+
+        insert = session.prepare(insertInto(TABLE_NAME)
+            .value(USER, bindMarker(USER))
+            .value(DEVICE_CLIENT_ID, bindMarker(DEVICE_CLIENT_ID))
+            .value(ID, bindMarker(ID))
+            .value(EXPIRES, bindMarker(EXPIRES))
+            .value(ZONE, bindMarker(ZONE))
+            .value(TYPES, bindMarker(TYPES))
+            .value(URL, bindMarker(URL))
+            .value(VERIFICATION_CODE, bindMarker(VERIFICATION_CODE))
+            .value(ENCRYPT_PUBLIC_KEY, bindMarker(ENCRYPT_PUBLIC_KEY))
+            .value(ENCRYPT_AUTH_SECRET, bindMarker(ENCRYPT_AUTH_SECRET))
+            .value(VALIDATED, bindMarker(VALIDATED)));
+
+        selectAll = session.prepare(select()
+            .from(TABLE_NAME)
+            .where(eq(USER, bindMarker(USER))));
+
+        deleteOne = session.prepare(delete()
+            .from(TABLE_NAME)
+            .where(eq(USER, bindMarker(USER)))
+            .and(eq(DEVICE_CLIENT_ID, bindMarker(DEVICE_CLIENT_ID))));
+
+        this.typeStateFactory = typeStateFactory;
+    }
+
+    public Mono<Void> insert(Username username, PushSubscription subscription) {
+        Set<String> typeNames = CollectionConverters.asJava(subscription.types()
+            .map(TypeName::asString)
+            .toSet());
+
+        BoundStatement insertSubscription = insert.bind()
+            .setString(USER, username.asString())
+            .setString(DEVICE_CLIENT_ID, subscription.deviceClientId())
+            .setUUID(ID, subscription.id().value())
+            .setTimestamp(EXPIRES, Date.from(subscription.expires().value().toInstant()))
+            .setString(ZONE, subscription.expires().value().getZone().toString())
+            .setSet(TYPES, typeNames)
+            .setString(URL, subscription.url().value().toString())
+            .setString(VERIFICATION_CODE, subscription.verificationCode())
+            .setBool(VALIDATED, subscription.validated());
+        if (subscription.keys().isDefined()) {
+            insertSubscription.setString(ENCRYPT_PUBLIC_KEY, subscription.keys().get().p256dh())
+                .setString(ENCRYPT_AUTH_SECRET, subscription.keys().get().auth());
+        }

Review comment:
       Please avoid chaining `.isDefined()` with `.get()`. Use .map + orElse to apply a fully functional style.
   
   You can convert it to a java option if you wish.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org