From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,URIBL_BLOCKED, URIBL_DBL_BLOCKED_OPENDNS autolearn=ham autolearn_force=no version=3.4.6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1696301542; bh=mUKPAReQNgCVZyujCTsdie7mfZ77zv0V8HDk2nDeQrw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=KEno0YYOe7owCH1QgvaKvCNClvobZVpqh4EhKrQOBdznXbmQdGl3qjgXeNkLKjafJ Qkny9DWfrjREffCPRx1g+GQeNzKja1tEBbrPv6B6cyViqcQBQzQ5xhU7AUpsoVZMQS ozMPxPy/nIow3+Yh3UaFJ+FyDu02PShYrG7UTPUchGENhRgex18fV2a847Q8JatNET YrBPSd79UoLQt/lgUvZqo+0fehi3Jufy8wabkQuyxRX+yZ4MMC/cWxZ9DyxIzFxzha Ao2jWnRy69FcCJDyDgYYsdq0xJ5dxRo6KAten9JGI8PnK865McsmyRhNn2PyaDFR/a mKuJK7SU+0OHA== Received: from localhost.localdomain (unknown [175.158.50.50]) by gnuweeb.org (Postfix) with ESMTPSA id ED71024B8B0; Tue, 3 Oct 2023 09:52:20 +0700 (WIB) From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , Alviro Iskandar Setiawan , Irvan Malik Azantha , Memet Zx , GNU/Weeb Mailing List Subject: [PATCH v1 04/13] feat(lib): add lib functions file Date: Tue, 3 Oct 2023 09:51:37 +0700 Message-Id: <20231003025146.1557-5-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20231003025146.1557-1-kiizuha@gnuweeb.org> References: <20231003025146.1557-1-kiizuha@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: This file contains helper functions, and there are no changes from the previous version. We've simply added some type annotations for TypeScript support. Signed-off-by: Muhammad Rizki --- src/lib/functions.ts | 95 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/lib/functions.ts diff --git a/src/lib/functions.ts b/src/lib/functions.ts new file mode 100644 index 0000000..0357e6b --- /dev/null +++ b/src/lib/functions.ts @@ -0,0 +1,95 @@ +import type { GitHubOrgsAPIResponseType, RecentMessagesReturnType } from "$types"; +import { API_URL } from "./constants"; + + +export const getOrgMembers = async (): Promise => { + const response = await fetch("https://api.github.com/orgs/gnuweeb/members"); + const data: GitHubOrgsAPIResponseType[] = await response.json(); + + data.sort(( + left: GitHubOrgsAPIResponseType, + right: GitHubOrgsAPIResponseType + ) => { + const usernameA = left.login.toLowerCase(); + const usernameB = right.login.toLowerCase(); + if (usernameA < usernameB) return -1; + if (usernameA > usernameB) return 1; + return 0; + }); + + return data; +} + +export const getRecentMessages = async (): Promise => { + try { + const response = await fetch(API_URL + "/zxc.php?action=get_messages&chat_id=-1001483770714&limit=50"); + const { data } = (await response.json()).result; + + const transformedData = data.map((item: any) => { + return { + user_id: item[0], + username: item[1], + first_name: item[2], + last_name: item[3], + user_photo: item[4], + message_id: item[5], + reply_to_message_id: item[6], + message_type: item[7], + text: item[8], + text_entities: item[9] ? JSON.parse(item[9]) : null, + file: item[10], + date: item[11] + }; + }); + + return transformedData.reverse(); + } catch { + return []; + } +} + +const hashCode = (name: string) => { + var hash = 0; + for (var i = 0; i < name.length; i++) { + var character = name.charCodeAt(i); + hash = ((hash << 5) - hash) + character; + } + return Math.abs(hash); +} + +export const getFixedRandomColor = (name: string) => { + const colorData: Record = { + red: "text-red-400 border-red-400", + yellow: "text-yellow-400 border-yellow-400", + blue: "text-blue-400 border-blue-400", + sky: "text-sky-400 border-sky-400", + purple: "text-purple-400 border-purple-400", + orange: "text-orange-400 border-orange-400", + amber: "text-amber-400 border-amber-400", + lime: "text-lime-400 border-lime-400", + green: "text-green-400 border-green-400", + emerald: "text-emerald-400 border-emerald-400", + teal: "text-teal-400 border-teal-400", + cyan: "text-cyan-400 border-cyan-400", + indigo: "text-indigo-400 border-indigo-400", + violet: "text-violet-400 border-violet-400", + fuchsia: "text-fuchsia-400 border-fuchsia-400", + pink: "text-pink-400 border-pink-400", + rose: "text-rose-400 border-rose-400", + }; + const colorNames = Object.keys(colorData); + return colorData[colorNames[hashCode(name) % colorNames.length]].split(" "); +} + +export const cleanMessageText = (text: string) => { + return text.replaceAll(/\n/g, " "); +} + +export const setupUserName = (first: string, last: string | null) => { + const firstName = first.trimEnd(); + let lastName = ""; + if(last !== null) { + lastName = " " + last.trimEnd(); + } + return [ firstName, lastName ]; +} -- Muhammad Rizki