UserChannel.java

  1. /*
  2.  * The coLAB project
  3.  * Copyright (C) 2021-2023 AlbaSim, MEI, HEIG-VD, HES-SO
  4.  *
  5.  * Licensed under the MIT License
  6.  */
  7. package ch.colabproject.colab.api.ws.channel.model;

  8. import ch.colabproject.colab.api.Helper;
  9. import ch.colabproject.colab.api.model.user.User;
  10. import ch.colabproject.colab.generator.model.annotations.ExtractJavaDoc;
  11. import java.util.Objects;

  12. /**
  13.  * The channel to be used to transmit data to a specific user. Eg. its accounts, everything the user
  14.  * sees in its lobby (project index and their teams, etc)
  15.  * <p>
  16.  * Usage :
  17.  * </p>
  18.  * <p>
  19.  * Subscription : as soon as possible on client side + on current user reload<br>
  20.  * Unsubscription : none<br>
  21.  * </p>
  22.  * <p>
  23.  * Can be watched by admin in the Who page
  24.  * </p>
  25.  *
  26.  * @author maxence
  27.  */
  28. @ExtractJavaDoc
  29. public class UserChannel implements WebsocketChannel {

  30.     private static final long serialVersionUID = 1L;

  31.     /**
  32.      * ID of the channel owner
  33.      */
  34.     private Long userId;

  35.     /**
  36.      * Get the value of userId
  37.      *
  38.      * @return the value of userId
  39.      */
  40.     public Long getUserId() {
  41.         return userId;
  42.     }

  43.     /**
  44.      * Set the value of userId
  45.      *
  46.      * @param userId new value of userId
  47.      */
  48.     public void setUserId(Long userId) {
  49.         this.userId = userId;
  50.     }

  51.     @Override
  52.     public String getUrn() {
  53.         return Helper.getColabBaseUrn(this) + "/" + userId;
  54.     }

  55.     @Override
  56.     public int hashCode() {
  57.         int hash = 5;
  58.         hash = 17 * hash + Objects.hashCode(this.userId);
  59.         return hash;
  60.     }

  61.     /**
  62.      * Channel equals if they both refer to the same user
  63.      *
  64.      * @param obj other channel
  65.      *
  66.      * @return true if both refer to the same user
  67.      */
  68.     @Override
  69.     public boolean equals(Object obj) {
  70.         if (this == obj) {
  71.             return true;
  72.         }
  73.         if (obj == null) {
  74.             return false;
  75.         }
  76.         if (getClass() != obj.getClass()) {
  77.             return false;
  78.         }
  79.         final UserChannel other = (UserChannel) obj;

  80.         return Objects.equals(this.userId, other.userId);
  81.     }

  82.     @Override
  83.     public String toString() {
  84.         return "UserChannel{" + "userId=" + userId + '}';
  85.     }

  86.     /**
  87.      * get the channel dedicated to the given user.
  88.      *
  89.      * @param user the user
  90.      *
  91.      * @return the user very own channel
  92.      */
  93.     public static UserChannel build(User user) {
  94.         UserChannel channel = new UserChannel();
  95.         channel.setUserId(user.getId());
  96.         return channel;
  97.     }
  98. }