Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Per User Settings #13

Closed
aronduby opened this issue Sep 17, 2016 · 19 comments
Closed

Per User Settings #13

aronduby opened this issue Sep 17, 2016 · 19 comments

Comments

@aronduby
Copy link

aronduby commented Sep 17, 2016

The system I'm building is tenated, so I need to be able to pull OAuth from each of the different models, instead of a site wide config. The site is tweeting as a team that is in the site, not the site itself. It looks like Laravel provides routing functions for the notifications, but this class isn't setup to use it. I can do some work and a PR to add it, but wanted to get your thoughts first.

First question is do you want this package to handle this use case?

If yes - the way I'm seeing it there's really two options: --we can either do specific with a fallback to the generic if the route function isn't specified--, or we have a way to switch between one or the other (either config or registering different providers or something). Do you have a preference? I think I prefer specific switch since there doesn't seem to be many use cases for the fallback, although fallback would probably be easier to code and maintain.

Any thoughts?


After thinking about it more, the fallback is not a good idea since it would be counter to how most, if not all, notifications work - that's just asking for trouble.

@ahmedash95
Copy link
Contributor

Hi @aronduby , as a contributor in this repo may i ask you to show code example of what you want to do , cause i can't get the full picture well ?

@aronduby
Copy link
Author

aronduby commented Sep 17, 2016

I'm not quite sure the best way to pull off the switch, but here's how the rest would happen, which is basically copy/paste from the Nexmo channel included by default.

In my tenanted model (for example the team)

class Team extends Model {
    public function routeNotificationForTwitter() {
        // return array with the twitter app settings or false if it's not set
    }
}

In the TwitterChannel

public function send($notifiable, Notification $notification)
{
    // could easily expand this to check a config setting or something
    if (! $settings = $notifiable->routeNotificationFor('twitter')) {
        return;
    }

    // now create the twitter oauth client and continue as normal
}

Then it's just the normal process laid out in the docs for sending the notification as the user

@ahmedash95
Copy link
Contributor

@aronduby what about change twitter oAuth client credential on fly ?

config()->set('services.twitter.access_token',$access_token);
config()->set('services.twitter.access_secret',$access_secret);

@aronduby
Copy link
Author

aronduby commented Sep 17, 2016

then you can't easily skip a notifiable if they don't have it enabled

@ahmedash95
Copy link
Contributor

i guess you should do this to get it work as you expected

class Team extends Model {
    public function routeNotificationForTwitter() {
         // check config data , if it's not set then return false
         return false;  
         // or change the twitter app settings
         config()->set('services.twitter.access_token',$access_token);
         config()->set('services.twitter.access_secret',$access_secret);
    }
}

@christophrumpel
Copy link
Collaborator

Hey @aronduby,

definitely open for more features. Will look into your cas that later today. I did some refactoring of the channel with a feature to send media too. I want to merge that in before any other addons. Thx @ahmedash95 for jumping in here too. I'll be back later today.

@ahmedash95
Copy link
Contributor

@christophrumpel never mind 😄 .. PS. i love the new way of handling requests, Good job man!

@mpociot
Copy link
Member

mpociot commented Sep 17, 2016

Instead of changing the config values on the fly, you could also extend the TwitterServiceProvider and modify the boot method to return tenant related configuration values:

https://github.com/laravel-notification-channels/twitter/blob/master/src/TwitterServiceProvider.php#L19-L24

@ahmedash95
Copy link
Contributor

@mpociot, but he need to change the credentials based on row data of the model

@christophrumpel
Copy link
Collaborator

@ahmedash95 thx man! With the additional media parameter I felt we needed to change the code to provide the users a better experience. Glad you liked it.

@aronduby Just had a few minutes to check out your scenario. We really should add some functionality to make this work with user dependant data. I am definitely open for a PR or I could implement it myself if you don't want to. I am just leaving tomorrow morning for one week vacation, so I won't be able to help before I will be back.

But of course @ahmedash95 and @mpociot can help here

@christophrumpel
Copy link
Collaborator

christophrumpel commented Oct 2, 2016

Sry guys for the late reply. It has been messy here. So I just looked into that problem.
My first solution would be like this. (not perfect yet, but working)

  1. Dev adds a function to return user specific Twitter keys inside the notifiable model. I tested it with different settings in my config, but in real this data would probably come from the user's DB.
// Inside user model for example
public function routeNotificationForTwitter()
    {
        return [
            config('services.twitter.consumer_key2'),
            config('services.twitter.consumer_secret2'),
            config('services.twitter.access_token2'),
            config('services.twitter.access_secret2')
        ];
    }
  1. In order to make the settings switch I would check for the user data and if it is given I create a new TwitterOAuth class with the new settings.
// Inside TwitterChannel
if ($twitterSettings = $notifiable->routeNotificationFor('twitter')) {
    $this->switchSettings($twitterSettings);
}

The switch could look something like this:

private function switchSettings($twitterSettings)
{
    $this->twitter = new TwitterOAuth(
        $twitterSettings[0],
        $twitterSettings[1],
        $twitterSettings[2],
        $twitterSettings[3]
    );
}

Place for improvement but maybe a good and working start =) What do you think guys?

@christophrumpel
Copy link
Collaborator

Any thoughts @mpociot or @themsaid ?

@christophrumpel
Copy link
Collaborator

Or maybe @freekmurze ?

@christophrumpel
Copy link
Collaborator

Another try =) @mpociot @themsaid @freekmurze

What do you think of this approach? I really think there should be a per-user setting too.

@themsaid
Copy link
Member

@christophrumpel I've used a similar in a cloned version of the pusher push notifications channel in which every tenant have his own pusher app credentials, so I'd say 👍

Sorry for the late reply :)

@christophrumpel
Copy link
Collaborator

Thx @themsaid ! No problem. Just wanted to get some feedback =) Then I will implement it and there will be time for changes afterwards too if there are better approaches or improvements =)

@christophrumpel
Copy link
Collaborator

Ok I did implement it. Anyone an idea how I can test that?

@christophrumpel
Copy link
Collaborator

Still not happy with the tests, but feel free to add a PR if you can and want to change that.

@aronduby
Copy link
Author

Sorry I've mostly dropped off the face of the earth in this conversation, and it will only get worse in the coming months, but figured I would try to throw in real quick while I can.

@christophrumpel I really like the solution you have above. The only quick thought I had was making it be able to do both global and per-user, but in that case it would be easy enough to create a "domain" model which does everything properly, which seems more inline with how the notification system works within Laravel.

Thanks for the great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants