import fetch from './utils/fetch' ; import { User , GroupMember } from './state/reducer' ; function saveUsername ( username ) { window . localStorage . setItem ( 'username' , username ) ; } param username param password param os param browser param environment export async function register ( username , password , os = '' , browser = '' , environment = '' ) { const [ err , user ] = await fetch ( 'register' , { username , password , os , browser , environment , } ) ; if ( err ) { return null ; } saveUsername ( user . username ) ; return user ; } param username param password param os param browser param environment export async function login ( username , password , os = '' , browser = '' , environment = '' ) { const [ err , user ] = await fetch ( 'login' , { username , password , os , browser , environment , } ) ; if ( err ) { return null ; } saveUsername ( user . username ) ; return user ; } param token param os param browser param environment export async function loginByToken ( token , os = '' , browser = '' , environment = '' ) { const [ err , user ] = await fetch ( 'loginByToken' , { token , os , browser , environment , } , { toast : false } ) ; if ( err ) { return null ; } saveUsername ( user . username ) ; return user ; } param os param browser param environment export async function guest ( os = '' , browser = '' , environment = '' ) { const [ err , res ] = await fetch ( 'guest' , { os , browser , environment } ) ; if ( err ) { return null ; } return res ; } param avatar export async function changeAvatar ( avatar ) { const [ error ] = await fetch ( 'changeAvatar' , { avatar } ) ; return ! error ; } param oldPassword param newPassword export async function changePassword ( oldPassword , newPassword ) { const [ error ] = await fetch ( 'changePassword' , { oldPassword , newPassword , } ) ; return ! error ; } param username export async function changeUsername ( username ) { const [ error ] = await fetch ( 'changeUsername' , { username , } ) ; return ! error ; } param groupId param name export async function changeGroupName ( groupId , name ) { const [ error ] = await fetch ( 'changeGroupName' , { groupId , name } ) ; return ! error ; } param groupId param name export async function changeGroupAvatar ( groupId , avatar ) { const [ error ] = await fetch ( 'changeGroupAvatar' , { groupId , avatar } ) ; return ! error ; } param name export async function createGroup ( name ) { const [ , group ] = await fetch ( 'createGroup' , { name } ) ; return group ; } param groupId export async function deleteGroup ( groupId ) { const [ error ] = await fetch ( 'deleteGroup' , { groupId } ) ; return ! error ; } param groupId export async function joinGroup ( groupId ) { const [ , group ] = await fetch ( 'joinGroup' , { groupId } ) ; return group ; } param groupId export async function leaveGroup ( groupId ) { const [ error ] = await fetch ( 'leaveGroup' , { groupId } ) ; return ! error ; } param userId export async function addFriend ( userId ) { const [ , user ] = await fetch < User > ( 'addFriend' , { userId } ) ; return user ; } param userId export async function deleteFriend ( userId ) { const [ err ] = await fetch ( 'deleteFriend' , { userId } ) ; return ! err ; } param linkmanIds export async function getLinkmansLastMessagesV2 ( linkmanIds ) { const [ , linkmanMessages ] = await fetch ( 'getLinkmansLastMessagesV2' , { linkmans : linkmanIds , } ) ; return linkmanMessages ; } param linkmanId param existCount export async function getLinkmanHistoryMessages ( linkmanId , existCount ) { const [ , messages ] = await fetch ( 'getLinkmanHistoryMessages' , { linkmanId , existCount , } ) ; return messages ; } param existCount export async function getDefaultGroupHistoryMessages ( existCount ) { const [ , messages ] = await fetch ( 'getDefaultGroupHistoryMessages' , { existCount , } ) ; return messages ; } param keywords export async function search ( keywords ) { const [ , result ] = await fetch ( 'search' , { keywords } ) ; return result ; } param keywords export async function searchExpression ( keywords ) { const [ , result ] = await fetch ( 'searchExpression' , { keywords } ) ; return result ; } param to param type param content export async function sendMessage ( to , type , content ) { return fetch ( 'sendMessage' , { to , type , content } ) ; } param messageId export async function deleteMessage ( messageId ) { const [ err ] = await fetch ( 'deleteMessage' , { messageId } ) ; return ! err ; } param groupId export const getGroupOnlineMembers = ( ( ) => { let cache = { groupId : '' , key : '' , members : [ ] , } ; return async function _getGroupOnlineMembers ( groupId ) : Promise < GroupMember [ ] > { const [ , result ] = await fetch ( 'getGroupOnlineMembersV2' , { groupId , cache : cache . groupId === groupId ? cache . key : undefined , } ) ; if ( ! result ) { return [ ] ; } if ( result . cache === cache . key ) { return cache . members as GroupMember [ ] ; } cache = { groupId , key : result . cache , members : result . members , } ; return result . members ; } ; } ) ( ) ; /**
 * 获取默认群组的在线用户列表
 */ export async function getDefaultGroupOnlineMembers ( ) { const [ , members ] = await fetch ( 'getDefaultGroupOnlineMembers' ) ; return members ; } param username export async function sealUser ( username ) { const [ err ] = await fetch ( 'sealUser' , { username } ) ; return ! err ; } param ip export async function sealIp ( ip ) { const [ err ] = await fetch ( 'sealIp' , { ip } ) ; return ! err ; } param userId export async function sealUserOnlineIp ( userId ) { const [ err ] = await fetch ( 'sealUserOnlineIp' , { userId } ) ; return ! err ; } /**
 * 获取封禁用户列表
 */ export async function getSealList ( ) { const [ , sealList ] = await fetch ( 'getSealList' ) ; return sealList ; } export async function getSystemConfig ( ) { const [ , systemConfig ] = await fetch ( 'getSystemConfig' ) ; return systemConfig ; } param username export async function resetUserPassword ( username ) { const [ , res ] = await fetch ( 'resetUserPassword' , { username } ) ; return res ; } param username param tag export async function setUserTag ( username , tag ) { const [ err ] = await fetch ( 'setUserTag' , { username , tag } ) ; return ! err ; } param userId export async function getUserIps ( userId ) { const [ , res ] = await fetch ( 'getUserIps' , { userId } ) ; return res ; } export async function getUserOnlineStatus ( userId ) { const [ , res ] = await fetch ( 'getUserOnlineStatus' , { userId } ) ; return res && res . isOnline ; } export async function updateHistory ( linkmanId , messageId ) { const [ , result ] = await fetch ( 'updateHistory' , { linkmanId , messageId } ) ; return ! ! result ; } export async function toggleSendMessage ( enable ) { const [ , result ] = await fetch ( 'toggleSendMessage' , { enable } ) ; return ! ! result ; } export async function toggleNewUserSendMessage ( enable ) { const [ , result ] = await fetch ( 'toggleNewUserSendMessage' , { enable } ) ; return ! ! result ; }