Just wanted to check, is there a way to tag/mention people in a post

bryce alvord Posted in General Discussion 4 years ago

I am honestly not remembering, is there a way to tag users in a post or anything like that. Im aware of the hashtags premium component but what Im talking about is doing like @Bryce Alfred for example and that gives a notification to that user that they were mentioned in a post.

If there isnt a component or functionality for this I might work on creating one because I think that would be a cool enhancement

Replies
us Bryce alvord Replied 4 years ago

Here is my readme from what I am calling v1.1 Let me know if you think this is something that should be released or if it needs more enhancement to be useful. Im not opposed to doing more but Im not looking to make it a full time job lol.

OSSN MENTION USERS COMPONENT

This component enables a user to mention other users in posts and comments by using the syntax @Full Name and that will send the mentioned user a notification telling them that they were mentioned in a post or comment with a link to that item.

This can be used in conjunction with the Display Username component as of v1.2 if you want to display usernames and mention @username instead of display Full Name and mention @Full Name. It will dynamically switch based whether or not the Display Username component is installed and turned on. You can find more info on it here: https://www.opensource-socialnetwork.org/component/view/3065/display-username

-When mentioning usernames using the Display Username component the exact casing must be used to match the user.

-When mentioning user full names the casing doesnt matter since that is not something that guarantees uniqueness.

-Mentions are not limited to the friends of the user, the entire user base can be mentioned

-Multiple mentions can be done in a post or comment.

TO INSTALL

Zip the MentionUsers subdirectory into a .zip to install as a package/component in OSSN

us Bryce alvord Replied 4 years ago

Steven,

Currently it is searching by the user fullname from the user object and not the username. If I were to go to the username, this would not be any issue at all because they are unique. I actually have added comments to my code on what to change if you were to use it in conjunction with ZMans component https://www.opensource-socialnetwork.org/component/view/3065/display-username so that you could see peoples usernames in the site

Tatar Steven downer Replied 4 years ago

Bryce,

"Any users with the same names would both get notified"

usernames are unique are they not?

or are you searching on firstname +lastname?

us Bryce alvord Replied 4 years ago

Yeah I already had a template written for the view portion and thanks to you guys for the help this morning or evening for you rather I was able to get v1 fully working. Im torn on a couple things though. Do I release v1? Do I try to do a UI for it in a v1.1?

v1 Features (pros and cons)
- Requires you to accurately type out the users full name, no typos
- Any users with the same names would both get notified since there is no way for me to tell who the intended recipient was
- Mentions are not limited to the friend base of the logged in user
- Multiple mentions are supported in comments or posts

Im fine with suggestions like limiting the scope to the friends list for example but I do feel like thatdecreases the usability. Thoughts?

Indonesian Arsalan Shah Replied 4 years ago

You are doing it wrong, you can not use ossn_add_hook inside other callback function, i beleive.

<?php
function my_com_init(){
     ossn_register_callback('wall', 'post:created', 'wall_created');
     ossn_register_callback('comment', 'created', 'comment_created');

     ossn_add_hook("notification:add", "mention:post", "mention_allow_notification");
}
function mention_allow_notification(){
     return true;   
}
function wall_created($callback, $type, $params){
           $new_wall_post_id = $params['object_guid'];
           $wall_post  = ossn_get_object($new_wall_post_$id);
           /*
            1. $text = get text from json
            2. $tags = find a @username  
            3. $loop throught the $tags -> loop : as $user_guid
            4.  $notifications->add('mention:post', $wall_post->poster_guid, $new_wall_post_id, $new_wall_post_id, $user_guid):
                because $notifications->add(type, guid of user who posting notification,  any subject guid,  addtioanl guid if you want else same as any subject guid,  guid of user who will get notification)
           */
}
function comment_created($callback, $type, $params){
          //same with comment
}

ossn_register_callback('ossn', 'init', 'my_com_init');

Further you should see https://www.opensource-socialnetwork.org/component/view/3305/group-invitation

With above code see if notification is added into system if so , you will later need to add ossn_add_hook('notification:view', ''mention:post'', 'mentionds_view_notification_template'); into init function

us Bryce alvord Replied 4 years ago

This is the full function responsible for sending the notification. It works for posts but something isnt right for comments because they arent working.

function com_notificationHandler($callback, $type, $params, $message) {
   if (strlen($params['poster_guid']) > 0) {
        ossn_add_hook("notification:add", "post:created", "com_mention_notifier");
   }
   else {
        ossn_add_hook("notification:add", "comments:post", "com_mention_notifier");
   }

   $messageArr = str_split($message, 1);
   $notifications = new OssnNotifications;
   $user = new OssnUser;
   $users = $user->getSiteUsers(['page_limit' => false]);

 foreach ($messageArr as $messagekey => $messagevalue) {
      if ($messagevalue == "@") {
           foreach ($users as $key => $value) {
                $len = strlen($value->fullname);
                if (substr($message, $messagekey + 1, $len) == $value->fullname) {
                     error_log("Matched Substr, Sending Notification");
                     // if we have a poster guid then this is a wall post else its a comment so the payload changes
                     if (strlen($params['poster_guid']) > 0) {
                          $notifications->add($type, $params['poster_guid'], $params['object_guid'], $params['object_guid'], $value->guid);
                     }
                     else {
                          $notifications->add($type, $params['owner_guid'], $params['subject_guid'], $params['id'], $value->guid);
                     }
                }
           }
      }
   }
}
German Michael Zülsdorff Replied 4 years ago

Honestly I don't quite understand your code...

If you implemented the creation of 'mention' notification records as Arsalan said, you'll end up with 2 different kinds of records - let's say of type post:mention and type comment:mention

And that would mean your component would need 2 'viewing' hooks, too ... like

ossn_add_hook('notification:view', 'post:mention, 'f1');
ossn_add_hook('notification:view', 'comment:mention', 'f2');

to be called by OssnNotifications->get()

making f1 and f2 fetch the records that match by type and add them to the list of other notifications to be displayed

us Bryce alvord Replied 4 years ago

Z-Man, I should have mentioned that I did implement Arsalans example but I do appreciate the breakdown you gave. Any thoughts on why my hook isn't working for comments? Wall posts work just fine and send the notification.

Tatar Steven downer Replied 4 years ago

I worked on a project some time ago to catalog artwork and the "tags" were stored in SQL database (artwork name, artist, date painted, cost, etc,etc) , similar to the ossn_users table can be for username, etc.

to "call up" a particular piece of work, we use to simply type in the "tag" reference in a field box for SQL to compute.... such as "NAME, ARTIST, DATE" and used the SQL to define it....

tags LIKE '% $tag %' OR tags LIKE '$tag %' OR tags LIKE '% $tag' OR tags = '$tag'

that worked for us....I am not quite familiar as to the workings of OSSN so I would not know how to inject that into a script without breaking it, but perhaps you could test a script outside of OSSN first to see if it works on the ossn_users table and then attempt to inject it into OSSN??

German Michael Zülsdorff Replied 4 years ago

To get an idea of the logic flow, let's have a look into what happens when a new wall post has been created, first:
At the end of OssnWall->Post() you find

ossn_trigger_callback('wall', 'post:created', $params);

This callback is registered in the com file of OssnNotifcations like

ossn_register_callback('wall', 'post:created', 'ossn_notification_walltag');

and if some conditions are met, a new notification of type 'wall:friends:tag' will be created in the ossn_notification_walltag function.

That said, you would need something similar for your mentions component. And in fact at the end of OssnComments->PostComment() you find

ossn_trigger_callback('comment', 'created', $params);

So, your component needs a

ossn_register_callback('comment', 'created', 'mentionNotification');

and

function mentionNotification($type, $ctype, $params) {
// some logic to find out whether someone has been mentioned
// and if so then create the notification

}

Indeed this is just repeating what Arsalan already said