Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

TwitterMessage.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.parser_twitter.api;
  23. import org.w3c.dom.Element;
  24. import org.w3c.dom.Node;
  25. import org.w3c.dom.NodeList;
  26. /**
  27. * Used for direct messages.
  28. *
  29. * @author shane
  30. */
  31. public class TwitterMessage implements Comparable<TwitterMessage> {
  32. /** ID of this message. */
  33. final long id;
  34. /** Contents of this message. */
  35. final String message;
  36. /** Owner of this message. */
  37. final String sender;
  38. /** Target of this message. */
  39. final String target;
  40. /** API Object that owns this. */
  41. private final TwitterAPI myAPI;
  42. /** Time this message was sent. */
  43. private final long time;
  44. /**
  45. * Create a new TwitterMessage
  46. *
  47. * @param api
  48. * @param message Message contents
  49. */
  50. protected TwitterMessage(final TwitterAPI api, final String message) {
  51. this(api, message, -1, "", "", System.currentTimeMillis());
  52. }
  53. /**
  54. * Create a new TwitterMessage
  55. *
  56. * @param api
  57. * @param message Message contents
  58. * @param id ID of message
  59. * @param sender User who send this.
  60. * @param time Time this message was sent
  61. */
  62. protected TwitterMessage(final TwitterAPI api, final String message, final long id, final String sender, final String target, final Long time) {
  63. this.myAPI = api;
  64. this.id = id;
  65. this.message = message;
  66. this.sender = sender;
  67. this.target = target;
  68. this.time = time;
  69. }
  70. /**
  71. * Create a twitter status from a node!
  72. *
  73. * @param api API that owns this.
  74. * @param node Node to use.
  75. */
  76. protected TwitterMessage(final TwitterAPI api, final Node node) {
  77. if (!(node instanceof Element)) { throw new TwitterRuntimeException("Can only use Element type nodes for message creation."); }
  78. this.myAPI = api;
  79. final Element element = (Element) node;
  80. this.message = TwitterAPI.getElementContents(element, "text", "");
  81. final TwitterUser senderUser;
  82. final NodeList senderNodes = element.getElementsByTagName("sender");
  83. if (senderNodes != null && senderNodes.getLength() > 0) {
  84. senderUser = new TwitterUser(api, senderNodes.item(0), null);
  85. this.sender = senderUser.getScreenName();
  86. myAPI.updateUser(senderUser);
  87. } else {
  88. senderUser = new TwitterUser(api, "unknown", -1, "realname", false);
  89. this.sender = senderUser.getScreenName();
  90. }
  91. final TwitterUser targetUser;
  92. final NodeList recipientNodes = element.getElementsByTagName("recipient");
  93. if (recipientNodes != null && recipientNodes.getLength() > 0) {
  94. targetUser = new TwitterUser(api, recipientNodes.item(0), null);
  95. this.target = targetUser.getScreenName();
  96. myAPI.updateUser(targetUser);
  97. } else {
  98. targetUser = new TwitterUser(api, "unknown", -1, "realname", false);
  99. this.target = targetUser.getScreenName();
  100. }
  101. this.id = TwitterAPI.parseLong(TwitterAPI.getElementContents(element, "id", ""), -1);
  102. this.time = TwitterAPI.timeStringToLong(TwitterAPI.getElementContents(element, "created_at", ""), 0);
  103. }
  104. /**
  105. * Get the screen name of the user who sent this message.
  106. *
  107. * @return Screen name of the user who sent this message.
  108. */
  109. public String getSenderScreenName() {
  110. return sender;
  111. }
  112. /**
  113. * Get the user who sent this message.
  114. *
  115. * @return The user who sent this message.
  116. */
  117. public TwitterUser getSender() {
  118. return myAPI.getCachedUser(sender);
  119. }
  120. /**
  121. * Get the screen name of the user who received this message.
  122. *
  123. * @return Screen name of the user who received this message.
  124. */
  125. public String getTargetScreenName() {
  126. return target;
  127. }
  128. /**
  129. * Get the user who received this message.
  130. *
  131. * @return The user who received this message.
  132. */
  133. public TwitterUser getTarget() {
  134. return myAPI.getCachedUser(target);
  135. }
  136. /**
  137. * Get the ID of this message.
  138. *
  139. * @return ID of this message.
  140. */
  141. public long getID() {
  142. return id;
  143. }
  144. /**
  145. * What time was this message sent?
  146. *
  147. * @return Time this message was sent.
  148. */
  149. public long getTime() {
  150. return this.time;
  151. }
  152. /**
  153. * Get the contents of this message.
  154. *
  155. * @return contents of this message.
  156. */
  157. public String getText() {
  158. return message;
  159. }
  160. /**
  161. * Is this equal to the given Status?
  162. * @param message
  163. * @return
  164. */
  165. @Override
  166. public boolean equals(final Object message) {
  167. if (message instanceof TwitterMessage) {
  168. return ((TwitterMessage)message).getID() == this.id;
  169. } else {
  170. return false;
  171. }
  172. }
  173. /**
  174. * Generate hashCode for this object.
  175. * @return
  176. */
  177. @Override
  178. public int hashCode() {
  179. int hash = 5;
  180. hash = 53 * hash + (int) (this.id ^ (this.id >>> 32));
  181. return hash;
  182. }
  183. /**
  184. * Compare the given object to this one.
  185. *
  186. * @param arg0
  187. * @return Comparison
  188. */
  189. @Override
  190. public int compareTo(final TwitterMessage arg0) {
  191. if (this.time < arg0.getTime()) {
  192. return -1;
  193. } else if (this.time > arg0.getTime()) {
  194. return 1;
  195. } else {
  196. return 0;
  197. }
  198. }
  199. }