| 1 | 2 |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |   | 
  | 17 |  |   | 
  | 18 |  |   | 
  | 19 |  |   | 
  | 20 |  |   | 
  | 21 |  |   | 
  | 22 |  |   | 
  | 23 |  |   | 
  | 24 |  |   | 
  | 25 |  |   | 
  | 26 |  |   | 
  | 27 |  |   | 
  | 28 |  |   | 
  | 29 |  |   | 
  | 30 |  |  package com.jcabi.github; | 
  | 31 |  |   | 
  | 32 |  |  import com.jcabi.aspects.Immutable; | 
  | 33 |  |  import com.jcabi.aspects.Loggable; | 
  | 34 |  |  import com.jcabi.http.Request; | 
  | 35 |  |  import com.jcabi.http.response.RestResponse; | 
  | 36 |  |  import java.io.IOException; | 
  | 37 |  |  import java.net.HttpURLConnection; | 
  | 38 |  |  import java.time.format.DateTimeFormatter; | 
  | 39 |  |  import java.util.Date; | 
  | 40 |  |  import javax.json.JsonObject; | 
  | 41 |  |  import lombok.EqualsAndHashCode; | 
  | 42 |  |   | 
  | 43 |  |   | 
  | 44 |  |   | 
  | 45 |  |   | 
  | 46 |  |   | 
  | 47 |  |   | 
  | 48 |  |   | 
  | 49 |  |   | 
  | 50 |  |  @Immutable | 
  | 51 |  |  @Loggable(Loggable.DEBUG) | 
  | 52 | 0 |  @EqualsAndHashCode(of = { "ghub", "request" }) | 
  | 53 |  |  @SuppressWarnings("PMD.TooManyMethods") | 
  | 54 |  |  final class RtUser implements User { | 
  | 55 |  |       | 
  | 56 |  |   | 
  | 57 |  |   | 
  | 58 |  |      private static final String NOTIF_PATH = "notifications"; | 
  | 59 |  |   | 
  | 60 |  |       | 
  | 61 |  |   | 
  | 62 |  |   | 
  | 63 |  |      private final transient Github ghub; | 
  | 64 |  |   | 
  | 65 |  |       | 
  | 66 |  |   | 
  | 67 |  |   | 
  | 68 |  |      private final transient Request request; | 
  | 69 |  |   | 
  | 70 |  |       | 
  | 71 |  |   | 
  | 72 |  |   | 
  | 73 |  |      private final transient String self; | 
  | 74 |  |   | 
  | 75 |  |       | 
  | 76 |  |   | 
  | 77 |  |   | 
  | 78 |  |   | 
  | 79 |  |   | 
  | 80 |  |      RtUser( | 
  | 81 |  |          final Github github, | 
  | 82 |  |          final Request req | 
  | 83 |  |      ) { | 
  | 84 | 7 |          this(github, req, ""); | 
  | 85 | 7 |      } | 
  | 86 |  |   | 
  | 87 |  |       | 
  | 88 |  |   | 
  | 89 |  |   | 
  | 90 |  |   | 
  | 91 |  |   | 
  | 92 |  |   | 
  | 93 |  |      RtUser( | 
  | 94 |  |          final Github github, | 
  | 95 |  |          final Request req, | 
  | 96 |  |          final String login | 
  | 97 | 38 |      ) { | 
  | 98 | 40 |          this.ghub = github; | 
  | 99 | 40 |          if (login.isEmpty()) { | 
  | 100 | 8 |              this.request = req.uri().path("/user").back(); | 
  | 101 |  |          } else { | 
  | 102 | 32 |              this.request = req.uri().path("/users").path(login).back(); | 
  | 103 |  |          } | 
  | 104 | 40 |          this.self = login; | 
  | 105 | 40 |      } | 
  | 106 |  |   | 
  | 107 |  |      @Override | 
  | 108 |  |      public String toString() { | 
  | 109 | 0 |          return this.request.uri().get().toString(); | 
  | 110 |  |      } | 
  | 111 |  |   | 
  | 112 |  |      @Override | 
  | 113 |  |      public Github github() { | 
  | 114 | 6 |          return this.ghub; | 
  | 115 |  |      } | 
  | 116 |  |   | 
  | 117 |  |      @Override | 
  | 118 |  |      public String login() throws IOException { | 
  | 119 |  |          final String login; | 
  | 120 | 8 |          if (this.self.isEmpty()) { | 
  | 121 | 2 |              login = this.json().getString("login"); | 
  | 122 |  |          } else { | 
  | 123 | 2 |              login = this.self; | 
  | 124 |  |          } | 
  | 125 | 4 |          return login; | 
  | 126 |  |      } | 
  | 127 |  |   | 
  | 128 |  |      @Override | 
  | 129 |  |      public UserOrganizations organizations() { | 
  | 130 | 2 |          return new RtUserOrganizations(this.ghub, this.ghub.entry(), this); | 
  | 131 |  |      } | 
  | 132 |  |   | 
  | 133 |  |      @Override | 
  | 134 |  |      public PublicKeys keys() { | 
  | 135 | 0 |          return new RtPublicKeys(this.ghub.entry(), this); | 
  | 136 |  |      } | 
  | 137 |  |   | 
  | 138 |  |      @Override | 
  | 139 |  |      public UserEmails emails() { | 
  | 140 | 2 |          return new RtUserEmails(this.ghub.entry()); | 
  | 141 |  |      } | 
  | 142 |  |   | 
  | 143 |  |      @Override | 
  | 144 |  |      public Notifications notifications() throws IOException { | 
  | 145 | 3 |          return new RtNotifications( | 
  | 146 | 1 |              this.github().entry().uri().path(RtUser.NOTIF_PATH).back() | 
  | 147 |  |          ); | 
  | 148 |  |      } | 
  | 149 |  |   | 
  | 150 |  |      @Override | 
  | 151 |  |      public void markAsRead(final Date lastread) throws IOException { | 
  | 152 | 4 |          this.github().entry().uri() | 
  | 153 | 2 |              .path(RtUser.NOTIF_PATH) | 
  | 154 | 2 |              .queryParam( | 
  | 155 |  |                  "last_read_at", | 
  | 156 | 2 |                  DateTimeFormatter.ISO_INSTANT.format(lastread.toInstant()) | 
  | 157 | 2 |              ).back() | 
  | 158 | 2 |              .method(Request.PUT) | 
  | 159 | 2 |              .fetch() | 
  | 160 | 2 |              .as(RestResponse.class) | 
  | 161 | 2 |              .assertStatus(HttpURLConnection.HTTP_RESET); | 
  | 162 | 1 |      } | 
  | 163 |  |   | 
  | 164 |  |      @Override | 
  | 165 |  |      public JsonObject json() throws IOException { | 
  | 166 | 54 |          return new RtJson(this.request).fetch(); | 
  | 167 |  |      } | 
  | 168 |  |   | 
  | 169 |  |      @Override | 
  | 170 |  |      public void patch( | 
  | 171 |  |          final JsonObject json) | 
  | 172 |  |          throws IOException { | 
  | 173 | 2 |          new RtJson(this.request).patch(json); | 
  | 174 | 1 |      } | 
  | 175 |  |  } | 
  | 176 |  |   |