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

Feature Request: Followers you know #59

Open
bigint opened this issue Jun 22, 2021 · 5 comments
Open

Feature Request: Followers you know #59

bigint opened this issue Jun 22, 2021 · 5 comments
Labels
duplicate This issue or pull request already exists enhancement New feature or request

Comments

@bigint
Copy link

bigint commented Jun 22, 2021

It would be great if this package support followers you know thing!

image

@mkwsra mkwsra added duplicate This issue or pull request already exists enhancement New feature or request labels Jun 23, 2021
@bigint
Copy link
Author

bigint commented Jun 29, 2021

Is this a duplicate @mkwsra?

@bigint
Copy link
Author

bigint commented Jun 29, 2021

Oh yes I created it earlier #44 🤣

@mkwsra
Copy link
Member

mkwsra commented Jun 29, 2021

I will work on this library as soon as I got sometime and I will implement this if I can 😍

@bigint
Copy link
Author

bigint commented Jun 29, 2021

Thanks, we are using this library on https://taskord.com that has around 4K interactions (like, subscribe, follow etc)

image

Thanks for making this @mkwsra 🙏🏻

@ronssij
Copy link

ronssij commented Mar 11, 2022

@bigint upon diggin the package for a week because I like it very much. I can suggest this.

Add it as a user model scope:

return $builder->when($hasUserAuthModel, function ($query) use ($model) {
            $table      = $model->getTable();
            $column     = $model->interactionColumn;
            $modelClass = get_class($model);

            // Default will always be a model/table id column if interactionColumn is not set on model.
            $interactionColumn = is_null($column) ? "{$table}.id" : "{$table}.{$column}";

            return $query->addSelect([
                'did_interact' => InteractionRelation::selectRaw('count(*)')
                    ->from('interactions as is_following_interactions')
                    ->whereIn('is_following_interactions.relation', [
                        Interaction::RELATION_FOLLOW,
                        Interaction::RELATION_SUBSCRIBE
                        ... // Depends on what you used interaction
                    ])
                    ->where(function ($query) use ($interactionColumn) {
                        return $query->whereColumn('is_blocked_interactions.subject_id', $interactionColumn)
                            ->where('is_blocked_interactions.user_id', user()->id);
                    })
                    ->whereColumn('subject_id', 'users.id')
                    ->take(1),
            ])
            ->withCasts([
                'did_interact' => 'boolean'
            ]);
        });

I wrapped it with when() so it will only be avaiable when there is an authticated user and it always be available when you use User model and relation with user model instance.

so for example getting the followers:
$user->followers()->havingRaw('did_interact > ?', [0])->get();

or adding a new relation since you have the pacakges traits.

    // On the trait
    /**
     * Return followers.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function followers()
    {
       return $this->morphToMany(Interaction::getUserModelName(), 'subject',
            config('acquaintances.tables.interactions'))
                    ->wherePivot('relation', '=', Interaction::RELATION_FOLLOW)
                    ->withPivot(...Interaction::$pivotColumns)
                    ->using(Interaction::getInteractionRelationModelName())
                    ->withTimestamps();
    }
    And in your user model
    
    public function interactedFollowers()
    {
        return $this->followers()->havingRaw('did_interact > ?', [0]);
    }

and now you can work with.

User::with(['interactedFollowers'])
        ->withCount(['interactedFollowers'])
        ->where('id', 1)
        ->first();

hope this helps!!

I made a also a workound using this because I cant use the isFollowedBy() and isFollowing on a resource since it uses the model relations exists() which is the Interaction::isRelationExists($this, 'followings', $target, $class);

image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants