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=1696657522; bh=mUKPAReQNgCVZyujCTsdie7mfZ77zv0V8HDk2nDeQrw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=LDhZa+zW/wB3Jg1skMcL1e4Vo0DHv/2kDVqYCe9IhCTV1oIjZoBKq57Bwkx0tTmI+ YegJzTbZ3NKb2uI7+/wlDpC2AfGvvEQ6H2Ixx4OKU1LsK+b9akqmPuHEfZrjKOeo9m VIw4JpijUYAeQtJQP5d/amrhsYk9WWHM0/rk0eFL9poMUeY91MurreemOsL/yxSf3h I24q9W0Ydru4zSQhhpo+y7BttFTAxzqVB0SA32PPvoqkSOXQ/GrPyHXp4Pq9XGC9L0 LMPalcNTkhoDmlw1+8Hn0RRz1PueNjHuGax3nEJDe+Py72rp5Edd7jvNJv2mrylE5d FZdsRiSWeHqeA== Received: from localhost.localdomain (unknown [175.158.50.50]) by gnuweeb.org (Postfix) with ESMTPSA id 68FB424B982; Sat, 7 Oct 2023 12:45: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 v2 05/14] feat(lib): add lib functions file Date: Sat, 7 Oct 2023 12:44:37 +0700 Message-Id: <20231007054446.1204-6-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20231007054446.1204-1-kiizuha@gnuweeb.org> References: <20231007054446.1204-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