import Axios from 'axios'; import Vue from 'vue'; import {InlineDisplayMode} from '../bbcode/interfaces'; import {initParser, standardParser} from '../bbcode/standard'; import CharacterLink from '../components/character_link.vue'; import CharacterSelect from '../components/character_select.vue'; import {setCharacters} from '../components/character_select/character_list'; import DateDisplay from '../components/date_display.vue'; import {registerMethod, Store} from '../site/character_page/data_store'; import { Character, CharacterCustom, CharacterFriend, CharacterImage, CharacterImageOld, CharacterInfo, CharacterInfotag, CharacterKink, CharacterSettings, Friend, FriendRequest, FriendsByCharacter, GuestbookState, KinkChoice, KinkChoiceFull, SharedKinks } from '../site/character_page/interfaces'; import '../site/directives/vue-select'; //tslint:disable-line:no-import-side-effect import * as Utils from '../site/utils'; import core from './core'; async function characterData(name: string | undefined): Promise { const data = await core.connection.queryApi('character-data.php', {name}) as CharacterInfo & { badges: string[] customs_first: boolean character_list: {id: number, name: string}[] custom_kinks: {[key: number]: {choice: 'fave' | 'yes' | 'maybe' | 'no', name: string, description: string, children: number[]}} custom_title: string kinks: {[key: string]: string} infotags: {[key: string]: string} memo: {id: number, memo: string} settings: CharacterSettings }; const newKinks: {[key: string]: KinkChoiceFull} = {}; for(const key in data.kinks) newKinks[key] = (data.kinks[key] === 'fave' ? 'favorite' : data.kinks[key]); const newCustoms: CharacterCustom[] = []; for(const key in data.custom_kinks) { const custom = data.custom_kinks[key]; newCustoms.push({ id: parseInt(key, 10), choice: custom.choice === 'fave' ? 'favorite' : custom.choice, name: custom.name, description: custom.description }); for(const childId of custom.children) newKinks[childId] = parseInt(key, 10); } const newInfotags: {[key: string]: CharacterInfotag} = {}; for(const key in data.infotags) { const characterInfotag = data.infotags[key]; const infotag = Store.kinks.infotags[key]; if(infotag === undefined) continue; newInfotags[key] = infotag.type === 'list' ? {list: parseInt(characterInfotag, 10)} : {string: characterInfotag}; } return { is_self: false, character: { id: data.id, name: data.name, title: data.custom_title, description: data.description, created_at: data.created_at, updated_at: data.updated_at, views: data.views, image_count: data.images!.length, inlines: data.inlines, kinks: newKinks, customs: newCustoms, infotags: newInfotags, online_chat: false }, memo: data.memo, character_list: data.character_list, badges: data.badges, settings: data.settings, bookmarked: core.characters.get(data.name).isBookmarked, self_staff: false }; } function contactMethodIconUrl(name: string): string { return `${Utils.staticDomain}images/social/${name}.png`; } async function fieldsGet(): Promise { if(Store.kinks !== undefined) return; //tslint:disable-line:strict-type-predicates try { const fields = (await(Axios.get(`${Utils.siteDomain}json/api/mapping-list.php`))).data as SharedKinks & { kinks: {[key: string]: {group_id: number}} infotags: {[key: string]: {list: string, group_id: string}} }; const kinks: SharedKinks = {kinks: {}, kink_groups: {}, infotags: {}, infotag_groups: {}, listitems: {}}; for(const id in fields.kinks) { const oldKink = fields.kinks[id]; kinks.kinks[oldKink.id] = { id: oldKink.id, name: oldKink.name, description: oldKink.description, kink_group: oldKink.group_id }; } for(const id in fields.kink_groups) { const oldGroup = fields.kink_groups[id]!; kinks.kink_groups[oldGroup.id] = { id: oldGroup.id, name: oldGroup.name, description: '', sort_order: oldGroup.id }; } for(const id in fields.infotags) { const oldInfotag = fields.infotags[id]; kinks.infotags[oldInfotag.id] = { id: oldInfotag.id, name: oldInfotag.name, type: oldInfotag.type, validator: oldInfotag.list, search_field: '', allow_legacy: true, infotag_group: parseInt(oldInfotag.group_id, 10) }; } for(const id in fields.listitems) { const oldListItem = fields.listitems[id]!; kinks.listitems[oldListItem.id] = { id: oldListItem.id, name: oldListItem.name, value: oldListItem.value, sort_order: oldListItem.id }; } for(const id in fields.infotag_groups) { const oldGroup = fields.infotag_groups[id]!; kinks.infotag_groups[oldGroup.id] = { id: oldGroup.id, name: oldGroup.name, description: oldGroup.description, sort_order: oldGroup.id }; } Store.kinks = kinks; } catch(e) { Utils.ajaxError(e, 'Error loading character fields'); throw e; } } async function friendsGet(id: number): Promise { return (await core.connection.queryApi<{friends: CharacterFriend[]}>('character-friends.php', {id})).friends; } async function imagesGet(id: number): Promise { return (await core.connection.queryApi<{images: CharacterImage[]}>('character-images.php', {id})).images; } async function guestbookGet(id: number, page: number): Promise { return core.connection.queryApi('character-guestbook.php', {id, page: page - 1}); } async function kinksGet(id: number): Promise { const data = await core.connection.queryApi<{kinks: {[key: string]: string}}>('character-data.php', {id}); return Object.keys(data.kinks).map((key) => { const choice = data.kinks[key]; return {id: parseInt(key, 10), choice: (choice === 'fave' ? 'favorite' : choice)}; }); } export function init(characters: {[key: string]: number}): void { Utils.setDomains('https://www.f-list.net/', 'https://static.f-list.net/'); initParser({ siteDomain: Utils.siteDomain, staticDomain: Utils.staticDomain, animatedIcons: false, inlineDisplayMode: InlineDisplayMode.DISPLAY_ALL }); Vue.component('character-select', CharacterSelect); Vue.component('character-link', CharacterLink); Vue.component('date-display', DateDisplay); setCharacters(Object.keys(characters).map((name) => ({name, id: characters[name]}))); core.connection.onEvent('connecting', () => { Utils.Settings.defaultCharacter = characters[core.connection.character]; }); Vue.directive('bbcode', (el, binding) => { while(el.firstChild !== null) el.removeChild(el.firstChild); el.appendChild(standardParser.parseEverything(binding.value)); }); registerMethod('characterData', characterData); registerMethod('contactMethodIconUrl', contactMethodIconUrl); registerMethod('sendNoteUrl', (character: CharacterInfo) => `${Utils.siteDomain}read_notes.php?send=${character.name}`); registerMethod('fieldsGet', fieldsGet); registerMethod('friendsGet', friendsGet); registerMethod('kinksGet', kinksGet); registerMethod('imagesGet', imagesGet); registerMethod('guestbookPageGet', guestbookGet); registerMethod('imageUrl', (image: CharacterImageOld) => image.url); registerMethod('memoUpdate', async(id: number, memo: string) => { await core.connection.queryApi('character-memo-save.php', {target: id, note: memo}); return {id, memo, updated_at: Date.now() / 1000}; }); registerMethod('imageThumbUrl', (image: CharacterImage) => `${Utils.staticDomain}images/charthumb/${image.id}.${image.extension}`); registerMethod('bookmarkUpdate', async(id: number, state: boolean) => { await core.connection.queryApi(`bookmark-${state ? 'add' : 'remove'}.php`, {id}); return state; }); registerMethod('characterFriends', async(id: number) => core.connection.queryApi('character-friend-list.php', {id})); registerMethod('friendRequest', async(target_id: number, source_id: number) => (await core.connection.queryApi<{request: FriendRequest}>('request-send2.php', {source_id, target_id})).request); registerMethod('friendDissolve', async(friend: Friend) => core.connection.queryApi('friend-remove.php', {source_id: friend.source.id, dest_id: friend.target.id})); registerMethod('friendRequestAccept', async(req: FriendRequest) => { await core.connection.queryApi('request-accept.php', {request_id: req.id}); return { id: undefined!, source: req.target, target: req.source, createdAt: Date.now() / 1000 }; }); registerMethod('friendRequestCancel', async(req: FriendRequest) => core.connection.queryApi('request-cancel.php', {request_id: req.id})); registerMethod('friendRequestIgnore', async(req: FriendRequest) => core.connection.queryApi('request-deny.php', {request_id: req.id})); }