Commit 36ac1c3a authored by Benoit Lavenier's avatar Benoit Lavenier

[enh] Like API: add level and comment, when notify user

parent 1bf1a9b2
Pipeline #7722 failed
......@@ -53,7 +53,8 @@ public enum UserEventCodes {
INVITATION_TO_CERTIFY,
// Like
LIKE,
DISLIKE,
ABUSE
LIKE_RECEIVED,
STAR__RECEIVED,
ABUSE_RECEIVED,
MODERATION_RECEIVED
}
......@@ -25,6 +25,7 @@ package org.duniter.elasticsearch.user.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import org.duniter.core.client.model.ModelUtils;
import org.duniter.core.client.model.elasticsearch.Record;
import org.duniter.core.exception.TechnicalException;
import org.duniter.core.service.CryptoService;
......@@ -37,6 +38,8 @@ import org.duniter.elasticsearch.user.PluginSettings;
import org.duniter.elasticsearch.user.dao.profile.UserProfileDao;
import org.duniter.elasticsearch.user.model.LikeRecord;
import org.duniter.elasticsearch.user.model.UserEvent;
import org.duniter.elasticsearch.user.model.UserEventCodes;
import org.duniter.elasticsearch.user.model.UserProfile;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
......@@ -51,6 +54,8 @@ import org.elasticsearch.index.query.QueryBuilders;
import org.nuiton.i18n.I18n;
import java.io.IOException;
import java.util.Map;
import java.util.Optional;
/**
* Created by Benoit on 30/03/2015.
......@@ -60,6 +65,13 @@ public class LikeService extends AbstractService {
public static final String INDEX = "like";
public static final String RECORD_TYPE = "record";
static {
// Reserve i18n
I18n.n("duniter.user.event.MODERATION_RECEIVED");
I18n.n("duniter.user.event.LIKE_RECEIVED");
I18n.n("duniter.user.event.ABUSE_RECEIVED");
I18n.n("duniter.user.event.STAR_RECEIVED");
}
private final AdminService adminService;
private final UserEventService userEventService;
......@@ -314,36 +326,55 @@ public class LikeService extends AbstractService {
}
public void notifyOnInsert(JsonNode actualObj) {
String kind = getMandatoryField(actualObj,LikeRecord.PROPERTY_KIND).asText();
LikeRecord.Kind kindEnum = LikeRecord.Kind.valueOf(kind);
// If not need notification: skip
if (kindEnum == LikeRecord.Kind.DISLIKE || kindEnum == LikeRecord.Kind.VIEW) return;
UserEvent.EventType eventType = kindEnum == LikeRecord.Kind.ABUSE ? UserEvent.EventType.WARN : UserEvent.EventType.INFO;
String index = getMandatoryField(actualObj, LikeRecord.PROPERTY_INDEX).asText();
String type = getMandatoryField(actualObj,LikeRecord.PROPERTY_TYPE).asText();
String id = getMandatoryField(actualObj,LikeRecord.PROPERTY_ID).asText();
String kind = getMandatoryField(actualObj,LikeRecord.PROPERTY_KIND).asText();
Long time = getMandatoryField(actualObj,LikeRecord.PROPERTY_TIME).asLong();
String anchor = getOptionalField(actualObj, LikeRecord.PROPERTY_ANCHOR).map(JsonNode::asText).orElse(null);
String issuer = getMandatoryField(actualObj,LikeRecord.PROPERTY_ISSUER).asText();
String level = getOptionalField(actualObj,LikeRecord.PROPERTY_LEVEL).map(JsonNode::asText).orElse(null);
LikeRecord.Kind kindEnum = LikeRecord.Kind.valueOf(kind);
UserEvent.EventType eventType = kindEnum == LikeRecord.Kind.ABUSE ? UserEvent.EventType.WARN : UserEvent.EventType.INFO;
UserEvent event = UserEvent.newBuilder(eventType, kindEnum.toString())
.setMessage(I18n.n("duniter.user.event." + kindEnum.toString()))
.setTime(time)
.setReference(index, type, id)
.setReferenceAnchor(anchor)
.build();
// Load some fields from the original document
Map<String, Object> docFields = docFields = client.getFieldsById(index, type, id, Record.PROPERTY_ISSUER, UserProfile.PROPERTY_TITLE);
String docIssuer = String.valueOf(docFields.get(Record.PROPERTY_ISSUER));
String docTitle = Optional.ofNullable(docFields.get(UserProfile.PROPERTY_TITLE)).orElse("?").toString();
// Notify admin if abuse
if (kindEnum == LikeRecord.Kind.ABUSE) {
adminService.notifyAdmin(event);
String comment = getMandatoryField(actualObj,LikeRecord.PROPERTY_COMMENT).asText();
UserEvent adminEvent = UserEvent.newBuilder(eventType, UserEventCodes.MODERATION_RECEIVED.toString())
.setMessage(I18n.n("duniter.user.event.MODERATION_RECEIVED"),
// Message params
issuer, ModelUtils.minifyPubkey(issuer), docTitle, comment, level
)
.setTime(time)
.setReference(index, type, id)
.setReferenceAnchor(anchor)
.build();
adminService.notifyAdmin(adminEvent);
}
// Notify the issuer of the document
if (kindEnum == LikeRecord.Kind.LIKE || kindEnum == LikeRecord.Kind.ABUSE || kindEnum == LikeRecord.Kind.STAR) {
String issuer = client.getMandatoryTypedFieldById(index, type, id, Record.PROPERTY_ISSUER);
UserEvent userEvent = UserEvent.newBuilder(event)
.setRecipient(issuer)
.build();
userEventService.notifyUser(userEvent);
}
String eventCode = kindEnum.toString() + "_RECEIVED";
UserEvent userEvent = UserEvent.newBuilder(eventType,eventCode )
.setRecipient(docIssuer)
.setMessage(I18n.n("duniter.user.event." + eventCode, ModelUtils.minifyPubkey(issuer)),
// Message params
issuer, ModelUtils.minifyPubkey(issuer), docTitle, level)
.setTime(time)
.setReference(index, type, id)
.setReferenceAnchor(anchor)
.build();
userEventService.notifyUser(userEvent);
}
/* -- Internal methods -- */
......
duniter.admin.event.subject.ERROR=[%s] Error message
duniter.admin.event.subject.INFO=[%s] Information message
duniter.admin.event.subject.WARN=[%s] Warning message
duniter.user.event.ABUSE_RECEIVED=You have been the subject of an report.
duniter.user.event.CERT_RECEIVED=You have received a certification from %2$s.
duniter.user.event.CERT_SENT=Your certification to %2$s was executed.
duniter.user.event.INVITATION_TO_CERTIFY=%2$s invites you to certify an identity.
duniter.user.event.LIKE_RECEIVED=%2$s liked your content.
duniter.user.event.MEMBER_ACTIVE=Your membership to %1$s has been renewed successfully.
duniter.user.event.MEMBER_EXCLUDE=
duniter.user.event.MEMBER_EXCLUDE=Your membership as a member is no longer valid, for lack of non-renewal or for lack of certifications.
duniter.user.event.MEMBER_JOIN=You are now a member of currency %1$s\!
duniter.user.event.MEMBER_LEAVE=You are not a member anymore of currency %1$s\!
duniter.user.event.MEMBER_REVOKE=
duniter.user.event.MEMBER_REVOKE=Your member account has been revoked. He can no longer become a member.
duniter.user.event.MESSAGE_RECEIVED=You received a message from %2$s.
duniter.user.event.MODERATION_RECEIVED=You have been asked for moderation.
duniter.user.event.NODE_BMA_DOWN=Duniter node [%1$s\:%2$s] is DOWN\: no access from ES node [%3$s]. Last connexion at %4$s. Blockchain indexation waiting.
duniter.user.event.NODE_BMA_UP=Duniter node [%1$s\:%2$s] is UP again.
duniter.user.event.NODE_STARTED=Your node ES API [%1$s] is UP.
duniter.user.event.STAR_RECEIVED=%2$s has rated you.
duniter.user.event.TX_RECEIVED=You received a payment from %2$s.
duniter.user.event.TX_SENT=Your payment to %2$s was executed.
duniter.user.share.description=Follow your "libre money" wallets easily
......
duniter.admin.event.subject.ERROR=%s Message d'erreur
duniter.admin.event.subject.INFO=%s Message d'information
duniter.admin.event.subject.WARN=%s Message d'avertissement
duniter.user.event.ABUSE=Vous avez reçu un signalement.
duniter.user.event.ABUSE_RECEIVED=Vous avez fait l'objet d'un signalement.
duniter.user.event.CERT_RECEIVED=%2$s vous a certifié (certification prise en compte).
duniter.user.event.CERT_SENT=Votre certification de %2$s a été pris en compte.
duniter.user.event.INVITATION_TO_CERTIFY=%2$s vous invite à certifier une identité.
duniter.user.event.LIKE_RECEIVED=%2$s a aimé votre contenu.
duniter.user.event.MEMBER_ACTIVE=Votre adhésion comme membre a bien été renouvellée.
duniter.user.event.MEMBER_EXCLUDE=Votre adhésion comme membre n'est plus valide, faute de non renouvellement ou par manque de certifications.
duniter.user.event.MEMBER_JOIN=Vous êtes maintenant membre de la monnaie %1$s \!
duniter.user.event.MEMBER_LEAVE=Votre adhésion a pris fin, suite à votre demande.
duniter.user.event.MEMBER_REVOKE=Votre compte membre a été révoquée. Il ne pourra plus devenir membre.
duniter.user.event.MESSAGE_RECEIVED=Vous avez reçu un message de %2$s.
duniter.user.event.MODERATION_RECEIVED=Une demande de modération vous a été faite.
duniter.user.event.NODE_BMA_DOWN=Noeud Duniter [%1$s\:%2$s] non joignable, depuis le noeud ES API [%3$s]. Dernière connexion à %4$s. Indexation de blockchain en attente.
duniter.user.event.NODE_BMA_UP=Noeud Duniter [%1$s\:%2$s] à nouveau accessible.
duniter.user.event.NODE_STARTED=Noeud ES API [%1$s] est démarré.
duniter.user.event.STAR_RECEIVED=%2$s vous a noté.
duniter.user.event.TX_RECEIVED=Vous avez recu un paiement de %2$s.
duniter.user.event.TX_SENT=Votre paiement à %2$s a bien été executé.
duniter.user.share.description=Suivez vos comptes de monnaie libre en toute simplicité \!
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment