How do I establish a connection to an existing components code from a new component

bryce alvord Posted in Component Development 6 years ago

Im writing a component that will use some core functionality of one of the stock OSSN components. Im wondering if there is something built into the OSSN framework that allows me to import a component into mine essentially so that I can call its functions and have the class accessible to me. I hate copying code that performs simple tasks and creating a new code fork vs. tapping into the core functionality in its pure form. I know I can include those files too, just wondering if there is a better way that is less sensitive for me to do this.

So lets say I am wanting to create a notification for example. Well a lot goes into thos notifications from what I can tell in the OSSN Notifications component so my thought process is why recreate the wheel when I can hopefully "import" OSSNNotifications and call a OSSNNotifications.CreateNotification() method (pseudo code obviously).

Thanks

This topic has been closed!

[Topic is Close]

Replies
us Bryce alvord Replied 6 years ago

Yes I am just doing this on a small site of less than 10 users that won't expand much if at all. So are there thoughts of how to keep the notifications setup I have and trigger the push notification code for the app? I can take the same concepts now and switch to messages if I must, just not sure how the app interfaces. Thanks!

German Michael Zülsdorff Replied 6 years ago

Talking about large(r) sites I agree completely. And actually I made Bryce aware of that issue in my second post already. But this was meant to be running on a very small family site, only - so I don't see it'll turn out to be a problem.

Indonesian Arsalan Shah Replied 6 years ago

Just my thoughts are here, foreach ($users as $key => $value) { ... suppose you have 1Million users and you running loop to add notification , this will take long time. Also the notifications are created by posting user.... so it will make the posting user wait until loop finished.

These type of things should be run on sever end via console/ or a cron jobs.

gb Rishi B Replied 6 years ago

bryce, messages in ossn will trigger a push notification to the mobile app, though notifications will not. since the mobile app is not open source we can't do much about that. maybe you want to dispatch a message to your users (perhaps from an admin) as a workaround.

us Bryce alvord Replied 6 years ago

last thing Z Man, not sure if you are familiar with the app code but as part of this I was hoping that by triggering a notification it would also send a push notification to the user that is logged into the app on their phone but it appears this doesnt seem to work as of now. I have a few things to try but I will let you know if anything pans out. Im interested in your feedback. Im guessing I will have to do something via the api to trigger a push notification through that component.

us Bryce alvord Replied 6 years ago

yay! All done and it works great, thanks Z Man!

German Michael Zülsdorff Replied 6 years ago

Great Bryce, you're almost done.
The only part still missing is the viewing hook. :)
So have a look into the com file of the OssnWall component and copy

ossn_add_hook('notification:view', 'like:post', 'ossn_likes_post_notifiation');

and the corresponding function to your component.

Rename it to something like

ossn_add_hook('notification:view', 'post:created', 'com_post_notifier_post_view_notification');

and click that notification globe again:

All that 's left to do is adding that placeholder to your language file like

'ossn:notifications:post:created' => '%s created a post',

and finding a meaningful icon.

It's getting late here - good night.

us Bryce alvord Replied 6 years ago

sweet, thank you. I am getting notifications showing in the counter over the globe notification icon but when I click the notification globe or go to the notifications page there is no notification. Is this because the subjectguid isnt a valid subject since I just stubbed in the objectguid to test with? If so, is there a list of these subjects so that I can add to it or hard code to use one of the existing ones?

German Michael Zülsdorff Replied 6 years ago

Correct: $params is an array, no object, so you have to fetch the value as

$params['poster_guid']

In case you're not sure, you may use the ossn_dump() function like

error_log('MY PARMS: ' . ossn_dump($params));

Anyway - and this time it's me to say sorry - this was not the reason for getting no notification records. You actually need that notification ADD hook. Without (see line 59 of OssnNotification->classes) the add method returns false. So add that hook to your component like

ossn_add_hook('notification:add', 'post:created', 'com_post_notifier_post_add_notification');

and the corresponding function like

function com_post_notifier_post_add_notification($hook, $type, $return, $params) {
        // error_log('NEW NOTI: ' . ossn_dump($params));
        $return = $params;
        $return['owner_guid'] = $params['notification_owner'];
        return $return;
}

If you verify the error log you'll find that basically everything is already in place, but the class code in line 62 expects a value in owner_guid, not notification_owner. That's why I copied it.
(Just made a pull request because the logic sequence appears strange to me)

Having applied all this you should see some new records in your ossn_notification table now.

us Bryce alvord Replied 6 years ago

Hmm, so now I am getting guids all the way across but the notification add method is not creating a notification for some reason using the below code. Any thoughts ?

function post_notifications() {
    ossn_register_callback("wall", "post:created", "notificationHandler");
}

function notificationHandler($callback, $type, $params) {
    $notifications = new OssnNotifications;
    $user = new OssnUser;
    $users = $user->getSiteUsers(['page_limit' => false]);

    foreach ($users as $key => $value) {
        // send the notification to all users except the posting user
        if ($value->guid != $params['poster_guid']) {
            error_log("TYPE: " . $type);
            error_log("POSTER GUID: " . $params['poster_guid']);
            error_log("OBJECT GUID: " . $params['object_guid']);
            error_log("VALUE GUID: " . $value->guid);
            $notifications->add($type, $params['poster_guid'], $params['object_guid'], $params['object_guid'], $value->guid);
        }
    }
}
Premium Version

Due to the many requests in the past for additonal features and components we have decided to develope a premium version. Features like Hashtags, Videos, Polls, Events, Stories, Link Preview, etc included in it.

$199 (Life Time)
Learn More

Other Questions