1- import { NamedNode , LiveStore , sym } from "rdflib" ;
2- import { ProfileLogic } from "../profile/ProfileLogic" ;
3- import { SolidNamespace } from "../types" ;
4- import { UtilityLogic } from "../util/UtilityLogic" ;
1+ import { NamedNode , sym } from "rdflib" ;
2+ import { InboxLogic } from "../types" ;
3+ import { getArchiveUrl } from "../util/utils" ;
54
6- /**
7- * Inbox-related logic
8- */
9- export class InboxLogic {
10- store : LiveStore ;
11- ns : SolidNamespace ;
12- profile : ProfileLogic ;
13- util : UtilityLogic ;
14-
15- constructor ( store : LiveStore , ns : SolidNamespace , profile : ProfileLogic , util : UtilityLogic ) {
16- this . store = store ;
17- this . ns = ns ;
18- this . profile = profile ;
19- this . util = util ;
5+ export function createInboxLogic ( store , profileLogic , utilityLogic , containerLogic , aclLogic ) : InboxLogic {
6+
7+ async function createInboxFor ( peerWebId : string , nick : string ) {
8+ const myWebId : NamedNode = await profileLogic . loadMe ( ) ;
9+ const podRoot : NamedNode = await profileLogic . getPodRoot ( myWebId ) ;
10+ const ourInbox = `${ podRoot . value } p2p-inboxes/${ encodeURIComponent ( nick ) } /` ;
11+ await containerLogic . createContainer ( ourInbox ) ;
12+ const aclDocUrl = await aclLogic . findAclDocUrl ( sym ( ourInbox ) )
13+ await utilityLogic . setSinglePeerAccess ( {
14+ ownerWebId : myWebId . value ,
15+ peerWebId,
16+ accessToModes : 'acl:Append' ,
17+ target : ourInbox
18+ } ) ;
19+ return ourInbox ;
2020 }
2121
22- async getNewMessages (
23- user ?: NamedNode
24- ) : Promise < NamedNode [ ] > {
25- if ( ! user ) {
26- user = await this . profile . loadMe ( ) ;
27- }
28- const inbox = await this . profile . getMainInbox ( user ) ;
29- const urls = await this . util . getContainerMembers ( inbox ) ;
30- return urls . filter ( url => ! this . util . isContainer ( url ) ) ;
22+ async function getNewMessages (
23+ user ?: NamedNode
24+ ) : Promise < NamedNode [ ] > {
25+ if ( ! user ) {
26+ user = await profileLogic . loadMe ( ) ;
27+ }
28+ const inbox = await profileLogic . getMainInbox ( user ) ;
29+ const urls = await containerLogic . getContainerMembers ( inbox ) ;
30+ return urls . filter ( url => ! containerLogic . isContainer ( url ) ) ;
3131 }
32- async createInboxFor ( ourPeerWebId : NamedNode , nick : string ) {
33- const myWebId : NamedNode = ( await this . profile . loadMe ( ) ) ;
34- const podRoot : NamedNode = await this . profile . getPodRoot ( myWebId ) ;
35- const ourInbox = `${ podRoot . value } p2p-inboxes/${ encodeURIComponent ( nick ) } /` ;
36- const toNode : NamedNode = sym ( ourInbox ) ;
37- await this . util . createContainer ( toNode ) ;
38- const aclDocUrl = await this . util . findAclDocUrl ( toNode ) ;
39- await this . util . setSinglePeerAccess ( {
40- ownerWebId : myWebId . value ,
41- peerWebId : ourPeerWebId . value ,
42- accessToModes : 'acl:Append' ,
43- target : ourInbox
44- } ) ;
45- return ourInbox ;
46- }
47- async markAsRead ( url : NamedNode , date : Date ) {
48- const nodeToStr = url . value ;
49- const downloaded = await this . store . fetcher . _fetch ( nodeToStr ) ;
32+
33+ async function markAsRead ( url : string , date : Date ) {
34+ const downloaded = await store . fetcher . _fetch ( url ) ;
5035 if ( downloaded . status !== 200 ) {
5136 throw new Error ( `Not OK! ${ url } ` ) ;
5237 }
53- const archiveUrl = this . util . getArchiveUrl ( nodeToStr , date ) ;
54- const options = {
38+ const archiveUrl = getArchiveUrl ( url , date ) ;
39+ const options = {
5540 method : 'PUT' ,
5641 body : await downloaded . text ( ) ,
5742 headers : [
58- [ 'Content-Type' , downloaded . headers . get ( 'Content-Type' ) || 'application/octet-stream' ]
43+ [ 'Content-Type' , downloaded . headers . get ( 'Content-Type' ) || 'application/octet-stream' ]
5944 ]
6045 } ;
61- const uploaded = await this . store . fetcher . _fetch ( archiveUrl , options ) ;
46+ const uploaded = await store . fetcher . _fetch ( archiveUrl , options ) ;
6247 if ( uploaded . status . toString ( ) [ 0 ] === '2' ) {
63- await this . store . fetcher ? ._fetch ( nodeToStr , {
48+ await store . fetcher . _fetch ( url , {
6449 method : 'DELETE'
6550 } ) ;
6651 }
6752 }
68- }
53+ return {
54+ createInboxFor,
55+ getNewMessages,
56+ markAsRead
57+ }
58+ }
0 commit comments