0.1.4 • Published 2 years ago

weverse v0.1.4

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

Usage

Provide credentials

See MujyKun's guide on finding your Weverse access token.

import { WeverseClient } from "weverse";

const myClient = new WeverseClient({token: 'my-access-token'})
// or
const myClient = new WeverseClient({username: 'jonah', password: 'top-secret'})

Initialize

import { WeverseClient } from "weverse";

const myClient = new WeverseClient({token: 'my-access-token'})
await myClient.init({allPosts: true, allNotifications: false})

myClient.communities.forEach(community => {
    // typesafe objects with autocompletion
    const details = {
        name: community.name
        posts: community.posts.length
    }
    // do something
})

Listen for new notifications

import { WeverseClient } from "weverse";

const myClient = new WeverseClient({token: 'my-access-token'})
myClient.init({allPosts: true, allNotifications: false})

myClient.on('init', async (ready) => {
    if (ready) {
        myClient.listen({listen: true, interval: 5000})
    }
})

myClient.on('comment', (comment, post) => {
    // all objects are typed
    const commenter = myClient.artistById(comment.artist.id)
    const postAuthor = myClient.artistById(post.artist.id)
    console.log(`${commenter.name} commented on ${postAuthor.name}'s post!`)
})

myClient.on('post', (post) => {
    if (post.photos.length) {
        post.photos.forEach(photo => {
            downloadImage(photo.orgImgUrl)
        })
    }
})

Credit

All credit to MujyKun for reverse-engineering most of the Weverse endpoints used by this module.

Classes

WeverseEmitter

WeverseEmitter allows the WeverseClient to emit events and provides methods for doing so

Kind: global class

weverseEmitter.newError(err)

Kind: instance method of WeverseEmitter

ParamTypeDescription
errErrorThe error to be emitted

weverseEmitter.ready(initialized)

Kind: instance method of WeverseEmitter

ParamTypeDescription
initializedbooleanwhether initialization succeeded

weverseEmitter.newNotif(notification)

Kind: instance method of WeverseEmitter

ParamTypeDescription
notificationWeverseNotificationnew Notification to be emitted

weverseEmitter.newPost(post)

Kind: instance method of WeverseEmitter

ParamTypeDescription
postWeversePostnew Post to be emitted

weverseEmitter.newMedia(media)

Kind: instance method of WeverseEmitter

ParamTypeDescription
mediaWeverseMedianew Media to be emitted

weverseEmitter.newComment(comment, post)

Kind: instance method of WeverseEmitter

ParamTypeDescription
commentWeverseCommentthe Comment that was retrieved
postWeversePostthe Post associated with the Comment

weverseEmitter.loginResult(result)

Kind: instance method of WeverseEmitter

ParamTypeDescription
resultbooleanboolean result of the login attempt

weverseEmitter.polled(status)

Kind: instance method of WeverseEmitter

ParamTypeDescription
statusbooleanresult of the poll attempt. If true, Weverse was successfully polled

WeverseClient

Client for the private Weverse api

Kind: global class
Emits: error, init, notification, post, media, comment, login, poll
Properties

NameTypeDescription
communitiesArray.<WeverseCommunity>The communities associated with the Weverse account
artistsArray.<WeverseArtist>All artists in all communities associated with the account
notificationsClientNotificationsSubclass handling all notifications for the account
postsArray.<WeversePost>All posts that have been retrieved by this client

new WeverseClient(authorization, verbose)

ParamTypeDescription
authorizationWeverseAuthorizationeither {token: string} or {username: string, password: string}
verbosebooleanoptional; defaults to false

weverseClient.init(options) ⇒ Promise.<void>

init options: allPosts: boolean - Whether to load all posts from each community into memory. This will be slow allNotifications: boolean - Whether to load all notifications for the Weverse account. Will be slow. allMedia: boolean - not currently implemented

Kind: instance method of WeverseClient
Access: public

ParamTypeDescription
optionsWeverseInitOptionsoptional

weverseClient.listen(opts)

Tells the client to start or stop listening for new notifications. Options: listen: boolean - Whether the client should be listening interval: boolean - Interval in MS to listen on process: boolean (optional) - Whether new notifications should be processed into Posts/Comments/Media

Kind: instance method of WeverseClient
Access: public

ParamType
optsListenOptions

weverseClient.checker(process) ⇒ Promise.<void>

Method passed to setInterval if client is listening for new notifications

Kind: instance method of WeverseClient
Access: protected

ParamTypeDescription
processbooleanWhether to process new notifications into Posts/Comments/Media

weverseClient.tryRefreshToken() ⇒ Promise.<boolean>

Attempts to use a refresh token to get a new Weverse access token

Kind: instance method of WeverseClient
Returns: Promise.<boolean> - Whether a new access token was granted
Access: public

weverseClient.login(credentials) ⇒ Promise.<void>

Only used for password authentication. Attempts to login either with login given when the client was created, or with optional credentials parameter

Kind: instance method of WeverseClient
Access: public

ParamTypeDescription
credentialsWeversePasswordAuthorizationoptional, will override initial credentials

weverseClient.checkLogin() ⇒ Promise.<boolean>

Force a credentials check. If login has already been converted to a token, token will be checked.

Kind: instance method of WeverseClient
Returns: Promise.<boolean> - - whether the check was successful
Access: public

weverseClient.getCommunities(opts) ⇒ Promise.<Array.<WeverseCommunity>>

Load all communities associated with this Weverse account. Returns the communities but also adds them to the cache. Options: init: boolean - Whether this method was called by the init method and should skip the login check

Kind: instance method of WeverseClient
Access: public

ParamTypeDescription
optsGetOptionsoptional

weverseClient.getCommunityArtists(c, opts) ⇒ Promise.<(Array.<WeverseArtist>|null)>

Get the artists in a community. Adds them to the cache and returns. Options: init - whether this method was called by init method and the login check should be skipped

Kind: instance method of WeverseClient
Returns: Promise.<(Array.<WeverseArtist>|null)> - returns null if failed to fetch artists

ParamTypeDescription
cWeverseCommunity
optsGetOptionsoptional

weverseClient.getNotifications(pages, process) ⇒ Promise.<(Array.<WeverseNotification>|null)>

Note: If process = true, events will be emitted for new notifications AND new Posts/Comments/Media

Kind: instance method of WeverseClient
Returns: Promise.<(Array.<WeverseNotification>|null)> - - Returns only new notifications not already in cache, or null on failure

ParamTypeDescription
pagesnumberOptional number of pages to get; defaults to 1
processbooleanWhether notifications should be processed into Posts/Comments/Media

weverseClient.getNewNotifications(opts) ⇒ Promise.<(Array.<WeverseNotification>|null)>

Get one page of the most recent notifications

Kind: instance method of WeverseClient

ParamTypeDescription
optsNewNotifications{process: boolean} - whether to process notifications into content

weverseClient.getMedia(id, community) ⇒ Promise.<(WeverseMedia|null)>

Get a specific media object by id Will first check local cache, then request from Weverse

Kind: instance method of WeverseClient
Returns: Promise.<(WeverseMedia|null)> - - Returns only if media did not exist in cache

ParamType
idnumber
communityWeverseCommunity

weverseClient.getComments(p, c, cId?) ⇒ Promise.<(Array.<WeverseComment>|null)>

Gets all artist comments on a given post. Returns only new comments.

Kind: instance method of WeverseClient

ParamType
pWeversePost
cWeverseCommunity
cId?number

weverseClient.getPost(id, communityId) ⇒ Promise.<(WeversePost|null)>

Get one post by id. First checks the cache, then requests from Weverse.

Kind: instance method of WeverseClient
Access: public

ParamType
idnumber
communityIdnumber

weverseClient.processNotification(n) ⇒ Promise.<void>

Process one notification. If it refers to a post, comment, or media, attempt to add to cache

Kind: instance method of WeverseClient

ParamType
nWeverseNotification

weverseClient.createLoginPayload() ⇒ void

Encrypt provided password with Weverse public RSA key and create payload to send to login endpoint Adds the payload as a property of the client, returns void

Kind: instance method of WeverseClient

weverseClient.checkToken() ⇒ Promise.<boolean>

Check if the current token (provided or recieved from Weverse) is valid

Kind: instance method of WeverseClient

weverseClient.handleResponse(response, url) ⇒ Promise.<boolean>

If the client receives a 401 unauthorized from Weverse, will attempt to refresh credentials

Kind: instance method of WeverseClient

ParamType
responseAxiosResponse
urlstring

weverseClient.log()

Log something if verbose = true

Kind: instance method of WeverseClient

ParamType
...valsany

weverseClient.communityById(id) ⇒ WeverseCommunity | null

Check the community hashmap for a given id

Kind: instance method of WeverseClient

ParamType
idnumber

weverseClient.artistById(id) ⇒ WeverseArtist | null

Check the artist hashmap for a given id

Kind: instance method of WeverseClient

ParamType
idnumber

weverseClient.post(id)

Check the post hashmap for a given id

Kind: instance method of WeverseClient

ParamType
idnumber

"error"

Error event

Kind: event emitted by WeverseClient

"init"

Init event Whether initialization was successful

Kind: event emitted by WeverseClient

"notification"

Notification event New notification

Kind: event emitted by WeverseClient

"post"

Post event New post

Kind: event emitted by WeverseClient

"media"

Media event New media

Kind: event emitted by WeverseClient

"comment"

Comment event New comment. Provides comment and post.

Kind: event emitted by WeverseClient

"login"

Login event Result of login attempt.

Kind: event emitted by WeverseClient

"poll"

Poll event Result of poll attempt.

Kind: event emitted by WeverseClient

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

1.0.0

2 years ago