How to use megalodon - 10 common examples

To help you get started, we’ve selected a few megalodon examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github h3poteto / whalebird-desktop / src / renderer / store / TimelineSpace / Modals / NewToot.ts View on Github external
if (state.attachedMedias.length > 4) {
        throw new NewTootAttachLength()
      }
      form = Object.assign(form, {
        media_ids: state.attachedMedias.map(m => {
          return m.id
        })
      })
      // Update media descriptions
      await dispatch('updateMedia', state.mediaDescriptions).catch(_ => {
        throw new NewTootMediaDescription()
      })
    }

    commit(MUTATION_TYPES.CHANGE_BLOCK_SUBMIT, true)
    const client = new Mastodon(
      rootState.TimelineSpace.account.accessToken,
      rootState.TimelineSpace.account.baseURL + '/api/v1',
      rootState.App.userAgent,
      rootState.App.proxyConfiguration
    )
    return client
      .post('/statuses', form)
      .then((res: Response) => {
        win.ipcRenderer.send('toot-action-sound')
        return res.data
      })
      .finally(() => {
        commit(MUTATION_TYPES.CHANGE_BLOCK_SUBMIT, false)
      })
  },
  openReply: ({ commit, rootState }, message: Status) => {
github h3poteto / whalebird-desktop / src / renderer / store / Login.ts View on Github external
confirmInstance: async ({ commit, rootState }, domain: string): Promise => {
    commit(MUTATION_TYPES.CHANGE_SEARCHING, true)
    const cleanDomain = domain.trim()
    try {
      await Mastodon.get('/api/v1/instance', {}, `https://${cleanDomain}`, rootState.App.proxyConfiguration)
      commit(MUTATION_TYPES.CHANGE_SEARCHING, false)
    } catch (err) {
      // https://gist.github.com/okapies/60d62d0df0163bbfb4ab09c1766558e8
      // Check /.well-known/host-meta to confirm mastodon instance.
      const res = await Mastodon.get('/.well-known/host-meta', {}, `https://${cleanDomain}`, rootState.App.proxyConfiguration).finally(
        () => {
          commit(MUTATION_TYPES.CHANGE_SEARCHING, false)
        }
      )
      const parser = new DOMParser()
      const dom = parser.parseFromString(res.data, 'text/xml')
      const link = dom.getElementsByTagName('Link')[0].outerHTML
      if (!link.includes(`https://${cleanDomain}/.well-known/webfinger`)) {
        throw new Error('domain is not activity pub')
      }
    }
    commit(MUTATION_TYPES.CHANGE_INSTANCE, cleanDomain)
    return true
  }
}
github h3poteto / whalebird-desktop / src / renderer / store / Login.ts View on Github external
confirmInstance: async ({ commit, rootState }, domain: string): Promise => {
    commit(MUTATION_TYPES.CHANGE_SEARCHING, true)
    const cleanDomain = domain.trim()
    try {
      await Mastodon.get('/api/v1/instance', {}, `https://${cleanDomain}`, rootState.App.proxyConfiguration)
      commit(MUTATION_TYPES.CHANGE_SEARCHING, false)
    } catch (err) {
      // https://gist.github.com/okapies/60d62d0df0163bbfb4ab09c1766558e8
      // Check /.well-known/host-meta to confirm mastodon instance.
      const res = await Mastodon.get('/.well-known/host-meta', {}, `https://${cleanDomain}`, rootState.App.proxyConfiguration).finally(
        () => {
          commit(MUTATION_TYPES.CHANGE_SEARCHING, false)
        }
      )
      const parser = new DOMParser()
      const dom = parser.parseFromString(res.data, 'text/xml')
      const link = dom.getElementsByTagName('Link')[0].outerHTML
      if (!link.includes(`https://${cleanDomain}/.well-known/webfinger`)) {
        throw new Error('domain is not activity pub')
      }
    }
github h3poteto / whalebird-desktop / src / renderer / store / TimelineSpace.ts View on Github external
detectPleroma: async ({ commit, state, rootState }) => {
    const res = await Mastodon.get('/instance', {}, state.account.baseURL + '/api/v1', rootState.App.proxyConfiguration)
    if (res.data.version.includes('Pleroma')) {
      commit(MUTATION_TYPES.CHANGE_PLEROMA, true)
    } else {
      commit(MUTATION_TYPES.CHANGE_PLEROMA, false)
    }
  },
  // -----------------------------------------------
github h3poteto / whalebird-desktop / src / renderer / store / TimelineSpace / Modals / MuteConfirm.ts View on Github external
submit: async ({ state, rootState, dispatch }, notify: boolean) => {
    const client = new Mastodon(
      rootState.TimelineSpace.account.accessToken!,
      rootState.TimelineSpace.account.baseURL + '/api/v1',
      rootState.App.userAgent,
      rootState.App.proxyConfiguration
    )
    const res: Response = await client.post(`/accounts/${state.account!.id}/mute`, {
      notifications: notify
    })
    // Reload relationship
    dispatch('TimelineSpace/Contents/SideBar/AccountProfile/fetchRelationship', state.account, { root: true })
    return res.data
  }
}
github h3poteto / whalebird-desktop / src / main / account.ts View on Github external
let json = {}
    try {
      const res = await client.get('/accounts/verify_credentials')
      json = {
        username: res.data.username,
        accountId: res.data.id,
        avatar: res.data.avatar
      }
    } catch (err) {
      log.error(err)
      log.info('Get new access token using refresh token...')
      // If failed to fetch account, get new access token usign refresh token.
      if (!account.refreshToken) {
        throw new RefreshTokenDoesNotExist()
      }
      const token = await Mastodon.refreshToken(account.clientId, account.clientSecret, account.refreshToken, account.baseURL, proxy)
      client = new Mastodon(token.access_token, account.baseURL + '/api/v1', 'Whalebird', proxy)
      const res = await client.get('/accounts/verify_credentials')
      json = {
        username: res.data.username,
        accountId: res.data.id,
        avatar: res.data.avatar,
        accessToken: token.accessToken,
        refreshToken: token.refreshToken
      }
    }
    return this.updateAccount(account._id!, json)
  }
github h3poteto / whalebird-desktop / src / renderer / store / TimelineSpace / Contents / SideBar / AccountProfile / Followers.js View on Github external
fetchRelationships ({ commit, rootState }, accounts) {
      const ids = accounts.map(a => a.id)
      const client = new Mastodon(
        rootState.TimelineSpace.account.accessToken,
        rootState.TimelineSpace.account.baseURL + '/api/v1'
      )
      return client.get(`/accounts/relationships`, {
        id: ids
      })
        .then(res => {
          commit('updateRelationships', res.data)
          return res.data
        })
    }
  }
github h3poteto / megalodon / example / typescript / favourite.ts View on Github external
import Mastodon, { Status, Response } from 'megalodon'

declare var process: {
  env: {
    MASTODON_ACCESS_TOKEN: string
  }
}

const BASE_URL: string = 'https://mastodon.social'

const access_token: string = process.env.MASTODON_ACCESS_TOKEN

const client = new Mastodon(access_token, BASE_URL + '/api/v1')

client.get<[Status]>('/favourites').then((res: Response<[Status]>) => {
  console.log(res.headers)
  console.log(res.data)
})
github h3poteto / megalodon / example / typescript / proxy_streaming.ts View on Github external
PROXY_PORT: number
    PROXY_PROTOCOL: 'http' | 'https' | 'socks4' | 'socks4a' | 'socks5' | 'socks5h' | 'socks'
  }
}

const BASE_URL: string = 'https://mastodon.social'

const access_token: string = process.env.MASTODON_ACCESS_TOKEN

const proxy: ProxyConfig = {
  host: process.env.PROXY_HOST,
  port: process.env.PROXY_PORT,
  protocol: process.env.PROXY_PROTOCOL
}

const client = new Mastodon(access_token, BASE_URL + '/api/v1', 'megalodon', proxy)

const stream: StreamListener = client.stream('/streaming/public')
stream.on('connect', _ => {
  console.log('connect')
})

stream.on('not-event-stream', (mes: string) => {
  console.log(mes)
})

stream.on('update', (status: Status) => {
  console.log(status)
})

stream.on('notification', (notification: Notification) => {
  console.log(notification)
github h3poteto / whalebird-desktop / src / renderer / store / TimelineSpace / Contents / SideBar / AccountProfile / Timeline.ts View on Github external
fetchTimeline: async ({ commit, rootState }, account: Account) => {
    commit('TimelineSpace/Contents/SideBar/AccountProfile/changeLoading', true, { root: true })
    const client = new Mastodon(
      rootState.TimelineSpace.account.accessToken!,
      rootState.TimelineSpace.account.baseURL + '/api/v1',
      rootState.App.userAgent,
      rootState.App.proxyConfiguration
    )
    const pinned: Response> = await client.get>(`/accounts/${account.id}/statuses`, { limit: 10, pinned: true })
    commit(MUTATION_TYPES.UPDATE_PINNED_TOOTS, pinned.data)
    const res: Response> = await client.get>(`/accounts/${account.id}/statuses`, { limit: 40 })
    commit('TimelineSpace/Contents/SideBar/AccountProfile/changeLoading', false, { root: true })
    commit(MUTATION_TYPES.UPDATE_TIMELINE, res.data)
    return res.data
  },
  lazyFetchTimeline: async ({ state, commit, rootState }, loadPosition: LoadPositionWithAccount): Promise => {

megalodon

Fediverse API client for node.js and browser

MIT
Latest version published 13 days ago

Package Health Score

78 / 100
Full package analysis