1setRefClass('dmList', 2 contains='twitterObjList' 3 ) 4 5setValidity('dmList', function(object) { 6 listClassValidity(object, 'directMessage') 7}) 8 9setRefClass("directMessage", 10 contains='twitterObj', 11 fields = list( 12 text = "character", 13 recipientSN = "character", 14 created = "POSIXct", 15 recipientID = "character", 16 sender = "user", 17 recipient = "user", 18 senderID = "character", 19 id = "character", 20 senderSN = "character"), 21 methods = list( 22 initialize = function(json, ...) { 23 if (!missing(json)) { 24 if (!is.null(json[['sender']])) 25 sender <<- buildUser(json[['sender']]) 26 if (!is.null(json[['recipient']])) 27 recipient <<- buildUser(json[['recipient']]) 28 if (!is.null(json[['text']])) 29 text <<- json[['text']] 30 if (!is.null(json[['recipient_screen_name']])) 31 recipientSN <<- json[['recipient_screen_name']] 32 if (!is.null(json[['created']])) 33 created <<- twitterDateToPOSIX(json[['created']]) 34 if (!is.null(json[['recipient']][['id_str']])) 35 recipientID <<- json[['recipient']][['id_str']] 36 if (!is.null(json[['sender']][['id_str']])) 37 senderID <<- json[['sender']][['id_str']] 38 if (!is.null(json[['sender_screen_name']])) 39 senderSN <<- json[['sender_screen_name']] 40 if (!is.null(json[['id_str']])) { 41 id <<- json[['id_str']] 42 } 43 } 44 callSuper(...) 45 }, 46 destroy = function() { 47 dmDestroy(.self) 48 } 49 ) 50 ) 51 52setMethod("show", signature="directMessage", function(object) { 53 print(paste(screenName(object$sender), "->", 54 screenName(object$recipient), ": ", 55 object$text, sep="")) 56}) 57 58dmFactory <- getRefClass("directMessage") 59dmFactory$accessors(names(dmFactory$fields())) 60 61 62dmGet <- function(n=25, sinceID=NULL, maxID=NULL, ...) { 63 dmGETBase(n, sinceID, maxID) 64} 65 66dmSent <- function(n=25, sinceID=NULL, maxID=NULL, ...) { 67 dmGETBase(n, sinceID, maxID, "/sent") 68} 69 70dmGETBase <- function(n, sinceID, maxID, type='', ...) { 71 if (!has_oauth_token()) 72 stop("dmGet requires OAuth authentication") 73 74 if (n <= 0) 75 stop("n must be positive") 76 else 77 n <- as.integer(n) 78 params <- buildCommonArgs(since_id=sinceID, max_id=maxID) 79 jsonList <- doPagedAPICall(paste('direct_messages', type, sep=''), 80 num=n, params=params, ...) 81 sapply(jsonList, function(x) dmFactory$new(x)) 82} 83 84dmDestroy <- function(dm, ...) { 85 if (!has_oauth_token()) 86 stop("dmDestroy requires OAuth authentication") 87 if (!inherits(dm, "directMessage")) 88 stop("dm must be of class directMessage") 89 json <- twInterfaceObj$doAPICall('direct_messages/destroy', 90 params=list(id=dm$getId()), 91 method='POST', ...) 92 if (is.null(json$errors)) { 93 TRUE 94 } else { 95 for (error in json$errors) { 96 cat(error$message, error$code, fill = TRUE) 97 } 98 FALSE 99 } 100} 101 102dmSend <- function(text, user, ...) { 103 if (!has_oauth_token()) { 104 stop("dmSend requires OAuth authentication") 105 } 106 if (inherits(user, "user")) { 107 u.params <- c(list(...)[["uParams"]], list(screen_name = screenName(user))) 108 } else { 109 uParams <- parseUsers(user) 110 } 111 if (nchar(text) > 140) { 112 stop("Maximum of 140 chars may be sent via a direct message") 113 } 114 params <- c(list(...)[["params"]], list(text=text)) 115 params[["user_id"]] <- uParams[["user_id"]] 116 params[["screen_name"]] <- uParams[["screen_name"]] 117 res <- twInterfaceObj$doAPICall('direct_messages/new', 118 params=params, method='POST', ...) 119 dmFactory$new(res) 120} 121