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



##########
File path: 
server/data/data-jmap-cassandra/src/main/java/org/apache/james/jmap/cassandra/pushsubscription/tables/CassandraPushSubscriptionTable.java
##########
@@ -0,0 +1,35 @@
+/****************************************************************
+ * 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.tables;
+
+public interface CassandraPushSubscriptionTable {
+    String TABLE_NAME = "push_subscription";
+    String USER = "user";
+    String DEVICE_CLIENT_ID = "device_client_id";
+    String ID = "id";
+    String EXPIRES = "expires";
+    String ZONE = "zone";

Review comment:
       `EXPIRES_TIME_ZONE` more clear?
   (When the first I read the source code, I have a bit confused with "zone" 
name)

##########
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())

Review comment:
       QA: 
   Cassandra doesn't support the `Instant` type?

##########
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()))
+            .switchIfEmpty(Mono.empty());
+    }
+
+    @Override
+    public Publisher<PushSubscription> get(Username username, 
Set<PushSubscriptionId> ids) {
+        return dao.selectAll(username)
+            .filter(subscription -> ids.contains(subscription.id()));
+    }
+
+    @Override
+    public Publisher<PushSubscription> list(Username username) {
+        return dao.selectAll(username);
+    }
+
+    private PushSubscriptionExpiredTime 
evaluateExpiresTime(Optional<ZonedDateTime> inputTime) {

Review comment:
       Should we move it in `PushSubscriptionRepository` or another for reuse 
in both `cassandra` and `inmemory` implements?
   Avoid the duplicate code




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to