Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert datetime objects to snowflake ids for max_id, min_id and since_id #153

Closed
Kudusch opened this issue Mar 12, 2024 · 1 comment
Closed

Comments

@Kudusch
Copy link
Contributor

Kudusch commented Mar 12, 2024

As Mastodon uses Snowflake IDs for statuses, it is possible to convert a date and time to a Snowflake ID (with a relatively low resolution). Giving users the ability to provide a POSIXct for max_id, min_id, and/or since_id could make it easier to search for posts between two dates/times.

The Python API client does this as well (and I found it very useful!): https://mastodonpy.readthedocs.io/en/stable/01_general.html#snowflake-ids

One way to implement this could be changing handle_params in utils. Converting a POSIXct datetime to a snowflake id can by done by bitshifting the timestamp like this: as.numeric(datetime)*(2^16))*1000

Might make sense to do a is(max_id, "POSIXct") check before converting, like so:

handle_params <- function(params, max_id, since_id, min_id) {
    if (!missing(max_id)) {
        if(is(max_id, "POSIXct")) {
            params$max_id <- as.numeric(max_id)*(2^16))*1000
        } else {
           params$max_id <- max_id
        }
    }
    if (!missing(since_id)) {
        if(is(max_id, "POSIXct")) {
            params$since_id <- as.numeric(since_id)*(2^16))*1000
        } else {
           params$since_id <- since_id
        }
    }
    if (!missing(min_id)) {
        if(is(min_id, "POSIXct")) {
            params$min_id <- as.numeric(min_id)*(2^16))*1000
        } else {
           params$min_id <- min_id
        }

    }
    params
}
@chainsawriot
Copy link
Collaborator

@Kudusch Are you interested in providing a PR? Thank you very much!

As this code repeats 3 times, it might make sense to spin it off as a helper function

 if(is(max_id, "POSIXct")) {
            params$since_id <- as.numeric(since_id)*(2^16))*1000
        } else {
           params$since_id <- since_id
        }
handle_id <- function(x) {
    ## convert x to snowflake id if it is POSIXct
    if (is (x, "POSIXct")) {
        return(as.numeric(x)*(2^16))*1000)
    }
    x
}

if (!missing(max_id)) {
    params$max_id <- handle_id(max_id)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants