Kinda fix networks

This commit is contained in:
Lucas
2026-01-31 17:52:49 +10:00
parent f70af3c4ea
commit 65d8c7d5e2
13 changed files with 97 additions and 29 deletions

View File

@@ -37,14 +37,14 @@ export const p2privateApi = {
},
async createMessage(from, to, content, medias){
const data = {"from":from, "to":to, "content":content, "medias":medias}
const data = {"content":content, "medias":medias}
const requestOptions = {
method: 'POST',
headers: {'Content-Type': 'application/json',},
body: JSON.stringify(data),
};
try {
const response = await fetch(p2privateApiUrl+"/messages", requestOptions);
const response = await fetch(p2privateApiUrl+"/messages/"+from+"/"+to, requestOptions);
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Error fetching posts: ${response.status} - ${errorText}`);
@@ -59,6 +59,21 @@ export const p2privateApi = {
}
},
async getChat(from, to){
try {
const response = await fetch(p2privateApiUrl+"/messages/"+from+"/"+to)
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Error fetching networks: ${response.status} - ${errorText}`);
}
const result = await response.json()
return result;
} catch (error) {
console.error("Error fetching networks:", error);
throw error;
}
},
// async createPosts(user, content, medias, networks){
// const data = {"user": user, "content": content, "medias": medias, "networks": networks};
// const requestOptions = {

View File

@@ -13,6 +13,7 @@ import { p2postApi } from '@/modules/p2post/api/p2postApi';
const friends = ref([])
const activeFriend = ref(null)
const myUsers = ref([])
const currentUser = ref({"pubkey":null})
const relays = ref([])
@@ -20,31 +21,39 @@ async function getRelays(){
relays.value = await p2privateApi.getNetworks()
}
async function getFriends(){
async function getFriends(user){
const result = await p2privateApi.getFriends()
for (const f of result){
const m1 = {id:1, senderId:'me', content:"test Meeeeeeeeeeeeeeeeeeeeee", timestamp:new Date(), status:"SENT"}
const m2 = {id:2, senderId:'peer', content:"test Peer", status:"RECEIVED", timestamp:new Date(), files:[{name:'file1', type:'image/jpeg', size:123}]}
const mes = [m1,m2]
const fr = {nickname:'Nickname', unreadCount:5, publicKey:f.pubkey, isOnline:true, messages:mes}
const chat = await p2privateApi.getChat(user.pubkey, f.pubkey)
const messages = []
for (const m of chat){
if (m.from === user.pubkey){
var senderId = "me"
}else{
var senderId = "peer"
}
var message = {id:m.hash, senderId:senderId, content:m.content, timestamp:m.timestamp, status:"SENT"}
messages.push(message)
}
const fr = {nickname:'Nickname', unreadCount:5, publicKey:f.pubkey, isOnline:true, messages:messages}
friends.value.push(fr)
}
}
async function getMyUsers(){
myUsers.value = await p2postApi.getMyUsers()
const randomIndex = Math.floor(Math.random() * myUsers.value.length);
currentUser.value = myUsers.value[randomIndex]
getFriends(currentUser.value)
}
const inputMessageText = ref("")
async function createMessage(){
const randomIndex = Math.floor(Math.random() * myUsers.value.length);
const randomUser = myUsers.value[randomIndex]
await p2privateApi.createMessage(randomUser.pubkey, activeFriend.value.publicKey, inputMessageText.value, [])
await p2privateApi.createMessage(currentUser.value.pubkey, activeFriend.value.publicKey, inputMessageText.value, [])
}
onMounted(()=>{
getRelays()
getFriends()
getMyUsers()
})
@@ -109,6 +118,7 @@ onMounted(()=>{
<UsersIcon class="w-5 h-5 mr-2" />
Friends ({{friends.length}})
</h3>
<span class="text-sm">{{ currentUser.pubkey }}</span>
</div>
<div class="flex-1 overflow-y-auto p-4 space-y-2">