chromeapp clear all notifications - google-chrome

I am trying to create a chromeapp that, when a hotkey is pressed, clears all notifications. I have the hotkey set up and working, but I can't seem to get the chrome.notifications.clear api to work, and I think it is because I can't/don't know how to get all notification ids. Is there any way to clear a notification without knowing its id, or just clear all notifications? Thanks!

Based on the documentation,
you need to get the notificationId to delete the notification.
The chrome.notifications.clear(string notificationId, function callback) it only clears a specified notification.
The id of the notification to be cleared is returned by notification.create method.
So if you dont know the notificationId in the system, you can get it by calling the chrome.notifications.getAll(function callback). It retrieves all the notification and notificationId in the system.

If someone still needs a code example:
chrome.notifications.getAll((items) => {
if ( items ) {
for (let key in items) {
chrome.notifications.clear(key);
}
}
});

Related

Clear all MUC messages when joining room using Conversejs

I've configured Conversejs to Auto join a room, with a preconfigured account and credentials. I would like it to clear the previous messages when the room is joined.
I've been able to put together this shell of a plugin, and have confirmed it executes but dont know the code to clear the chat text.
converse.plugins.add('myplugin', {
initialize: function () {
this._converse.api.listen.on('roomsAutoJoined', () => {
// How to clear chat ??
});
}
});
Basicically executing the /clear command for the user automatically when joining room.
There will always be another user signed into the room, or else I know it would clear automatically.
Also I'm using ejabberd if it matters.
You can set clear_muc_messages_on_reconnection to true.

Validation for three unique fields and soft deletes

Last year I made a laravel site with an events table where I needed three fields to be unique for any event (place, date and time). I wasn't able to set up a validation request to do this so I added an unique index for these three fields directly through phpmyadmin and catching the exception that could happen if a duplicated event was inserted.
So basically my store() method has a try/catch like this:
try {
$event = new Event;
$event->place = $request->input('place');
$event->date = $request->input('date');
$event->time = $request->input('time');
$event->save();
return view(...);
} catch (\Illuminate\Database\QueryException $e) {
// Exception if place-date-time is duplicated
if($e->getCode() === '23000') {
return view('event.create')
->withErrors("Selected date and time is not available");
}
}
Well, now I had to change the app so events could be soft deleted and I simply added the 'deleted_at' field to the unique index, thinking it would be so easy... This approach doesn't work anymore so I've been reading here and there about this problem and the only thing I get is I should do it through a validation request with unique, but honestly I just don't get the syntax for this validation rule with three fields that can't be equal while a fourth one, deleted_at, being null.
My app checks for the available places, dates and times and doesn't let the user choose any not available event but no matter how many times I've told them there's always someone who uses the browser back button and saves the event again :(
Any help will be much appreciated. Thank you!
This is not a good approach to solve the problem.
You can do follow things to solve this problem
Before insert into database get a specific row if exist from database
and store into a variable.
Then check the data is already stored into the database or not.
If data is already there create custom validation message using Message Bag Like below.
$ifExist = $event
->wherePlace(request->input('place'))
->whereDate(request->input('date'))
->whereTime(request->input('time'))
->exist();
if ($ifExist) return 'already exist';
It might help you.
#narayanshama91 have pointed the right way.
You said you would like to use the unique rule to validate the input but the problem is that last week there was a post in Laravel Blog warning users of a possible SQL Injection via the unique rule if the input is provided by the user.
I would highly advise you to NOT USE this rule in this case since you depend on users input.
The correct approach in your case would be #narayanshama91 answer.
$ifExist = $event
->wherePlace(request->input('place'))
->whereDate(request->input('date'))
->whereTime(request->input('time'))
->exist();
if ($ifExist) {
return 'already exist';
}

Laravel Eloquent how to limit access to logged in user only

I have a small app where users create things that are assigned to them.
There are multiple users but all the things are in the same table.
I show the things belonging to a user by retrieving all the things with that user's id but nothing would prevent a user to see another user's things by manually typing the thing's ID in the URL.
Also when a user wants to create a new thing, I have a validation rule set to unique but obviously if someone else has a thing with the same name, that's not going to work.
Is there a way in my Eloquent Model to specify that all interactions should only be allowed for things belonging to the logged in user?
This would mean that when a user tries to go to /thing/edit and that he doesn't own that thing he would get an error message.
The best way to do this would be to check that a "thing" belongs to a user in the controller for the "thing".
For example, in the controller, you could do this:
// Assumes that the controller receives $thing_id from the route.
$thing = Things::find($thing_id); // Or how ever you retrieve the requested thing.
// Assumes that you have a 'user_id' column in your "things" table.
if( $thing->user_id == Auth::user()->id ) {
//Thing belongs to the user, display thing.
} else {
// Thing does not belong to the current user, display error.
}
The same could also be accomplished using relational tables.
// Get the thing based on current user, and a thing id
// from somewhere, possibly passed through route.
// This assumes that the controller receives $thing_id from the route.
$thing = Users::find(Auth::user()->id)->things()->where('id', '=', $thing_id)->first();
if( $thing ) {
// Display Thing
} else {
// Display access denied error.
}
The 3rd Option:
// Same as the second option, but with firstOrFail().
$thing = Users::find(Auth::user()->id)->things()->where('id', '=', $thing_id)->firstOrFail();
// No if statement is needed, as the app will throw a 404 error
// (or exception if errors are on)
Correct me if I am wrong, I am still a novice with laravel myself. But I believe this is what you are looking to do. I can't help all that much more without seeing the code for your "thing", the "thing" route, or the "thing" controller or how your "thing" model is setup using eloquent (if you use eloquent).
I think the functionality you're looking for can be achieved using Authority (this package is based off of the rails CanCan gem by Ryan Bates): https://github.com/machuga/authority-l4.
First, you'll need to define your authority rules (see the examples in the docs) and then you can add filters to specific routes that have an id in them (edit, show, destroy) and inside the filter you can check your authority permissions to determine if the current user should be able to access the resource in question.

How to replace chrome.tabs.onSelectionChanged deprecated method?

I need to get data from the tab the user is leaving (switching on other tab or going on other program).
But chrome.tabs seems not providing an event allowing that..
Before there was apparently chrome.tabs.onSelectionChanged (not tested) but it's deprecated.
And other event are giving the data of the new tab not the one the user just left...
I try also jQuery $(window).blur event, but i have to make a call to the chrome.storage of the tab left by the user (i create a storage for each tab named by the tab id) and i did not get the response of storage in time with this event (the result of the storage is used in a if() to know if i have or not to display an confirm box.
Someone could help me ?
Thx !
To detect a tab change, just use chrome.tabs.onActivated. Since you're interested in the previous tab, store the result of the event at the end of each event. For instance, like this:
var storedWindowInfo = {};
chrome.tabs.onActivated.addListener(function(activeInfo) {
var windowLastTabId = storedWindowInfo[activeInfo.windowId];
if (windowLastTabId) {
// Do something with previous tab, e.g. send a message:
chrome.tabs.sendMessage(windowLastTabId);
}
// Update ID of currently active tab in the current window
storedWindowInfo[activeInfo.windowId] = activeInfo.tabId;
});
Note: Only the tabID and windowID are provided to this event. You need to issue chrome.tabs.query to get more information, such as whether the tab still exists, its URL, etc.

Check if IndexedDB objectStore already contains key

Adding an object to an IndexedDB objectStore will fail if the key already exists. How can I check for the existence of an object with a given key – preferably synchronously (no reason for another layer of callbacks) and without pulling the object.
I know how to do get requests asynchronously via transactions, but it seems a bit of an ordeal to go through every time I want to add an object.
note Solution only has to work in Chrome (if that helps)
The best way to check existence of a key is objectStore.count(key). Which is async.
In your case, the best option is openCursor of your key. If exists, cursor will come up.
var req = objectStore.openCursor(key);
req.onsuccess = function(e) {
var cursor = e.target.result;
if (cursor) { // key already exist
cursor.update(obj);
} else { // key not exist
objectStore.add(obj)
}
};
So far none of the browsers have the sync API implemented so you're going to have to do it async. An objectStore exposes a get method which you provide it with a key and it'll return you the object (or null) that matches the key.
There's a really good tutorial on MDN that covers using IDB, and getting a record is covered too, but the inlined code is:
db.transaction("customers").objectStore("customers").get("444-44-4444").onsuccess = function(event) {
alert("Name for SSN 444-44-4444 is " + event.target.result.name);
};
If you don't want retrieve the record then you can always used the count method on an index as explained here in the spec. Based on the result of that you can either use add or put to modify the record, it'll save extracting the record if you don't need to.
A bit late for an answer, but possible it helps others. I still stumbled -as i guess- over the same problem, but it's very simple:
If you want to INSERT or UPDATE records you use objectStore.put(object) (help)
If you only want to INSERT records you use objectStore.add(object) (help)
So if you use add(object), and a record key still exists in DB, it will not overwritten and fires error 0 "ConstraintError: Key already exists in the object store".
If you use put(object), it will be overwritten.
The question is why do you want to know this? Maybe you should use another approach?
You can use auto increment, this way you don't need to check if a key exists, you will always get a unique one.
You can also use the put method instead of the add method. With the put the data will be updated if the key exists and if the key doesn't exist the data is added.
Everything depends on the reason why you want to check if something exists.